繁体   English   中英

JUNG API中最短路径算法的性能

[英]Performance of shortest path algorithm in JUNG API

我使用JUNG API计算中型大型图形(20至100个节点)中几个节点之间的最短路径。 现在,我正在遍历节点,并使用简单的“ ShortetsPath”功能来计算两个节点的最短路径。 所有最短路径都放在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);
}

}

我想加快计算速度,因为我必须为许多图形和节点进行计算。 据我所知,JUNG API中仅提供Dijkstra。 所以我的问题是:我可以使用Dijkstra来提高性能吗? JUNG API是否提供其他算法? 使用另一个为最短路径提供更多不同方法的图形实现是否有意义?

到目前为止谢谢:)

JUNG中的UnweightedShortestPath类使用广度优先搜索算法,该算法具有O(n ^ 2)运行时。 Dijkstra算法的工作原理基本相同,只是用于加权图而不是未加权图,因此它的运行时间也是O(n ^ 2)。

但是,您似乎对图形中所有可能的节点对之间的距离感兴趣,但您使用的是成对方法。 因此,您的总运行时间为O(n * n ^ 2)= O(n ^ 3)。 相反,您可以使用像约翰逊算法( http://en.wikipedia.org/wiki/Johnson's_algorithm )之类的全局最短路径算法。 那是运行时O(n ^ 2 * log(n + ne))。 因此总体上要快一些。

据我所知,它在JUNG中尚未实现,但是您可以从Google代码搜索中获取它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM