Download presentation
Presentation is loading. Please wait.
Published byErin Palmer Modified over 9 years ago
1
Data Structures Haim Kaplan & Uri Zwick December 2013 Binary Heaps 1
2
Required ADT Maintain items with keys subject to Insert(x,Q) min(Q) Deletemin(Q) Decrease-key(x,Q,Δ) Delete(x,Q) 2
3
Required ADT Decrease-key(x,Q,Δ): Can simulate by Delete(x,Q), insert(x-Δ,Q) We can use Red-Black trees to implement all operations in O(log n) time We want to implement Decrease-key in O(1) (amortized) time 3
4
Motivation Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees 4
5
Motivation Want to find the shortest route from New York to San Francisco Model the road-map with a graph 5
6
A Graph G=(V,E) V is a set of vertices E is a set of edges (pairs of vertices) 6
7
Model driving distances by weights on the edges 2 19 9 1 5 13 17 10 14 8 21 V is a set of vertices E is a set of edges (pairs of vertices) 7
8
Source and destination V is a set of vertices E is a set of edges (pairs of vertices) 2 19 9 1 5 13 17 10 14 8 21 8
9
Dijkstra’s algorithm Assume all weights are non-negative Finds the shortest path from some fixed vertex s to every other vertex 9
10
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 Example 10
11
Maintain an upper bound d(v) on the shortest path to v 0 ∞ ∞ ∞ ∞ s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 Example 11
12
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 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 12
13
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 Pick a vertex v in Q with minimum d(v) and add it to S 0 ∞ ∞ ∞ ∞ Initially S = and Q = V 13
14
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 Pick a vertex v in Q with minimum d(v) and add it to S 0 ∞ ∞ ∞ ∞ Initially S = and Q = V v w 15 For every edge (v,w) where w in Q relax(v,w) 14
15
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 Relax(v,w) If d(v) + w(v,w) < d(w) then d(w) ← d(v) + w(v,w) π(w) ← v 0 ∞ ∞ ∞ ∞ v w For every edge (v,w) where w in Q relax(v,w) 15
16
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 ∞ ∞ ∞ ∞ S = {s} Relax(s,s 4 ) 16
17
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 ∞ ∞ ∞ Relax(s,s 4 ) S = {s} 17
18
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 ∞ ∞ ∞ Relax(s,s 4 ) Relax(s,s 3 ) S = {s} 18
19
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 ∞ ∞ 10 Relax(s,s 4 ) Relax(s,s 3 ) S = {s} 19
20
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 ∞ ∞ 10 Relax(s,s 4 ) Relax(s,s 3 ) Relax(s,s 1 ) S = {s} 20
21
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 ∞ 10 Relax(s,s 4 ) Relax(s,s 3 ) Relax(s,s 1 ) S = {s} 21
22
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 ∞ 10 S = {s} Pick a vertex v in Q with minimum d(v) and add it to S 22
23
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 ∞ 10 S = {s,s 1 } Pick a vertex v in Q with minimum d(v) and add it to S 23
24
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 ∞ 10 S = {s,s 1 } Pick a vertex v in Q with minimum d(v) and add it to S Relax(s 1,s 3 ) 24
25
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 ∞ 9 S = {s,s 1 } Pick a vertex v in Q with minimum d(v) and add it to S Relax(s 1,s 3 ) 25
26
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 ∞ 9 S = {s,s 1 } Pick a vertex v in Q with minimum d(v) and add it to S Relax(s 1,s 3 ) Relax(s 1,s 2 ) 26
27
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 6 9 S = {s,s 1 } Pick a vertex v in Q with minimum d(v) and add it to S Relax(s 1,s 3 ) Relax(s 1,s 2 ) 27
28
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 6 9 S = {s,s 1 } Pick a vertex v in Q with minimum d(v) and add it to S 28
29
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 6 9 S = {s,s 1,s 2 } Pick a vertex v in Q with minimum d(v) and add it to S 29
30
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 6 9 S = {s,s 1,s 2 } Pick a vertex v in Q with minimum d(v) and add it to S Relax(s 2,s 3 ) 30
31
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 6 8 S = {s,s 1,s 2 } Pick a vertex v in Q with minimum d(v) and add it to S Relax(s 2,s 3 ) 31
32
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 6 8 S = {s,s 1,s 2 } Pick a vertex v in Q with minimum d(v) and add it to S 32
33
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 6 8 S = {s,s 1,s 2,s 3 } Pick a vertex v in Q with minimum d(v) and add it to S 33
34
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 6 8 S = {s,s 1,s 2,s 3,s 4 } Pick a vertex v in Q with minimum d(v) and add it to S 34
35
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 6 8 S = {s,s 1,s 2,s 3,s 4 } When Q = then the d() values are the distances from s The π function gives the shortest path tree 35
36
s s1s1 5 10 3 1 4 2 s3s3 s2s2 s4s4 15 8 0 5 6 8 S = {s,s 1,s 2,s 3,s 4 } When Q = then the d() values are the distances from s The π function gives the shortest path tree 36
37
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 37
38
Required ADT Maintain items with keys subject to Insert(x,Q) min(Q) Deletemin(Q) Decrease-key(x,Q,Δ) 38
39
Required ADT Insert(x,Q) min(Q) Deletemin(Q) Decrease-key(x,Q,Δ): Can simulate by Delete(x,Q), insert(x-Δ,Q) 39
40
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| Total running time: O(m log n) → O(m + n log n) 40
41
Heap Heap is An almost complete binary tree The items at the children of v are greater than the one at v Heap-ordered tree 23 7 17 3365 19 24 2629 7940 41
42
Representing a heap with an array Get the elements from top to bottom, from left to right Array Representation 23 7 17 3365 19 24 2629 723172429261965334079 40 Q 42
43
Array Representation 23 7 17 3365 19 24 2629 723172429261965334079 40 Left(i): 2i+1 Right(i): 2i+2 Parent(i): (i-1)/2 Q 43 012345678910
44
Heap Representation I info 29 key 11 Q array len 9 n Store associated information directly in the array Should only be used only for succinct information john
45
Heap Representation II info key 11 Q array len 9 n Store pointers to associated information 29
46
4 4 info key pos x Heap Representation III 11 Q array len 9 n Allow decrease-key operations
47
Find the minimum 23 7 17 3365 19 24 2629 723172429261965334079 40 Return Q[0] Q 47
48
Delete the minimum 23 7 17 3365 19 24 2629 723172429261965334079 40 Q 48
49
Delete the minimum 2317 3365 19 24 2629 23172429261965334079 40 Q 49
50
Delete the minimum 23 79 17 3365 19 24 2629 79231724292619653340 Q 50
51
Delete the minimum 23 17 79 3365 19 24 2629 17237924292619653340 Q 51
52
Delete the minimum 23 17 19 3365 79 24 2629 17231924292679653340 Q 52
53
Delete the minimum 23 17 19 3365 79 24 2629 17231924292679653340 Q Q[0] ← Q[size(Q)-1] Heapify-down(Q,0) size(Q) ← size(Q)-1 53
54
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) 54
55
Inserting an item 23 17 19 3365 79 24 2629 17231924292679653340 Q Insert(15,Q) 55
56
Inserting an item 23 17 19 3365 79 24 2629 1723192429267965334015 40 Q Insert(15,Q) Q[size(Q)] ← 15 15 Heapify-up(Q,size(Q)) size(Q) ← size(Q) + 1 56
57
Heapify-up 23 17 19 3365 79 24 2615 1723192415267965334029 40 Q 29 57
58
15 17 19 3365 79 24 2623 1715192423267965334029 40 Q 29 58 Heapify-up
59
17 15 19 3365 79 24 2623 1517192423267965334029 40 Q 29 59 Heapify-up
60
Q Heapify-up(Q, i) while i > 0 and Q[i] < Q[parent(i)] do Q[i] ↔ Q[parent(i)] i ← parent(i) 17 15 19 3365 79 24 2623 4029 1517192423267965334029 60
61
Other operations Decrease-key(x,Q,Δ) Delete(x,Q) Can implement them easily using heapify 61
62
Heapsort (Williams, Floyd, 1964) Put the elements in an array Turn the array into a heap Do a delete-min and put the deleted element at the last position of the array Reverse the array, or use a max-heap 62
63
65 79 26 3323 29 24 1519 796526241915292333407 7 Q Turn an array into a heap 63 0 1 2 3 456 7 8 910
64
65 79 26 3323 29 24 1519 796526241915292333407 7 Q Heapify-down(Q,4) 64 4 Turn an array into a heap
65
65 79 26 3323 29 24 157 796526247152923334019 40 Q Heapify-down(Q,4) 65 Turn an array into a heap
66
65 79 26 3323 29 24 157 796526247152923334019 40 Q Heapify-down(Q,3) 66 3 Turn an array into a heap
67
65 79 26 3324 29 23 157 796526237152924334019 40 Q Heapify-down(Q,3) 67 Turn an array into a heap
68
65 79 26 3324 29 23 157 796526237152924334019 40 Q Heapify-down(Q,2) 68 2 Turn an array into a heap
69
65 79 15 3324 29 23 267 796515237262924334019 40 Q Heapify-down(Q,2) 69 Turn an array into a heap
70
65 79 15 3324 29 23 267 796515237262924334019 40 Q Heapify-down(Q,1) 70 1 Turn an array into a heap
71
7 79 15 3324 29 23 2665 797152365262924334019 40 Q Heapify-down(Q,1) 71 Turn an array into a heap
72
7 79 15 3324 29 23 2619 797152319262924334065 40 Q Heapify-down(Q,1) 72 Turn an array into a heap
73
7 79 15 3324 29 23 2619 797152319262924334065 40 Q Heapify-down(Q,0) 73 0 Turn an array into a heap
74
79 7 15 3324 29 23 2619 779152319262924334065 40 Q Heapify-down(Q,0) 74 Turn an array into a heap
75
19 7 15 3324 29 23 2679 719152379262924334065 40 Q Heapify-down(Q,0) 75 Turn an array into a heap
76
19 7 15 3324 29 23 2640 719152340262924337965 79 Q Heapify-down(Q,0) 76 Turn an array into a heap
77
23 7 15 3379 29 24 2619 6540 How much time does it take to build the heap this way ? Total time = 77 At most n/2 nodes heapified at height 1 At most n/4 nodes heapified at height 2 At most n/8 nodes heapified at height 3
78
23 7 15 3379 29 24 2619 6540 How much time does it take to build the heap this way ? Total time = 78 At most n/2 nodes heapified at height 1 At most n/4 nodes heapified at height 2 At most n/8 nodes heapified at height 3
79
Summary We can build the heap in linear time We still have to deletemin the elements one by one in order to sort that will take O(nlog(n)) 79
80
An example Given an array of length n, find the k smallest numbers. 80
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.