I've searched on here for how to find the longest simple path in a directed cyclic graph (simple meaning that each node is visited only once, to avoid the path being infinite), and have came across solutions like this. All such solutions I've found however only show how to calculate the length of the longest path, not the actual nodes involved in that path.
My question is hence, how could an algorithm like that be modified so that it extracted the nodes involved in the longest path? Similar to how the Floyd-Warshall all-pairs shortest path algorithm may be modified to allow path reconstruction.
To find the actual path, all you need is to keep track of the longest distance path ( you get this path from the predecessors).
The longest path of vj= the longest path among precedessors U {vj}
Here is how:
v1 > v2 >... > vn
..vj
...vj
) be the longest distance fromv1
tovj
. Then dist(vj
)=max(dist(u1
)+1,dist(u2
)+1,...,dist(uk
)+1) whereu1,u2,...,uk
are the predecessors ofvj
.path(vj)=path(ui)U{vj}
whereui
is the predecessor with maximum length (i.e. the one we chose in dist(vj
) ) .vj
.dist(vj)
with actual pathpath(vj)
.