CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23....

56
CS 4102: Algorithms Lecture 20: Shortest Path Algorithms David Wu Fall 2019

Transcript of CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23....

Page 1: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

CS 4102: AlgorithmsLecture 20: Shortest Path Algorithms

David WuFall 2019

Page 2: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Warm-Up

2

Show that no cycle crosses a cut exactly once

• Consider an edge 𝑒 = (𝑢, 𝑣) that crosses the cut

• After removing the edge 𝑒 from the graph, there is still a pathfrom 𝑢 ∈ 𝑆 to 𝑣 ∉ 𝑆

• At least one edge along the pathfrom cross the cut

𝑣

𝑢

𝑆

𝑉 − 𝑆

Page 3: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Today’s Keywords

GraphsShortest paths algorithmsDijkstra’s algorithmBreadth-first search (BFS)

3

CLRS Readings: Chapter 22, 23

Page 4: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Homework

HW7 due Thursday, November 14, 11pm• Graph algorithms• Written (use LaTeX!) – Submit both zip and pdf (two separate attachments)!

HW10B also out today, due Thursday, November 14, 11pm• No late submissions allowed (no exceptions)

4

Page 5: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Minimum Spanning Tree

5

A tree 𝑇 = (𝑉., 𝐸.) is a minimum spanning tree for an undirected graph 𝐺 = (𝑉, 𝐸) if 𝑇 is a spanning tree of

minimal cost

Cost 𝑇 = 56∈78

𝑤(𝑒)

10

2

6 11

95

8

3

7

3

1

8

12

9

Page 6: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Minimum Spanning Tree

6

Two greedy algorithms:

Kruskal: add minimum-weight edge that does not introduce a cycle

10

2

6 11

95

8

3

7

3

1

8

12

9

Page 7: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Minimum Spanning Tree

7

Two greedy algorithms:

Kruskal: add minimum-weight edge that does not introduce a cycle

10

2

6 11

95

8

3

7

3

1

8

12

9

Page 8: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Minimum Spanning Tree

8

Two greedy algorithms:

Kruskal: add minimum-weight edge that does not introduce a cycle

10

2

6 11

95

8

3

7

3

1

8

12

9

Page 9: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Minimum Spanning Tree

9

Two greedy algorithms:

Kruskal: add minimum-weight edge that does not introduce a cycle

10

2

6 11

95

8

3

7

3

1

8

12

9

Page 10: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Minimum Spanning Tree

10

Two greedy algorithms:

Prim: “grow” a tree by adding minimum-weight edgefrom the tree to an external node

10

2

6 11

95

8

3

7

3

1

8

12

9

Page 11: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

10

2

6 11

95

8

3

7

3

1

8

12

9

Minimum Spanning Tree

11

Two greedy algorithms:

Prim: “grow” a tree by adding minimum-weight edgefrom the tree to an external node

Page 12: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

10

2

6 11

95

8

3

7

3

1

8

12

9

Minimum Spanning Tree

12

Two greedy algorithms:

Prim: “grow” a tree by adding minimum-weight edgefrom the tree to an external node

Page 13: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

10

2

6 11

95

8

3

7

3

1

8

12

9

Minimum Spanning Tree

13

Two greedy algorithms:

Prim: “grow” a tree by adding minimum-weight edgefrom the tree to an external node

Page 14: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

14

1. Start with an empty tree 𝑇 and pick a start node and add it to 𝑇2. Repeat 𝑉 − 1 times:• Add the min-weight edge which connects a node in 𝑇 with a node not in 𝑇

Implementation (with nodes in the priority queue):initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢,𝑤 𝑣, 𝑢𝑢. parent = 𝑣

each node also maintains a parent, initially NULL

Page 15: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

15

Implementation (with nodes in the priority queue):initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

10

2

6 11

95

8

3

7

3

1

8

12

90

∞∞

Page 16: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

16

10

2

6 11

95

8

3

7

3

1

8

12

90

10

12

∞∞

Implementation (with nodes in the priority queue):initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 17: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

17

10

2

6 11

95

8

3

7

3

1

8

12

90

10

9

8

∞∞

Implementation (with nodes in the priority queue):initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 18: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

