AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation...

70
Algorithms and Theory of Computation Lecture 7: Shortest Path Problem, Priority Queue Xiaohui Bei MAS 714 September 1, 2020 Nanyang Technological University MAS 714 September 1, 2020 1 / 24

Transcript of AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation...

Page 1: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Algorithms and Theory of Computation

Lecture 7: Shortest Path Problem, Priority Queue

Xiaohui Bei

MAS 714

September 1, 2020

Nanyang Technological University MAS 714 September 1, 2020 1 / 24

Page 2: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Shortest Paths Problems

Shortest Paths ProblemsGiven a (undirected or directed) weighted graph G = (V, E) with edge lengths (or weights).For edge e = (u, v), `(e) = `(u, v) is its length.

1 Given vertices s, t, find a shortest path from s to t.2 Given vertex s, find shortest paths from s to all other vertices.3 Find shortest paths for all pair of vertices.

The length of a path is the sum of its edge weights.

Nanyang Technological University MAS 714 September 1, 2020 2 / 24

Page 3: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Single-Source Shortest Path

Single-Source Shortest PathGiven a directed weighted graph G = (V, E) with non-negative edge lengths (or weights). Foredge e = (u, v), `(e) = `(u, v) is its length.

1 Given vertices s, t, find a shortest path from s to t.2 Given vertex s, find shortest paths from s to all other vertices.

Focus on directed graphs.undirected graph problem can be reduced to directed graph problem

Uniform case: all edges lengths are 1.BFS(s) solves the problem in O(m+ n) time.

Nanyang Technological University MAS 714 September 1, 2020 3 / 24

Page 4: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Single-Source Shortest Path

Single-Source Shortest PathGiven a directed weighted graph G = (V, E) with non-negative edge lengths (or weights). Foredge e = (u, v), `(e) = `(u, v) is its length.

1 Given vertices s, t, find a shortest path from s to t.2 Given vertex s, find shortest paths from s to all other vertices.

Focus on directed graphs.undirected graph problem can be reduced to directed graph problem

Uniform case: all edges lengths are 1.BFS(s) solves the problem in O(m+ n) time.

Nanyang Technological University MAS 714 September 1, 2020 3 / 24

Page 5: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Ideas

Why does BFS work?

BFS(s) explores nodes in increasing distance from s

Let dist(s, v) denote the shortest path length from s to v.

Algorithm: ShortestPathAlgorithm(s):Initialize dist(s, v) = ∞ for each vertex v ∈ V;Initialize S = ∅;while S 6= V do

Find vertex v ∈ V − S that is the closest to s;Update dist(s, v);S = S ∪ v

How to find the next closest vertex?

Nanyang Technological University MAS 714 September 1, 2020 4 / 24

Page 6: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Ideas

Why does BFS work?

BFS(s) explores nodes in increasing distance from s

Let dist(s, v) denote the shortest path length from s to v.

Algorithm: ShortestPathAlgorithm(s):Initialize dist(s, v) = ∞ for each vertex v ∈ V;Initialize S = ∅;while S 6= V do

Find vertex v ∈ V − S that is the closest to s;Update dist(s, v);S = S ∪ v

How to find the next closest vertex?

Nanyang Technological University MAS 714 September 1, 2020 4 / 24

Page 7: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Ideas

Why does BFS work?

BFS(s) explores nodes in increasing distance from s

Let dist(s, v) denote the shortest path length from s to v.

Algorithm: ShortestPathAlgorithm(s):Initialize dist(s, v) = ∞ for each vertex v ∈ V;Initialize S = ∅;while S 6= V do

Find vertex v ∈ V − S that is the closest to s;Update dist(s, v);S = S ∪ v

How to find the next closest vertex?

Nanyang Technological University MAS 714 September 1, 2020 4 / 24

Page 8: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Ideas

Why does BFS work?

BFS(s) explores nodes in increasing distance from s

Let dist(s, v) denote the shortest path length from s to v.

Algorithm: ShortestPathAlgorithm(s):Initialize dist(s, v) = ∞ for each vertex v ∈ V;Initialize S = ∅;while S 6= V do

Find vertex v ∈ V − S that is the closest to s;Update dist(s, v);S = S ∪ v

How to find the next closest vertex?Nanyang Technological University MAS 714 September 1, 2020 4 / 24

Page 9: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Finding the ith Closest Vertex

At the beginning of the ith iteration, S already stores the i− 1 closest vertices to s.

What do we know about the ith closest vertex?

Claim 1Let P be a shortest path from s to v where v is the ith closest vertex. Then all intermediatevertices in P belong to S.

Proof: Let v ′ be an intermediate vertex in path P, then dist(s, v ′) < dist(s, v). Thus v ′ ∈ S.

CorollaryAt each step, the next closest vertex is adjacent to S.

Nanyang Technological University MAS 714 September 1, 2020 5 / 24

Page 10: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Finding the ith Closest Vertex

At the beginning of the ith iteration, S already stores the i− 1 closest vertices to s.

What do we know about the ith closest vertex?

Claim 1Let P be a shortest path from s to v where v is the ith closest vertex. Then all intermediatevertices in P belong to S.

Proof: Let v ′ be an intermediate vertex in path P, then dist(s, v ′) < dist(s, v). Thus v ′ ∈ S.

CorollaryAt each step, the next closest vertex is adjacent to S.

Nanyang Technological University MAS 714 September 1, 2020 5 / 24

Page 11: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Finding the ith Closest Vertex

At the beginning of the ith iteration, S already stores the i− 1 closest vertices to s.

What do we know about the ith closest vertex?

Claim 1Let P be a shortest path from s to v where v is the ith closest vertex. Then all intermediatevertices in P belong to S.

Proof: Let v ′ be an intermediate vertex in path P, then dist(s, v ′) < dist(s, v). Thus v ′ ∈ S.

CorollaryAt each step, the next closest vertex is adjacent to S.

Nanyang Technological University MAS 714 September 1, 2020 5 / 24

Page 12: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Finding the ith Closest Vertex

At the beginning of the ith iteration, S already stores the i− 1 closest vertices to s.

What do we know about the ith closest vertex?

Claim 1Let P be a shortest path from s to v where v is the ith closest vertex. Then all intermediatevertices in P belong to S.

Proof: Let v ′ be an intermediate vertex in path P, then dist(s, v ′) < dist(s, v). Thus v ′ ∈ S.

CorollaryAt each step, the next closest vertex is adjacent to S.

Nanyang Technological University MAS 714 September 1, 2020 5 / 24

Page 13: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Finding the ith Closest Vertex

Claim 2If s = v0 → v1 → · · · → vk = v is a shortest path from s to v,then for any 1 ≤ i < k:

s = v0 → v1 → · · · → vi is a shortest path from s to vi

Assume that S contains the i− 1 closest vertices to s. For each u ∈ V − S, let

π(u) = mina∈S

(dist(s, a) + `(a, u))

LemmaIf u is the ith closest vertex to s, then π(u) = dist(s, u).

CorollaryThe ith closest vertex to s is the vertex u ∈ V − S such that π(u) = minv∈V−S π(v).

Nanyang Technological University MAS 714 September 1, 2020 6 / 24

Page 14: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Finding the ith Closest Vertex

Claim 2If s = v0 → v1 → · · · → vk = v is a shortest path from s to v,then for any 1 ≤ i < k:

s = v0 → v1 → · · · → vi is a shortest path from s to vi

Assume that S contains the i− 1 closest vertices to s. For each u ∈ V − S, let

π(u) = mina∈S

(dist(s, a) + `(a, u))

LemmaIf u is the ith closest vertex to s, then π(u) = dist(s, u).

CorollaryThe ith closest vertex to s is the vertex u ∈ V − S such that π(u) = minv∈V−S π(v).

Nanyang Technological University MAS 714 September 1, 2020 6 / 24

Page 15: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Finding the ith Closest Vertex

Claim 2If s = v0 → v1 → · · · → vk = v is a shortest path from s to v,then for any 1 ≤ i < k:

s = v0 → v1 → · · · → vi is a shortest path from s to vi

Assume that S contains the i− 1 closest vertices to s. For each u ∈ V − S, let

π(u) = mina∈S

(dist(s, a) + `(a, u))

LemmaIf u is the ith closest vertex to s, then π(u) = dist(s, u).

CorollaryThe ith closest vertex to s is the vertex u ∈ V − S such that π(u) = minv∈V−S π(v).

Nanyang Technological University MAS 714 September 1, 2020 6 / 24

Page 16: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Finding the ith Closest Vertex

Claim 2If s = v0 → v1 → · · · → vk = v is a shortest path from s to v,then for any 1 ≤ i < k:

s = v0 → v1 → · · · → vi is a shortest path from s to vi

Assume that S contains the i− 1 closest vertices to s. For each u ∈ V − S, let

π(u) = mina∈S

(dist(s, a) + `(a, u))

