Shortest Path: Dijkstra's Algorithm

Dijkstra's Algorithm is a greedy algorithm used to find the shortest path from a single source vertex to all other vertices in a Weighted Graph. It is the foundation for network routing protocols (OSI Layer 3) and map-based navigation systems.


1. Requirement: Non-Negative Weights

CRITICAL: Dijkstra's algorithm only works correctly on graphs where all edge weights are Non-Negative.


2. The Core Mechanism: Relaxation

The algorithm maintains a "Shortest Distance" estimates for all nodes, initialized to Infinity. It then repeatedly:

  1. Select: Pick the unvisited node with the smallest distance estimate.
  2. Relax: For all neighbors of this node, check if the path through the current node is shorter than the neighbor's current estimate.
  3. Update: If it is shorter, update the neighbor's estimate.

3. Complexity Analysis

Efficiency depends on the data structure used to find the "Minimum Distance" node.

Implementation Time Complexity Use Case
Array O(V²) Dense graphs (many edges).
Priority Queue (Min-Heap) O((V + E) log V) Sparse graphs (Standard industry approach).
Fibonacci Heap O(E + V log V) Theoretical optimum (rarely used in practice).

4. Implementation (Priority Queue)

function dijkstra(graph, startNode) {
    let distances = {};
    let pq = new PriorityQueue(); // Min-Heap

    for (let node in graph) {
        distances[node] = Infinity;
    }
    distances[startNode] = 0;
    pq.enqueue(startNode, 0);

    while (!pq.isEmpty()) {
        let { node: u, priority: dist } = pq.dequeue();

        if (dist > distances[u]) continue; // Optimization: Skip stale values

        for (let neighbor in graph[u]) {
            let weight = graph[u][neighbor];
            let newDist = distances[u] + weight;

            if (newDist < distances[neighbor]) { // RELAXATION
                distances[neighbor] = newDist;
                pq.enqueue(neighbor, newDist);
            }
        }
    }
    return distances;
}

5. Interview Pro-Tips: Comparison with BFS


Technical Summary

  1. Greedy: Always processing the closest known node.
  2. Relaxation: Updating neighbors based on better paths.
  3. Min-Heap: The engine that makes the algorithm efficient for sparse graphs.
  4. Non-Negative: The fundamental constraint for algorithm correctness.