CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using...

58
CS 4102: Algorithms Shortest Path Algorithms Tom Horton and Robbie Hott Spring 2020

Transcript of CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using...

Page 1: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

CS 4102: AlgorithmsShortest Path Algorithms

Tom Horton and Robbie HottSpring 2020

Page 2: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

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: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Today’s Keywords

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

3

CLRS Readings: Chapter 22, 23

Page 4: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Minimum Spanning Tree

4

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

minimal cost

Cost 𝑇 = 3"∈$!

𝑤(𝑒)

10

2

6 11

95

8

3

7

3

1

8

12

9

Page 5: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Minimum Spanning Tree

5

Reminder: Kruskal’s is the first of 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 6: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

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: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Minimum Spanning Tree

7

Reminder: Kruskal’s is the first of 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: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Minimum Spanning Tree

8

Reminder: Kruskal’s is the first of 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

And so on… See previous lecture slides

Page 9: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Minimum Spanning Tree

9

Reminder: Prim’s is the second of 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 10: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

10

2

6 11

95

8

3

7

3

1

8

12

9

Minimum Spanning Tree

10

Reminder: Prim’s is the second of two greedy algorithms!

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

Page 11: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

10

2

6 11

95

8

3

7

3

1

8

12

9

Minimum Spanning Tree

11

Reminder: Prim’s is the second of two greedy algorithms!

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

Page 12: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

10

2

6 11

95

8

3

7

3

1

8

12

9

Minimum Spanning Tree

12

Reminder: Prim’s is the second of two greedy algorithms!

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

And so on… See previous lecture slides

Page 13: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Prim’s Algorithm Implementation

13

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 𝑑$ = 0while PQ is not empty:

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

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

each node also maintains a parent, initially NULL

Page 14: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Prim’s Algorithm Implementation

14

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 𝑑" = 0while PQ is not empty:

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

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

10

2

6 11

95

8

3

7

3

1

8

12

90

∞∞

Page 15: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Prim’s Algorithm Implementation

15

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 𝑑" = 0while PQ is not empty:

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

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

Page 16: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Prim’s Algorithm Implementation

16

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 𝑑" = 0while PQ is not empty:

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

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

Page 17: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Prim’s Algorithm Implementation

17

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 𝑑" = 0while PQ is not empty:

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

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

Page 18: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Prim’s Algorithm Implementation

18

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 𝑑" = 0while PQ is not empty:

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

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

Page 19: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Prim’s Algorithm Implementation

19

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 𝑑" = 0while PQ is not empty:

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

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

Page 20: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

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 𝑑" = 0while PQ is not empty:

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

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

Page 21: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

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 𝑑" = 0while PQ is not empty:

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

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

Page 22: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Prim’s Algorithm Implementation

22

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 𝑑" = 0while PQ is not empty:

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

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

Page 23: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

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 𝑑" = 0while PQ is not empty:

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

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

Page 24: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

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 𝑑" = 0while PQ is not empty:

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

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

Prim’s Algorithm Running Time

24

𝑂 𝑉Initialization:

𝑉 iterations𝑂 log 𝑉𝐸 iterations total

𝑂 log 𝑉

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

Page 25: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Single-Source Shortest Path

25

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 26: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

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

Dijkstra’s SP Algorithm

26

10

2

6 11

95

8

3

7

3

1

8

12

9

Page 27: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Prim’s MST Algorithm

27

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 28: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Prim’s MST Algorithm Implementation

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 𝑇

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

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

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

each node also maintains a parent, initially NULL

PQ’s key: weight of a single connecting edge

Page 29: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s SP Algorithm Implementation

29

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 𝑑$ = 0while PQ is not empty:

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

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

each node also maintains a parent, initially NULL

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

Page 30: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s Algorithm Implementation

30

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

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

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

10

2

6 11

95

8

3

7

3

1

8

12

90

∞∞

Page 31: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s Algorithm Implementation

31

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 𝑑" = 0while PQ is not empty:

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

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

Page 32: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s Algorithm Implementation

32

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 𝑑" = 0while PQ is not empty:

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

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

Page 33: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s Algorithm Implementation

33

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 𝑑" = 0while PQ is not empty:

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

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

Page 34: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s Algorithm Implementation

34

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 𝑑" = 0while PQ is not empty:

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

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

Page 35: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s Algorithm Implementation

35

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 𝑑" = 0while PQ is not empty:

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

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

Page 36: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s Algorithm Implementation

36

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 𝑑" = 0while PQ is not empty:

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

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

Page 37: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s Algorithm Implementation

37

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 𝑑" = 0while PQ is not empty:

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

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

Page 38: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s Algorithm Implementation

38

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 𝑑" = 0while PQ is not empty:

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

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

Page 39: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s Algorithm Implementation

39

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 𝑑" = 0while PQ is not empty:

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

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

Page 40: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

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

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

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

Dijkstra’s Algorithm Running Time

40

𝑂 𝑉Initialization:

𝑉 iterations𝑂 log 𝑉2 𝐸 iterations total

𝑂 log 𝑉

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

Page 41: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s Algorithm Proof Strategy

Proof by induction

Proof Idea: we will show that when node 𝑢 is removed from the priority queue, 𝑑J = 𝛿(𝑠, 𝑢)

