Advertisement
Guest User

Untitled

a guest
Apr 1st, 2014
1,546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. N     = 6
  2. inf   = float("inf")
  3. dist  = [[inf]*N for v in range(N)]
  4. f     = [[None]*N for v in range(N)]
  5. nocost= 0.01
  6. edges = [
  7.     (0, 4, nocost),
  8.     (0, 3, nocost),
  9.     (1, 4, 1),
  10.     (1, 3, nocost),
  11.     (1, 2, nocost),
  12.     (2, 0, nocost),
  13.     (2, 5, nocost),
  14.     (4, 0, nocost),
  15.     (5, 2, nocost),
  16.     (5, 3, nocost)]
  17.  
  18. #self-edges
  19. edges += [(v, v, nocost) for v in range(N)]
  20.  
  21. for first, second, weight in edges:
  22.     dist[first][second] = weight
  23.     f[first][second] = second
  24.  
  25. for k in range(N):
  26.     for i in range(N):
  27.         for j in range(N):
  28.             if dist[i][j] >= dist[i][k] + dist[k][j]:
  29.                 dist[i][j] = dist[i][k] + dist[k][j]
  30.                 if dist[i][j] < inf:
  31.                     #prevent circular routes for unreachable vertices
  32.                     f[i][j]    = f[i][k]
  33.  
  34. for l in dist:
  35.     print(l)
  36. for l in f:
  37.     print(l)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement