davegimo

dijkstra

Dec 30th, 2022
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. #---------- Lezione 18.1 -----------
  2. # Grafo pesato rappresentati con matrice di adiacenza
  3. # Algoritmo di Dijkstra
  4.  
  5. inf = float("inf")
  6.  
  7. def createGraph(n):
  8.     M = []
  9.     for i in range(n):
  10.         M.append([])
  11.         for j in range(n):
  12.             M[i].append(inf)
  13.     return M
  14.  
  15. def size(G):  #restituisce i nodi
  16.     return len(G)
  17.  
  18. def printGraph(G):
  19.     for i in range(len(G)):
  20.         print(G[i])             # theta(n)
  21.  
  22. def insert(G,i,j,w):
  23.     G[i][j] = w
  24.  
  25. def delete(G,i,j):            # theta(1)
  26.     G[i][j] = inf
  27.  
  28. def isEdge(G,i,j):
  29.     return G[i][j] != inf
  30.  
  31. def weight(G,i,j):
  32.     return G[i][j]
  33.  
  34. def outDegree(G,i):
  35.     s = 0;
  36.     for j in range(len(G)):
  37.         if G[i][j] != inf:
  38.             s = s + 1
  39.     return s
  40.  
  41. def InDegree(G,j):
  42.     s = 0;
  43.     for i in range(len(G)):
  44.         if G[i][j] != inf:
  45.             s = s + 1
  46.     return s
  47.  
  48. def neighbors(G,i):
  49.     V = []
  50.     for j in range(len(G)):
  51.         if G[i][j] != inf:
  52.             V.append([ j, G[i][j] ])
  53.     return V
  54.  
  55. #---------- Programma principale ------------
  56.  
  57. G = createGraph(6)
  58. printGraph(G)
  59. insert(G, 0, 1, 2)
  60. insert(G, 0, 5, 9)
  61. insert(G, 1, 3, 8)
  62. insert(G, 1, 2, 6)
  63. insert(G, 1, 5, 5)
  64. insert(G, 2, 3, 1)
  65. insert(G, 4, 3, 3)
  66. insert(G, 4, 2, 7)
  67. insert(G, 5, 4, 3)
  68. printGraph(G)
  69.  
  70. def minVertex(D,V):
  71.     mindistance = inf
  72.     nodo = -1
  73.  
  74.     for i in range(len(D)):
  75.         if (not V[i]) and (D[i] < mindistance):
  76.             nodo = i
  77.             mindistance = D[i]
  78.            
  79.     return nodo
  80.  
  81. def Dijkstra(G,s):
  82.     #preparo variabili
  83.     n = size(G)
  84.     dist = []
  85.     pred = []
  86.     visited = []
  87.  
  88.     #impostazione per inizio algoritmo
  89.     for i in range(n):
  90.         dist.append(inf)
  91.         visited.append(False)
  92.         pred.append(-1)
  93.  
  94.     #imposto a 0 perchè è il mio punto di partenza
  95.     dist[s] = 0
  96.  
  97.     for i in range(n-1):
  98.         next = minVertex(dist,visited)
  99.         visited[next] = True
  100.         V = neighbors(G,next)
  101.         for [z,w] in V:
  102.             d = dist[next] + w
  103.             if (dist[z] > d):
  104.                 #fase di rilassamento
  105.                 dist[z] = d
  106.                 pred[z] = next
  107.  
  108.     return [dist, pred]
  109.  
  110. D = Dijkstra(G,0)
  111. print("")
  112. print(D[0])
  113. print(D[1])
Advertisement
Add Comment
Please, Sign In to add comment