You are not logged in. Please login at www.codechef.com to post your questions!

×

# Floyd-Warshall Algorithm doubt!

 2 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[i][j] = min(adj[i][j],adj[i][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[i][j] = min(adj[i][j],adj[i][k]+adj[k][j]); } } }  asked 23 Dec '16, 21:36 2.6k●1●10●34 accept rate: 7%

 2 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. EDIT: I found similar question on CodeChef here : https://discuss.codechef.com/questions/5583/floyd-warshall-algorithm answered 23 Dec '16, 21:56 371●2 accept rate: 23% @nikhil_chandak thanks (24 Dec '16, 19:20)
 2 answered 23 Dec '16, 21:41 2★emin3m 62●9 accept rate: 3% @emim3m that was a nice link (23 Dec '16, 23:04)
 0 The correct code is adj[j][k] = min(adj[j][k],adj[j][i]+adj[i][k]); answered 23 Dec '16, 21:39 593●3●32 accept rate: 8% i know that this is the correct code but i want to ask that why does the second one give WA (23 Dec '16, 21:41) Because in the second code you changed the loop variables position (23 Dec '16, 21:41) btw @coder_voder from where are you learning graphs? (23 Dec '16, 21:48) 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? (23 Dec '16, 22:04) geeksforgeeks and commonlounge (23 Dec '16, 22:30) Which topics have you prepared till now? (23 Dec '16, 22:39) There's practice in dp and shortest paths and DAGs left (23 Dec '16, 22:56) Are you comfortable with Stacks, Queues, Greedy Algorithms, Data Structures etc. (23 Dec '16, 23:06) Yes pretty comfortable (23 Dec '16, 23:08) showing 5 of 9 show all
 0 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). answered 23 Dec '16, 22:35 42●4 accept rate: 9% @tanmay_sachan thanks (23 Dec '16, 23:04)

#include<stdio.h>

# include<stdlib.h>

int arr,d,p;

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

            if(d[i][k]+d[k][j]<d[i][j] && i!=j)
{
d[i][j]=d[i][k]+d[k][j];
p[i][j]=p[k][j];
}
}
}
printf("\n D matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("\t%d",d[i][j]);
}
printf("\n");
}
}


}

int main() { int n,i,j,k; FILE *fp; printf("\n 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[i][j]); } floyed(arr,n); }

answered 23 Dec '16, 23:05 -1
accept rate: 0%

2

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

(23 Dec '16, 23:23)
 toggle preview community wiki:
Preview

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×374
×65

question asked: 23 Dec '16, 21:36

question was seen: 867 times

last updated: 24 Dec '16, 19:20