18

10

2

6 11

95

8

3

7

3

1

8

12

90

10

9

7

8

∞5

8

Implementation (with nodes in the priority queue):initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 19: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

19

10

2

6 11

95

8

3

7

3

1

8

12

90

10

9

7

8

65

11

8

Implementation (with nodes in the priority queue):initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 20: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

20

10

2

6 11

95

8

3

7

3

1

8

12

90

10

1

3

8

65

11

8

Implementation (with nodes in the priority queue):initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 21: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

21

10

2

6 11

95

8

3

7

3

1

8

12

90

10

1

3

8

65

11

8

Implementation (with nodes in the priority queue):initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 22: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

22

10

2

6 11

95

8

3

7

3

1

8

12

90

10

1

3

8

65

11

8

Implementation (with nodes in the priority queue):initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 23: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

23

10

2

6 11

95

8

3

7

3

1

8

12

90

10

1

3

8

65

2

8

Implementation (with nodes in the priority queue):initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 24: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

24

10

2

6 11

95

8

3

7

3

1

8

12

90

10

1

3

8

65

2

8

Implementation (with nodes in the priority queue):initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 25: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Implementation (with nodes in the priority queue):initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Prim’s Algorithm Running Time

25

𝑂 𝑉Initialization:

𝑉 iterations𝑂 log 𝑉𝐸 iterations total

𝑂 log 𝑉

Overall running time: 𝑂 𝑉 log 𝑉 + 𝐸 log 𝑉 = 𝑂 𝐸 log 𝑉

Page 26: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Single-Source Shortest Path

26

Find the shortest path from UVA to each of these other placesGiven a graph 𝐺 = (𝑉, 𝐸) and a start node (i.e., source) 𝑠 ∈ 𝑉,

for each 𝑣 ∈ 𝑉 find the minimum-weight path from 𝑠 → 𝑣 (call this weight 𝛿(𝑠, 𝑣))Assumption (for now): all edge weights are positive

10

2

11

95

8

3

7

3

1

8

12

9

Page 27: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

1. Start with an empty tree 𝑇 and add the source to 𝑇2. Repeat 𝑉 − 1 times:• Add the “nearest” node not yet in 𝑇 to 𝑇

Dijkstra’s Algorithm

27

10

2

6 11

95

8

3

7

3

1

8

12

9

Page 28: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm

28

1. Start with an empty tree 𝑇 and pick a start node and add it to 𝑇2. Repeat 𝑉 − 1 times:• Add the min-weight edge which connects a node in 𝑇 with a node not in 𝑇

10

2

6 11

95

8

3

7

3

1

8

12

9

Page 29: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

29

1. Start with an empty tree 𝑇 and pick a start node and add it to 𝑇2. Repeat 𝑉 − 1 times:• Add the min-weight edge which connects a node in 𝑇 with a node not in 𝑇

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢,𝑤 𝑣, 𝑢𝑢. parent = 𝑣

each node also maintains a parent, initially NULL

Page 30: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Implementation

30

1. Start with an empty tree 𝑇 and add the source to 𝑇2. Repeat 𝑉 − 1 times:• Add the “nearest” node not yet in 𝑇 to 𝑇

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

each node also maintains a parent, initially NULL

key: length of shortest path 𝑠 → 𝑢 using nodes in PQ

Page 31: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Prim’s Algorithm Implementation

31

1. Start with an empty tree 𝑇 and pick a start node and add it to 𝑇2. Repeat 𝑉 − 1 times:• Add the min-weight edge which connects a node in 𝑇 with a node not in 𝑇

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keypick a starting node 𝑠 and set 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢,𝑤 𝑣, 𝑢𝑢. parent = 𝑣

each node also maintains a parent, initially NULL

key: minimum cost to connect 𝑢 to nodes in PQ

Page 32: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Implementation

32

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

10

2

6 11

95

8

3

7

3

1

8

12

90

∞∞

Page 33: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Implementation

33

10

2

6 11

95

8

3

7

3

1

8

12

90

10

12

∞∞

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 34: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Implementation

34

10

2

6 11

95

8

3

7

3

1

8

12

