Question

I am looking at the Floyd-Warshall algorithm.

let dist be a |V| × |V| array of minimum distances initialized to ∞ (infinity)

// part 1 
for each vertex v
   dist[v][v] ← 0

// part 2
for each edge (u,v)
   dist[u][v] ← w(u,v)  // the weight of the edge (u,v)

// part 3
for k from 1 to |V|
   for i from 1 to |V|
      for j from 1 to |V|
         if dist[i][k] + dist[k][j] < dist[i][j] then
            dist[i][j] ← dist[i][k] + dist[k][j]

In the page, it says the Floyd–Warshall algorithm assumes that there are no negative cycles. So my question is what will happen if the entry graph hides negative circle. Will the output dist represents another graph hiding negative circle? Does not part 1 invalid this?

Was it helpful?

Solution

Floyd-Warshall algorithm is for find the shortest path. If negative circle exist, there is no shortest path(you can find a infinitely small(negative) path).

If negative circle exists then what will happen? I will say the output of Floyd means nothing, that is to say, that algorithm doesn't work(since there is no shortest path, how it can work?).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top