User profile
Speed: 500ms
dijkstra.py
def dijkstra(graph, start):
distances = {node: float('inf') for node in graph}
distances[start] = 0
pq = [(0, start)]
while pq:
curr_dist, curr_node = heapq.heappop(pq)
if curr_dist > distances[curr_node]:
continue
for neighbor, weight in graph[curr_node]:
# Calculate new path cost
distance = curr_dist + weight
if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(pq, (distance, neighbor))

Priority Queue (Min-Heap)

ACTIVE
Queue is empty

Distance Table

ComplexityO(E log V)
Step1 / 0
Current Logic

Select start and end nodes to begin.