Chapter 6: Shortest Paths

Algorithms
RelaxationDijkstraBellman-FordNegative cycles Floyd-WarshallA* searchUnweighted paths with BFSComparison
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Concepts

Distinguish single-source, all-pairs and single-pair shortest-path problems; explain relaxation and the predecessor (π) array.

Dijkstra and Bellman-Ford

Run both algorithms step by step on an example graph, fill in the distance and π tables, and trace the Java code.

Negative edges and cycles

Show why Dijkstra fails on negative edges; detect negative cycles with Bellman-Ford and Floyd-Warshall.

Floyd-Warshall and A*

Update the all-pairs D and Π matrices for k = 1…n; apply A*'s goal-directed search with f = g + h, and compare the algorithms.

Introduction

A Road Network Is a Graph

  • Intersections are vertices, roads are edges, and road lengths or travel times are weights.
  • The length of a path = the sum of its edge weights.
  • A shortest path between two vertices is a path of minimum total weight; its length is written δ(s, v).
  • Example: from Home to School, Home–1–2–4–School = 4 + 2 + 3 + 3 = 12; Home–1–4–School has fewer edges but costs 13.
432632723Home12Mkt3Lake4School
In a weighted graph, the path with the fewest edges need not be the shortest. Navigation, network routing (OSPF, RIP), pathfinding in games and logistics all solve this problem.

Introduction

Kinds of Shortest-Path Problems

Single-source From s to every vertex. Dijkstra, Bellman-Ford, BFS.
All-pairs Every pair (u, v); the result is an n × n matrix. Floyd-Warshall.
Single-pair Only s → t; goal-directed search. A*.
AlgorithmWhat it findsRestriction / note
Dijkstrasource to all verticesedge weights must be nonnegative
Bellman-Fordsource to all verticeshandles negative edges, detects negative cycles; slower than Dijkstra
Floyd-Warshallall pairshandles negative edges; detects negative cycles on the diagonal
A*start to goalspeeds up search with a heuristic estimate
BFSsource to all verticesunweighted (or equal-weight) graphs only

Introduction

The Basic Operation: Relaxation

Each vertex keeps two values:

  • d[v]: the shortest-path estimate so far (∞ at first, 0 at the source)
  • π[v]: the vertex before v on that path (its predecessor)
RELAX(u, v, w):
  if d[u] + w(u, v) < d[v]:
    d[v] = d[u] + w(u, v)
    π[v] = u
3suv059 → 8…

d[u] + w = 5 + 3 = 8 < 9 → d[v] = 8, π[v] = u

Every algorithm in this chapter relies on relaxation; they differ in the order in which they relax edges and how many times. d[v] can only decrease and never drops below the true δ(s, v).

Introduction

Properties of Shortest Paths

Optimal substructure

Every subpath of a shortest path is itself a shortest path. If s ⇝ u ⇝ v is shortest, so is s ⇝ u. Dynamic programming and greedy methods rest on this.

Triangle inequality

For every edge (u, v), δ(s, v) ≤ δ(s, u) + w(u, v). Relaxation fixes edges that violate it.

Negative cycle

If a cycle of negative total weight is reachable from the source, each lap makes the path shorter: the shortest path is undefined (−∞).

Simple paths

Without negative cycles a shortest path has no cycle, so it has at most V − 1 edges. That is where Bellman-Ford's V − 1 passes come from.

To rebuild a path, follow π from the target back to the source and list the vertices in reverse. Together, all π edges form a shortest-path tree.

Dijkstra

Dijkstra's Algorithm

  • Finds shortest paths from a source vertex to every other vertex (single-source).
  • Devised by Edsger W. Dijkstra in 1956 and published in 1959.
  • Correct only on graphs whose edge weights are nonnegative.
  • Greedy: at each step it picks the not-yet-settled vertex with the smallest d value and settles it.
  • The neighbors of the chosen vertex are relaxed.
The ripple analogyA ripple from a stone dropped at the source reaches the vertices in order of distance: the nearest first, then the next… Dijkstra settles vertices in exactly this order. The set of settled vertices is called S.
sd=0ad=2bd=4cd=6ripple order: s, a, b, c

Dijkstra

Dijkstra: The Steps

Step 1: Choose the start vertex and set its distance to 0; every other vertex gets ∞.

Initialization: d[s] = 0, d[v] = ∞, π[v] = undefined, S = ∅.

Step 2: From the chosen vertex, compute distances to its not-yet-processed neighbors.

Candidate distance for neighbor v: d[u] + w(u, v).

Step 3: If a shorter path is found, update the neighbor's distance (relaxation).

If d[u] + w(u, v) < d[v], d[v] and π[v] change.

Step 4: Mark the processed vertex (add it to S) and select the unmarked vertex with the smallest distance.

The selection uses an array or a priority queue.

Step 5: Repeat steps 2–4 until every vertex is processed.

Unreachable vertices keep distance ∞.

Dijkstra

Dijkstra Example 1: Undirected Graph, Source 1

79141015112691234560∞∞∞∞∞
vertexdπ
10–
2∞–
3∞–
4∞–
5∞–
6∞–

Initialization: source 1, d[1] = 0, all others ∞. No vertex is settled.

79141015112691234560∞∞∞∞∞
vertexdπ
10–
2∞–
3∞–
4∞–
5∞–
6∞–

Smallest d among unsettled vertices: 1 (d = 0); it is settled.

791410151126912345607∞∞∞∞
vertexdπ
10–
271
3∞–
4∞–
5∞–
6∞–

Edge (1, 2): d[1] + 7 = 7 < ∞ → d[2] = 7, π[2] = 1.

7914101511269123456079∞∞∞
vertexdπ
10–
271
391
4∞–
5∞–
6∞–

Edge (1, 3): d[1] + 9 = 9 < ∞ → d[3] = 9, π[3] = 1.

7914101511269123456079∞∞14
vertexdπ
10–
271
391
4∞–
5∞–
6141

Edge (1, 6): d[1] + 14 = 14 < ∞ → d[6] = 14, π[6] = 1.

7914101511269123456079∞∞14
vertexdπ
10–
271
391
4∞–
5∞–
6141

Smallest d among unsettled vertices: 2 (d = 7); it is settled. Settled neighbors (1) are skipped.

7914101511269123456079∞∞14
vertexdπ
10–
271
391
4∞–
5∞–
6141

Edge (2, 3): d[2] + 10 = 17 ≥ 9 → no change.

791410151126912345607922∞14
vertexdπ
10–
271
391
4222
5∞–
6141

Edge (2, 4): d[2] + 15 = 22 < ∞ → d[4] = 22, π[4] = 2.

791410151126912345607922∞14
vertexdπ
10–
271
391
4222
5∞–
6141

Smallest d among unsettled vertices: 3 (d = 9); it is settled. Settled neighbors (1, 2) are skipped.

791410151126912345607920∞14
vertexdπ
10–
271
391
4203
5∞–
6141

Edge (3, 4): d[3] + 11 = 20 < 22 → d[4] = 20, π[4] = 3.

791410151126912345607920∞11
vertexdπ
10–
271
391
4203
5∞–
6113

Edge (3, 6): d[3] + 2 = 11 < 14 → d[6] = 11, π[6] = 3.

791410151126912345607920∞11
vertexdπ
10–
271
391
4203
5∞–
6113

Smallest d among unsettled vertices: 6 (d = 11); it is settled. Settled neighbors (1, 3) are skipped.

7914101511269123456079202011
vertexdπ
10–
271
391
4203
5206
6113

Edge (6, 5): d[6] + 9 = 20 < ∞ → d[5] = 20, π[5] = 6.

7914101511269123456079202011
vertexdπ
10–
271
391
4203
5206
6113

Smallest d among unsettled vertices: 4 (d = 20); it is settled. (4 and 5 tie at 20; the lower-numbered 4 is taken.) Settled neighbors (2, 3) are skipped.

7914101511269123456079202011
vertexdπ
10–
271
391
4203
5206
6113

Edge (4, 5): d[4] + 6 = 26 ≥ 20 → no change.