LemmaIf u is the ith closest vertex to s, then π(u) = dist(s, u).

CorollaryThe ith closest vertex to s is the vertex u ∈ V − S such that π(u) = minv∈V−S π(v).

Nanyang Technological University MAS 714 September 1, 2020 6 / 24

Page 17: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Algorithm

Algorithm: ShortestPathAlgorithm(s):Initialize π(v) = ∞ for each vertex v ∈ V;Initialize S = ∅, π(s) = 0;while S 6= V do

Find vertex u ∈ V − S such that π(u) = minv∈V−S π(v);dist(s, u) = π(u);S = S ∪ u;foreach vertex v ∈ V − S do

π(v) = mina∈S(dist(s, a) + `(a, v));

Correctness: by induction using previous lemmas.Running Time: O(n(n+m)) time.

n outer iterations. O(m+ n) time in each iteration.

Nanyang Technological University MAS 714 September 1, 2020 7 / 24

Page 18: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Algorithm

Algorithm: ShortestPathAlgorithm(s):Initialize π(v) = ∞ for each vertex v ∈ V;Initialize S = ∅, π(s) = 0;while S 6= V do

Find vertex u ∈ V − S such that π(u) = minv∈V−S π(v);dist(s, u) = π(u);S = S ∪ u;foreach vertex v ∈ V − S do

π(v) = mina∈S(dist(s, a) + `(a, v));

Correctness: by induction using previous lemmas.

Running Time: O(n(n+m)) time.n outer iterations. O(m+ n) time in each iteration.

Nanyang Technological University MAS 714 September 1, 2020 7 / 24

Page 19: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Algorithm

Algorithm: ShortestPathAlgorithm(s):Initialize π(v) = ∞ for each vertex v ∈ V;Initialize S = ∅, π(s) = 0;while S 6= V do

Find vertex u ∈ V − S such that π(u) = minv∈V−S π(v);dist(s, u) = π(u);S = S ∪ u;foreach vertex v ∈ V − S do

π(v) = mina∈S(dist(s, a) + `(a, v));

Correctness: by induction using previous lemmas.Running Time: O(n(n+m)) time.

n outer iterations. O(m+ n) time in each iteration.Nanyang Technological University MAS 714 September 1, 2020 7 / 24

Page 20: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Example

a

b

cd

e

f

g

h

9

6

13

10

308

18

11

16

25

6

6

6

0

Nanyang Technological University MAS 714 September 1, 2020 8 / 24

Page 21: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Example

a

b

cd

e

f

g

h

9

6

13

10

308

18

11

16

25

6

6

6

0

9

6

13

0

Nanyang Technological University MAS 714 September 1, 2020 8 / 24

Page 22: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Example

a

b

cd

e

f

g

h

9

6

13

10

308

18

11

16

25

6

6

6

0

9

6

13

24

36

066

Nanyang Technological University MAS 714 September 1, 2020 8 / 24

Page 23: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Example

a

b

cd

e

f

g

h

9

6

13

10

308

18

11

16

25

6

6

6

0

9

6

13

24

36

19

06

9

6

9

Nanyang Technological University MAS 714 September 1, 2020 8 / 24

Page 24: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Example

a

b

cd

e

f

g

h

9

6

13

10

308

18

11

16

25

6

6

6

0

9

6

13

24

36

19

38

06

9

13

6

9

13

Nanyang Technological University MAS 714 September 1, 2020 8 / 24

Page 25: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Example

a

b

cd

e

f

g

h

9

6

13

10

308

18

11

16

25

6

6

6

0

9

6

13

24

36

19

38

25

06

9

13

19

6

9

13

10

Nanyang Technological University MAS 714 September 1, 2020 8 / 24

Page 26: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Example

a

b

cd

e

f

g

h

9

6

13

10

308

18

11

16

25

6

6

6

0

9

6

13

24

36

19

38

25

36

06

9

13

19

256

9

13

10

6

Nanyang Technological University MAS 714 September 1, 2020 8 / 24

Page 27: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Example

a

b

cd

e

f

g

h

9

6

13

10

308

18

11

16

25

6

6

6

0

9

6

13

24

36

19

38

25

36

06

9

13

19

25

36

6

9

13

10

6

11

Nanyang Technological University MAS 714 September 1, 2020 8 / 24

Page 28: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Example

a

b

cd

e

f

g

h

9

6

13

10

308

18

11

16

25

6

6

6

0

9

6

13

24

36

19

38

25

36

06

9

13

19

25

36

38

6

9

13

10

6

11

25

Nanyang Technological University MAS 714 September 1, 2020 8 / 24

Page 29: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

More Efficient Implementation

Critical Optimization For each unexplored vertex v, explicitly maintain π(v) instead ofcomputing directly from formula:

π(v) = minu∈S

(dist(s, u) + `(u, v)).

