🖨️ Printing Instructions: Press Ctrl/Cmd + P and select "Save as PDF".
1

Dijkstra's Algorithm

2

Learning Goals

3

The Shortest Path Problem

4

Goal

5

Shortest Path Variants

6

Optimal Substructure

7

Relaxation

8

Interactive Demo

9

The Core Operation

10

Relax(u, v, w)

if d[v] > d[u] + w(u, v)
  d[v] = d[u] + w(u, v)
  pi[v] = u
11

Properties of Relaxation

12

Dijkstra's Algorithm

13

Interactive Demo

14

Greedy Strategy

15

Dijkstra(G, s)

Init-Single-Source(G, s) // d[s]=0, others inf
S = empty
Q = V // Min-Priority Queue keyed by d[]
while Q not empty
  u = Extract-Min(Q)
  S = S U {u}
  for each v in Adj[u]
    Relax(u, v, w)
16

Example Trace

17

Similarity to Prim's MST

18

Analysis

19

Complexity

20

Priority Queue Comparison

ImplementationExtract-MinDecrease-KeyTotal
Array$O(V)$$O(1)$$O(V^2)$
Binary Heap$O(\log V)$$O(\log V)$$O(E \log V)$
Fibonacci Heap$O(\log V)$ amortized$O(1)$ amortized$O(V \log V + E)$
Complexity depends on the priority queue implementation
21

Correctness Proof

22

Theorem

23

Proof Sketch (Loop Invariant)

24

Limitations & Negative Edges

25

Interactive Demo

26

Why Dijkstra Fails with Negatives

27

Negative Cycles

28

Path Reconstruction

29

Building the Actual Path

30

Common Pitfalls

31

Watch Out For...

32

All Interactive Demos

33

Lecture Summary

34

Supplementary Resources