2

i have written the code below,it works for shortest distance but not for shortest path,

         import math
         def floyd(dist_mat):
         n=len(dist_mat)
         p=[[0]*n]*n
            for k in range(n):
                for i in range(n):
                    for j in range(n):
                        if dist_mat[i][j]>dist_mat[i][k]+dist_mat[k][j]:
                            dist_mat[i][j] = dist_mat[i][k] + dist_mat[k][j]
                            p[i][j] = k+1
            return p

        if __name__ == '__main__':
             print(floyd([[0,5,9999,9999],
                   [50,0,15,5],
                   [30,9999,0,15],
                   [15,9999,5,0]]))

result of this code is: [[4, 1, 4, 2], [4, 1, 4, 2], [4, 1, 4, 2], [4, 1, 4, 2]]
true result is: [[0, 0, 4, 2], [4, 0, 4, 0], [0, 1, 0, 0], [0, 1, 0, 0]],

I will be happy to receive your ideas about why it works wrong soon

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.