For each v /∈ S, π(v) can only decrease (because S can only increase).More specifically, suppose u is added to S and there is an edge (u, v) leaving u. Then, itsuffices to update

π(v) = minπ(v), dist(s, u) + `(u, v)

Nanyang Technological University MAS 714 September 1, 2020 9 / 24

Page 30: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

More Efficient Implementation

Critical Optimization For each unexplored vertex v, explicitly maintain π(v) instead ofcomputing directly from formula:

π(v) = minu∈S

(dist(s, u) + `(u, v)).

For each v /∈ S, π(v) can only decrease (because S can only increase).More specifically, suppose u is added to S and there is an edge (u, v) leaving u. Then, itsuffices to update

π(v) = minπ(v), dist(s, u) + `(u, v)

Nanyang Technological University MAS 714 September 1, 2020 9 / 24

Page 31: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Dijkstra’s Algorithm

1 eliminate π(v) and let dist(s, v) maintain it2 update dist values after adding v by scanning edges out of v

Algorithm: Dijkstra(s):Initialize dist(s, v) = ∞ for each vertex v ∈ V;Initialize S = ∅, dist(s, s) = 0;while S 6= V do

Find vertex u ∈ V − S such that dist(s, u) = minv∈V−S dist(s, v);S = S ∪ u;foreach vertex v ∈ Adj(u) do

dist(s, v) = min dist(s, v), dist(s, u) + `(u, v);

How to maintain dist values efficiently? Priority Queues

Nanyang Technological University MAS 714 September 1, 2020 10 / 24

Page 32: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Dijkstra’s Algorithm

1 eliminate π(v) and let dist(s, v) maintain it2 update dist values after adding v by scanning edges out of v

Algorithm: Dijkstra(s):Initialize dist(s, v) = ∞ for each vertex v ∈ V;Initialize S = ∅, dist(s, s) = 0;while S 6= V do

Find vertex u ∈ V − S such that dist(s, u) = minv∈V−S dist(s, v);S = S ∪ u;foreach vertex v ∈ Adj(u) do

dist(s, v) = min dist(s, v), dist(s, u) + `(u, v);

How to maintain dist values efficiently?

Priority Queues

Nanyang Technological University MAS 714 September 1, 2020 10 / 24

Page 33: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Dijkstra’s Algorithm

1 eliminate π(v) and let dist(s, v) maintain it2 update dist values after adding v by scanning edges out of v

Algorithm: Dijkstra(s):Initialize dist(s, v) = ∞ for each vertex v ∈ V;Initialize S = ∅, dist(s, s) = 0;while S 6= V do

Find vertex u ∈ V − S such that dist(s, u) = minv∈V−S dist(s, v);S = S ∪ u;foreach vertex v ∈ Adj(u) do

dist(s, v) = min dist(s, v), dist(s, u) + `(u, v);

How to maintain dist values efficiently? Priority QueuesNanyang Technological University MAS 714 September 1, 2020 10 / 24

Page 34: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Priority Queues

Priority QueuesStore a set S of n elements, where each element v ∈ S has an associated real/integer keyk(v), with the following operations:

1 Make-Queue: create an empty queue2 Find-Min: find the minimum key in S

3 Extract-Min: remove v ∈ S with the smallest key and return it4 Insert(v, k(v)): insert new element v with key k(v) to S

5 Delete(v): remove element v from S

6 Decrease-Key(v, k ′(v)): decrease key of v from k(v) to k ′(v)

Decrease-Key is implemented via delete and insert.

Nanyang Technological University MAS 714 September 1, 2020 11 / 24

Page 35: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Priority Queues

Applications:Prim’s MST algorithmDijkstra’s shortest-path algorithmheapsortonline medianetc.

Implementations:binary heapsd-ary heapsbinomial heapsFibonacci heaps

Nanyang Technological University MAS 714 September 1, 2020 12 / 24

Page 36: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Priority Queues

Applications:Prim’s MST algorithmDijkstra’s shortest-path algorithmheapsortonline medianetc.

Implementations:binary heapsd-ary heapsbinomial heapsFibonacci heaps

