-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra_algorithm.py
More file actions
39 lines (30 loc) · 1.43 KB
/
dijkstra_algorithm.py
File metadata and controls
39 lines (30 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# You're given an integer 'start' and a list of 'edges' of pairs of integers.
#
# Write a function that computes the lengths of the shortest paths between 'start'
# and all of the other vertices in the graph using Dijkstra's algorithm and returns them in an array.
# Each index 'i' in the output array should represent the length of the shortest path between 'start' and vertex 'i'.
# If no path is found from 'start' to vertex 'i', then 'output[i]' should be -1.
#
# Note that the graph represented by 'edges' won't contain any
# self-loops (vertices that have an outbound edge to themselves) and will only
# have positively weighted edges (i.e., no negative distances).
"""
>>> dijkstrasAlgorithm(0, [[[1, 7]], [[2, 6], [3, 20], [4, 3]], [[3, 14]], [[4, 2]], [], []])
[0, 7, 13, 27, 10, -1]
"""
import heapq
def dijkstrasAlgorithm(start, edges):
distances = [float('inf') for _ in range(len(edges))]
distances[start] = 0
pq = [(start, 0)]
while pq:
currentVertex, currentWeight = heapq.heappop(pq)
if currentWeight > distances[currentVertex]:
continue
neighbors = edges[currentVertex]
for vertex, weight in neighbors:
distance = currentWeight + weight
if distance < distances[vertex]:
distances[vertex] = distance
heapq.heappush(pq, (vertex, distance))
return [distance if distance != float('inf') else -1 for distance in distances]