""" Floyd's algorithm find all minimum path distances between pairs of vertices Runs in O(n**3) """ inf = float('inf') vertices = range(5) # adjacency matrix a = [[0, 1, inf, 4, inf], [1, 0, 1, 1, 5], [inf, 1, 0, 6, 1], [4, 1, 6, 0, inf], [inf, 5, 1, inf, 0]] for x in vertices: for v1 in vertices: for v2 in vertices: dis = a[v1][x] + a[x][v2] if dis < a[v1][v2]: a[v1][v2] = a[v2][v1] = dis print a