jgrapht class in jgrapht that will allow me to construct a graph dynamically

689 views Asked by At

I'm looking for a class in jgrapht that will allow me to construct a graph dynamically and generate new edge in the given graph at run time based on branch of the algorithm . I need to implements the algorithm branch & bound I saw this class ListenableDirectedWeightedGraph

Is it possible to add edges to a graph through the use of a loop with ListenableDirectedWeightedGraph ?

1

There are 1 answers

0
Anatoliy Semenov On

I build my graph with following code:

private void fillGraph(Tree tree, Graph<Vertex, Edge> graph)
{
    Vertex root = tree.getRootVertex();
    for (Edge edge : root.getEdges())
        addEdge(edge, graph);
}

private void addEdge(Edge edge, Graph<Vertex, Edge> graph)
{
    Vertex source = edge.getSource();
    Vertex target = edge.getTarget();
    if (!graph.containsVertex(source))
        graph.addVertex(source);
    graph.addVertex(target);
    graph.addEdge(source, target, edge);
    if (!target.getEdges().isEmpty())
        for (Edge e: target.getEdges())
            addEdge(e, graph);
}