Floyd-Warshall Algorithm doubt!

floyd-warshall
graphs

#1

So I know that the algo is -

for (int k = 0; k < n; k ++){
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            adj*[j] = min(adj*[j],adj*[k]+adj[k][j]);
        }
    }
}

I wanted to ask that why does it become wrong when we change it to -

for (int i = 0; i < n; i ++){
    for (int j = 0; j < n; j++){
        for (int k = 0; k < n; k++){
            adj*[j] = min(adj*[j],adj*[k]+adj[k][j]);
        }
    }
}

#2

The correct code is adj[j][k] = min(adj[j][k],adj[j]+adj[k]);


#3

Similar discussion : https://www.quora.com/Why-is-the-order-of-the-loops-in-Floyd-Warshall-algorithm-important-to-its-correctness


#4

Since smaller sub-problems need to be solved first and the sub-problems size is controlled by k so we should always put k in the outermost for loop (As k governs which vertices you are permitted to use internal to your path from source i to destination j). The order of i and j are irrelevant but k must go first as it controls the size of the sub-problems.

This may help : http://stackoverflow.com/questions/27700629/why-does-order-of-3-loops-in-floyd-warshall-matters

Or may be this one :

EDIT: I found similar question on CodeChef here : https://discuss.codechef.com/questions/5583/floyd-warshall-algorithm


#5

That would change the order in which the subproblems are solved in the DP

it would try to take values from data which has not been computed yet(it would try to solve bigger subproblems before the smaller ones)…

as a result giving garbage values(or infinity, i.e distance between 2 non connected vertices).


#6

#include<stdio.h>
#include<stdlib.h>
int arr[10][10],d[10][10],p[10][10];

void floyed(int c[][10],int n)
{
int i,j,k,q;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
d*[j]=arr*[j];
p*[j]=-1;
}
}
for(i=1;i<=n;i++)
{
for(i=1;i<=n;i++)
{
d**=0;
p**=-1;
}
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{

			if(d*[k]+d[k][j]<d*[j] && i!=j)
			{
				d*[j]=d*[k]+d[k][j];
				p*[j]=p[k][j];
			}
		}
	}
	printf("

D matrix:
“);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(” %d",d*[j]);
}
printf("
");
}
}
}

int main()
{
int n,i,j,k;
FILE fp;
printf("
Enter no. of vertices:");
scanf("%d",&n);
fp=fopen(“floyed.txt”,“r”);
if(fp==NULL)
exit(0);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
fscanf(fp,"%d",&arr
[j]);
}
floyed(arr,n);
}


#7

i know that this is the correct code but i want to ask that why does the second one give WA


#8

Because in the second code you changed the loop variables position


#9

btw @coder_voder from where are you learning graphs?


#10

Actually my INOI Preparation is not going well. Btw i was seeing Tushar Roy’s Videos and some tutorials from GeeksForGeeks and from where you are learning this?


#11

geeksforgeeks and commonlounge


#12

Which topics have you prepared till now?


#13

There’s practice in dp and shortest paths and DAGs left


#14

@emim3m that was a nice link


#15

@tanmay_sachan thanks


#16

Are you comfortable with Stacks, Queues, Greedy Algorithms, Data Structures etc.


#17

Yes pretty comfortable


#18

@sweetnandy He didn’t ask for Floyd-Warshall algorithm! Please read the question carefully. :slight_smile:


#19

@nikhil_chandak thanks