views:

136

answers:

1

I use the JUNG API to calculate shortest paths between several nodes in medium large graphs (20 to 100 nodes). Right now I'm iterating over my nodes and use the simple 'ShortetsPath' function to calculate the shortest path for two nodes. All the shortest paths are put in an ArrayList.

UnweightedShortestPath<Vertex, SEdge> dist = new UnweightedShortestPath<Vertex, SEdge>(undir);
ArrayList<Vertex> tv = new ArrayList<Vertex>(); // contains nodes for shortestpath
ArrayList<Integer> distances = new ArrayList<Integer>(); // for the distances

for (int j = 0; j <tv.size()-1;j++){ //iterate over nodes
Vertex one = tv.get(j);

for (int k = j+1; k<tv.size();k++){ //iterate over next nodes
    Vertex two = tv.get(k);
    Number n = dist.getDistance(one, two);
    int d;
    if (n == null) {
        d = 5000000;
    }
    else {
        d = n.intValue();
    }
    distances.add(d);
}

}

I would like to speed up the calculation because I have to calculate this for many graphs and nodes. As far as I know, only Dijkstra is available in the JUNG API. So my questions are: Can I use Dijkstra to boost performance? Are other algorithms available in the JUNG API? Would it make sense to use another graph implementation which offers more different methods for shortest paths?

Thanks so far :)

A: 

The UnweightedShortestPath class in JUNG uses a Breadth-First-Search Algorithm, which has O(n^2) runtime. The Dijkstra algorithm works essentially the same, just for weighted graphs instead of unweighted graphs, so it's runtime is also O(n^2).

However, it looks like you're interested in the distances between all possible pairs of nodes in your graph, but you're using a pairwise approach. So your total runtime is O(n * n^2) = O(n^3). Instead you could use a global shortest path algorithm like the Johnson's algorithm (http://en.wikipedia.org/wiki/Johnson's_algorithm). That's runtime O(n^2 * log(n+ne)). So a bit faster overall.

It's not implemented in JUNG as far as I know, but you might be able to grab it off google code search.

jweile