7914101511269123456079202011
vertexdπ
10–
271
391
4203
5206
6113

Smallest d among unsettled vertices: 5 (d = 20); it is settled. Settled neighbors (4, 6) are skipped.

7914101511269123456079202011
vertexdπ
10–
271
391
4203
5206
6113

Done: distances from 1 are 2:7, 3:9, 6:11, 4:20, 5:20. The green edges form the shortest-path tree (e.g. 1 → 3 → 6 → 5).

Dijkstra

Dijkstra Example 2: Directed Graph, Sets Q and S

6332114212345∞∞∞∞0

Q

duπ
05/
∞1/
∞2/
∞3/
∞4/

S

udπ

Initialization: source 5 has distance 0, all others ∞. Every vertex is in Q, S = ∅.

6332114212345∞4∞20

Q

duπ
245
425
∞1/
∞3/

S

udπ
50/

Step 1: take 5 from Q and put it in S with d = 0. Relax: (5,2): ∞ → 4; (5,4): ∞ → 2.

6332114212345∞3320

Q

duπ
324
334
∞1/

S

udπ
50/
425

Step 2: take 4 from Q and put it in S with d = 2. Relax: (4,2): 4 → 3; (4,3): ∞ → 3.

633211421234563320

Q

duπ
334
612

S

udπ
50/
425
234

Step 3: take 2 from Q and put it in S with d = 3. Relax: (2,1): ∞ → 6. (2 and 3 tie at 3; the lower-numbered 2 is taken.)

633211421234563320

Q

duπ
612

S

udπ
50/
425
234
334

Step 4: take 3 from Q and put it in S with d = 3. Relax: (3,4): 4 is already in S. No shorter path (no relaxation).

633211421234563320

Q

duπ

S

udπ
50/
425
234
334
162

Step 5: take 1 from Q and put it in S with d = 6. Relax: (1,3): 3 is already in S; (1,4): 4 is already in S. No shorter path (no relaxation).

633211421234563320
12345
d63320
π2445/

Final state: shortest paths from 5. Only the π edges (green) remain: 5→4→2→1 (6), 5→4→3 (3).

Dijkstra

Dijkstra: Pseudocode

DIJKSTRA(G, source):
  dist = [ ]; visited = [ ]
  pq = { }
  for each v in V:
    dist[v] = infinity
    visited[v] = 0
  dist[source] = 0
  pq.add(source, 0)
  while pq is not empty do:
    u = pq.extractMin()   // smallest distance
    if visited[u] == 0:
      visited[u] = 1
      for each (u, v) in G[u]:
        if dist[u] + weight(u, v) < dist[v]:
          dist[v] = dist[u] + weight(u, v)
          pq.add(v, dist[v])
  return dist
Lazy deletion: a vertex may enter the queue several times; when a stale entry comes out, the visited check skips it. To recover paths, relaxation also stores prev[v] = u.

Dijkstra

Dijkstra (Priority Queue): Step Through the Code

void dijkstra(int src) {
  int[] dist = new int[V + 1];
  int[] prev = new int[V + 1];
  boolean[] visited = new boolean[V + 1];
  Arrays.fill(dist, INF);
  dist[src] = 0;
  PriorityQueue<int[]> pq =
    new PriorityQueue<>((a, b) -> a[1] - b[1]);
  pq.add(new int[] { src, 0 });
  while (!pq.isEmpty()) {
    int u = pq.poll()[0];
    if (visited[u]) continue; // stale entry
    visited[u] = true;
    for (Edge e : adj.get(u)) {
      int v = e.to, w = e.weight;
      if (!visited[v]
          && dist[u] + w < dist[v]) {
        dist[v] = dist[u] + w;
        prev[v] = u;
        pq.add(new int[] { v, dist[v] });
      }
    }
  }
}
6332114212345∞∞∞∞0

Set up the arrays (vertices 1..5, index 0 unused); dist[5] = 0 and (5, 0) is added to the queue.

6332114212345∞∞∞∞0

Poll the entry with the smallest distance, (5, 0): u = 5.

6332114212345∞∞∞∞0

5 is settled: visited[5] = true.

6332114212345∞∞∞∞0

Edge 5→2 (w = 4): 0 + 4 < ∞ → condition true.

6332114212345∞4∞∞0

dist[2] = 4, prev[2] = 5; add (2, 4) to the queue.

6332114212345∞4∞∞0

Edge 5→4 (w = 2): 0 + 2 < ∞ → condition true.

6332114212345∞4∞20

dist[4] = 2, prev[4] = 5; add (4, 2) to the queue.

6332114212345∞4∞20

Poll the entry with the smallest distance, (4, 2): u = 4.

6332114212345∞4∞20

4 is settled: visited[4] = true.

6332114212345∞4∞20

Edge 4→2 (w = 1): 2 + 1 < 4 → condition true.

6332114212345∞3∞20

dist[2] = 3, prev[2] = 4; add (2, 3) to the queue.

6332114212345∞3∞20

Edge 4→3 (w = 1): 2 + 1 < ∞ → condition true.

6332114212345∞3320

dist[3] = 3, prev[3] = 4; add (3, 3) to the queue.

6332114212345∞3320

Poll the entry with the smallest distance, (2, 3): u = 2.

6332114212345∞3320

2 is settled: visited[2] = true.

6332114212345∞3320

Edge 2→1 (w = 3): 3 + 3 < ∞ → condition true.

633211421234563320

dist[1] = 6, prev[1] = 2; add (1, 6) to the queue.

633211421234563320

Poll the entry with the smallest distance, (3, 3): u = 3.

633211421234563320

3 is settled: visited[3] = true.

633211421234563320

Edge 3→4 (w = 2): visited[4] = true → condition false.

633211421234563320

Poll the entry with the smallest distance, (2, 4): u = 2.

633211421234563320

visited[2] is already true: this is 2's stale entry (2, 4) → skipped with continue (lazy deletion).

633211421234563320

Poll the entry with the smallest distance, (1, 6): u = 1.

633211421234563320

1 is settled: visited[1] = true.

633211421234563320

Edge 1→3 (w = 6): visited[3] = true → condition false.

633211421234563320

Edge 1→4 (w = 3): visited[4] = true → condition false.

633211421234563320

The queue is empty and the loop ends. dist = [6, 3, 3, 2, 0], prev = [2, 4, 4, 5, –].

Dijkstra

Path Reconstruction: The π (Predecessor) Array

Result of Example 2: π = [2, 4, 4, 5, /]. Walking back from target 1:

1 ← π[1] = 2 ← π[2] = 4 ← π[4] = 5 (source)

Reversed, the path is 5 → 4 → 2 → 1, of length 2 + 1 + 3 = 6 = d[1].

void printPath(int[] prev, int[] dist,
            int src, int target) {
  if (dist[target] == INF) {   // unreachable
    System.out.print("no path"); return;
  }
  Deque<Integer> stack = new ArrayDeque<>();
  for (int v = target; v != src; v = prev[v])
    stack.push(v);         // walk back
  stack.push(src);
  while (!stack.isEmpty())
    System.out.print(stack.pop() + " ");
}
633211421234563320

A stack turns the vertices collected backward into the right order.

Dijkstra · Step through the code

Path Reconstruction: Tracing printPath(prev, dist, 5, 1)

void printPath(int[] prev, int[] dist,
            int src, int target) {
  if (dist[target] == INF) {   // unreachable
    System.out.print("no path"); return;
  }
  Deque<Integer> stack = new ArrayDeque<>();
  for (int v = target; v != src; v = prev[v])
    stack.push(v);         // walk back
  stack.push(src);
  while (!stack.isEmpty())
    System.out.print(stack.pop() + " ");
}
6332114212345stack

From Dijkstra Example 2 (source 5, target 1). d[1] = 6 ≠ INF: reachable. Start with an empty stack.

6332114212345stack1← top

1 ≠ source → push it. Next, v = prev[1] = 2.

6332114212345stack21← top

2 ≠ source → push it. Next, v = prev[2] = 4.

6332114212345stack421← top

