0

I have created this algorithm as to get the shortest point between two selected points on a map.

First I filled the matrix entirely with the distance, weight. I named it so that for example: dist[0,1] refers to the road between point 0 and point 1. Every point has a number assigned to it.

The matrix is all filled accordingly and then the Fjord Warshall Algorithm runs:

for (int k = 0; k < count; ++k)
            {
                for (int i = 0; i < count; ++i)
                {
                    for (int j = 0; j < count; ++j)
                    {
                        if (dist[i,j] > (dist[i,k]+dist[k,j]))
                        {
                            dist[i, j] = dist[i, k] + dist[k, j];
                        }
                    }
                }
            }

This derives the shortest point between every single path. I then check the path that I have as to get it's shortest point:

                shortest = dist[x, y];

Which return the correct value and everything works well. Here is my issue. I need to set it as to see through which points it passes. By this I mean that if I want to go from Point 1 to point 5 and the shortest route is through 3 and 6, it would display 1,3,6,5.

Any ideas? Completely stuck on this one.

NetUser101
  • 272
  • 2
  • 5
  • 20
  • 2
    Does it need to be Floyd-Warshall? Djikstra's algorithm solves the same problem with less computional complexity, and is easier to backtrack the result. – Mephy May 16 '14 at 18:59
  • 1
    Two things: if you look at the [Floyd–Warshall (F–W) algorithm description](https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm) (or pseudocode) you will see how the path is computed. What you have so far is only half the algorithm. Secondly, F–W is ill-suited to compute the shortest path between two points: it’s damn inefficient. You want something like [Dijkstra’s algorithm](https://en.wikipedia.org/wiki/Dijkstra's_algorithm) instead. F–W is for *all pairwise* shortest paths. It’s also worth noting that these Wikipedia articles aren’t very good, textbooks describe this much better – Konrad Rudolph May 16 '14 at 18:59
  • 1
    [Path reconstruction](http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm#Path_reconstruction) – Sergey Kalinichenko May 16 '14 at 19:08
  • I have tried a good number of times as to construct the path, however none of these work. – NetUser101 May 16 '14 at 20:18
  • Keep a matrix that has as entry (i,j) the predecessor of j on the (currently memoized) shortest path from i to j; think about what you want to initialize the matrix to. Whenever you update dist[i][j], also update predecessor[i][j]. – G. Bach May 16 '14 at 20:28

1 Answers1

3

Create another array with the same dimensions as dist, lets call it vert.

Below the line:

dist[i, j] = dist[i, k] + dist[k, j];

Add,

vert[i, j] = k;

Then call a recursive function defined as,

void pr(int x, int y)
{
    if(x == y)
        return;
    pr(x, vert[x, vert[x, y]]);
    cout << vert[x, y];
    pr(vert[vert[x, y]], y);
}
shebang
  • 413
  • 2
  • 8