Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floyd's Algorithm implemented in Python. two dimensional print array

I am attempting to print my python program that implements floyds algorithm.

n=5
    for k in range(n):
        for j in range(n):
            for i in range(n):
                if A[i][k]+A[k][j]<A[i][j]:
                    A[i][j]=A[i][k]+A[k][j]

I am trying to print the solution in the same format as below: (not including the first column and row)

  0 1 2 3 4
  -----------
0|0 1 4 500 3
1|1 0 2 500 4
2|4 2 0 1   5
3|500 500 1 0 3
4|3 4 5 3   0

500 indicates infinity

Any ideas? I am hoping indices will do the trick.

Also does anyone know the order of magnitude of this algorithm?

like image 875
user2627901 Avatar asked Jan 27 '26 11:01

user2627901


1 Answers

You could use this.

>>> A = [[0, 1, 4, 500, 3], [1, 0, 2, 500, 4],[4,2,0,1,5],[500,500,1,0,3],[3,4,5,3,0]]
>>> for elem in A:
        print "\t".join(['Inf' if val == 500 else str(val) for val in elem])


0   1   4   Inf 3
1   0   2   Inf 4
4   2   0   1   5
Inf Inf 1   0   3
3   4   5   3   0
like image 76
Sukrit Kalra Avatar answered Jan 30 '26 05:01

Sukrit Kalra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!