Nanyang Technological University MAS 714 September 1, 2020 12 / 24

Page 37: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Complete Binary Tree

A binary tree is a tree in which each vertex has at most two children.

A complete tree is a binary tree such that every level, except possibly the last level, iscompletely filled, and all vertices are as far left as possible.

PropertyThe depth of a complete binary tree with n vertices is blog2 nc.

Proof. The depth increases (by 1) only when n is a power of 2.

Nanyang Technological University MAS 714 September 1, 2020 13 / 24

Page 38: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Complete Binary Tree

A binary tree is a tree in which each vertex has at most two children.

A complete tree is a binary tree such that every level, except possibly the last level, iscompletely filled, and all vertices are as far left as possible.

PropertyThe depth of a complete binary tree with n vertices is blog2 nc.

Proof. The depth increases (by 1) only when n is a power of 2.Nanyang Technological University MAS 714 September 1, 2020 13 / 24

Page 39: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Tree in Nature

Nanyang Technological University MAS 714 September 1, 2020 14 / 24

Page 40: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap

Binary HeapA Binary Heap is a complete binary tree such that for every element v at some vertex, theelement w at this vertex’s parent satisfies k(w) ≤ k(v).

6

10

12

21 17

18

19

8

11 25

parent

child child

Nanyang Technological University MAS 714 September 1, 2020 15 / 24

Page 41: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Explicit ImplementationPointer representation. Vertex has a pointer to parent and two children.

maintain number of elements n

maintain pointer to root vertex

6

10

12

21 17

18

19

8

11 25

root

Nanyang Technological University MAS 714 September 1, 2020 16 / 24

Page 42: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Implicit ImplementationArray representation. Indices start at 1.

take vertices in level orderparent of vertex at k is at bk/2cchildren of vertex at k are at 2k and 2k+ 1

6

10

12

21 17

18

19

8

11 25

1

2 3

4 5 6 7

8 9 10

Nanyang Technological University MAS 714 September 1, 2020 17 / 24

Page 43: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap: Insert

Insert. Add element in new vertex at end; repeatedly exchange new element with element inits parent until heap order is restored.

6

10

12

21 17

18

19 7

8

11 25

swim up

Time = O(logn)

Nanyang Technological University MAS 714 September 1, 2020 18 / 24

Page 44: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap: Insert

Insert. Add element in new vertex at end; repeatedly exchange new element with element inits parent until heap order is restored.

6

10

12

21 17

18

19 7

8

11 25

18

7

swim up

Time = O(logn)

Nanyang Technological University MAS 714 September 1, 2020 18 / 24

Page 45: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap: Insert

Insert. Add element in new vertex at end; repeatedly exchange new element with element inits parent until heap order is restored.

6

10

12

21 17

18

19 7

8

11 25

18

10

7

swim up

Time = O(logn)

Nanyang Technological University MAS 714 September 1, 2020 18 / 24

Page 46: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap: Insert

Insert. Add element in new vertex at end; repeatedly exchange new element with element inits parent until heap order is restored.

6

10

12

21 17

18

19 7

8

11 25

18

10

7

swim up

Time = O(logn)

Nanyang Technological University MAS 714 September 1, 2020 18 / 24

Page 47: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap: Extract-Min

Extract-Min. Exchange element in root vertex with last vertex; repeatedly exchange element inroot with its smaller child until heap order is restored.

6

7

12

21 17

10

19 18

8

11 25

sink down

Time = O(logn)

Nanyang Technological University MAS 714 September 1, 2020 19 / 24

Page 48: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap: Extract-Min

Extract-Min. Exchange element in root vertex with last vertex; repeatedly exchange element inroot with its smaller child until heap order is restored.

6

7

12

21 17

10

19 18

8

11 25

18

6

sink down

Time = O(logn)

Nanyang Technological University MAS 714 September 1, 2020 19 / 24

Page 49: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap: Extract-Min

Extract-Min. Exchange element in root vertex with last vertex; repeatedly exchange element inroot with its smaller child until heap order is restored.

6

7

12

21 17

10

19 18

8

11 25

18

66

sink down

Time = O(logn)

Nanyang Technological University MAS 714 September 1, 2020 19 / 24

Page 50: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap: Extract-Min

Extract-Min. Exchange element in root vertex with last vertex; repeatedly exchange element inroot with its smaller child until heap order is restored.

6

7

12

21 17

10

19 18

8

11 25

18

66

7

18

sink down

Time = O(logn)

