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

Beyond Dijkstra: Negative Edges & All-Pairs

2

Learning Goals

3

Bellman-Ford Algorithm

4

Motivation

5

Interactive Demo

6

Key Idea

7

Bellman-Ford(G, s)

Init-Single-Source(G, s) // d[s]=0, others inf
for i = 1 to |V| - 1
  for each edge (u, v) in E
    Relax(u, v, w)
// Negative cycle detection:
for each edge (u, v) in E
  if d[v] > d[u] + w(u, v)
    return "Negative cycle exists"
return d[], pi[]
8

Example Trace

9

Complexity

10

Negative Cycle Detection

11

How It Works

12

Negative Cycle Implications

13

All-Pairs Shortest Paths

14

The APSP Problem

15

Naive Approaches

16

Floyd-Warshall Algorithm

17

Interactive Demo

18

DP Formulation

19

Understanding the Recurrence

20

Floyd-Warshall(W)

n = |V|
D = W  // Initialize with edge weights
for k = 1 to n
  for i = 1 to n
    for j = 1 to n
      D[i][j] = min(D[i][j], D[i][k] + D[k][j])
return D
21

Space Optimization

22

Floyd-Warshall Trace

23

Example Graph

24

Pivot Through Each Vertex

25

Floyd-Warshall Properties

26

Path Reconstruction

27

Predecessor Matrix

28

Transitive Closure

29

Warshall's Algorithm

30

Johnson's Algorithm (Overview)

31

DP Connection

32

Shortest Paths as Dynamic Programming

33

Common Pitfalls

34

Watch Out For...

35

All Interactive Demos

36

Lecture Summary

37

Supplementary Resources