4 ≠ source → push it. Next, v = prev[4] = 5.

6332114212345stack421← top

v = 5 = source: the loop ends. 5 is not on the stack yet.

6332114212345stack5421← top

Push the source 5: on top, with the target at the bottom.

6332114212345stack421← top

Pop and print 5 (LIFO: last in, first out).

6332114212345stack21← top

Pop and print 4 (LIFO: last in, first out).

6332114212345stack1← top

Pop and print 2 (LIFO: last in, first out).

6332114212345stack

Pop and print 1. Path 5 → 4 → 2 → 1, length 2 + 1 + 3 = 6.

Dijkstra

Dijkstra: Running Time

The cost depends on how we select the vertex with the smallest distance:

Selection structureSelectRelaxTotalBest for
Array (linear scan)V × O(V)E × O(1)O(V²)dense (E ≈ V²)
Binary heap (priority queue)O(log V)O(log V)O((V + E) log V)sparse (E ≪ V²)
Fibonacci heapO(log V) amortizedO(1) amortizedO(E + V log V)theoretical best
A numeric exampleV = 10⁴, E = 5·10⁴ (a road network): array ≈ 10⁸ operations, heap ≈ 6·10⁴ · 14 ≈ 8.4·10⁵ operations.
MemoryThe d, π and visited arrays take O(V); with lazy deletion the queue holds at most E entries → O(V + E).

Dijkstra

Why Does Dijkstra Fail on Negative Edges?

  • The correctness proof relies on this: once u is settled, any path leaving S has length at least d[u], because edges are nonnegative and a path can only grow.
  • A negative edge breaks that assumption: a vertex processed later can open a shorter path to an already settled vertex.
  • Adding a constant to every weight is no fix: paths with more edges are penalized more, so the shortest path changes.
  • With negative edges, use Bellman-Ford.
23-2ABC023

A is settled; relaxation gives d[B] = 2, d[C] = 3.

23-2ABC023

Smallest d: B (2). Dijkstra settles B, assuming "no shorter path to B can exist".

23-2ABC02 ✗3

C is processed: d[C] + (−2) = 1 < 2, but B is already in S. The result d[B] = 2 is wrong; the true δ(A, B) = 1 (A → C → B).

Bellman-Ford

The Bellman-Ford Algorithm

  • Finds shortest paths from a source vertex to every other vertex (single-source).
  • Named after Richard Bellman (1958) and Lester Ford Jr. (1956); E. F. Moore gave the same method in 1959.
  • Handles negative edge weights.
  • Detects negative cycles reachable from the source.
  • Slower than Dijkstra: O(V · E).
How it worksIt keeps an array of shortest-path estimates, all ∞ except 0 at the source. It examines every edge in turn and updates the estimates, repeating this sweep V − 1 times. There is no vertex selection and nothing greedy: it is dynamic programming, where pass i builds shortest paths of at most i edges from the previous pass.

Bellman-Ford

Bellman-Ford: The Steps

Step 1: Set the start vertex's distance to 0 and every other vertex's to ∞.

π[v] = undefined.

Step 2: Examine the edges one by one and update distances by relaxation; repeat this pass V − 1 times.

The edge order is fixed within a pass; it affects how many passes are needed, not the result.

Step 3: Once a vertex's distance improves, its neighbors' distances may improve in later checks.

Each pass pushes the improvement at least one edge further along the path.

Step 4: To check for negative cycles, examine every edge once more; if some edge still relaxes, a negative cycle is reachable from the source.

Without such a cycle no edge relaxes after V − 1 passes.

Bellman-Ford

Why V − 1 Passes? Complexity

  • Without negative cycles every shortest path has at most V − 1 edges.
  • Invariant: after pass i, d[v] = δ(s, v) for every v that has a shortest path with at most i edges.
  • Pass i is sure to relax the path's i-th edge; V − 1 passes complete every path.
  • Each pass examines E edges: O(V · E) time in total, O(V) extra memory.
pass 1pass 2pass 3pass 4sv₁v₂v₃v₄after pass i, the path of i edges is final
GraphTime
Sparse, E ≈ VO(V²)
Dense, E ≈ V²O(V³)
Best case (early exit)O(E)
Early exit: if no d changes during a pass, later passes cannot change anything either; leave the loop.

Bellman-Ford

Bellman-Ford Example 1: Relaxing Edge by Edge

93622122sv₁v₄v₂v₃0∞∞∞∞
(s,v₁) 9(s,v₂) 3(v₁,v₂) 6(v₁,v₄) 2(v₂,v₁) 2(v₂,v₃) 1(v₃,v₂) 2(v₃,v₄) 2

Initialization: d[s] = 0, all others ∞. Each pass examines the edges in the order shown above.

93622122sv₁v₄v₂v₃09∞∞∞
(s,v₁) 9(s,v₂) 3(v₁,v₂) 6(v₁,v₄) 2(v₂,v₁) 2(v₂,v₃) 1(v₃,v₂) 2(v₃,v₄) 2

Pass 1, edge (s, v₁): 0 + 9 = 9 < ∞ → d[v₁] = 9.

93622122sv₁v₄v₂v₃093∞∞
(s,v₁) 9(s,v₂) 3(v₁,v₂) 6(v₁,v₄) 2(v₂,v₁) 2(v₂,v₃) 1(v₃,v₂) 2(v₃,v₄) 2

Pass 1, edge (s, v₂): 0 + 3 = 3 < ∞ → d[v₂] = 3.

93622122sv₁v₄v₂v₃093∞∞
(s,v₁) 9(s,v₂) 3(v₁,v₂) 6(v₁,v₄) 2(v₂,v₁) 2(v₂,v₃) 1(v₃,v₂) 2(v₃,v₄) 2

Pass 1, edge (v₁, v₂): 9 + 6 = 15 ≥ 3 → no change.

93622122sv₁v₄v₂v₃093∞11
(s,v₁) 9(s,v₂) 3(v₁,v₂) 6(v₁,v₄) 2(v₂,v₁) 2(v₂,v₃) 1(v₃,v₂) 2(v₃,v₄) 2

Pass 1, edge (v₁, v₄): 9 + 2 = 11 < ∞ → d[v₄] = 11.

93622122sv₁v₄v₂v₃053∞11
(s,v₁) 9(s,v₂) 3(v₁,v₂) 6(v₁,v₄) 2(v₂,v₁) 2(v₂,v₃) 1(v₃,v₂) 2(v₃,v₄) 2

Pass 1, edge (v₂, v₁): 3 + 2 = 5 < 9 → d[v₁] = 5.

93622122sv₁v₄v₂v₃053411
(s,v₁) 9(s,v₂) 3(v₁,v₂) 6(v₁,v₄) 2(v₂,v₁) 2(v₂,v₃) 1(v₃,v₂) 2(v₃,v₄) 2

Pass 1, edge (v₂, v₃): 3 + 1 = 4 < ∞ → d[v₃] = 4.

93622122sv₁v₄v₂v₃053411
(s,v₁) 9(s,v₂) 3(v₁,v₂) 6(v₁,v₄) 2(v₂,v₁) 2(v₂,v₃) 1(v₃,v₂) 2(v₃,v₄) 2

Pass 1, edge (v₃, v₂): 4 + 2 = 6 ≥ 3 → no change.

93622122sv₁v₄v₂v₃05346
(s,v₁) 9(s,v₂) 3(v₁,v₂) 6(v₁,v₄) 2(v₂,v₁) 2(v₂,v₃) 1(v₃,v₂) 2(v₃,v₄) 2

Pass 1, edge (v₃, v₄): 4 + 2 = 6 < 11 → d[v₄] = 6.

93622122sv₁v₄v₂v₃05346
(s,v₁) 9(s,v₂) 3(v₁,v₂) 6(v₁,v₄) 2(v₂,v₁) 2(v₂,v₃) 1(v₃,v₂) 2(v₃,v₄) 2

Pass 2: none of the eight edges relaxes → early exit. Result: v₁ = 5, v₂ = 3, v₃ = 4, v₄ = 6 (with this order one pass was enough).

