Dijkstra’s algorithm is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks.

The algorithm exists in many variants; Dijkstra’s original variant found the shortest path between two nodes, but a more common variant fixes a single node as the “source” node and finds shortest paths from the source to all other nodes in the graph, producing a shortest-path tree.

Dijkstra

Dijkstra’s algorithm to find the shortest path between a and b. It picks the unvisited vertex with the lowest distance, calculates the distance through it to each unvisited neighbor, and updates the neighbor’s distance if smaller. Mark visited (set to red) when done with neighbors.

References

Implementations

  • 1
    
    # TODO: Implement in Python
    
  • 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
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    
    const G = Randomize.Graph({ N: 5, ratio: 1, directed: false, weighted: true });
    const MAX_VALUE = Infinity;
    const S = []; // S[end] returns the distance from start node to end node
    for (let i = 0; i < G.length; i++) S[i] = MAX_VALUE;
    
    function Dijkstra(start, end) {
      let minIndex;
      let minDistance;
      const D = []; // D[i] indicates whether the i-th node is discovered or not
      for (let i = 0; i < G.length; i++) D.push(false);
      S[start] = 0; // Starting node is at distance 0 from itself
      
      let k = G.length;
      while (k--) {
        // Finding a node with the shortest distance from S[minIndex]
        minDistance = MAX_VALUE;
        for (let i = 0; i < G.length; i++) {
          if (S[i] < minDistance && !D[i]) {
            minDistance = S[i];
            minIndex = i;
          }
        }
        if (minDistance === MAX_VALUE) break; // If there is no edge from current node, jump out of loop
        D[minIndex] = true;
        
        // For every unvisited neighbour of current node, we check
        // whether the path to it is shorter if going over the current node
        for (let i = 0; i < G.length; i++) {
          if (G[minIndex][i] && S[i] > S[minIndex] + G[minIndex][i]) {
            S[i] = S[minIndex] + G[minIndex][i];
            
          }
        }
        
      }
      
      if (S[end] === MAX_VALUE) {
        console.log(`there is no path from ${start} to ${end}`);
      } else {
        console.log(`the shortest path from ${start} to ${end} is ${S[end]}`);
      }
      
    }
    
    const s = Randomize.Integer({ min: 0, max: G.length - 1 }); // s = start node
    let e; // e = end node
    do {
      e = Randomize.Integer({ min: 0, max: G.length - 1 });
    } while (s === e);
    
    console.log(`finding the shortest path from ${s} to ${e}`);
    Tracer.delay();
    
    Dijkstra(s, e);