Nanyang Technological University MAS 714 September 1, 2020 19 / 24

Page 51: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap: Extract-Min

Extract-Min. Exchange element in root vertex with last vertex; repeatedly exchange element inroot with its smaller child until heap order is restored.

6

7

12

21 17

10

19 18

8

11 25

18

66

7

10

18

sink down

Time = O(logn)

Nanyang Technological University MAS 714 September 1, 2020 19 / 24

Page 52: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap: Extract-Min

Extract-Min. Exchange element in root vertex with last vertex; repeatedly exchange element inroot with its smaller child until heap order is restored.

6

7

12

21 17

10

19 18

8

11 25

18

66

7

10

18

sink down

Time = O(logn)

Nanyang Technological University MAS 714 September 1, 2020 19 / 24

Page 53: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap: Delete and Find-Min

Delete. Exchange the element in this vertex with the last vertex; either swim up or sink downthe vertex until heap order is restored.

Time = O(logn)

Decrease-Key. Repeatedly swim up the vertex until heap order is restored.Time = O(logn)

Find-Min. Return the element in the root vertex.Time = O(1)

Nanyang Technological University MAS 714 September 1, 2020 20 / 24

Page 54: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Binary Heap: Analysis

Dijkstra’a Algorithm with a priority queueO(n) Insert operationsO(n) Extract-Min operationsO(m) Decrease-Key operations

Dijkstra’s AlgorithmDijkstra’s algorithm can be implemented in O((n+m) logn) time.

Nanyang Technological University MAS 714 September 1, 2020 21 / 24

Page 55: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.

Can be done in O(n logn) time by inserting each element.Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 26

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 56: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.Can be done in O(n logn) time by inserting each element.

Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 26

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 57: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.Can be done in O(n logn) time by inserting each element.

Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 2621

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 58: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.Can be done in O(n logn) time by inserting each element.

Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 2615

21

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 59: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.Can be done in O(n logn) time by inserting each element.

Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 2615

21

7

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 60: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.Can be done in O(n logn) time by inserting each element.

Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 2615

21

9

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 61: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.Can be done in O(n logn) time by inserting each element.

Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 2615

21

3

9

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 62: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.Can be done in O(n logn) time by inserting each element.

Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 2615

21

3

9

12

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 63: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.Can be done in O(n logn) time by inserting each element.

Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 2615

21

3

9

7

12

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 64: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.Can be done in O(n logn) time by inserting each element.

Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 2615

21

3

9

7

11

12

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 65: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.Can be done in O(n logn) time by inserting each element.

Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 2615

21

3

9

7

11

12

8

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 66: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.Can be done in O(n logn) time by inserting each element.

Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 2615

21

3

9

7

11

12

3

8

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 67: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

Heapify. Given n elements, construct a binary heap containing them.Can be done in O(n logn) time by inserting each element.

Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with itssmaller child until subtree rooted at i is heap-ordered.

8

12

7

14 11

21

15 22

9

3 2615

21

3

9

7

11

12

3

8

Nanyang Technological University MAS 714 September 1, 2020 22 / 24

Page 68: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

TheoremGiven n elements, a binary heap containing those n elements can be constructed in O(n) time.

Intuition:There are at most dn/2h+1e vertices of height h.The amount of work to sink a vertex is proportional to its height h.

Nanyang Technological University MAS 714 September 1, 2020 23 / 24

Page 69: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

Heapify

TheoremGiven n elements, a binary heap containing those n elements can be constructed in O(n) time.

Intuition:There are at most dn/2h+1e vertices of height h.The amount of work to sink a vertex is proportional to its height h.

Nanyang Technological University MAS 714 September 1, 2020 23 / 24

Page 70: AlgorithmsandTheoryofComputation … · 2020. 9. 1. · AlgorithmsandTheoryofComputation Lecture7:ShortestPathProblem,PriorityQueue XiaohuiBei MAS714 September1,2020 NanyangTechnologicalUniversity

More on Dijkstra and Priority Queues

1 Dijkstra’s algorithm gives shortest paths from s to all vertices in V.

2 Dijkstra’s algorithm can be implemented in O(n logn+m) time using Fibonacci Heaps.If m = Ω(nlogn), running time is linear in input size.

3 Data structures are complicated to analyze/implement. Recent work has obtained datastructures that are easier to analyze and implement, and perform well in practice.Rank-Pairing Heaps (European Symposium on Algorithms, September 2009)

Nanyang Technological University MAS 714 September 1, 2020 24 / 24