liveBook · Manning

Dijkstra's algorithm

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

A graph algorithm that computes shortest paths from a source node when all edge weights are nonnegative.

Dijkstra's algorithm finds shortest paths from one source through a weighted graph whose edge costs are nonnegative. Unlike BFS or DFS, it optimizes cumulative distance, making it useful for routing from home to work or between map locations. section 3.2 section 3.4

The search maintains distances and predecessors while repeatedly expanding the currently closest unrelaxed node:

  • Initialize every distance to infinity, set the source distance to zero, and place nodes in the unrelaxed set. section 3.4.1
  • Select the node with the smallest cumulative distance, mark it seen, and inspect each child. section 3.4.1
  • Relax each edge: replace a child's distance and predecessor when the route through the current node is shorter. section 3.4.1
  • Continue until the relevant nodes are processed, then retrace predecessors to recover the route. section 3.4.1 section 3.5

A small route

Starting at S, the algorithm first processes S, then C, D, and A as their cumulative distances become smallest, updating predecessors and eventually reaching B. On map data, parallel edges should be reduced to the lowest-weight edge and self-loops ignored; NetworkX provides dijkstra_path for weighted routes. section 3.4.1 section 3.5

Algorithm 3.3 Dijkstra’s algorithm
Inputs: A graph with weighted edges and a source node
Output: Shortest path from the source to all other nodes in the graph
  
Initialize shortest_dist ← empty
Initialize unrelaxed_nodes ← empty
Initialize seen ← empty
  
For node in graph
    Set shortest_dist[node] = Infinity
    Add node to unrelaxed_nodes
    Set shortest_dist[source] ← 0
  
While unrelaxed_nodes is not empty do
    Set node ← unrelaxed_nodes.pop()
    Add node to seen
    For child in node.expand() do
        If child in seen then skip
        Update distance ← shortest_dist[node] + length of edge to child
        If distance < shortest_dist[child] then
           Update shortest_dist[child] ← distance
           Update child.parent ← node
Return shortest_dist

Use the pseudocode to follow initialization, node expansion, edge relaxation, and predecessor updates. section 3.4.1

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Explains priority-ordered expansion, distance relaxation, predecessors, pseudocode, and graph-data adaptations without formal proof.
Examples
Uses a home-to-work problem, an S-C-D-A-B graph walkthrough, Toronto map routing, and OpenStreetMap graph irregularities.
What it assumes
Assumes weighted graphs, source and destination nodes, and graph traversal; introduces shortest-path search alongside BFS, DFS, and uniform-cost search.
What you can do after
Apply it via NetworkX or an adapted implementation to compute optimal weighted routes and compare exploration and memory trade-offs.
How it connects
Connects shortest-path search with BFS, DFS, uniform-cost search, bidirectional search, dynamic programming, and routing.

Also explained in

Compare how 5 books explain Dijkstra's algorithm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.