问题描述
我目前正在研究 JGraphT 库以在 Java 中操作图形,特别是我试图确定一个简单图形中 2 个节点之间的最长路径,并且我知道我可以通过使用递归方法到达那里。 无论如何,我在 Java 文档中找到了 AllDirectedGraphs 库,可以在有向图的情况下处理这项工作,这是一个简单的例子:
//the nodes of the graph are countries and the edges are the routes that connect the countries by land
AllDirectedpaths<Country,DefaultEdge> paths = new AllDirectedpaths<Country,DefaultEdge(graph);
//getting all the paths between 2 random countries
List<GraPHPath<Country,DefaultEdge>> longestPath = paths.getAllPaths(countries.get(220),countries.get(325),true,null);
GraPHPath<Country,DefaultEdge> obj = null;
double maxlenght=0;
for( GraPHPath<Country,DefaultEdge> pa :longestPath ) {
if(pa.getLength()>maxlenght)
obj= pa;
}
return obj;
}
很明显,使用相同方法的无向图会抛出异常,所以我想知道是否有类似的解决方法,因为我无法自己找到它。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)