90

10

12

18

∞∞

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 35: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Implementation

35

10

2

6 11

95

8

3

7

3

1

8

12

90

10

12

15

18

13∞

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 36: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Implementation

36

10

2

6 11

95

8

3

7

3

1

8

12

90

10

12

15

18

1319

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 37: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Implementation

37

10

2

6 11

95

8

3

7

3

1

8

12

90

10

12

15

18

1319

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 38: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Implementation

38

10

2

6 11

95

8

3

7

3

1

8

12

90

10

12

15

18

1319

26

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 39: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Implementation

39

10

2

6 11

95

8

3

7

3

1

8

12

90

10

12

15

18

1319

30

26

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 40: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Implementation

40

10

2

6 11

95

8

3

7

3

1

8

12

90

10

12

15

18

1319

28

26

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 41: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Implementation

41

10

2

6 11

95

8

3

7

3

1

8

12

90

10

12

15

18

1319

28

26

Observe: shortest paths from a source forms a tree, but not a minimum spanning tree

Every subpath of a shortest path is itself a shortest path (optimal substructure)

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 42: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Running Time

42

𝑂 𝑉Initialization:

𝑉 iterations𝑂 log 𝑉𝐸 iterations total

𝑂 log 𝑉

Overall running time: 𝑂 𝑉 log 𝑉 + 𝐸 log 𝑉 = 𝑂 𝐸 log 𝑉

Implementation:initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 43: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm Proof Strategy

Proof by induction

Proof Idea: we will show that when node 𝑢 is removed from the priority queue, 𝑑M = 𝛿(𝑠, 𝑢)• Claim 1: There is a path of length 𝑑M (as long as 𝑑M < ∞) from 𝑠 to 𝑢 in 𝐺• Claim 2: For every path 𝑠, … , 𝑢 , 𝑤 𝑠,… , 𝑢 ≥ 𝑑M

43

Page 44: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Correctness of Dijkstra’s Algorithm

Inductive hypothesis: Suppose that nodes 𝑣a = 𝑠,… , 𝑣b have been removed from PQ, and for each of them 𝑑<c = 𝛿(𝑠, 𝑣b), and there is a path from 𝑠 to 𝑣b with distance 𝑑<c (whenever 𝑑<c < ∞)

Base case:• 𝑖 = 0: 𝑣a = 𝑠• Claim holds trivially

44

Page 45: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Correctness of Dijkstra’s Algorithm: Claim 1

Let 𝑢 be the 𝑖 + 1 fg node extractedClaim 1: There is a path of length 𝑑M (as long as 𝑑M < ∞) from 𝑠 to 𝑢 in 𝐺Proof:

• Suppose 𝑑M < ∞• This means that PQ. decreaseKey was invoked on node 𝑢 on an earlier

iteration• Consider the last time PQ. decreaseKey is invoked on node 𝑢• PQ. decreaseKey is only invoked when there exists an edge 𝑣, 𝑢 ∈ 𝐸 and

node 𝑣 was extracted from PQ in a previous iteration• In this case, 𝑑M = 𝑑< + 𝑤 𝑣, 𝑢• By the inductive hypothesis, there is a path 𝑠 → 𝑣 of length 𝑑< in 𝐺 and since

there is an edge 𝑣, 𝑢 ∈ 𝐸, there is a path 𝑠 → 𝑢 of length 𝑑M in 𝐺45

Page 46: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Correctness of Dijkstra’s Algorithm: Claim 2

Let 𝑢 be the 𝑖 + 1 fg node extractedClaim 2: For every path 𝑠, … , 𝑢 , 𝑤 𝑠,… , 𝑢 ≥ 𝑑M

46

extracted nodes

𝑠𝑢

Extracted nodes define a cut (𝑆, 𝑉 − 𝑆) of 𝐺

𝑆

Page 47: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Correctness of Dijkstra’s Algorithm: Claim 2

Let 𝑢 be the 𝑖 + 1 fg node extractedClaim 2: For every path 𝑠, … , 𝑢 , 𝑤 𝑠,… , 𝑢 ≥ 𝑑M

47

extracted nodes

𝑠𝑢