Bellman-Ford

Bellman-Ford Example 2: Negative Edges

3-1-2-214-4SABCDE0∞∞∞∞∞
(E,D) -4(E,A) 4(C,D) -2(C,E) 1(B,C) -2(A,B) -1(S,A) 3

A graph with negative edges, source S. Each pass examines the edges in the order shown above (back to front: the worst order).

3-1-2-214-4SABCDE03∞∞∞∞
(E,D) -4(E,A) 4(C,D) -2(C,E) 1(B,C) -2(A,B) -1(S,A) 3

Pass 1: only (S,A): ∞ → 3. The other edges still start at ∞.

3-1-2-214-4SABCDE032∞∞∞
(E,D) -4(E,A) 4(C,D) -2(C,E) 1(B,C) -2(A,B) -1(S,A) 3

Pass 2: only (A,B): ∞ → 2.

3-1-2-214-4SABCDE0320∞∞
(E,D) -4(E,A) 4(C,D) -2(C,E) 1(B,C) -2(A,B) -1(S,A) 3

Pass 3: only (B,C): ∞ → 0.

3-1-2-214-4SABCDE0320-21
(E,D) -4(E,A) 4(C,D) -2(C,E) 1(B,C) -2(A,B) -1(S,A) 3

Pass 4: only (C,D): ∞ → -2; (C,E): ∞ → 1.

3-1-2-214-4SABCDE0320-31
(E,D) -4(E,A) 4(C,D) -2(C,E) 1(B,C) -2(A,B) -1(S,A) 3

Pass 5: only (E,D): -2 → -3. Going through E gives D a shorter path: 1 + (−4) = −3.

3-1-2-214-4SABCDE0320-31
(E,D) -4(E,A) 4(C,D) -2(C,E) 1(B,C) -2(A,B) -1(S,A) 3

Check pass: no edge relaxes → no negative cycle (A→B→C→E→A = −1 −2 + 1 + 4 = +2). All V − 1 = 5 passes were needed.

Bellman-Ford

Bellman-Ford Example 3: The Graph from Dijkstra Example 2

6332114212345∞∞∞∞0
12345
d∞∞∞∞0
π/////
(1,3) 6(1,4) 3(2,1) 3(3,4) 2(4,2) 1(4,3) 1(5,2) 4(5,4) 2

Initialization: source 5 has distance 0, all others ∞. Edge order: (1,3), (1,4), (2,1), (3,4), (4,2), (4,3), (5,2), (5,4).

6332114212345∞4∞20
12345
d∞4∞20
π/5/5/
(1,3) 6(1,4) 3(2,1) 3(3,4) 2(4,2) 1(4,3) 1(5,2) 4(5,4) 2

Pass 1: (5,2): ∞ → 4; (5,4): ∞ → 2.

633211421234573320
12345
d73320
π2445/
(1,3) 6(1,4) 3(2,1) 3(3,4) 2(4,2) 1(4,3) 1(5,2) 4(5,4) 2

Pass 2: (2,1): ∞ → 7; (4,2): 4 → 3; (4,3): ∞ → 3. Edge (4,2) found a shorter path to 2.

633211421234563320
12345
d63320
π2445/
(1,3) 6(1,4) 3(2,1) 3(3,4) 2(4,2) 1(4,3) 1(5,2) 4(5,4) 2

Pass 3: (2,1): 7 → 6. Since 2 improved in the previous pass, (2,1) relaxes again.

633211421234563320
12345
d63320
π2445/
(1,3) 6(1,4) 3(2,1) 3(3,4) 2(4,2) 1(4,3) 1(5,2) 4(5,4) 2

Pass 4: no shorter path found (no edge relaxes).

Bellman-Ford

Checking for Negative Cycles (Pass V)

Try relaxing every edge one last time: if some edge still has d[v] > d[u] + w(u, v), a negative cycle is reachable from the source. For the result of Example 3:

edged[v]d[u] + w(u, v)result
(1, 3)d[3] = 3d[1] + w = 6 + 6 = 123 ≯ 12 ✓
(1, 4)d[4] = 2d[1] + w = 6 + 3 = 92 ≯ 9 ✓
(2, 1)d[1] = 6d[2] + w = 3 + 3 = 66 ≯ 6 ✓
(3, 4)d[4] = 2d[3] + w = 3 + 2 = 52 ≯ 5 ✓
(4, 2)d[2] = 3d[4] + w = 2 + 1 = 33 ≯ 3 ✓
(4, 3)d[3] = 3d[4] + w = 2 + 1 = 33 ≯ 3 ✓
(5, 2)d[2] = 3d[5] + w = 0 + 4 = 43 ≯ 4 ✓
(5, 4)d[4] = 2d[5] + w = 0 + 2 = 22 ≯ 2 ✓
No edge relaxes → no negative cycle; d = [6, 3, 3, 2, 0] is final.

Bellman-Ford

Detecting a Negative Cycle: Example

21-31SABC0-11-2

Cycle A → B → C → A: 1 + (−3) + 1 = −1. Edge order: (S,A), (A,B), (B,C), (C,A).

passSABC
10130
2002-1
30-11-2
  • After V − 1 = 3 passes d = [0, −1, 1, −2]; the values on the cycle drop by 1 every pass.
  • Check: for (A, B), d[A] + 1 = 0 < d[B] = 1 → a negative cycle exists.
  • Vertices on the cycle, and those reachable from it, have shortest-path length −∞ (undefined).

Bellman-Ford

Bellman-Ford: Pseudocode

BELLMAN_FORD(G, source):
  dist = [ ]
  for each v in V:
    dist[v] = infinity
  dist[source] = 0
  repeat for i = 1 to |V| - 1:
    for each (u, v, weight) in E:
      if dist[u] + weight < dist[v]:
        dist[v] = dist[u] + weight
  for each (u, v, weight) in E:
    if dist[u] + weight < dist[v]:
      error "Graph contains a negative-weight cycle"
  return dist
Two partsAn outer loop of V − 1 passes over all edges, then a single check pass.
Edge listBellman-Ford never selects vertices, so storing the graph as an edge list E is enough.

Bellman-Ford

Bellman-Ford: Step Through the Code

void bellmanFord(int src) {
  int[] dist = new int[V];
  int[] prev = new int[V];
  Arrays.fill(dist, INF);
  dist[src] = 0;
  for (int i = 1; i < V; i++) {
    for (Edge e : edges) {
      int u = e.from, v = e.to, w = e.weight;
      if (dist[u] != INF
          && dist[u] + w < dist[v]) {
        dist[v] = dist[u] + w;
        prev[v] = u;
      }
    }
  }
  for (Edge e : edges) { // check
    int u = e.from, v = e.to, w = e.weight;
    if (dist[u] != INF
        && dist[u] + w < dist[v])
      throw new IllegalStateException(
          "Negative cycle");
  }
}
-245301230∞∞∞

Source 0. dist = [0, ∞, ∞, ∞]. Edge list: (2,1,−2), (0,1,4), (0,2,5), (1,3,3).

-245301230∞∞∞

Pass 1, edge (2,1): dist[2] = ∞ → condition false, skip.

-245301230∞∞∞

Pass 1, edge (0,1): 0 + (4) = 4 < ∞ → condition true.

-2453012304∞∞

dist[1] = 4, prev[1] = 0.

-2453012304∞∞

Pass 1, edge (0,2): 0 + (5) = 5 < ∞ → condition true.

-24530123045∞

dist[2] = 5, prev[2] = 0.

-24530123045∞

Pass 1, edge (1,3): 4 + (3) = 7 < ∞ → condition true.

-245301230457

dist[3] = 7, prev[3] = 1. Pass 1 is over: edge (2,1) was wasted because it was examined while 2 was still ∞.

-245301230457

Pass 2, edge (2,1): 5 + (-2) = 3 < 4 → condition true.

-245301230357

dist[1] = 3, prev[1] = 2.

-245301230357

Pass 2, edge (0,1): 0 + (4) = 4 ≥ 3 → no change.

-245301230357

