我认为你应该做的是计算根叶的所有最短路径,然后取最长的计算路径。一般来说,这将是一个难题,但幸运的是,您正在使用有向无环图。实际上,由于这种限制,该算法会表现得很好。下面的代码可以帮助你,它与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;
}
}