𝑥𝑦

Since 𝑢 ∉ 𝑆, 𝑠, … , 𝑢 crosses the cut somewhere• Let 𝑥, 𝑦 be last edge in the path that crosses

the cut

𝑤 𝑠,… , 𝑢 ≥ 𝛿 𝑠, 𝑥 + 𝑤 𝑥, 𝑦 + 𝑤(𝑦, … , 𝑢)

𝑤 𝑠, … , 𝑢 = 𝑤 𝑠,… , 𝑥 + 𝑤 𝑥, 𝑦 + 𝑤(𝑦, … , 𝑢)

𝑤 𝑠, … , 𝑥 ≥ 𝛿(𝑠, 𝑥) since 𝛿(𝑠, 𝑥) is weight of shortest path from 𝑠 to 𝑥

𝑆

Take any path 𝑠, … , 𝑢Extracted nodes define a cut (𝑆, 𝑉 − 𝑆) of 𝐺

Page 48: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Correctness of Dijkstra’s Algorithm: Claim 2

Let 𝑢 be the 𝑖 + 1 fg node extractedClaim 2: For every path 𝑠, … , 𝑢 , 𝑤 𝑠,… , 𝑢 ≥ 𝑑M

48

extracted nodes

𝑠𝑢

𝑥𝑦

𝑤 𝑠, … , 𝑢 ≥ 𝛿 𝑠, 𝑥 + 𝑤 𝑥, 𝑦 + 𝑤(𝑦, … , 𝑢)= 𝑑j + 𝑤 𝑥, 𝑦 + 𝑤(𝑦, … , 𝑢)

Inductive hypothesis: since 𝑥 was extracted before, 𝑑j = 𝛿(𝑠, 𝑥)

Since 𝑢 ∉ 𝑆, 𝑠, … , 𝑢 crosses the cut somewhere• Let 𝑥, 𝑦 be last edge in the path that crosses

the cut𝑆

Take any path 𝑠, … , 𝑢Extracted nodes define a cut (𝑆, 𝑉 − 𝑆) of 𝐺

Page 49: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Correctness of Dijkstra’s Algorithm: Claim 2

Let 𝑢 be the 𝑖 + 1 fg node extractedClaim 2: For every path 𝑠, … , 𝑢 , 𝑤 𝑠,… , 𝑢 ≥ 𝑑M

49

extracted nodes

𝑠𝑢

𝑥𝑦

𝑤 𝑠, … , 𝑢 ≥ 𝛿 𝑠, 𝑥 + 𝑤 𝑥, 𝑦 + 𝑤(𝑦, … , 𝑢)

≥ 𝑑k + 𝑤(𝑦, … , 𝑢)= 𝑑j + 𝑤 𝑥, 𝑦 + 𝑤(𝑦, … , 𝑢)

By construction of Dijkstra’s algorithm, when 𝑥 is extracted, 𝑑k is updated to satisfy

𝑑k ≤ 𝑑j + 𝑤(𝑥, 𝑦)

Since 𝑢 ∉ 𝑆, 𝑠, … , 𝑢 crosses the cut somewhere• Let 𝑥, 𝑦 be last edge in the path that crosses

the cut𝑆

Take any path 𝑠, … , 𝑢Extracted nodes define a cut (𝑆, 𝑉 − 𝑆) of 𝐺

Page 50: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Correctness of Dijkstra’s Algorithm: Claim 2

Let 𝑢 be the 𝑖 + 1 fg node extractedClaim 2: For every path 𝑠, … , 𝑢 , 𝑤 𝑠,… , 𝑢 ≥ 𝑑M

50

extracted nodes

𝑠𝑢

𝑥𝑦

𝑤 𝑠, … , 𝑢 ≥ 𝛿 𝑠, 𝑥 + 𝑤 𝑥, 𝑦 + 𝑤(𝑦, … , 𝑢)

≥ 𝑑k + 𝑤(𝑦, … , 𝑢)≥ 𝑑M + 𝑤(𝑦, … , 𝑢)

= 𝑑j + 𝑤 𝑥, 𝑦 + 𝑤(𝑦, … , 𝑢)

