0

Concerning

floyds(int a[][100],int n).

What does 'a' and represent and what does each of the two dimensions of a represent?

What does 'n' represent?

I have a list of locations, with a list of connections between those locations and have computed the distance between those connections that are connect to each other. Now I need to find shortest path between any given two locations (floyd's) - but need to understand how to apply floyds(int a[][100],int n) to my locations array, city dictionaries, and connection arrays.

FYI - Using objective C - iOS.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
user1278974
  • 215
  • 3
  • 11
  • It would help if you told us where you found that function declaration. – Kurt Revis Jun 07 '12 at 03:46
  • Kurt - I had mentioned where I found it in the notes below. (Why am I getting down votes?) Here it is again: prabhakargouda.hubpages.com/hub/floyds-algorithm – user1278974 Jun 07 '12 at 09:56

3 Answers3

2

n is the number of nodes in the graph.

a is an distance matrix of the graph. a[i][j] is the cost (or distance) of the edge from node i to node j.

(Also read the definition of adjacency matrix if you need more help with the concept.)

Kurt Revis
  • 27,695
  • 5
  • 68
  • 74
1
/* Assume a function edgeCost(i,j) which returns the cost of the edge from i to j

2    (infinity if there is none).

3    Also assume that n is the number of vertices and edgeCost(i,i) = 0

4 */

5

6     int path[][];

7     /* A 2-dimensional matrix. At each step in the algorithm, path[i][j] is the shortest path

8        from i to j using intermediate vertices (1..k−1).  Each path[i][j] is initialized to

9        edgeCost(i,j).

10     */

12     procedure FloydWarshall ()

13        for k := 1 to n

14           for i := 1 to n

15              for j := 1 to n

16                 path[i][j] = min ( path[i][j], path[i][k]+path[k][j] );

http://en.wikipedia.org/wiki/Floyd-Warshall

wiki is very good~~~

kaitian521
  • 548
  • 2
  • 10
  • 25
  • The link you provide does not have a function header like the one I asked about [floyds(int a[][100],int n)]. I found a version of the implementation here http://prabhakargouda.hubpages.com/hub/floyds-algorithm which had a function header as follows : floyds(int a[][100],int n), hence my initial question. – user1278974 Jun 07 '12 at 03:43
  • p.s. Given my inputs are source, destination, number of vertices (locations), which locations any location is connected to, and finally the cost or distance associated with any two directed connections, which of these go to a[] and a[][] and n? – user1278974 Jun 07 '12 at 04:02
0
floyd-warshall(W) // W is the adjacent matrix representation of graph..
 n=W.rows;
 for k=1 to n
    for i=1 to n
       for j=1 to n
            w[i][j]=min(W[i][j],W[i][k]+W[k][j]);
 return W;

It's a dp-algorithm.At the k-th iteration here W[i][j] is the shortest path between i and j and the vertices of the shortest path(excluding i and j) are from the set {1,2,3...,k-1,k}.In min(W[i][j],W[i][k]+W[k][j]), W[i][j] is the computed shortest path between i and j at k-1-th iteration and here since the intermediate vertices are from the set {1,2...k-1},so this path does not include vertex k. In W[i][k]+W[k][j],we include vertex k in the path.whichever between the two is minimum is the shortest path at k-th iteration. Basically we check that whether we should include vertex k in the path or not.

kaoski
  • 1