Dijkstra's Algorithm

Dijkstra's algorithm finds the shortest path from a source vertex to all other vertices in a weighted graph with non-negative edge weights. It uses a priority queue and is widely used in routing and network protocols.

Dijkstra's Algorithm Visualization

Complexity

Time Complexity: O((V + E) log V)
Space Complexity: O(V)

Pseudocode

function Dijkstra(graph, start):
  dist = {v: ∞ for all v}
  dist[start] = 0
  pq = priority queue with (0, start)
  
  while pq is not empty:
    current = pq.extract_min()
    
    for each neighbor with weight w:
      new_dist = dist[current] + w
      if new_dist < dist[neighbor]:
        dist[neighbor] = new_dist
        pq.insert(new_dist, neighbor)