i'm trying to understand how this algorithm works.
given a question to search the paths from a source s to all the vertices in the graph ,
I thought that i have to proceed as follows:
if no cycle in the graph:
topological sort of the graph
one iteration to calculate the shortest path
else if there is a cycle in the graph:
put s in the queue
v=q.deque
while q is not empty
relax v
My question are :
Is my proceeding good or i have to change it.
When i must check that there is a negative cycle? Thank you
I don't remember details of Bellman-Ford, but basically, assume you have
n
edges andm
vertex,This part can be found on the internet easily.
Related to
When i must check that there is a negative cycle?
, you will do one more iteration and if any value in the last array(the array aftern-1-th iteration
) changes, you will say there is negative cycle, if nothing changes, it indicates there is no negative cycle.This youtube link explains Bellman-Ford well with an example.