I have seen people say that Floyd-Warshall can be modified (easily) to detect cycles. Note: I'm assuming the graph does not have negative cycles. I want to know how to modify the algorithm? And why is it correct? Also, what cycles can you make it detect? Shortest,Longest, of length k? -- and what adjustments in the algorithm do you need to make?
I read this question, that got me started thinking about this question: Find cycle of shortest length in a directed graph with positive weights
For the above link, I don't see why making the diagonal of adj. matrix = INFINITY gives us the cycles of shortest length going through (i,i).
Lastly, this site: http://en.algoritmy.net/article/45708/Floyd-Warshall-algorithm says:
Floyd-Warshall algorithm can be easily modified to detect cycles.
If we fill negative infinity value at the diagonal of the matrix and run the
algorithm, then the matrix of predecessors will contain also all cycles in the graph
(the diagonal will not contain only zeros, if there is a cycle in the graph).
So I don't think I understand this because this seems wrong because if memory serves me correct, detecting all cycles can't be done in poly time. Am I misunderstanding what is being said? (Also, not really sure what is meant by matrix of predecessors.)
I believe matrix of predecessors is the end result of computing the distances from any vertex to any other vertex, F_ij=length of shortest route from node i to node j, 0 if there is no route. If the diagonals of the adjacency matrix are set to infinity, it effectively prohibits self loops, so the only way F_ii can be non-zero is if there is a path from node i to itself through at least one other node, i.e. a loop. In general any path finding algorithm can be turned into a loop finding algorithm. Take the node you want to find a loop through, say v, and split it into two nodes v_out and v_in, with v-out having only the edges out of v and v_in having only the edges into v. Now use the algorithm to find a path from v_out to v_in, this will become a loop if v_out and v_in are merged back to v.