Download presentation
Presentation is loading. Please wait.
Published byJeffery Morrison Modified over 9 years ago
1
Priority Queue What is that? Implementation with linked list with O(n) behaviour The Heap (O(log(n)) An implementation using an array Your mission …
2
Q.insert(e) e = Q.removeMin() Q.size() Q.isEmpty() Q.min() Priority queue Store a collection of prioritized elements Elements are comparable Allow insertion of an element Can only remove the element with highest priority Comes first in order We present 3 implementations
3
Priority queue Example applications of a priority queue Dispatching processes in a computer Hospital waiting lists Standby passengers for a flight Queuing at call centres …
4
Store a collection of prioritized elements Elements are comparable Allow insertion of an element Can only remove the element with highest priority Comes first in order Priority queueHow can we compare objects? Two examples on comparing things … …
5
An exampleHow can we compare objects? This is a Vertex
6
An exampleHow can we compare objects? This is a Comparator for vertices
7
An exampleHow can we compare objects? Using the comparator to sort an array of Vertex
8
An exampleHow can we compare objects? Another example: a Car
9
An exampleHow can we compare objects? This is a CarComparator
10
An exampleHow can we compare objects? Using the CarComparator
11
unsorted list We might use a linked list To insert we add to the front of the list To find the minimum we must iterate over entire the list To remove the minimum we must find the minimum and remove it Maintain a counter of number of elements in the list MethodTime sizeO(1) isEmptyO(1) insertO(1) removeMinO(n) minO(n) Implementing a priority queue with an unsorted list
12
sorted list We might use a linked list The list is maintained in non-decreasing order To insert we scan to find position and splice in (see below) To find the minimum we deliver the first element in the list To remove the minimum we return and remove the first element Implementing a priority queue with an sorted list MethodTime sizeO(1) isEmptyO(1) insertO(n) removeMinO(1) minO(1)
13
An alternative the heap
14
heap a heap H is a binary tree
15
heap a heap H is a binary tree H is a complete binary tree
16
heap a heap H is a binary tree H is a complete binary tree Fill up level d before moving to level d+1 each level but the last must be full in last level fill from left to right
17
heap a heap H is a binary tree H is a complete binary tree Fill up level d before moving to level d+1 each level but the last must be full in last level fill from left to right
18
heap a heap H is a binary tree H is a complete binary tree Fill up level d before moving to level d+1 each level but the last must be full in last level fill from left to right
19
heap a heap H is a binary tree H is a complete binary tree Fill up level d before moving to level d+1 each level but the last must be full in last level fill from left to right Not a heap!
20
heap a heap H is a binary tree H is a complete binary tree heap order property is maintained
21
heap a heap H is a binary tree H is a complete binary tree heap order property is maintained Given a node v (not the root) the parent of v is less than or equal to v
22
heap a heap H is a binary tree H is a complete binary tree heap order property is maintained 4 5 915 6 207 Given a node v (not the root) the parent of v is less than or equal to v
23
heap a heap H is a binary tree H is a complete binary tree heap order property is maintained 4 5 915 6 207 Given a node v (not the root) the parent of v is less than or equal to v A heap H with n nodes has height O(log(n))
24
Example: adding to a heap
25
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 11 20
26
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 11 20 Insert 8
27
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 11 8 20 Insert 8
28
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 11 8 20 Insert 8 8 is greater than parent (7)... done
29
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 118 20
30
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 118 20 Insert 2
31
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 118 20 2 Insert 2
32
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 118 20 2 Insert 2 2 is less than parent 20
33
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 118 20 2 Insert 2 2 is less than parent 20... swap!
34
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 118 2 20 Insert 2
35
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 118 2 20 Insert 2
36
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 118 2 20 Insert 2 2 is less than parent 6
37
heap Example: adding to a heap 4 5 6 15 1625 9 1412 7 118 2 20 Insert 2 2 is less than parent 6... swap!
38
heap Example: adding to a heap 4 5 2 15 1625 9 1412 7 118 6 20 Insert 2 2 is less than parent 6... swap!
39
heap Example: adding to a heap 4 5 2 15 1625 9 1412 7 118 6 20 Insert 2
40
heap Example: adding to a heap 4 5 2 15 1625 9 1412 7 118 6 20 Insert 2 2 is less than parent 4
41
heap Example: adding to a heap 4 5 2 15 1625 9 1412 7 118 6 20 Insert 2 2 is less than parent 4... swap!
42
heap Example: adding to a heap 2 5 4 15 1625 9 1412 7 118 6 20 Insert 2 2 is less than parent 4... swap!
43
heap Example: adding to a heap 2 5 4 15 1625 9 1412 7 118 6 20 Insert 2
44
heap Example: adding to a heap 2 5 4 15 1625 9 1412 7 118 6 20 Insert 2 Done!
45
heap Example: adding to a heap 2 5 4 15 1625 9 1412 7 118 6 20
46
Example: removal from a heap NOTE: it is a heap in a different state
47
heap Example: removal from a heap 4 5 6 15 1625 9 1412 7 1113 20
48
heap Example: removal from a heap 4 5 6 15 1625 9 1412 7 1113 20 Save off top of heap
49
heap Example: removal from a heap 4 5 6 15 1625 9 1412 7 1113 20 Save off top of heap 4
50
heap Example: removal from a heap 4 5 6 15 1625 9 1412 7 1113 20 Copy last item in heap to top of heap 4
51
heap Example: removal from a heap 4 5 6 15 1625 9 1412 7 11 13 20 Copy last item in heap to top of heap 4
52
heap Example: removal from a heap 4 5 6 15 1625 9 1412 7 11 13 20 Copy last item in heap to top of heap 4
53
heap Example: removal from a heap 13 5 6 15 1625 9 1412 7 11 13 20 Copy last item in heap to top of heap 4
54
heap Example: removal from a heap 13 5 6 15 1625 9 1412 7 11 13 20 Delete last item in heap 4
55
heap Example: removal from a heap 13 5 6 15 1625 9 1412 7 11 20 4
56
heap Example: removal from a heap 13 5 6 15 1625 9 1412 7 11 20 Compare current node with its children 4
57
heap Example: removal from a heap 13 5 6 15 1625 9 1412 7 11 20 Compare current node with its children 4
58
heap Example: removal from a heap 13 5 6 15 1625 9 1412 7 11 20 If greater then swap with smallest child 4
59
heap Example: removal from a heap 13 5 6 15 1625 9 1412 7 11 20 If greater then swap with smallest child 4
60
heap Example: removal from a heap 5 13 6 15 1625 9 1412 7 11 20 If greater then swap with smallest child 4
61
heap Example: removal from a heap 5 13 6 15 1625 9 1412 7 11 20 4
62
heap Example: removal from a heap 5 13 6 15 1625 9 1412 7 11 20 If greater then swap with smallest child 4 Compare current node with its children
63
heap Example: removal from a heap 5 13 6 15 1625 9 1412 7 11 20 If greater then swap with smallest child 4
64
heap Example: removal from a heap 5 13 6 15 1625 9 1412 7 11 20 If greater then swap with smallest child 4
65
heap Example: removal from a heap 5 9 6 15 1625 13 1412 7 11 20 If greater then swap with smallest child 4
66
heap Example: removal from a heap 5 9 6 15 1625 13 1412 7 11 20 4
67
heap Example: removal from a heap 5 9 6 15 1625 13 1412 7 11 20 4 Compare current node with its children
68
heap Example: removal from a heap 5 9 6 15 1625 13 1412 7 11 20 4 If greater then swap with smallest child
69
heap Example: removal from a heap 5 9 6 15 1625 13 14 12 7 11 20 4 If greater then swap with smallest child
70
heap Example: removal from a heap 5 9 6 15 1625 13 14 12 7 11 20 4 If greater then swap with smallest child
71
heap Example: removal from a heap 5 9 6 15 1625 12 14 13 7 11 20 4 If greater then swap with smallest child
72
heap Example: removal from a heap 5 9 6 15 1625 12 1413 7 11 20 4
73
4 heap Example: removal from a heap 5 9 6 15 1625 12 1413 7 11 20 Return result
74
heap Example: removal from a heap 5 9 6 15 1625 12 1413 7 11 20 Done
75
upheap bubbling: when we add to the heap downheap bubbling: when we remove from the heap What we just saw Add and remove are O(log(n)) processes
76
An implementation of a Heap data structure
78
Number the vertices as follows
79
An implementation of a Heap data structure 1 23 4 89 5 1011 6 1213 7 1415 Number the vertices as follows
80
An implementation of a Heap data structure 1 23 4 89 5 1011 6 1213 7 1415 Number the vertices as follows Note: parent of node i is i/2....
81
An implementation of a Heap data structure 1 2 3 4 89 5 1011 6 1213 7 1415 Number the vertices as follows Note: parent of node i is i/2....
82
An implementation of a Heap data structure 1 23 4 89 5 10 11 6 1213 7 1415 Number the vertices as follows Note: parent of node i is i/2....
83
An implementation of a Heap data structure 1 2 3 4 89 5 1011 6 1213 7 1415 Number the vertices as follows Note: left child of i is i×2
84
An implementation of a Heap data structure 1 2 3 4 89 5 1011 6 1213 7 1415 Number the vertices as follows Note: right child of i is (i×2) +1
85
An implementation of a Heap data structure 1 23 4 89 5 1011 6 1213 7 1415 Represent as a one dimensional array
86
An implementation of a Heap data structure 1 23 4 89 5 1011 6 1213 7 1415 Represent as a one dimensional array S 0123456789101112131415 S
87
An implementation of a Heap data structure 1 23 4 89 5 1011 6 1213 7 1415 Represent as a one dimensional array S ** 0123456789101112131415 S To simplify implementation we do not use S[0]
88
An implementation of a Heap data structure ** 0123456789101112131415 Require two integer variables, last and capacity where last is initially 0 In our example capacity is 15 Represent as a one dimensional array S S
89
An implementation of a Heap data structure ** 0123456789101112131415 Represent as a one dimensional array S S last: 0 capacity: 15
90
An implementation of a Heap data structure Consider the following heap H
91
An implementation of a Heap data structure 4 98 17 1969 26 3293 50 55 16 Consider the following heap H S last: 12 capacity: 15 **498172650161969329355 0123456789101112131415
92
An implementation of a Heap data structure 4 98 17 1969 26 3293 50 55 16 S last: 12 capacity: 15 **498172650161969329355 0123456789101112131415 H.add(6)
93
An implementation of a Heap data structure 4 98 17 1969 26 3293 50 55 6 16 S last: 12 capacity: 15 **4981726501619693293556 0123456789101112131415 S[last+1] = 6
94
An implementation of a Heap data structure 4 98 17 1969 26 3293 50 55 6 16 S last: 13 capacity: 15 **4981726501619693293556 0123456789101112131415 last++
95
An implementation of a Heap data structure 4 98 17 1969 26 3293 50 55 6 16 S last: 13 capacity: 15 **4981726501619693293556 0123456789101112131415 upheapBubble(13)
96
An implementation of a Heap data structure 4 98 17 1969 26 3293 50 55 6 16 S last: 13 capacity: 15 **4981726501619693293556 0123456789101112131415 upheapBubble(13)
97
An implementation of a Heap data structure 4 98 17 1969 26 3293 50 55 6 16 S last: 13 capacity: 15 **4981726501619693293556 0123456789101112131415 upheapBubble(13) S[6] > S[13] ?
98
An implementation of a Heap data structure 4 98 17 1969 26 3293 6 55 50 16 S last: 13 capacity: 15 **4981726616196932935550 0123456789101112131415 upheapBubble(13) swap S[6] S[13]
99
An implementation of a Heap data structure 4 98 17 1969 26 3293 6 55 50 16 S last: 13 capacity: 15 **4981726616196932935550 0123456789101112131415
100
An implementation of a Heap data structure 4 9 8 17 1969 26 3293 6 55 50 16 S last: 13 capacity: 15 **4981726616196932935550 0123456789101112131415 upheapBubble(6)
101
An implementation of a Heap data structure 4 9 8 17 1969 26 3293 6 55 50 16 S last: 13 capacity: 15 **4981726616196932935550 0123456789101112131415 upheapBubble(6)
102
An implementation of a Heap data structure 4 9 8 17 1969 26 3293 6 55 50 16 S last: 13 capacity: 15 **4981726616196932935550 0123456789101112131415 upheapBubble(6) S[3] > S[6] ?
103
An implementation of a Heap data structure 4 9 6 17 1969 26 3293 8 55 50 16 S last: 13 capacity: 15 **4961726816196932935550 0123456789101112131415 upheapBubble(6) swap S[3] S[6]
104
An implementation of a Heap data structure 4 9 6 17 1969 26 3293 8 55 50 16 S last: 13 capacity: 15 **4961726816196932935550 0123456789101112131415
105
An implementation of a Heap data structure 4 9 6 17 1969 26 3293 8 55 50 16 S last: 13 capacity: 15 **4961726816196932935550 0123456789101112131415 upheapBubble(3)
106
An implementation of a Heap data structure 4 9 6 17 1969 26 3293 8 55 50 16 S last: 13 capacity: 15 **4961726816196932935550 0123456789101112131415 upheapBubble(3)
107
An implementation of a Heap data structure 4 9 6 17 1969 26 3293 8 55 50 16 S last: 13 capacity: 15 **4961726816196932935550 0123456789101112131415 upheapBubble(3) S[1] > S[3] ?
108
An implementation of a Heap data structure 4 9 6 17 1969 26 3293 8 55 50 16 S last: 13 capacity: 15 **4961726816196932935550 0123456789101112131415 Done!
109
Removal from the heap H
110
An implementation of a Heap data structure 4 9 6 17 1969 26 3293 8 55 50 16 S last: 13 capacity: 15 **4961726816196932935550 0123456789101112131415 H.remove()
111
An implementation of a Heap data structure 4 9 6 17 1969 26 3293 8 55 50 16 S last: 13 capacity: 15 **4961726816196932935550 0123456789101112131415 Save S[1] 4
112
An implementation of a Heap data structure 4 9 6 17 1969 26 3293 8 55 50 16 S last: 13 capacity: 15 **4961726816196932935550 0123456789101112131415 S[1] = S[last] 4
113
An implementation of a Heap data structure 50 9 6 17 1969 26 3293 8 55 50 16 S last: 13 capacity: 15 **50961726816196932935550 0123456789101112131415 S[1] = S[last] 4
114
An implementation of a Heap data structure 50 9 6 17 1969 26 3293 8 55 16 S last: 12 capacity: 15 **509617268161969329355 0123456789101112131415 last-- 4
115
An implementation of a Heap data structure 50 9 6 17 1969 26 3293 8 55 16 S last: 12 capacity: 15 **509617268161969329355 0123456789101112131415 4 downHeapBubble(1)
116
An implementation of a Heap data structure 50 96 17 1969 26 3293 8 55 16 S last: 12 capacity: 15 **509617268161969329355 0123456789101112131415 4 downHeapBubble(1) findMin(S[2],S[3])
117
An implementation of a Heap data structure 50 96 17 1969 26 3293 8 55 16 S last: 12 capacity: 15 **509617268161969329355 0123456789101112131415 4 downHeapBubble(1) findMin(S[2],S[3])
118
An implementation of a Heap data structure 50 9 6 17 1969 26 3293 8 55 16 S last: 12 capacity: 15 **509617268161969329355 0123456789101112131415 4 downHeapBubble(1) swap(S[1],S[3])
119
An implementation of a Heap data structure 6 9 50 17 1969 26 3293 8 55 16 S last: 12 capacity: 15 **695017268161969329355 0123456789101112131415 4 downHeapBubble(1) swap(S[1],S[3])
120
An implementation of a Heap data structure 6 9 50 17 1969 26 3293 8 55 16 S last: 12 capacity: 15 **695017268161969329355 0123456789101112131415 4 downHeapBubble(1)
121
An implementation of a Heap data structure 6 9 50 17 1969 26 3293 8 55 16 S last: 12 capacity: 15 **695017268161969329355 0123456789101112131415 4 downHeapBubble(3) findMin(S[6],S[7])
122
An implementation of a Heap data structure 6 9 50 17 1969 26 3293 8 55 16 S last: 12 capacity: 15 **695017268161969329355 0123456789101112131415 4 downHeapBubble(3) findMin(S[6],S[7])
123
An implementation of a Heap data structure 6 9 50 17 1969 26 3293 8 55 16 S last: 12 capacity: 15 **695017268161969329355 0123456789101112131415 4 downHeapBubble(3) swap(S[3],S[6])
124
An implementation of a Heap data structure 6 9 8 17 1969 26 3293 50 55 16 S last: 12 capacity: 15 **698172650161969329355 0123456789101112131415 4 downHeapBubble(3) swap(S[3],S[6])
125
An implementation of a Heap data structure 6 9 8 17 1969 26 3293 50 55 16 S last: 12 capacity: 15 **698172650161969329355 0123456789101112131415 4 downHeapBubble(3)
126
An implementation of a Heap data structure 6 9 8 17 1969 26 3293 50 55 16 S last: 12 capacity: 15 **698172650161969329355 0123456789101112131415 4 downHeapBubble(3) findMin(S[12])
127
An implementation of a Heap data structure 6 9 8 17 1969 26 3293 50 55 16 S last: 12 capacity: 15 **698172650161969329355 0123456789101112131415 4 downHeapBubble(3) No swap
128
An implementation of a Heap data structure 6 9 8 17 1969 26 3293 50 55 16 S last: 12 capacity: 15 **698172650161969329355 0123456789101112131415 4 downHeapBubble(3) No swap
129
An implementation of a Heap data structure 6 9 8 17 1969 26 3293 50 55 16 S last: 12 capacity: 15 **698172650161969329355 0123456789101112131415 Return result 4
130
MethodTime sizeO(1) isEmptyO(1) insertO(log(n)) removeMinO(log(n)) minO(1) heap
131
Have a look at priority queue as given in Java distribution
133
… based on a priority heap
134
Different method names … add rather than insert
135
Exercise 4 (assessed) Implement the Heap Use it for sorting Your mission, should you choose to accept it …
136
fin
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.