Search code examples
javajung

how to properly use DijkstraDistance reset


I have an UndirectedSparseGraph g with some nodes and links, I then take the shortest paths and calculate a function:

alg = new DijkstraDistance<Long, String>(g);
// 
alg.enableCaching(false);
// 
for(Node n:g.getVertices()){
    for(Node m:g.getVertices(){
         findAValue(alg.getDistance(n, m));
    }
}

then I update the graph by adding an edge, or removing one edge, like:

g.addEdge(id, n, m, EdgeType.UNDIRECTED);

what should I have to do now to compute again the distances? Should I just type:

alg = new DijkstraDistance<Long, String>(Hummon.g);

or should I better do:

alg.reset();
alg = new DijkstraDistance<Long, String>(Hummon.g);

I have to compute many times the distances by adding a removing an edge to the graph, so I really wish to use the most efficient approach.

BTW: is there something like .update() for the distances? Thanks in advance!


Solution

  • following the suggestion of @Erwin Bolwidt, I have tested .reset() with this code:

     g = new UndirectedSparseGraph<Long, String>();
        // add some vertices
        for(long i=0;i<5;i++){
            g.addVertex(i);
        }
        // add some edges
        g.addEdge("0-1", 0l, 1l, EdgeType.UNDIRECTED);
        g.addEdge("0-2", 0l, 2l, EdgeType.UNDIRECTED);
        g.addEdge("1-3", 1l, 3l, EdgeType.UNDIRECTED);
        g.addEdge("3-4", 3l, 4l, EdgeType.UNDIRECTED);
        alg = new DijkstraDistance<Long, String>(g);
        for(Long n:g.getVertices()){
            for(Long m:g.getVertices()){
                System.out.println(n+"-"+m+" dist "+alg.getDistance(n, m));
            }
        }
        System.out.println("TOPA\n\n\n");
        g.addEdge("2-4", 2l, 4l, EdgeType.UNDIRECTED);
        alg.reset(2l);
        for(Long n:g.getVertices()){
            for(Long m:g.getVertices()){
                System.out.println(n+"-"+m+" dist "+alg.getDistance(n, m));
            }
        }
    

    it's easy to see that .reset() updates the distances.