# How to implement the Floyd Algorithm?

I have to implement the Floyd Algorithm in Python.

I have to use this template of code. The adjacency Matrix is given in the exercise however I have to convert it into the bimatrix as seen below. Then lastly the Floyd algorithm Needs to be executed onto the bimatrix.

My main Problem right now consists of converting the adjacency Matrix into the bimatrix, which contains the distance as well as the previous Knot.

``````import math
import pprint as pp

def createBiMatrix(mat):

bimatrix = []

pass

return bimatrix

def updateForNode(bimat, node):

pass;

return bimat

def determinePath(bimat, start, end):

recursionStep(bimat, start, end)

print(end)

def recursionStep(bimat, start, end):

pass

return

if __name__ == "__main__":

matsize = 5

mat = [matsize * [math.inf] for i in range(matsize)]
mat = 0
mat = 2
mat = 10
mat = 0
mat = 3
mat = 12
mat = 10
mat = 3
mat = 0
mat = 1
mat = 12
mat = 0
mat = 1
mat = 6
mat = 0

bim = createBiMatrix(mat)
pp.pprint(bim)

for i in range(matsize):
bim = updateForNode(bim, i)
print("Step " + str(i) + ":")
pp.pprint(bim)

start = 0
end = 3
print("shortest path (" + str(start) + " nach " + str(end) + "):")
determinePath(bim, start, end)
`````` On

Assuming, that the `bimatrix` is meant to be a list that on index `i` stores shortest path to vertex `i` and the preceding vertex

``````createBiMatrix(mat):
bimatrix = [(math.inf, None) for _ in range(len(mat))]

for from in range(len(mat)):
for to in range(len(mat)):
if mat[from][to] < bimatrix[to]:
bimatrix[to] = (mat[from][to], from)
return bimatrix
``````