Pass 2, edge (0,2): 0 + (5) = 5 ≥ 5 → no change.

-245301230357

Pass 2, edge (1,3): 3 + (3) = 6 < 7 → condition true.

-245301230356

dist[3] = 6, prev[3] = 1. Pass 2 is over: the negative edge brought 1 down to 3, and that brought 3 down to 6.

-245301230356

Pass 3, edge (2,1): 5 + (-2) = 3 ≥ 3 → no change.

-245301230356

Pass 3, edge (0,1): 0 + (4) = 4 ≥ 3 → no change.

-245301230356

Pass 3, edge (0,2): 0 + (5) = 5 ≥ 5 → no change.

-245301230356

Pass 3, edge (1,3): 3 + (3) = 6 ≥ 6 → no change.

-245301230356

Pass 3 changed nothing. Check pass (lines 16–20): no edge relaxes and no exception is thrown. Result [0, 3, 5, 6].

Bellman-Ford · Step through the code

Bellman-Ford: Tracing the Negative-Cycle Check

for (int i = 1; i < V; i++) {
  for (Edge e : edges) {
    int u = e.from, v = e.to, w = e.weight;
    if (dist[u] != INF
        && dist[u] + w < dist[v]) {
      dist[v] = dist[u] + w;
      prev[v] = u;
    }
  }
}
for (Edge e : edges) { // check
  int u = e.from, v = e.to, w = e.weight;
  if (dist[u] != INF
      && dist[u] + w < dist[v])
    throw new IllegalStateException(
        "Negative cycle");
}
21-31S0A∞B∞C∞start

S → A (2) and the cycle A → B → C → A: 1 + (−3) + 1 = −1. dist = [S, A, B, C] = [0, ∞, ∞, ∞]; the edges SA, AB, BC, CA are relaxed in that order.

21-31S0A2B∞C∞pass 1

Pass 1, edge SA: 0 + (2) = 2 is smaller → dist[A] = 2.

21-31S0A2B3C∞pass 1

Pass 1, edge AB: 2 + (1) = 3 is smaller → dist[B] = 3.

21-31S0A2B3C0pass 1

Pass 1, edge BC: 3 + (-3) = 0 is smaller → dist[C] = 0.

21-31S0A1B3C0pass 1

Pass 1, edge CA: 0 + (1) = 1 is smaller → dist[A] = 1. Going around the cycle brought A down from 2 to 1.

21-31S0A0B2C-1pass 2

Pass 2 is over: three of the four edges relaxed again (B = 2, C = -1, A = 0). The values on the cycle drop by 1 every pass.

21-31S0A-1B1C-2pass 3

Pass 3 is over: three of the four edges relaxed again (B = 1, C = -2, A = -1). The values on the cycle drop by 1 every pass.

21-31S0A-1B1C-2check pass

V − 1 = 3 passes are over; the check pass begins. Edge SA: 0 + 2 = 2 is not < -1, fine.

21-31S0A-1B1C-2check passnegative cycle!

Check, edge AB: -1 + 1 = 0 < 1 → it still relaxes after V − 1 passes: a negative cycle exists, and the exception is thrown.

Floyd-Warshall

The Floyd-Warshall Algorithm

  • Finds shortest paths between all pairs of vertices.
  • Found independently by Bernard Roy (1959), Stephen Warshall (1962, transitive closure) and Robert Floyd (1962, shortest paths).
  • Works with negative edge weights and detects negative cycles (in which case shortest paths are undefined).
  • Uses dynamic programming, keeping the results in a V × V matrix D.
Single-source or all-pairs?Bellman-Ford and Dijkstra find shortest paths from one source. Floyd-Warshall computes shortest paths between all pairs at once with three nested loops. The code is tiny, and it is practical on dense graphs.

Floyd-Warshall

Floyd-Warshall: Steps and Recurrence

  1. Step 1: Fill D with the edge weights: D[i][i] = 0, and ∞ where there is no direct edge.
  2. Step 2: Take intermediate vertices k = 1, 2, …, n in turn; for every pair (i, j)…
  3. Step 3: Compute the length of the path through k, D[i][k] + D[k][j], and compare it with the current D[i][j].
  4. Step 4: If it is shorter, write the new value into the matrix (and update Π as well).
SubproblemD(k)[i][j]: the length of a shortest i ⇝ j path whose intermediate vertices all come from {1, …, k}.
d[i][j]d[i][k]d[k][j]ikj
D(k)[i][j] = min( D(k−1)[i][j], D(k−1)[i][k] + D(k−1)[k][j] )
Either k is not on the path, or the path passes through k once. D(n) is the answer.

Floyd-Warshall

Floyd-Warshall: Complexity

  • Time Θ(V³): three nested for loops, each running V times.
    • the first (outermost) loop runs over all intermediate vertices (k),
    • the second over all source vertices (i),
    • the last over all target vertices (j).
  • Memory Θ(V²): D can be updated in place; row and column k do not change during pass k.
For all pairsTime
Floyd-WarshallO(V³)
V × Dijkstra (array)O(V³)
V × Dijkstra (heap)O(V(V + E) log V)
V × Bellman-FordO(V² E)
Loop order matters: k must be outermost. With k inside, the result is wrong.

Floyd-Warshall

Floyd-Warshall Example: The D and Π Matrices

6332114212345

D(0)

12345
10∞63∞
230∞∞∞
3∞∞02∞
4∞110∞
5∞4∞20

Π(0)

12345
1//11/
22////
3///3/
4/44//
5/5/5/

k = 0 (initialization): fill D with edge weights, 0 on the diagonal, ∞ where there is no edge. Π[i][j] = i if there is a direct edge, otherwise /.

6332114212345

D(1)

12345
10∞63∞
23096∞
3∞∞02∞
4∞110∞
5∞4∞20

Π(1)

12345
1//11/
22/11/
3///3/
4/44//
5/5/5/

k = 1: via vertex 1 we find the paths 2 ⇝ 3 (3 + 6 = 9) and 2 ⇝ 4 (3 + 3 = 6).

6332114212345

D(2)

12345
10∞63∞
23096∞
3∞∞02∞
44110∞
5741320

Π(2)

12345
1//11/
22/11/
3///3/
4244//
52515/

k = 2: via vertex 2 we find 4 ⇝ 1 (1 + 3 = 4), 5 ⇝ 1 (4 + 3 = 7) and 5 ⇝ 3 (4 + 9 = 13).

6332114212345

D(3)

12345
10∞63∞
23096∞
3∞∞02∞
44110∞
5741320

Π(3)

12345
1//11/
22/11/
3///3/
4244//
52515/

k = 3: no shorter path via vertex 3 (3 has an edge only to 4, and no value improves).

6332114212345

D(4)

12345
10443∞
23076∞
36302∞
44110∞
563320

Π(4)

12345
1/441/
22/41/
324/3/
4244//
52445/

k = 4: via vertex 4, the paths 1⇝2, 1⇝3, 2⇝3, 3⇝1, 3⇝2, 5⇝1, 5⇝2, 5⇝3 get shorter (8 cells). E.g. 5⇝3: 2 + 1 = 3 < 13.

6332114212345

D(5)

12345
10443∞
23076∞
36302∞
44110∞
563320

Π(5)

12345
1/441/
22/41/
324/3/
4244//
52445/

k = 5: no shorter path via vertex 5 (no edge enters 5, so D[i][5] = ∞). The final matrices are ready.

Floyd-Warshall

Final State and Reading a Path

D (distances)

12345
10443∞
23076∞
36302∞
44110∞
563320

Π (predecessors)

12345
1/441/
22/41/
324/3/
4244//
52445/
  • Π[i][j]: the vertex just before j on a shortest path from i to j.
  • 5 ⇝ 1: Π[5][1] = 2, Π[5][2] = 4, Π[5][4] = 5 → 5 → 4 → 2 → 1, D[5][1] = 6.
  • 1 ⇝ 2: Π[1][2] = 4, Π[1][4] = 1 → 1 → 4 → 2, D[1][2] = 4.
  • Column 5 is ∞: no vertex can reach 5.
  • Row 5 matches the [6, 3, 3, 2, 0] that Dijkstra and Bellman-Ford found for source 5.

