Priority Queue
description
Transcript of Priority Queue
![Page 1: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/1.jpg)
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 …
![Page 2: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/2.jpg)
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
![Page 3: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/3.jpg)
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• …
![Page 4: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/4.jpg)
• 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 … …
![Page 5: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/5.jpg)
An exampleHow can we compare objects?
This is a Vertex
![Page 6: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/6.jpg)
An exampleHow can we compare objects?
This is a Comparator for vertices
![Page 7: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/7.jpg)
An exampleHow can we compare objects?
Using the comparator to sort an array of Vertex
![Page 8: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/8.jpg)
An exampleHow can we compare objects?
Another example: a Car
![Page 9: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/9.jpg)
An exampleHow can we compare objects?
This is a CarComparator
![Page 10: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/10.jpg)
An exampleHow can we compare objects?
Using the CarComparator
![Page 11: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/11.jpg)
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
Method Time
size O(1)
isEmpty O(1)
insert O(1)
removeMin O(n)
min O(n)
Implementing a priority queue with an unsorted list
![Page 12: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/12.jpg)
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
Method Time
size O(1)
isEmpty O(1)
insert O(n)
removeMin O(1)
min O(1)
![Page 13: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/13.jpg)
An alternative the heap
![Page 14: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/14.jpg)
heap
• a heap H is a binary tree
![Page 15: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/15.jpg)
heap
• a heap H is a binary tree • H is a complete binary tree
![Page 16: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/16.jpg)
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
![Page 17: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/17.jpg)
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
![Page 18: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/18.jpg)
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
![Page 19: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/19.jpg)
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!
![Page 20: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/20.jpg)
heap
• a heap H is a binary tree • H is a complete binary tree• heap order property is maintained
![Page 21: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/21.jpg)
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
![Page 22: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/22.jpg)
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
![Page 23: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/23.jpg)
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))
![Page 24: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/24.jpg)
Example: adding to a heap
![Page 25: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/25.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11
20
![Page 26: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/26.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11
20
Insert 8
![Page 27: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/27.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11 8
20
Insert 8
![Page 28: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/28.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11 8
20
Insert 8 8 is greater than parent (7) ... done
![Page 29: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/29.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11 8
20
![Page 30: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/30.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11 8
20
Insert 2
![Page 31: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/31.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11 8
20
2
Insert 2
![Page 32: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/32.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11 8
20
2
Insert 2 2 is less than parent 20
![Page 33: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/33.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11 8
20
2
Insert 2 2 is less than parent 20 ... swap!
![Page 34: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/34.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11 8
2
20
Insert 2
![Page 35: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/35.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11 8
2
20
Insert 2
![Page 36: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/36.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11 8
2
20
Insert 2 2 is less than parent 6
![Page 37: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/37.jpg)
heapExample: adding to a heap
4
5 6
15
16 25
9
14 12
7
11 8
2
20
Insert 2 2 is less than parent 6 ... swap!
![Page 38: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/38.jpg)
heapExample: adding to a heap
4
5 2
15
16 25
9
14 12
7
11 8
6
20
Insert 2 2 is less than parent 6 ... swap!
![Page 39: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/39.jpg)
heapExample: adding to a heap
4
5 2
15
16 25
9
14 12
7
11 8
6
20
Insert 2
![Page 40: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/40.jpg)
heapExample: adding to a heap
4
5 2
15
16 25
9
14 12
7
11 8
6
20
Insert 2 2 is less than parent 4
![Page 41: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/41.jpg)
heapExample: adding to a heap
4
5 2
15
16 25
9
14 12
7
11 8
6
20
Insert 2 2 is less than parent 4 ... swap!
![Page 42: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/42.jpg)
heapExample: adding to a heap
2
5 4
15
16 25
9
14 12
7
11 8
6
20
Insert 2 2 is less than parent 4 ... swap!
![Page 43: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/43.jpg)
heapExample: adding to a heap
2
5 4
15
16 25
9
14 12
7
11 8
6
20
Insert 2
![Page 44: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/44.jpg)
heapExample: adding to a heap
2
5 4
15
16 25
9
14 12
7
11 8
6
20
Insert 2 Done!
![Page 45: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/45.jpg)
heapExample: adding to a heap
2
5 4
15
16 25
9
14 12
7
11 8
6
20
![Page 46: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/46.jpg)
Example: removal from a heap
NOTE: it is a heap in a different state
![Page 47: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/47.jpg)
heapExample: removal from a heap
4
5 6
15
16 25
9
14 12
7
11 13
20
![Page 48: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/48.jpg)
heapExample: removal from a heap
4
5 6
15
16 25
9
14 12
7
11 13
20
Save off top of heap
![Page 49: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/49.jpg)
heapExample: removal from a heap
4
5 6
15
16 25
9
14 12
7
11 13
20
Save off top of heap
4
![Page 50: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/50.jpg)
heapExample: removal from a heap
4
5 6
15
16 25
9
14 12
7
11 13
20
Copy last item in heap to top of heap
4
![Page 51: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/51.jpg)
heapExample: removal from a heap
4
5 6
15
16 25
9
14 12
7
11 13
20
Copy last item in heap to top of heap
4
![Page 52: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/52.jpg)
heapExample: removal from a heap
4
5 6
15
16 25
9
14 12
7
11 13
20
Copy last item in heap to top of heap
4
![Page 53: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/53.jpg)
heapExample: removal from a heap
13
5 6
15
16 25
9
14 12
7
11 13
20
Copy last item in heap to top of heap
4
![Page 54: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/54.jpg)
heapExample: removal from a heap
13
5 6
15
16 25
9
14 12
7
11 13
20
Delete last item in heap
4
![Page 55: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/55.jpg)
heapExample: removal from a heap
13
5 6
15
16 25
9
14 12
7
11
20
4
![Page 56: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/56.jpg)
heapExample: removal from a heap
13
5 6
15
16 25
9
14 12
7
11
20
Compare current node with its children
4
![Page 57: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/57.jpg)
heapExample: removal from a heap
13
5 6
15
16 25
9
14 12
7
11
20
Compare current node with its children
4
![Page 58: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/58.jpg)
heapExample: removal from a heap
13
5 6
15
16 25
9
14 12
7
11
20
If greater then swap with smallest child
4
![Page 59: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/59.jpg)
heapExample: removal from a heap
13
5 6
15
16 25
9
14 12
7
11
20
If greater then swap with smallest child
4
![Page 60: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/60.jpg)
heapExample: removal from a heap
5
13 6
15
16 25
9
14 12
7
11
20
If greater then swap with smallest child
4
![Page 61: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/61.jpg)
heapExample: removal from a heap
5
13 6
15
16 25
9
14 12
7
11
20
4
![Page 62: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/62.jpg)
heapExample: removal from a heap
5
13 6
15
16 25
9
14 12
7
11
20
If greater then swap with smallest child
4
Compare current node with its children
![Page 63: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/63.jpg)
heapExample: removal from a heap
5
13 6
15
16 25
9
14 12
7
11
20
If greater then swap with smallest child
4
![Page 64: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/64.jpg)
heapExample: removal from a heap
5
13 6
15
16 25
9
14 12
7
11
20
If greater then swap with smallest child
4
![Page 65: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/65.jpg)
heapExample: removal from a heap
5
9 6
15
16 25
13
14 12
7
11
20
If greater then swap with smallest child
4
![Page 66: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/66.jpg)
heapExample: removal from a heap
5
9 6
15
16 25
13
14 12
7
11
20
4
![Page 67: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/67.jpg)
heapExample: removal from a heap
5
9 6
15
16 25
13
14 12
7
11
20
4
Compare current node with its children
![Page 68: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/68.jpg)
heapExample: removal from a heap
5
9 6
15
16 25
13
14 12
7
11
20
4
If greater then swap with smallest child
![Page 69: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/69.jpg)
heapExample: removal from a heap
5
9 6
15
16 25
13
14 12
7
11
20
4
If greater then swap with smallest child
![Page 70: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/70.jpg)
heapExample: removal from a heap
5
9 6
15
16 25
13
14 12
7
11
20
4
If greater then swap with smallest child
![Page 71: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/71.jpg)
heapExample: removal from a heap
5
9 6
15
16 25
12
14 13
7
11
20
4
If greater then swap with smallest child
![Page 72: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/72.jpg)
heapExample: removal from a heap
5
9 6
15
16 25
12
14 13
7
11
20
4
![Page 73: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/73.jpg)
4
heapExample: removal from a heap
5
9 6
15
16 25
12
14 13
7
11
20
Return result
![Page 74: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/74.jpg)
heapExample: removal from a heap
5
9 6
15
16 25
12
14 13
7
11
20
Done
![Page 75: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/75.jpg)
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
![Page 76: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/76.jpg)
An implementation of a Heap data structure
![Page 77: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/77.jpg)
An implementation of a Heap data structure
![Page 78: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/78.jpg)
An implementation of a Heap data structureNumber the vertices as follows
![Page 79: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/79.jpg)
An implementation of a Heap data structure
1
2 3
4
8 9
5
10 11
6
12 13
7
14 15
Number the vertices as follows
![Page 80: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/80.jpg)
An implementation of a Heap data structure
1
2 3
4
8 9
5
10 11
6
12 13
7
14 15
Number the vertices as follows
Note: parent of node i is i/2 ....
![Page 81: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/81.jpg)
An implementation of a Heap data structure
1
2 3
4
8 9
5
10 11
6
12 13
7
14 15
Number the vertices as follows
Note: parent of node i is i/2 ....
![Page 82: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/82.jpg)
An implementation of a Heap data structure
1
2 3
4
8 9
5
10 11
6
12 13
7
14 15
Number the vertices as follows
Note: parent of node i is i/2 ....
![Page 83: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/83.jpg)
An implementation of a Heap data structure
1
2 3
4
8 9
5
10 11
6
12 13
7
14 15
Number the vertices as follows
Note: left child of i is i×2
![Page 84: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/84.jpg)
An implementation of a Heap data structure
1
2 3
4
8 9
5
10 11
6
12 13
7
14 15
Number the vertices as follows
Note: right child of i is (i×2) +1
![Page 85: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/85.jpg)
An implementation of a Heap data structure
1
2 3
4
8 9
5
10 11
6
12 13
7
14 15
Represent as a one dimensional array
![Page 86: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/86.jpg)
An implementation of a Heap data structure
1
2 3
4
8 9
5
10 11
6
12 13
7
14 15
Represent as a one dimensional array S
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15S
![Page 87: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/87.jpg)
An implementation of a Heap data structure
1
2 3
4
8 9
5
10 11
6
12 13
7
14 15
Represent as a one dimensional array S
**
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15S
To simplify implementation we do not use S[0]
![Page 88: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/88.jpg)
An implementation of a Heap data structure
**
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Require two integer variables, last and capacity where last is initially 0In our example capacity is 15
Represent as a one dimensional array S
S
![Page 89: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/89.jpg)
An implementation of a Heap data structure
**
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Represent as a one dimensional array S
S
last: 0capacity: 15
![Page 90: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/90.jpg)
An implementation of a Heap data structure
Consider the following heap H
![Page 91: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/91.jpg)
An implementation of a Heap data structure
4
9 8
17
19 69
26
32 93
50
55
16
Consider the following heap H
S
last: 12capacity: 15
** 4 9 8 17 26 50 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
![Page 92: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/92.jpg)
An implementation of a Heap data structure
4
9 8
17
19 69
26
32 93
50
55
16
S
last: 12capacity: 15
** 4 9 8 17 26 50 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
H.add(6)
![Page 93: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/93.jpg)
An implementation of a Heap data structure
4
9 8
17
19 69
26
32 93
50
55 6
16
S
last: 12capacity: 15
** 4 9 8 17 26 50 16 19 69 32 93 55 6
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
S[last+1] = 6
![Page 94: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/94.jpg)
An implementation of a Heap data structure
4
9 8
17
19 69
26
32 93
50
55 6
16
S
last: 13capacity: 15
** 4 9 8 17 26 50 16 19 69 32 93 55 6
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
last++
![Page 95: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/95.jpg)
An implementation of a Heap data structure
4
9 8
17
19 69
26
32 93
50
55 6
16
S
last: 13capacity: 15
** 4 9 8 17 26 50 16 19 69 32 93 55 6
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
upheapBubble(13)
![Page 96: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/96.jpg)
An implementation of a Heap data structure
4
9 8
17
19 69
26
32 93
50
55 6
16
S
last: 13capacity: 15
** 4 9 8 17 26 50 16 19 69 32 93 55 6
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
upheapBubble(13)
![Page 97: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/97.jpg)
An implementation of a Heap data structure
4
9 8
17
19 69
26
32 93
50
55 6
16
S
last: 13capacity: 15
** 4 9 8 17 26 50 16 19 69 32 93 55 6
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
upheapBubble(13)S[6] > S[13] ?
![Page 98: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/98.jpg)
An implementation of a Heap data structure
4
9 8
17
19 69
26
32 93
6
55 50
16
S
last: 13capacity: 15
** 4 9 8 17 26 6 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
upheapBubble(13)swap S[6] S[13]
![Page 99: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/99.jpg)
An implementation of a Heap data structure
4
9 8
17
19 69
26
32 93
6
55 50
16
S
last: 13capacity: 15
** 4 9 8 17 26 6 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
![Page 100: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/100.jpg)
An implementation of a Heap data structure
4
9 8
17
19 69
26
32 93
6
55 50
16
S
last: 13capacity: 15
** 4 9 8 17 26 6 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
upheapBubble(6)
![Page 101: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/101.jpg)
An implementation of a Heap data structure
4
9 8
17
19 69
26
32 93
6
55 50
16
S
last: 13capacity: 15
** 4 9 8 17 26 6 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
upheapBubble(6)
![Page 102: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/102.jpg)
An implementation of a Heap data structure
4
9 8
17
19 69
26
32 93
6
55 50
16
S
last: 13capacity: 15
** 4 9 8 17 26 6 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
upheapBubble(6)S[3] > S[6] ?
![Page 103: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/103.jpg)
An implementation of a Heap data structure
4
9 6
17
19 69
26
32 93
8
55 50
16
S
last: 13capacity: 15
** 4 9 6 17 26 8 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
upheapBubble(6)swap S[3] S[6]
![Page 104: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/104.jpg)
An implementation of a Heap data structure
4
9 6
17
19 69
26
32 93
8
55 50
16
S
last: 13capacity: 15
** 4 9 6 17 26 8 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
![Page 105: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/105.jpg)
An implementation of a Heap data structure
4
9 6
17
19 69
26
32 93
8
55 50
16
S
last: 13capacity: 15
** 4 9 6 17 26 8 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
upheapBubble(3)
![Page 106: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/106.jpg)
An implementation of a Heap data structure
4
9 6
17
19 69
26
32 93
8
55 50
16
S
last: 13capacity: 15
** 4 9 6 17 26 8 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
upheapBubble(3)
![Page 107: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/107.jpg)
An implementation of a Heap data structure
4
9 6
17
19 69
26
32 93
8
55 50
16
S
last: 13capacity: 15
** 4 9 6 17 26 8 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
upheapBubble(3)S[1] > S[3] ?
![Page 108: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/108.jpg)
An implementation of a Heap data structure
4
9 6
17
19 69
26
32 93
8
55 50
16
S
last: 13capacity: 15
** 4 9 6 17 26 8 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Done!
![Page 109: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/109.jpg)
Removal from the heap H
![Page 110: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/110.jpg)
An implementation of a Heap data structure
4
9 6
17
19 69
26
32 93
8
55 50
16
S
last: 13capacity: 15
** 4 9 6 17 26 8 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
H.remove()
![Page 111: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/111.jpg)
An implementation of a Heap data structure
4
9 6
17
19 69
26
32 93
8
55 50
16
S
last: 13capacity: 15
** 4 9 6 17 26 8 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Save S[1]
4
![Page 112: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/112.jpg)
An implementation of a Heap data structure
4
9 6
17
19 69
26
32 93
8
55 50
16
S
last: 13capacity: 15
** 4 9 6 17 26 8 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
S[1] = S[last]
4
![Page 113: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/113.jpg)
An implementation of a Heap data structure
50
9 6
17
19 69
26
32 93
8
55 50
16
S
last: 13capacity: 15
** 50 9 6 17 26 8 16 19 69 32 93 55 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
S[1] = S[last]
4
![Page 114: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/114.jpg)
An implementation of a Heap data structure
50
9 6
17
19 69
26
32 93
8
55
16
S
last: 12capacity: 15
** 50 9 6 17 26 8 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
last--
4
![Page 115: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/115.jpg)
An implementation of a Heap data structure
50
9 6
17
19 69
26
32 93
8
55
16
S
last: 12capacity: 15
** 50 9 6 17 26 8 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(1)
![Page 116: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/116.jpg)
An implementation of a Heap data structure
50
9 6
17
19 69
26
32 93
8
55
16
S
last: 12capacity: 15
** 50 9 6 17 26 8 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(1)findMin(S[2],S[3])
![Page 117: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/117.jpg)
An implementation of a Heap data structure
50
9 6
17
19 69
26
32 93
8
55
16
S
last: 12capacity: 15
** 50 9 6 17 26 8 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(1)findMin(S[2],S[3])
![Page 118: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/118.jpg)
An implementation of a Heap data structure
50
9 6
17
19 69
26
32 93
8
55
16
S
last: 12capacity: 15
** 50 9 6 17 26 8 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(1)swap(S[1],S[3])
![Page 119: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/119.jpg)
An implementation of a Heap data structure
6
9 50
17
19 69
26
32 93
8
55
16
S
last: 12capacity: 15
** 6 9 50 17 26 8 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(1)swap(S[1],S[3])
![Page 120: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/120.jpg)
An implementation of a Heap data structure
6
9 50
17
19 69
26
32 93
8
55
16
S
last: 12capacity: 15
** 6 9 50 17 26 8 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(1)
![Page 121: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/121.jpg)
An implementation of a Heap data structure
6
9 50
17
19 69
26
32 93
8
55
16
S
last: 12capacity: 15
** 6 9 50 17 26 8 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(3)findMin(S[6],S[7])
![Page 122: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/122.jpg)
An implementation of a Heap data structure
6
9 50
17
19 69
26
32 93
8
55
16
S
last: 12capacity: 15
** 6 9 50 17 26 8 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(3)findMin(S[6],S[7])
![Page 123: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/123.jpg)
An implementation of a Heap data structure
6
9 50
17
19 69
26
32 93
8
55
16
S
last: 12capacity: 15
** 6 9 50 17 26 8 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(3)swap(S[3],S[6])
![Page 124: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/124.jpg)
An implementation of a Heap data structure
6
9 8
17
19 69
26
32 93
50
55
16
S
last: 12capacity: 15
** 6 9 8 17 26 50 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(3)swap(S[3],S[6])
![Page 125: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/125.jpg)
An implementation of a Heap data structure
6
9 8
17
19 69
26
32 93
50
55
16
S
last: 12capacity: 15
** 6 9 8 17 26 50 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(3)
![Page 126: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/126.jpg)
An implementation of a Heap data structure
6
9 8
17
19 69
26
32 93
50
55
16
S
last: 12capacity: 15
** 6 9 8 17 26 50 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(3)findMin(S[12])
![Page 127: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/127.jpg)
An implementation of a Heap data structure
6
9 8
17
19 69
26
32 93
50
55
16
S
last: 12capacity: 15
** 6 9 8 17 26 50 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(3)No swap
![Page 128: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/128.jpg)
An implementation of a Heap data structure
6
9 8
17
19 69
26
32 93
50
55
16
S
last: 12capacity: 15
** 6 9 8 17 26 50 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4
downHeapBubble(3)No swap
![Page 129: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/129.jpg)
An implementation of a Heap data structure
6
9 8
17
19 69
26
32 93
50
55
16
S
last: 12capacity: 15
** 6 9 8 17 26 50 16 19 69 32 93 55
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Return result
4
![Page 130: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/130.jpg)
Method Time
size O(1)
isEmpty O(1)
insert O(log(n))
removeMin O(log(n))
min O(1)
heap
![Page 131: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/131.jpg)
Have a look at priority queue as given in Java distribution
![Page 132: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/132.jpg)
![Page 133: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/133.jpg)
… based on a priority heap
![Page 134: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/134.jpg)
Different method names … add rather than insert
![Page 135: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/135.jpg)
Exercise 4 (assessed)• Implement the Heap• Use it for sorting
Your mission, should you choose to accept it …
![Page 136: Priority Queue](https://reader035.fdocuments.net/reader035/viewer/2022062323/56816479550346895dd65c66/html5/thumbnails/136.jpg)
fin