Dijkstra's Algorithm

Dijkstra's algorithm grows a set of finalized nodes outward from the start, always finalizing the unvisited node with the smallest known distance next (using a priority queue). It relaxes each neighbour's tentative distance as it goes.

Time O((V + E) log V) Weighted Informed

How it works

Dijkstra's algorithm grows a set of finalized nodes outward from the start, always finalizing the unvisited node with the smallest known distance next (using a priority queue). It relaxes each neighbour's tentative distance as it goes.

On a grid with non-negative weights it finds the true lowest-cost path, generalizing BFS to weighted terrain.

Implementation

function dijkstraPathfind(grid, start, end, allowDiag) {
  const rows = grid.length;
  const cols = grid[0].length;
  const dist = Matrix(rows, cols, Infinity);
  const visited = new Set();
  const prev = new Map();
  const heap = PriorityQueue("priority");
  dist[start[0]][start[1]] = 0;
  priorityQueuePush(heap, {
    priority: 0,
    row: start[0],
    col: start[1]
  });
  while (priorityQueueSize(heap)) {
    const __pattern1 = priorityQueuePop(heap);
    const cr = __pattern1.row;
    const cc = __pattern1.col;
    const currentKey = key(cr, cc);
    if (visited.has(currentKey)) continue;
    visited.add(currentKey);
    visitNode(cr, cc);
    if (cr === end[0] && cc === end[1]) {
      reconstructPath(prev, start, end);
      return;
    }
    for (const [nr, nc] of getNeighbors(grid, cr, cc, allowDiag)) {
      const neighborKey = key(nr, nc);
      if (visited.has(neighborKey)) continue;
      const nd = dist[cr][cc] + stepCost(grid, cr, cc, nr, nc);
      if (nd < dist[nr][nc]) {
        dist[nr][nc] = nd;
        prev.set(neighborKey, [cr, cc]);
        priorityQueuePush(heap, {
          priority: nd,
          row: nr,
          col: nc
        });
        pushFrontier(nr, nc);
      }
    }
  }
  reportNoPath();
}

Advantages

  • Finds the optimal lowest-cost path on graphs with non-negative weights.
  • Handles weighted terrain that BFS cannot.

Disadvantages

  • Expands in all directions without goal awareness, so it visits more nodes than A*.
  • Does not support negative edge weights.

When to use it

Use it for weighted shortest paths when you have no admissible heuristic; add a heuristic and it becomes A*.