Floyd-Warshall

Floyd-Warshall: Pseudocode

1. Initialization

FLOYD_WARSHALL(G):
  dist = [ ][ ]
  for each u in V:
    for each v in V:
      if u == v:
        dist[u][v] = 0
      else if (u, v) in E:
        dist[u][v] = weight(u, v)
      else:
        dist[u][v] = infinity

2. Three nested loops and the check

  for each k in V:
    for each i in V:
      for each j in V:
        if dist[i][k] + dist[k][j]
             < dist[i][j]:
          dist[i][j] =
            dist[i][k] + dist[k][j]
  for each i in V:
    if dist[i][i] < 0:
      error "Graph contains a negative-weight cycle"
  return dist

Floyd-Warshall

Floyd-Warshall: Java

void floydWarshall(int[][] d) {  // V x V
  for (int k = 0; k < V; k++)       // via
    for (int i = 0; i < V; i++)     // from
      for (int j = 0; j < V; j++)   // to
        if (d[i][k] != INF
            && d[k][j] != INF
            && d[i][k] + d[k][j] < d[i][j]) {
          d[i][j] = d[i][k] + d[k][j];
          prev[i][j] = prev[k][j];
        }
  for (int i = 0; i < V; i++)
    if (d[i][i] < 0)
      throw new IllegalStateException(
          "Negative cycle");
}
  • The matrix is updated in place; no separate copy of D(k) is needed.
  • The INF check prevents overflow when adding to Integer.MAX_VALUE.
  • prev[i][j] = prev[k][j]: on the new path, the vertex before j is the same as on the k ⇝ j path.
  • Negative cycle: some vertex has a negative distance to itself (D[i][i] < 0).

Floyd-Warshall · Step through the code

Floyd-Warshall: Step Through the Code

void floydWarshall(int[][] d) {  // V x V
  for (int k = 0; k < V; k++)       // via
    for (int i = 0; i < V; i++)     // from
      for (int j = 0; j < V; j++)   // to
        if (d[i][k] != INF
            && d[k][j] != INF
            && d[i][k] + d[k][j] < d[i][j]) {
          d[i][j] = d[i][k] + d[k][j];
          prev[i][j] = prev[k][j];
        }
  for (int i = 0; i < V; i++)
    if (d[i][i] < 0)
      throw new IllegalStateException(
          "Negative cycle");
}
38722510123d0011223303∞7802∞5∞012∞∞0prev00112233/0/01/1/2//23///

Start: d holds the edge weights (0 on the diagonal, ∞ where there is no edge); prev[i][j] = i. The matrix on the right is d, the one below it prev.

38722510123d0011223303∞7802155∞012∞∞0prev00112233/0/01/102//23///gray: row and column k = 0

k = 0 as intermediate: d[1][0] + d[0][3] = 8 + 7 = 15 < ∞ → d[1][3] = 15, prev[1][3] = prev[0][3] = 0.

38722510123d0011223303∞78021558012∞∞0prev00112233/0/01/1020/23///gray: row and column k = 0

k = 0 as intermediate: d[2][0] + d[0][1] = 5 + 3 = 8 < ∞ → d[2][1] = 8, prev[2][1] = prev[0][1] = 0.

38722510123d0011223303∞780215580125∞0prev00112233/0/01/1020/230//gray: row and column k = 0

k = 0 as intermediate: d[3][0] + d[0][1] = 2 + 3 = 5 < ∞ → d[3][1] = 5, prev[3][1] = prev[0][1] = 0.

38722510123d00112233035780215580125∞0prev00112233/0101/1020/230//gray: row and column k = 1

k = 1 as intermediate: d[0][1] + d[1][2] = 3 + 2 = 5 < ∞ → d[0][2] = 5, prev[0][2] = prev[1][2] = 1.

38722510123d0011223303578021558012570prev00112233/0101/1020/2301/gray: row and column k = 1

k = 1 as intermediate: d[3][1] + d[1][2] = 5 + 2 = 7 < ∞ → d[3][2] = 7, prev[3][2] = prev[1][2] = 1.

38722510123d0011223303568021558012570prev00112233/0121/1020/2301/gray: row and column k = 2

k = 2 as intermediate: d[0][2] + d[2][3] = 5 + 1 = 6 < 7 → d[0][3] = 6, prev[0][3] = prev[2][3] = 2.

38722510123d0011223303567021558012570prev00112233/0122/1020/2301/gray: row and column k = 2

k = 2 as intermediate: d[1][2] + d[2][0] = 2 + 5 = 7 < 8 → d[1][0] = 7, prev[1][0] = prev[2][0] = 2.

38722510123d001122330356702358012570prev00112233/0122/1220/2301/gray: row and column k = 2

k = 2 as intermediate: d[1][2] + d[2][3] = 2 + 1 = 3 < 15 → d[1][3] = 3, prev[1][3] = prev[2][3] = 2.

38722510123d001122330356502358012570prev00112233/0123/1220/2301/gray: row and column k = 3

k = 3 as intermediate: d[1][3] + d[3][0] = 3 + 2 = 5 < 7 → d[1][0] = 5, prev[1][0] = prev[3][0] = 3.

38722510123d001122330356502338012570prev00112233/0123/1230/2301/gray: row and column k = 3

k = 3 as intermediate: d[2][3] + d[3][0] = 1 + 2 = 3 < 5 → d[2][0] = 3, prev[2][0] = prev[3][0] = 3.

38722510123d001122330356502336012570prev00112233/0123/1230/2301/gray: row and column k = 3

k = 3 as intermediate: d[2][3] + d[3][1] = 1 + 5 = 6 < 8 → d[2][1] = 6, prev[2][1] = prev[3][1] = 0.

38722510123d001122330356502336012570prev00112233/0123/1230/2301/

All four passes are done. No negative value on the diagonal → no negative cycle. Sample path: prev[1][3] = 2, prev[1][2] = 1 → 1 → 2 → 3, d[1][3] = 3.

A* Search

The A* (A-Star) Search Algorithm

  • A search algorithm that finds a shortest path between two points (start → goal).
  • Developed in 1968 by Peter Hart, Nils Nilsson and Bertram Raphael.
  • Combines Dijkstra's actual cost (uniform-cost search) with the goal estimate of greedy best-first search.
  • It needs a good estimate function (a heuristic) to work well.
  • Time and memory grow quickly as the number of vertices increases.
How it worksEach vertex has an estimate h(n) of its distance to the goal. At every step, among the vertices in the open list, it picks the one with the smallest sum of actual cost and estimated cost. This way it heads toward the goal while still keeping the cheapest path.

A* Search

How Does A* Work? f(n) = g(n) + h(n)

g(n): actual costThe cost of the path found so far from the start to n (the "g-cost"). Same as d[n] in Dijkstra.
h(n): heuristicThe estimated cost from n to the goal (the "h-cost"), such as straight-line or Manhattan distance.
f(n) = g(n) + h(n)The estimated total cost of a solution through n. The vertex with the smallest f in the open list is expanded.
Selection ruleAlgorithmBehavior
g(n) onlyDijkstraspreads equally in all directions; correct but expands many vertices
h(n) onlyGreedy best-firstrushes toward the goal; fast, but no shortest-path guarantee
g(n) + h(n)A*heads toward the goal; with a suitable h, guarantees a shortest path
Vertices that look close to the goal by the heuristic get priority, but thanks to g a vertex reached by an expensive path is held back.

A* Search

A*: The Steps

1. Set g(start) = 0 and add it to the open list (a priority queue). All other vertices get g = ∞.

Open list: discovered but not yet expanded vertices.

2. Remove the vertex with the smallest f = g + h from the open list; put it on the closed list.

The rule is the sum f = g + h, not g or h alone.

3. If it is the goal, stop: rebuild the path from the predecessors.

The goal is final when removed, not when added.

4. Otherwise, for each neighbor v let g' = g(u) + w(u, v); if g' is smaller, update g, f and the predecessor and add v to the open list.

