I am able to use this algorithm to find the longest path in a weighted DAG (using topological sort and then relaxing each edge). My question now is if there is an algorithm to find the top 3 longest paths of the DAG? Or, is there any javascript or java library that implements this algorithm?
1 Answers
You can easily compute first longest path, and you can use following algorithm for finding the next longest path:
Delete each edge of the main longest path one by one and the run the algorithm again to find the longest path on the modified graph then put back the deleted edge and delete one other edge.
Why this works?
You need a path the is not exactly the first longest path, so the second longest path must be different in at least one edge, so if you ignore one edge and find the longest path for each of the edges you eventually find a longest path that don't share at least one edge with the main longest path.
The third longest path is a path that is a longest path and doesn't share at least one edge with the first and the second longest path.