1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest...
-
date post
22-Dec-2015 -
Category
Documents
-
view
216 -
download
0
Transcript of 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest...
![Page 1: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/1.jpg)
1
Heaps
Chapter 6 in CLRS
![Page 2: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/2.jpg)
2
Motivation
• Router: • Dijkstra’s algorithm for single
source shortest path
• Prim’s algorithm for minimum spanning trees
![Page 3: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/3.jpg)
3
Motivation
• Want to find the shortest route from New York to San Francisco
• Model the road-map with a graph
![Page 4: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/4.jpg)
4
A Graph G=(V,E)
V is a set of vertices
E is a set of edges (pairs of vertices)
![Page 5: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/5.jpg)
5
Model driving distances by weights on the edges
2 19
9
1
5
13
1710
148
21
V is a set of vertices
E is a set of edges (pairs of vertices)
![Page 6: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/6.jpg)
6
Source and destination
V is a set of vertices
E is a set of edges (pairs of vertices)
2 19
9
1
5
13
1710
148
21
![Page 7: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/7.jpg)
7
Dijkstra’s algorithm
• Assume all weights are non-negative
• Finds the shortest path from some fixed vertex s to every other vertex
![Page 8: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/8.jpg)
8
s
s15
103
1
4
2s3
s2
s4
15 8
Example
![Page 9: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/9.jpg)
9
Maintain an upper bound d(v) on the shortest path to v
0
∞
∞
∞
∞
s
s15
103
1
4
2s3
s2
s4
15 8
Example
![Page 10: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/10.jpg)
10
s
s15
103
1
4
2s3
s2
s4
15 8
Maintain an upper bound d(v) on the shortest path to v
0
∞
∞
∞
∞
A node is either scanned (in S) or labeled (in Q)
Initially S = and Q = V
![Page 11: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/11.jpg)
11
s
s15
103
1
4
2s3
s2
s4
15 8
Pick a vertex v in Q with minimum d(v) and add it to S
0
∞
∞
∞
∞
Initially S = and Q = V
![Page 12: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/12.jpg)
12
s
s15
103
1
4
2s3
s2
s4
15 8
Pick a vertex v in Q with minimum d(v) and add it to S
0
∞
∞
∞
∞
Initially S = and Q = V
v
w15
For every edge (v,w) where w in Q relax(v,w)
![Page 13: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/13.jpg)
13
s
s15
103
1
4
2s3
s2
s4
15 8
Relax(v,w)
If d(v) + w(v,w) < d(w) then d(w) ← d(v) + w(v,w) π(v) ← w
0
∞
∞
∞
∞
v
w
For every edge (v,w) where w in Q relax(v,w)
15
![Page 14: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/14.jpg)
14
s
s15
103
1
4
2s3
s2
s4
15 8
0
∞
∞
∞
∞
S = {s}
Relax(s,s4)
![Page 15: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/15.jpg)
15
s
s15
103
1
4
2s3
s2
s4
15 8
0
∞
∞
15
∞
Relax(s,s4)
S = {s}
![Page 16: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/16.jpg)
16
s
s15
103
1
4
2s3
s2
s4
15 8
0
∞
∞
15
∞
Relax(s,s4)Relax(s,s3)
S = {s}
![Page 17: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/17.jpg)
17
s
s15
103
1
4
2s3
s2
s4
15 8
0
∞
∞
15
10
Relax(s,s4)Relax(s,s3)
S = {s}
![Page 18: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/18.jpg)
18
s
s15
103
1
4
2s3
s2
s4
15 8
0
∞
∞
15
10
Relax(s,s4)Relax(s,s3)Relax(s,s1)
S = {s}
![Page 19: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/19.jpg)
19
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
∞
15
10
Relax(s,s4)Relax(s,s3)Relax(s,s1)
S = {s}
![Page 20: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/20.jpg)
20
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
∞
15
10
S = {s}
Pick a vertex v in Q with minimum d(v) and add it to S
![Page 21: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/21.jpg)
21
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
∞
15
10
S = {s,s1}
Pick a vertex v in Q with minimum d(v) and add it to S
![Page 22: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/22.jpg)
22
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
∞
15
10
S = {s,s1}
Pick a vertex v in Q with minimum d(v) and add it to S
Relax(s1,s3)
![Page 23: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/23.jpg)
23
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
∞
15
9
S = {s,s1}
Pick a vertex v in Q with minimum d(v) and add it to S
Relax(s1,s3)
![Page 24: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/24.jpg)
24
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
∞
15
9
S = {s,s1}
Pick a vertex v in Q with minimum d(v) and add it to S
Relax(s1,s3)Relax(s1,s2)
![Page 25: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/25.jpg)
25
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
6
15
9
S = {s,s1}
Pick a vertex v in Q with minimum d(v) and add it to S
Relax(s1,s3)Relax(s1,s2)
![Page 26: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/26.jpg)
26
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
6
15
9
S = {s,s1}
Pick a vertex v in Q with minimum d(v) and add it to S
![Page 27: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/27.jpg)
27
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
6
15
9
S = {s,s1,s2}
Pick a vertex v in Q with minimum d(v) and add it to S
![Page 28: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/28.jpg)
28
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
6
15
9
S = {s,s1,s2}
Pick a vertex v in Q with minimum d(v) and add it to S
Relax(s2,s3)
![Page 29: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/29.jpg)
29
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
6
15
8
S = {s,s1,s2}
Pick a vertex v in Q with minimum d(v) and add it to S
Relax(s2,s3)
![Page 30: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/30.jpg)
30
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
6
15
8
S = {s,s1,s2}
Pick a vertex v in Q with minimum d(v) and add it to S
![Page 31: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/31.jpg)
31
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
6
15
8
S = {s,s1,s2,s3}
Pick a vertex v in Q with minimum d(v) and add it to S
![Page 32: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/32.jpg)
32
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
6
15
8
S = {s,s1,s2,s3,s4}
Pick a vertex v in Q with minimum d(v) and add it to S
![Page 33: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/33.jpg)
33
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
6
15
8
S = {s,s1,s2,s3,s4}
When Q = then the d() values are the distances from s
The π function gives the shortest path tree
![Page 34: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/34.jpg)
34
s
s15
103
1
4
2s3
s2
s4
15 8
0
5
6
15
8
S = {s,s1,s2,s3,s4}
When Q = then the d() values are the distances from s
The π function gives the shortest path tree
![Page 35: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/35.jpg)
35
Implementation of Dijkstra’s algorithm
• We need to find efficiently the vertex with minimum d() in Q
• We need to update d() values of vertices in Q
![Page 36: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/36.jpg)
36
Required ADT
• Maintain items with keys subject to
• Insert(x,Q)• min(Q)• Deletemin(Q)• Decrease-key(x,Q,Δ)
![Page 37: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/37.jpg)
37
Required ADT
• Insert(x,Q)• min(Q)• Deletemin(Q)• Decrease-key(x,Q,Δ): Can simulate
by Delete(x,Q), insert(x-Δ,Q)
![Page 38: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/38.jpg)
38
• Insert(x,Q)• min(Q)• Deletemin(Q)• Decrease-key(x,Q,Δ): Can simulate
by Delete(x,Q), insert(x-Δ,Q)
How many times we do these operations ?
n = |V|
n
n
m = |E|
![Page 39: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/39.jpg)
39
Do we know an algorithm for this ADT ?
• Insert(x,Q)• min(Q)• Deletemin(Q)• Decrease-key(x,Q,Δ): Can simulate
by Delete(x,Q), insert(x-Δ,Q)Yes! Use binary search trees, we had insert and delete and also min in the dictionary data type
![Page 40: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/40.jpg)
40
Heap (min)
• Heap is A complete binary tree For any node v, both children’s keys
are larger than its key
Heap-ordered tree
23
7
17
3365
1924 2629
7940
![Page 41: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/41.jpg)
41
• Representing a heap with an array Get the elements from top to
bottom, from left to right
Array Representation
23
7
17
3365
1924 2629
7 23 17 24 29 26 19 65 33 40 79
7940
Q
![Page 42: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/42.jpg)
42
Array Representation
23
7
17
3365
1924 2629
7 23 17 24 29 26 19 65 33 40 79
7940
• Left(i): 2i+1• Right(i): 2i+2• Parent(i): (i-1)/2
Q
![Page 43: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/43.jpg)
43
Find the minimum
23
7
17
3365
1924 2629
7 23 17 24 29 26 19 65 33 40 79
7940
• Return Q[0]
Q
![Page 44: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/44.jpg)
44
Delete the minimum
23
7
17
3365
1924 2629
7 23 17 24 29 26 19 65 33 40 79
7940
Q
![Page 45: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/45.jpg)
45
Delete the minimum
23 17
3365
1924 2629
23 17 24 29 26 19 65 33 40 79
7940
Q
![Page 46: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/46.jpg)
46
Delete the minimum
23
79
17
3365
1924 2629
79 23 17 24 29 26 19 65 33 40
40
Q
![Page 47: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/47.jpg)
47
Delete the minimum
23
17
79
3365
1924 2629
17 23 79 24 29 26 19 65 33 40
40
Q
![Page 48: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/48.jpg)
48
Delete the minimum
23
17
19
3365
7924 2629
17 23 19 24 29 26 79 65 33 40
40
Q
![Page 49: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/49.jpg)
49
Delete the minimum
23
17
19
3365
7924 2629
17 23 19 24 29 26 79 65 33 40
40
Q
Q[0] ← Q[size(Q)-1]
Heapify-down(Q,0)
size(Q) ← size(Q)-1
![Page 50: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/50.jpg)
50
Heapify-down(Q,i)
Heapify-down(Q, i)
l ← left(i)
r ← right(i)
smallest ← i
if l < size(Q) and Q[l] < Q[smallest]
then smallest ← l
if r < size(Q) and Q[r] < Q[smallest]
then smallest ← r
if smallest > i then Q[i] ↔ Q[smallest]
Heapify-down(Q, smallest)
![Page 51: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/51.jpg)
51
Inserting an item
23
17
19
3365
7924 2629
17 23 19 24 29 26 79 65 33 40
40
Q
Insert(15,Q)
![Page 52: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/52.jpg)
52
Inserting an item
23
17
19
3365
7924 2629
17 23 19 24 29 26 79 65 33 40 15
40
Q
Insert(15,Q)Q[size(Q)] ← 15
15
Heapify-up(Q,size(Q))size(Q) ← size(Q) + 1
![Page 53: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/53.jpg)
53
Inserting an item
23
17
19
3365
7924 2615
17 23 19 24 15 26 79 65 33 40 29
40
Q
29
![Page 54: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/54.jpg)
54
Inserting an item
15
17
19
3365
7924 2623
17 15 19 24 23 26 79 65 33 40 29
40
Q
29
![Page 55: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/55.jpg)
55
Inserting an item
17
15
19
3365
7924 2623
15 17 19 24 23 26 79 65 33 40 29
40
Q
29
![Page 56: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/56.jpg)
56Q
Heapify-upH
eapify-up(A, i)
while i > 0 and A[i] < A[parent(i)]
do A[i] ↔ A[parent(i)]
i ← parent(i)
17
15
19
3365
7924 2623
40 29
15 17 19 24 23 26 79 65 33 40 29
![Page 57: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/57.jpg)
57
Other operations
• Decrease-key(x,Q,Δ)• Delete(x,Q)
• Can implement them easily using heapify
Assume a pointer is given to element x (no
need to find!!
![Page 58: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/58.jpg)
58
Heapsort (Williams, Floyd, 1964)
• Put the elements in an array• Make the array into a heap• Do a deletemin and put the
deleted element at the last position of the array
![Page 59: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/59.jpg)
59
Put the elements in the heap
65
79
26
3323
2924 1519
79 65 26 24 19 15 29 23 33 40 7
740
Q
![Page 60: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/60.jpg)
60
65
79
26
3323
2924 1519
79 65 26 24 19 15 29 23 33 40 7
740
Q
Make the elements into a heap
![Page 61: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/61.jpg)
61
Make the elements into a heap
65
79
26
3323
2924 1519
79 65 26 24 19 15 29 23 33 40 7
740
Q
Heapify-down(Q,4)
![Page 62: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/62.jpg)
62
65
79
26
3323
2924 157
79 65 26 24 7 15 29 23 33 40 19
1940
Q
Heapify-down(Q,4)
![Page 63: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/63.jpg)
63
65
79
26
3323
2924 157
79 65 26 24 7 15 29 23 33 40 19
1940
Q
Heapify-down(Q,3)
![Page 64: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/64.jpg)
64
65
79
26
3324
2923 157
79 65 26 23 7 15 29 24 33 40 19
1940
Q
Heapify-down(Q,3)
![Page 65: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/65.jpg)
65
65
79
26
3324
2923 157
79 65 26 23 7 15 29 24 33 40 19
1940
Q
Heapify-down(Q,2)
![Page 66: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/66.jpg)
66
65
79
15
3324
2923 267
79 65 15 23 7 26 29 24 33 40 19
1940
Q
Heapify-down(Q,2)
![Page 67: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/67.jpg)
67
65
79
15
3324
2923 267
79 65 15 23 7 26 29 24 33 40 19
1940
Q
Heapify-down(Q,1)
![Page 68: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/68.jpg)
68
7
79
15
3324
2923 2665
79 7 15 23 65 26 29 24 33 40 19
1940
Q
Heapify-down(Q,1)
![Page 69: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/69.jpg)
69
7
79
15
3324
2923 2619
79 7 15 23 19 26 29 24 33 40 65
6540
Q
Heapify-down(Q,1)
![Page 70: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/70.jpg)
70
7
79
15
3324
2923 2619
79 7 15 23 19 26 29 24 33 40 65
6540
Q
Heapify-down(Q,0)
![Page 71: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/71.jpg)
71
79
7
15
3324
2923 2619
7 79 15 23 19 26 29 24 33 40 65
6540
Q
Heapify-down(Q,0)
![Page 72: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/72.jpg)
72
19
7
15
3324
2923 2679
7 19 15 23 79 26 29 24 33 40 65
6540
Q
Heapify-down(Q,0)
![Page 73: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/73.jpg)
73
19
7
15
3324
2923 2640
7 19 15 23 40 26 29 24 33 79 65
6579
Q
Heapify-down(Q,0)
![Page 74: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/74.jpg)
74
23
7
15
3379
2924 2619
6540
We have at most n/2 nodes heapified at height 1
How much time it takes to build the heap this way ?
We have at most n/4 nodes heapified at height 2We have at most n/8 nodes heapified at height 3
1
1 2 3 ......... ( )2 4 8 2hn n n h
O O n O n
![Page 75: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/75.jpg)
75
Summary
• We can build the heap in linear time (we already did this analysis)
• We still have to deletemin the elements one by one in order to sort that will take O(nlog(n))
![Page 76: 1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.](https://reader031.fdocuments.in/reader031/viewer/2022032704/56649d7e5503460f94a60718/html5/thumbnails/76.jpg)
76
Comparison to RB / 2-3
• We can build the heap in linear time (we already did this analysis)
• If we start with unsorted elements: Can get percentiles efficiently (e.g. 10 smallest)
• Constants are smaller than in RB and 2-3
• Great for sorting• Great for priority queue (can be done
also by RB)