Dijkstra's relaxation. A closed v with a better g is reopened (never with a consistent h).

5. Go back to step 2. If the open list empties, there is no path.

The choice is from the whole open list, not just the last vertex's neighbors.

A* Search

Properties of a Heuristic

Admissibleh(n) ≤ h*(n): it never overestimates the true remaining cost. Then, when A* removes the goal, the path it found is shortest.
Consistenth(u) ≤ w(u, v) + h(v) and h(goal) = 0. With a consistent h, a vertex is final once it enters the closed list (it is never reopened).
Grid heuristics4 directions: Manhattan |Δx| + |Δy|. 8 directions: octile 14·min + 10·(max − min). Free plane: Euclidean distance.
Extreme casesh = 0 → A* = Dijkstra. h = h* (perfect) → only vertices on a shortest path are expanded. h > h* → fast, but no shortest-path guarantee.

A* Search

A*: Running Time

  • Worst-case time and memory in terms of the search tree O(bd):
    • b: branching factor (average number of neighbors), d: depth of the goal (number of steps in the solution).
  • On a finite graph with a consistent h, A* does at most O((V + E) log V) work, like Dijkstra.
  • The better the heuristic, the fewer vertices are expanded: with a perfect estimate (h = h*) only the d vertices on the path are expanded.
  • Its weak point is memory: the open and closed lists grow; variants such as IDA* and SMA* bound the memory.
O(bd) is the bound for the worst case (a useless h, e.g. h = 0), not for a perfect estimate; with a perfect estimate the search is linear in the path length.

A* Search

A* Example: s to t (h values next to the vertices)

1.5234232sdaebcth=4.5h=4h=2h=2h=4goal

Open list

vertexghf
a1.545.5
d24.56.5

Closed: s

Expand s: neighbors a (g = 1.5) and d (g = 2) are opened. f(a) = 1.5 + 4 = 5.5, f(d) = 2 + 4.5 = 6.5.

1.5234232sdaebcth=4.5h=4h=2h=2h=4goal

Open list

vertexghf
b3.525.5
d24.56.5

Closed: s, a

Smallest f: a (5.5). Expand a → b: g = 3.5, f(b) = 3.5 + 2 = 5.5.

1.5234232sdaebcth=4.5h=4h=2h=2h=4goal

Open list

vertexghf
d24.56.5
c6.5410.5

Closed: s, a, b

Expand b (5.5 < 6.5) → c: g = 6.5, f(c) = 6.5 + 4 = 10.5. This path is getting expensive.

1.5234232sdaebcth=4.5h=4h=2h=2h=4goal

Open list

vertexghf
e527
c6.5410.5

Closed: s, a, b, d

Now the smallest f is d (6.5). The search switches branches → e: g = 5, f(e) = 5 + 2 = 7.

1.5234232sdaebcth=4.5h=4h=2h=2h=4goal

Open list

vertexghf
t707
c6.5410.5

Closed: s, a, b, d, e

Expand e → t: g = 5 + 2 = 7, f(t) = 7 + 0 = 7 < f(c) = 10.5.

1.5234232sdaebcth=4.5h=4h=2h=2h=4goal

Open list

vertexghf
c6.5410.5

Closed: s, a, b, d, e, t

t is removed from the open list: the goal. Shortest path s → d → e → t, cost 7. c was never expanded.

A* Search · Step through the code

A*: Step Through the Code

double aStar(int s, int t) {
  Arrays.fill(g, INF); g[s] = 0;
  Queue<Integer> open = new PriorityQueue<>(
    Comparator.comparingDouble(v -> g[v] + h[v]));
  open.add(s);
  while (!open.isEmpty()) {
    int u = open.poll();      // smallest f
    if (u == t) return g[t];  // goal
    for (Edge e : adj[u])
      if (g[u] + e.w < g[e.v]) {
        g[e.v] = g[u] + e.w; parent[e.v] = u;
        open.remove(e.v); open.add(e.v);
      }
  }
  return INF;
}
1.5234232sh=5 g=0ah=4bh=2ch=4dh=4.5eh=2th=0

Undirected graph, goal t; h beside each vertex estimates the distance to t. g(s) = 0; open = {s} (f in parentheses).

1.5234232sh=5 g=0ah=4bh=2ch=4dh=4.5eh=2th=0

Remove s, the smallest f (0 + 5 = 5). Not the goal; examine its neighbors.

1.5234232sh=5 g=0ah=4 g=1.5bh=2ch=4dh=4.5 g=2eh=2th=0

g(a) = 0 + 1.5 = 1.5, f = 5.5; g(d) = 0 + 2 = 2, f = 6.5.

1.5234232sh=5 g=0ah=4 g=1.5bh=2ch=4dh=4.5 g=2eh=2th=0

Remove a, the smallest f (1.5 + 4 = 5.5). Not the goal; examine its neighbors.

1.5234232sh=5 g=0ah=4 g=1.5bh=2 g=3.5ch=4dh=4.5 g=2eh=2th=0

g(b) = 1.5 + 2 = 3.5, f = 5.5.

1.5234232sh=5 g=0ah=4 g=1.5bh=2 g=3.5ch=4dh=4.5 g=2eh=2th=0

Remove b, the smallest f (3.5 + 2 = 5.5). Not the goal; examine its neighbors.

1.5234232sh=5 g=0ah=4 g=1.5bh=2 g=3.5ch=4 g=6.5dh=4.5 g=2eh=2th=0

g(c) = 3.5 + 3 = 6.5, f = 10.5. Too large: d (f = 6.5) is next.

1.5234232sh=5 g=0ah=4 g=1.5bh=2 g=3.5ch=4 g=6.5dh=4.5 g=2eh=2th=0

Remove d, the smallest f (2 + 4.5 = 6.5). Not the goal; examine its neighbors.

1.5234232sh=5 g=0ah=4 g=1.5bh=2 g=3.5ch=4 g=6.5dh=4.5 g=2eh=2 g=5th=0

g(e) = 2 + 3 = 5, f = 7.

1.5234232sh=5 g=0ah=4 g=1.5bh=2 g=3.5ch=4 g=6.5dh=4.5 g=2eh=2 g=5th=0

Remove e, the smallest f (5 + 2 = 7). Not the goal; examine its neighbors.

1.5234232sh=5 g=0ah=4 g=1.5bh=2 g=3.5ch=4 g=6.5dh=4.5 g=2eh=2 g=5th=0 g=7

g(t) = 5 + 2 = 7, f = 7.

1.5234232sh=5 g=0ah=4 g=1.5bh=2 g=3.5ch=4 g=6.5dh=4.5 g=2eh=2 g=5th=0 g=7

Smallest f: t (7), the goal → return g[t] = 7. Path s → d → e → t; c never expanded.

A* Search

A* Example: Around a Wall on a Grid

B142842103848144862103848A105262144862105262145670
openclosedpathwalltop left g · top right h · center f

Expand A: 8 neighbors are opened. A straight step costs 10, a diagonal 14; h = octile distance (walls ignored). Smallest f = 42.

B242448142842103848144862283462103848A105262144862105262145670
openclosedpathwalltop left g · top right h · center f

Expansion 2: the cell with the smallest f = 42 (orange frame) moves to the closed list; its neighbors are opened or updated.

B342054242448142842103848144862383068203454103848A105262244468144862105262145670
openclosedpathwalltop left g · top right h · center f

Expansion 4: the cell with the smallest f = 48 (orange frame) moves to the closed list; its neighbors are opened or updated.

B244468442468342054242448142842103848144862483482303060203454103848A105262344074244468144862105262145670
openclosedpathwalltop left g · top right h · center f

Expansion 7: the cell with the smallest f = 54 (orange frame) moves to the closed list; its neighbors are opened or updated. Cells below the wall, with f = 42…62, are tried first.

B244468442468342054242448142842103848144862403474303060203454103848A105262444488344074244468144862105262145670285482245882286290
openclosedpathwalltop left g · top right h · center f