Greedy choice property: we always extract the node of minimal distance so 𝑑M ≤ 𝑑k

Since 𝑢 ∉ 𝑆, 𝑠, … , 𝑢 crosses the cut somewhere• Let 𝑥, 𝑦 be last edge in the path that crosses

the cut𝑆

Take any path 𝑠, … , 𝑢Extracted nodes define a cut (𝑆, 𝑉 − 𝑆) of 𝐺

Page 51: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Correctness of Dijkstra’s Algorithm: Claim 2

Let 𝑢 be the 𝑖 + 1 fg node extractedClaim 2: For every path 𝑠, … , 𝑢 , 𝑤 𝑠,… , 𝑢 ≥ 𝑑M

51

extracted nodes

𝑠𝑢

𝑥𝑦

𝑤 𝑠, … , 𝑢 ≥ 𝛿 𝑠, 𝑥 + 𝑤 𝑥, 𝑦 + 𝑤(𝑦, … , 𝑢)

≥ 𝑑k + 𝑤(𝑦, … , 𝑢)≥ 𝑑M + 𝑤(𝑦, … , 𝑢)≥ 𝑑M

= 𝑑j + 𝑤 𝑥, 𝑦 + 𝑤(𝑦, … , 𝑢)

All edge weights assumed to be positive

Since 𝑢 ∉ 𝑆, 𝑠, … , 𝑢 crosses the cut somewhere• Let 𝑥, 𝑦 be last edge in the path that crosses

the cut𝑆

Take any path 𝑠, … , 𝑢Extracted nodes define a cut (𝑆, 𝑉 − 𝑆) of 𝐺

Page 52: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Correctness of Dijkstra’s Algorithm

Proof by induction

Proof Idea: we will show that when node 𝑢 is removed from the priority queue, 𝑑M = 𝛿(𝑠, 𝑢)• Claim 1: There is a path of length 𝑑M (as long as 𝑑M < ∞) from 𝑠 to 𝑢 in 𝐺• Claim 2: For every path 𝑠, … , 𝑢 , 𝑤 𝑠,… , 𝑢 ≥ 𝑑M

52

Page 53: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Breadth-First Search

Input: a node 𝑠Behavior: Start with node 𝑠, visit all neighbors of 𝑠, then all neighbors of neighbors of 𝑠, until all nodes have been visitedOutput: lots of choices!• Is the graph connected?• Is there a path from 𝑠 to 𝑢?• Smallest number of “hops” from 𝑠 to 𝑢

53

Sounds like a “shortest path” property!

Page 54: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Dijkstra’s Algorithm

54

initialize 𝑑< = ∞ for each node 𝑣add all nodes 𝑣 ∈ 𝑉 to the priority queue PQ, using 𝑑< as the keyset 𝑑A = 0while PQ is not empty:

𝑣 = PQ. extractMin()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑢 ∈ PQ and 𝑑< + 𝑤 𝑣, 𝑢 < 𝑑M:PQ. decreaseKey 𝑢, 𝑑< + 𝑤 𝑣, 𝑢𝑢. parent = 𝑣

Page 55: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

Breadth-First Search

55

initialize a flag 𝑑< = 0 for each node 𝑣pick a start node 𝑠Q. push(𝑠)while Q is not empty:

𝑣 = Q. pop() and set 𝑑< = 1for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑑M = 0:Q. push(𝑢)

Key observation: replace the priority queue with a queue

flag to denote whether a node has been visited or not

Page 56: CS 4102: Algorithmsjh2jf/courses/fall2019/cs4102/lectures/... · CLRS Readings: Chapter 22, 23. Homework HW7due Thursday, November 14, 11pm •Graph algorithms •Written (use LaTeX!)

BFS to Count Number of Hops

56

initialize a counter 𝑑< = ∞ for each node 𝑣pick a start node 𝑠 and set 𝑑A = 0Q. push(𝑠)while Q is not empty:

𝑣 = Q. pop()for each 𝑢 ∈ 𝑉 such that 𝑣, 𝑢 ∈ 𝐸:

if 𝑑M = ∞:Q. push(𝑢)𝑑M = 𝑑< + 1

counter to denote number of hops from the source