我認爲你應該做的是計算根葉的所有最短路徑,然後取最長的計算路徑。一般來說,這將是一個難題,但幸運的是,您正在使用有向無環圖。實際上,由於這種限制,該算法會表現得很好。下面的代碼可以幫助你,它與yWorks開發,並從該site採取:
// To hold an edge list for every path.
YList allShortestPaths = new YList();
// The actual number of proper shortest paths is unknown, so we start with a really great value for 'k'.
YCursor pathCursor = ShortestPaths.kShortestPathsCursor(graph, edgeCostsDP, startNode, endNode, Integer.MAX_VALUE);
if (pathCursor.ok())
{
// The first path between the two nodes having least costs.
final EdgeList firstPath = (EdgeList)pathCursor.current();
final double costsOfFirstPath = calculateCostsForPath(firstPath, edgeCostsDP);
allShortestPaths.add(firstPath);
pathCursor.next();
// Look further.
while (pathCursor.ok())
{
EdgeList currPath = (EdgeList)pathCursor.current();
double currCosts = calculateCostsForPath(currPath, edgeCostsDP);
// If the current path is also a proper shortest path with costs equal to those
// of the first path, then add it to the list.
if (!(currCosts > costsOfFirstPath))
{
allShortestPaths.add(currPath);
pathCursor.next();
}
else
break;
}
}