Expansion 9: the cell with the smallest f = 62 (orange frame) moves to the closed list; its neighbors are opened or updated.

B582482244468285482542882442468342054242448142842103848144862245882583896403474303060203454103848A105262206282444488344074244468144862105262145670246690285482245882206282246690
openclosedpathwalltop left g · top right h · center f

Expansion 13: the cell with the smallest f = 68 (orange frame) moves to the closed list; its neighbors are opened or updated. The lower route is blocked: f has risen to 68, and the search spreads to both sides of the wall.

721082621476522476483482524496680B 68581068482068383068344074385088582482244468285482542882442468342054242448142842103848144862245882583896403474303060203454103848A105262206282444488344074244468144862105262145670246690285482245882206282246690
openclosedpathwalltop left g · top right h · center f

Expansion 17 (f = 68): the path around the right of the wall has reached B; B is in the open list with f = 68.

721082621476522476483482524496680B 68581068482068383068344074385088582482244468285482542882442468342054242448142842103848144862245882583896403474303060203454103848A105262206282444488344074244468144862105262145670246690285482245882206282246690
openclosedpathwalltop left g · top right h · center f

B is removed: the path goes up from A → diagonal up-right → diagonal up-left → three steps left over the wall, cost 68 (2 diagonals × 14 + 4 straight × 10). 18 cells were expanded.

Related Methods

BFS: Shortest Paths in an Unweighted Graph

  • When all edges are equal (weight 1), the shortest path = the path with the fewest edges.
  • Breadth-first search (BFS) visits vertices level by level, in order of distance from the source: d[v] = d[u] + 1.
  • A FIFO queue replaces Dijkstra's priority queue: O(V + E).
  • Example (graph at right): the shortest path 0 → 6 is 0 → 2 → 4 → 6 (3 edges).
  • Bidirectional search: run BFS from both the source and the target and stop when the two waves meet; about 2·bd/2 vertices are expanded.
0172345601112233

Numbers: BFS level (edge count). Green: the BFS tree.

Comparison

Comparing Shortest-Path Algorithms

AlgorithmProblemNegative edgesNegative cycleTimeApproach
BFSsingle source, unweighted——O(V + E)level by level
Dijkstrasingle sourcenonoO(V²) / O((V + E) log V)greedy
Bellman-Fordsingle sourceyesdetectsO(V · E)repeated relaxation (DP)
Floyd-Warshallall pairsyesdetectsΘ(V³)dynamic programming
A*single pairnonoworst case O(bd)heuristic + greedy
Unweighted?BFS is enough.
Weights ≥ 0?Dijkstra; A* if there is a single target and a good h.
Negative edges?Bellman-Ford.
All pairs, small V?Floyd-Warshall.

Summary

Summary

TopicKey points
Relaxationif d[u] + w < d[v], update d[v] and π[v]; the operation all the algorithms share
Dijkstrasettle the vertex with the smallest d, relax its neighbors; weights ≥ 0; O((V + E) log V)
Bellman-Fordrelax every edge for V − 1 passes; an edge that relaxes in pass V = a negative cycle reachable from the source; O(V · E)
Floyd-WarshallD[i][j] = min(D[i][j], D[i][k] + D[k][j]), k outermost; D[i][i] < 0 → negative cycle; Θ(V³)
A*f = g + h; shortest path with an admissible h; h = 0 → Dijkstra
BFSfewest-edge path in an unweighted graph; O(V + E)
Pathsfollow the π (Π) pointers back from the target; shortest-path tree

Summary

Common Mistakes

Dijkstra with negative edgesA shorter path to a settled vertex can appear later; the result is silently wrong. Negative edges need Bellman-Ford.
∞ + w overflowInteger.MAX_VALUE + w wraps around to a negative number and looks "shorter". That is why d[u] != INF is checked first.
Floyd-Warshall loop orderThe intermediate-vertex loop k must be outermost; the order i, j, k gives wrong results.
Number of passesBellman-Ford may need all V − 1 passes (Example 2); one pass is not always enough. The check pass is separate.
Stopping at the goal in A*Stop when the goal is removed from the open list, not when it is added. An h that overestimates can miss the shortest path.
Negative weight on an undirected edgeAn undirected negative edge is a negative cycle by itself: u → v → u.

Summary

Worked Problems

Directed graph: A→B 4, A→C 2, C→B 1, B→D 5, C→D 8, C→E 10, D→E 3; source A.

  1. In what order does Dijkstra settle the vertices? What are the final d values?
  2. Write the shortest path from A to E using the π array.
  3. If the edges are examined in alphabetical order (A→B, A→C, B→D, C→B, C→D, C→E, D→E), how many relaxations happen in each pass of Bellman-Ford?
  4. What is the final Floyd-Warshall D matrix for the graph 1→2 (4), 2→3 (1), 1→3 (7), 3→1 (−2)? Is there a negative cycle?
  5. V = 1000, E = 5000, and there are negative edges. Which algorithm, and roughly how many operations?
Show answers
  1. A, C, B, D, E; d = A:0, B:3, C:2, D:8, E:11 (B improves via C, 2 + 1).
  2. π[E] = D, π[D] = B, π[B] = C, π[C] = A → A → C → B → D → E = 2 + 1 + 5 + 3 = 11.
  3. Pass 1: 5 (B=4, C=2, D=9, B=3, E=12); pass 2: 2 (D=8, E=11); passes 3–4: 0 → early exit is possible.
  4. D = [[0, 4, 5], [−1, 0, 1], [−2, 2, 0]]; the diagonal is 0 → the cycles 1→2→3→1 = +3 and 1→3→1 = +5, no negative cycle.
  5. Bellman-Ford: V · E = 1000 · 5000 = 5 · 10⁶ relaxation attempts (Dijkstra is unreliable with negative edges).

Summary

Exercises

  1. Find all distances in the graph of Dijkstra Example 1 with source 5.
  2. dijkstra(src, target): write a version that stops when the target leaves the queue.
  3. Extend Dijkstra to also count the number of shortest paths to each vertex.
  4. Add early exit to Bellman-Ford; what is the best-case running time?
  5. Adapt Floyd-Warshall to compute the transitive closure (Warshall) with a boolean matrix.
  6. Why is Manhattan distance admissible on a 4-direction grid but not on an 8-direction grid?
Answers / hints
  1. d = 1:20, 2:21, 3:11, 4:6, 5:0, 6:9 (e.g. 3: 5→6→3 = 9 + 2; 2: 5→4→2 = 6 + 15).
  2. Before line 13, if (u == target) break; — when a vertex is added its value is not final yet.
  3. count[src] = 1; if d[u] + w < d[v], count[v] = count[u]; if equal, count[v] += count[u].
  4. If the changed flag stays false, leave the loop; best case O(E).
  5. t[i][j] = t[i][j] || (t[i][k] && t[k][j]); again k outermost, Θ(V³).
  6. Each step lowers |Δx| + |Δy| by at most 1 → h ≤ h*. A diagonal step lowers it by 2 but costs 1 (or 1.4) → an overestimate.

Summary

References

  • Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed., 2022 (Chapters 22–23).
  • Sedgewick and Wayne, Algorithms, 4th ed., 2011 (Section 4.4: Shortest Paths).
  • E. W. Dijkstra, "A Note on Two Problems in Connexion with Graphs", Numerische Mathematik 1, 1959.
  • R. Bellman, "On a Routing Problem", Quarterly of Applied Mathematics 16, 1958.
  • R. W. Floyd, "Algorithm 97: Shortest Path", Communications of the ACM 5(6), 1962; S. Warshall, "A Theorem on Boolean Matrices", Journal of the ACM 9(1), 1962.
  • P. E. Hart, N. J. Nilsson and B. Raphael, "A Formal Basis for the Heuristic Determination of Minimum Cost Paths", IEEE Trans. SSC 4(2), 1968.

The End

Chapter 6: Shortest Paths
Previous: Chapter 5: Graph Traversal · Next: Chapter 7: Minimum Spanning Trees
1 / 1 Sercan KÜLCÜ. All rights reserved.