Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. import numpy as np
  2.  
  3.  
  4. def floyd(d, p):
  5.     for k in range(0, len(d)):
  6.         for i in range(0, len(d)):
  7.             for j in range(0, len(d)):
  8.                 if d[i][k] + d[k][j] < d[i][j]:
  9.                     d[i][j] = d[i][k] + d[k][j]
  10.                     p[i][j] = p[i][k]
  11.     return d, p
  12.  
  13.  
  14. p = np.array([[1, 2, 3, 4, 5, 6, 7, 8],
  15.               [1, 2, 3, 4, 5, 6, 7, 8],
  16.               [1, 2, 3, 4, 5, 6, 7, 8],
  17.               [1, 2, 3, 4, 5, 6, 7, 8],
  18.               [1, 2, 3, 4, 5, 6, 7, 8],
  19.               [1, 2, 3, 4, 5, 6, 7, 8],
  20.               [1, 2, 3, 4, 5, 6, 7, 8],
  21.               [1, 2, 3, 4, 5, 6, 7, 8]])
  22.  
  23. d = np.array([[0, 9, np.inf, 3, np.inf, np.inf, np.inf, np.inf],
  24.               [9, 0, 2, np.inf, 7, np.inf, np.inf, np.inf],
  25.               [np.inf, 2, 0, 2, 4, 8, 6, np.inf],
  26.               [3, np.inf, 2, 0, np.inf, np.inf, 5, np.inf],
  27.               [np.inf, 7, 4, np.inf, 0, 10, np.inf, np.inf],
  28.               [np.inf, np.inf, 8, np.inf, 10, 0, 7, np.inf],
  29.               [np.inf, np.inf, 6, 5, np.inf, 7, 0, np.inf],
  30.               [np.inf, np.inf, np.inf, np.inf, 9, 12, 10, 0]])
  31.  
  32.  
  33. print floyd(d, p)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement