0

I am referring this tutorial: http://www.geeksforgeeks.org/dynamic-programming-set-16-floyd-warshall-algorithm/

The author mentions this:

We can modify the solution to print the shortest paths also by storing the predecessor information in a separate 2D matrix.

Am a bit confused at what this predecessor information is.

So, how do I store the path for displaying later?

Vpp Man
  • 2,384
  • 8
  • 43
  • 74

1 Answers1

0

If you want to reconstruct the path from node x_1 to x_n, you can do so by going to node x_2 and reconstructing the path from x_2 to x_n. The path from x_2 to x_n does not change if x_1 change. So what you want to store is information about what node is next when going from x_1 to x_n. This can be done with a |V| x |V| matrix, where entry [i][j] gives the index of the next node on the path from i to j.

In this part of the code

if (dist[i][k] + dist[k][j] < dist[i][j])
    dist[i][j] = dist[i][k] + dist[k][j];

you should add an assignment to a next node matrix.

Silas
  • 468
  • 3
  • 7