• Claim 1: There is a path of length 𝑑% (as long as 𝑑% < ∞) from 𝑠 to 𝑢 in 𝐺• Claim 2: For every path 𝑠, … , 𝑢 , 𝑤 𝑠,… , 𝑢 ≥ 𝑑%

41

Page 42: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Correctness of Dijkstra’s Algorithm

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

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

42

Page 43: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Correctness of Dijkstra’s Algorithm: Claim 1

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

• Suppose 𝑑% < ∞• 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, 𝑑% = 𝑑# + 𝑤 𝑣, 𝑢• By the inductive hypothesis, there is a path 𝑠 → 𝑣 of length 𝑑# in 𝐺 and since

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

Page 44: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Correctness of Dijkstra’s Algorithm: Claim 2

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

44

extracted nodes

𝑠𝑢

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

𝑆

Page 45: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Correctness of Dijkstra’s Algorithm: Claim 2

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

45

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 46: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Correctness of Dijkstra’s Algorithm: Claim 2

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

46

extracted nodes

𝑠𝑢

𝑥𝑦

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

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

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 47: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Correctness of Dijkstra’s Algorithm: Claim 2

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

47

extracted nodes

𝑠𝑢

𝑥𝑦

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

≥ 𝑑" + 𝑤(𝑦, … , 𝑢)= 𝑑! + 𝑤 𝑥, 𝑦 + 𝑤(𝑦, … , 𝑢)

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

𝑑" ≤ 𝑑! + 𝑤(𝑥, 𝑦)

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 48: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Correctness of Dijkstra’s Algorithm: Claim 2

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

48

extracted nodes

𝑠𝑢

𝑥𝑦

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

≥ 𝑑" + 𝑤(𝑦, … , 𝑢)

≥ 𝑑# + 𝑤(𝑦, … , 𝑢)

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

Greedy choice property: we always extract the node of minimal distance so 𝑑# ≤ 𝑑"

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: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Correctness of Dijkstra’s Algorithm: Claim 2

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

49

extracted nodes

𝑠𝑢

𝑥𝑦

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

≥ 𝑑" + 𝑤(𝑦, … , 𝑢)

≥ 𝑑# + 𝑤(𝑦, … , 𝑢)≥ 𝑑#

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

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 50: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Correctness of Dijkstra’s Algorithm

Proof by induction

Proof Idea: we will show that when node 𝑢 is removed from the priority queue, 𝑑J = 𝛿(𝑠, 𝑢)

• Claim 1: There is a path of length 𝑑% (as long as 𝑑% < ∞) from 𝑠 to 𝑢 in 𝐺• Claim 2: For every path 𝑠, … , 𝑢 , 𝑤 𝑠,… , 𝑢 ≥ 𝑑%

50

Page 51: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Breadth-First Search

Input: a graph 𝐺 (weighted or unweighted) and a node 𝑠Behavior: Start with node 𝑠, visit all neighbors of 𝑠, then all neighbors of neighbors of 𝑠, until all nodes have been visitedOutput: BFS can be used to do many useful things, so lots of choices!

• Is the graph connected?• Is there a path from 𝑠 to 𝑢?• Smallest number of “hops” from 𝑠 to 𝑢

Notes: BFS doesn’t use edge weights at all!Also, depth-first search (DFS) also similarly useful

51

Sounds like a “shortest path” property!

Page 52: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Dijkstra’s SP Algorithm

52

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

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

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

Page 53: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Breadth-First Search

53

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

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

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

Key observation: replace the priority queue with a queue

flag to denote whether a node has been visited or not

Page 54: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Breadth-First Search: Time Complexity

54

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

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

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

Initialization: 𝑂 𝑉

𝑉 iterations

2 𝐸 iterations total

Overall running time: 𝑂 𝐸 + 𝑉The larger of 𝐸 and 𝑉 . (For graphs, we call this “linear”.)

Page 55: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

BFS to Count Number of Hops

55

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

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

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

counter to denote number of hops from the source

Page 56: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

BFS Trees

Let’s draw a BFS tree, a trace of its execution• Number each node as visited• Distance from start• Tree edges vs non-tree edges

10

2

6

11

95

8

3

7

3

1

8

12

9A

B

C

D

E

FG

I

H

Page 57: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

(Duplicated slide) BFS Trees

Let’s draw a BFS tree, a trace of its execution• Number each node as visited• Distance from start• Tree edges vs non-tree edges

10

2

6

11

95

8

3

7

3

1

8

12

9A

B

C

D

E

FG

I

H

Page 58: CS 4102: Algorithms · #=∞for each node ’ add all nodes ’∈"to the priority queue PQ, using $ #as the key pick a starting node +and set $ $=0 while PQis not empty: ’=PQ.extractMin()

Summary

Shortest path in weighted-graphs (single-source)• Dijkstra’s SP Algorithm

• Greedy algorithm• Similar in structure to Prim’s MST algorithm• Priority queue ordered by distance from start (not connecting edge weight)

Unweighted graphs, number of “hops”• Distance is number of edges (not sum of edge weights)• Breadth-first Search (BFS)

• Not greedy. Doesn’t used edge weights

BFS (and DFS) useful to solve many other graph problems• Connectivity, find cycles, ….