Changing the loops

int the video its given
For k = 1 to 26:
For i = 1 to 26:
For j = 1 t 26:
Can[i][j] = min( Can[i][k] +Can[k][j] , Can[i][j] )
but if just swap the condition of k with i ,
For i = 1 to 26:
For j = 1 to 26:
For k= 1 t 26:
Can[i][j] = min( Can[i][k] +Can[k][j] , Can[i][j] )
i think it should give sam result but not, olese explain

@vinishthanai,
It’s because in dp, you have to solve the smaller subproblem before solving bigger one, so reversing the order of loop does something like, you want to find dp[I]=f(dp[i-1),dp(i-2)) (just a random expression) but you are iterating backwards, i.e you are trying to calculate dp[i], before calculating dp[i-1] or dp[i-2], which obviously is wrong.

i didn’t get it. i was trying to run this on a normal matrix it worked fine, but while submission its giving wrong reslut


https://www.codechef.com/LRNDSA08/problems/ACM14KG3/
please see this question and in this i am getting WA and changing k,i,j in this manner its giving AC,??

for(int k=0;k<26;k++){
for(int i=0;i<26;i++){
for(int j=0;j<26;j++){
parent[i][j]|=(parent[i][k] & parent[k][j]);
}
}
}
for(int i=0;i<26;i++){
for(int j=0;j<26;j++){
for(int k=0;k<26;k++){
parent[i][j]|=(parent[i][k] & parent[k][j]);
}
}
}these 2 cses

@vinishthanai,
I don’t think you have understood the concept of Floyd warshall, you can not simply swap the loops, try reading this. It is one of the best explanations available on FW. If you want to understand how the loops actually work, you will have to understand the proof of the algorithm, which I won’t recommend now.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.