Jhoveran
Uploaded by
26 SLIDES
7 VUES
1LIKES

Dijkstra's Algorithm using Python

DESCRIPTION

Visual explanation of Dijkstra's algorithm using Python

1 / 26

Télécharger la présentation

Dijkstra's Algorithm using Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript

Playing audio...

  1. #Función para construir adyacencia Dijkstra's Algorithm def constructAdj(edges, V): adj = [[] for _ in range(V)] for edge in edges: u, v, wt = edge adj[u].append([v, wt]) Source adj[v].append([u, wt]) 4 9 1 0 2 return adj 1 9 def dijkstra(V, edges, src): adj = constructAdj(edges, V) pq = [] # Cola de prioridad (min-heap) 2 3 1 7 6 dist = [sys.maxsize] * V heapq.heappush(pq, [0, src]) 3 dist[src] = 0 5 4 3 5 while pq: 9 2 u = heapq.heappop(pq)[1] for x in adj[u]: v, weight = x[0], x[1] if dist[v] > dist[u] + weight: # Actualizar la distancia de v dist[v] = dist[u] + weight heapq.heappush(pq, [dist[v], v]) return dist

  2. #Función para construir adyacencia Dijkstra's Algorithm def constructAdj(edges, V): adj = [[] for _ in range(V)] for edge in edges: u, v, wt = edge adj[u].append([v, wt]) adj[v].append([u, wt]) return adj def dijkstra(V, edges, src): adj = constructAdj(edges, V) pq = [] # Cola de prioridad (min-heap) dist = [sys.maxsize] * V heapq.heappush(pq, [0, src]) dist[src] = 0 while pq: u = heapq.heappop(pq)[1] for x in adj[u]: v, weight = x[0], x[1] Edsger W. Dijkstra (1930-2002) if dist[v] > dist[u] + weight: # Actualizar la distancia de v dist[v] = dist[u] + weight heapq.heappush(pq, [dist[v], v]) return dist

  3. Dijkstra's Algorithm #Función para construir adyacencia Source def constructAdj(edges, V): 4 9 1 0 2 adj = [[] for _ in range(V)] 1 9 for edge in edges: u, v, wt = edge 2 3 1 7 6 adj[u].append([v, wt]) adj[v].append([u, wt]) 3 5 return adj 4 3 5 9 2 adj = [[1,9], [2,4], [0,9], [0,4], [ ], [ ], [ ], [ ], [ ], [ ], [2,1], [2,1], [2,1], [2,1], [3, 9] [2,1], [3, 9] [2,1], [3, 9], [7, 3] adj = [[1,9], [2,4], [3,3] [0,9], [4,2], [6,1] [0,4], [0,4], [5,1] [0,4], [5,1], [7,9] [0,4], [5,1], [7,9] [0,4], [5,1], [7,9] [0,4], [5,1], [7,9] [0,4], [5,1], [7,9] adj = [[1,9], [2,4], [3,3] [0,9], [4,2], [6,1] [0,9], [4,2], [6,1] [0,9], [4,2], [6,1] [0,9], [4,2], [6,1] [0,9], [4,2], [6,1] [0,9], [4,2], [6,1] adj = [[1,9], [2,4], [3,3] [0,9], [4,2] [0,4], [0,3], [0,3], [0,3], [0,3], [0,3], [4, 2] [0,3], [4, 2], [5, 9] [0,3], [4, 2], [5, 9] [0,3], [4, 2], [5, 9] adj = [[ ], [ ], [ ], [ ], [ ], [ ], [ ], [ ],] [ ],] [ ],] [ ],] [ ],] [ ],] [ ],] [2,9],] [2,9],] [2,9],] [2,9],] [2,9], [5, 3]] adj = [[1,9], [0,9], [ ], [ ], [ ], [ ], [ ], [ ], [ ], [ ], [1,1], [1,1], [1,1], [1,1], [1,1], [1,1], [4, 5] [1,1], [4, 5] adj = [[1,9], [2,4], [3,3] [0,9], [0,4], [0,3], [ ], [1,2], [1,2], [1,2], [1,2], [1,2], [3, 2] [1,2], [3, 2] [1,2], [3, 2], [6, 5] [1,2], [3, 2], [6, 5] adj = [[1,9], [2,4], [3,3] adj = [[1,9], [2,4], [3,3] adj = [[1,9], [2,4], [3,3] adj = [[1,9], [2,4], [3,3] adj = [[1,9], [2,4], [3,3] adj[0] adj[1] u v wt Input edges = [[0, 1, 9], [0, 2, 4], [0, 3, 3], [1, 4, 2], [1, 6, 1], [2, 5, 1], [2, 7, 9], [3, 4, 2], [3, 5, 9], [4, 6, 5], [5, 7, 3]] v = 8

  4. Dijkstra's Algorithm 0 def dijkstra(V, edges, src): ∞ ∞ Source 4 9 adj = constructAdj(edges, V) 1 0 2 1 pq = []# Cola de prioridad (min-heap) 9 dist = [sys.maxsize] * V 2 3 1 7 ∞ 6 ∞ heapq.heappush(pq, [0, src]) dist[src] = 0 3 5 4 ∞ 3 5 9 2 dist nodo ∞ ∞ pq = [0, 0] ∞ ∞ ∞ ∞ ∞ ∞ ∞ 0 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  5. Dijkstra's Algorithm 0 while pq: ∞ ∞ Source 4 u = heapq.heappop(pq)[1] 9 1 0 2 for x in adj[u]: 1 9 v, weight = x[0], x[1] if dist[v] > dist[u] + weight: 2 3 1 7 ∞ 6 ∞ # Actualizar la distancia de v dist[v] = dist[u] + weight 3 heapq.heappush(pq, [dist[v], v]) 5 4 ∞ 3 ∞ 5 return dist 9 2 ∞ pq = [0, 0] = [1,9], [2,4], [3,3] adj[0] 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ dist[ ] = vértice = 0 1 2 3 4 5 6 7

  6. Dijkstra's Algorithm 0 while pq: ∞ 9 Source 4 u = heapq.heappop(pq)[1] 9 1 0 2 for x in adj[u]: 1 9 v, weight = x[0], x[1] if dist[v] > dist[u] + weight: 2 3 1 7 ∞ 6 ∞ # Actualizar la distancia de v dist[v] = dist[u] + weight 3 heapq.heappush(pq, [dist[v], v]) 5 4 ∞ 3 ∞ 5 return dist 9 2 ∞ pq = [0, 0], [9, 1] adj[0] = [1,9], [2,4], [3,3] 0 9 ∞ ∞ ∞ ∞ ∞ ∞ dist[ ] = vértice = 0 1 2 3 4 5 6 7

  7. Dijkstra's Algorithm 0 while pq: ∞ 9 Source 4 u = heapq.heappop(pq)[1] 9 1 0 2 for x in adj[u]: 1 9 v, weight = x[0], x[1] if dist[v] > dist[u] + weight: 2 3 1 7 ∞ 6 ∞ # Actualizar la distancia de v dist[v] = dist[u] + weight 3 heapq.heappush(pq, [dist[v], v]) 5 4 ∞ 3 ∞ 5 return dist 9 2 ∞ pq = [0, 0], [9, 1] adj[0] = [1,9], [2,4], [3,3] 0 9 3 ∞ ∞ ∞ ∞ ∞ dist[ ] = vértice = 0 1 2 3 4 5 6 7

  8. Dijkstra's Algorithm 0 while pq: 4 9 Source 4 u = heapq.heappop(pq)[1] 9 1 0 2 for x in adj[u]: 1 9 v, weight = x[0], x[1] if dist[v] > dist[u] + weight: 2 3 1 7 ∞ 6 ∞ # Actualizar la distancia de v dist[v] = dist[u] + weight 3 heapq.heappush(pq, [dist[v], v]) 5 4 ∞ 3 ∞ 5 return dist 9 2 ∞ pq = [0, 0], [4, 2], [9, 1] adj[0] = [1,9], [2,4], [3,3] 0 9 3 ∞ ∞ ∞ ∞ ∞ dist[ ] = vértice = 0 1 2 3 4 5 6 7

  9. Dijkstra's Algorithm 0 while pq: 4 9 Source 4 u = heapq.heappop(pq)[1] 9 1 0 2 for x in adj[u]: 1 9 v, weight = x[0], x[1] if dist[v] > dist[u] + weight: 2 3 1 7 ∞ 6 ∞ # Actualizar la distancia de v dist[v] = dist[u] + weight 3 heapq.heappush(pq, [dist[v], v]) 5 4 ∞ 3 ∞ 5 return dist 9 2 ∞ pq = [0, 0], [4, 2], [9, 1] = [1,9], [2,4], [3,3] adj[0] 0 9 3 ∞ ∞ ∞ ∞ ∞ dist[ ] = vértice = 0 1 2 3 4 5 6 7

  10. Dijkstra's Algorithm 0 while pq: 4 9 Source 4 u = heapq.heappop(pq)[1] 9 1 0 2 for x in adj[u]: 1 9 v, weight = x[0], x[1] if dist[v] > dist[u] + weight: 2 3 1 7 ∞ 6 ∞ # Actualizar la distancia de v dist[v] = dist[u] + weight 3 heapq.heappush(pq, [dist[v], v]) 5 4 ∞ 3 3 5 return dist 9 2 ∞ pq = [0, 0], [3, 3], [4, 2], [9, 1] adj[0] = [1,9], [2,4], [3,3] 0 9 4 3 ∞ ∞ ∞ ∞ dist[ ] = vértice = 0 1 2 3 4 5 6 7

  11. Dijkstra's Algorithm 0 4 9 Source pop 4 9 1 0 2 1 9 {0,0} push {3,3} 2 3 1 7 ∞ 6 ∞ {4,2} 3 5 {9,1} 4 ∞ 3 3 5 9 2 ∞ 0 0 9 1 4 2 3 3 dist[ ] = vértice = 4 5 6 7

  12. Dijkstra's Algorithm 0 4 9 Source pop 4 9 1 0 2 1 9 {3,3} push 2 3 1 7 ∞ 6 ∞ {4,2} 3 5 {9,1} 4 ∞ 3 3 5 9 2 ∞ 0 0 9 1 4 2 3 3 dist[ ] = vértice = 4 5 6 7

  13. Dijkstra's Algorithm 0 4 9 Source pop 4 9 1 0 2 1 9 {4,2} push {5,4} 2 3 1 7 ∞ 6 ∞ {9,1} 3 5 {11,5} 4 5 3 3 5 11 9 2 0 9 4 3 5 11 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  14. Dijkstra's Algorithm 0 4 9 Source pop 4 9 1 0 2 1 9 {4,2} push {5,4} 2 3 1 7 ∞ 6 ∞ {9,1} 3 5 {11,5} 4 5 3 3 5 11 9 2 0 9 4 3 5 11 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  15. Dijkstra's Algorithm 0 4 9 Source pop 4 9 1 0 2 1 9 {5,4} push {5,5} 2 3 1 13 7 6 ∞ {9,1} 3 5 {13,7} 4 5 3 3 5 5 9 2 0 9 4 3 5 5 13 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  16. Dijkstra's Algorithm 0 4 9 Source pop 4 9 1 0 2 1 9 {5,4} push {5,5} 2 3 1 13 7 6 ∞ {9,1} 3 5 {13,7} 4 5 3 3 5 5 9 2 0 9 4 3 5 5 13 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  17. Dijkstra's Algorithm 0 4 7 Source pop 4 9 1 0 2 1 9 {5,5} push {7,1} 2 3 1 13 7 10 6 {10,6} 3 5 {13,7} 4 5 3 3 5 5 9 2 0 7 4 3 5 5 10 13 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  18. Dijkstra's Algorithm 0 4 7 Source pop 4 9 1 0 2 1 9 {5,5} push {7,1} 2 3 1 13 7 10 6 {10,6} 3 5 {13,7} 4 5 3 3 5 5 9 2 0 7 4 3 5 5 10 13 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  19. Dijkstra's Algorithm 0 4 7 Source pop 4 9 1 0 2 1 9 {5,5 push {7,1} 2 3 1 8 7 10 6 {8,7} 3 5 {10,6} 4 5 3 3 5 5 9 2 0 7 4 3 5 5 10 8 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  20. Dijkstra's Algorithm 0 4 7 Source pop 4 9 1 0 2 1 9 {7,1} push 2 3 1 8 7 10 6 {8,7} 3 5 {10,6} 4 5 3 3 5 5 9 2 0 7 4 3 5 5 10 8 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  21. Dijkstra's Algorithm 0 4 7 Source pop 4 9 1 0 2 1 9 {5,5 push 2 3 1 8 7 6 8 {8,6} 3 5 {8,7} 4 5 3 3 5 5 9 2 0 7 4 3 5 5 8 8 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  22. Dijkstra's Algorithm 0 4 7 Source pop 4 9 1 0 2 1 9 {8,6} {5,5 push 2 3 1 8 7 6 8 3 5 {8,7} 4 5 3 3 5 5 9 2 0 7 4 3 5 5 8 8 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  23. Dijkstra's Algorithm 0 4 7 Source pop 4 9 1 0 2 1 9 {8,6} {5,5 push 2 3 1 8 7 6 8 3 5 {8,7} 4 5 3 3 5 5 9 2 0 7 4 3 5 5 8 8 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  24. Dijkstra's Algorithm 0 4 7 Source pop 4 9 1 0 2 1 9 {8,6} {8,7} {5,5 push 2 3 1 8 7 6 8 3 5 4 5 3 3 5 5 9 2 0 7 4 3 5 5 8 8 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  25. Dijkstra's Algorithm 0 4 7 Source pop 4 9 1 0 2 1 9 {5,5 push 2 3 1 8 7 6 8 3 5 4 5 3 3 5 5 9 2 0 7 4 3 5 5 8 8 dist[ ] = vértice = 0 1 2 3 4 5 6 7

  26. Dijkstra's Algorithm 0 4 7 Source 4 9 1 0 2 1 9 {8,7} {5,5 2 3 1 8 7 6 8 3 5 4 5 3 3 5 5 2 9 0 7 4 3 5 5 8 8 dist[ ] = vértice = 0 1 2 3 4 5 6 7

More Related