![Page 1: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/1.jpg)
Chapter 6: Chapter 6: Priority QueuesPriority Queues
Priority QueuesBinary Heaps
Mark Allen Weiss: Data Structures and Algorithm Analysis in Java
Lydia Sinapova, Simpson College
![Page 2: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/2.jpg)
2
Priority Queues
The modelImplementationsBinary heapsBasic operationsAnimation
![Page 3: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/3.jpg)
3
The ModelA priority queue is a queue where:
Requests are inserted in the order of arrival
The request with highest priority is processed first (deleted from the queue)
The priority is indicated by a number, the lower the number - the higher the priority.
![Page 4: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/4.jpg)
4
Implementations
Linked list:
- Insert at the beginning - O(1)
- Find the minimum - O(N)
Binary search tree (BST):
- Insert O(logN)
- Find minimum - O(logN)
![Page 5: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/5.jpg)
5
Implementations
Binary heap
Better than BST because it does not support links
- Insert O(logN)
- Find minimum O(logN)
Deleting the minimal element takes a constant time, however after that the heap structure has to be adjusted, and this requires O(logN) time.
![Page 6: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/6.jpg)
6
Binary Heap
Heap-Structure Property:
Complete Binary Tree - Each node has two children, except for the last two levels.
The nodes at the last level do not have children. New nodes are inserted at the last level from left to right.
Heap-Order Property:
Each node has a higher priority than its children
![Page 7: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/7.jpg)
7
6
1012
15 17 18 23
20 19 34
Binary Heap
Next node to be inserted - right child of the yellow node
![Page 8: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/8.jpg)
8
Binary heap implementation with an array
Root - A(1)
Left Child of A(i) - A(2i)
Right child of A(i) - A(2i+1)
Parent of A(I) - A([i/2]).
The smallest element is always at the root, the access time to the element with highest priority is constant O(1).
![Page 9: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/9.jpg)
9
Example
6 10 12 15 17 18 23 20 19 34
Consider 17:
position in the array - 5.
parent 10 is at position [5/2] = 2
left child is at position 5*2 = 10 (this is 34)
right child - position 2*5 + 1 = 11 (empty.)
![Page 10: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/10.jpg)
10
Problems
Problem 1:
2 8 10 16 17 18 23 20 21 30
Reconstruct the binary heap
![Page 11: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/11.jpg)
11
Problems
Problem 2: Give the array representation for
3
1016
13 12 18 23
15 19 30 14
![Page 12: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/12.jpg)
12
Basic Operations
Insert a node – Percolate Up Delete a node – Percolate Down Decrease key, Increase key, Remove key Build the heap
![Page 13: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/13.jpg)
13
Percolate Up – Insert a Node
A hole is created at the bottom of the tree, in the next available position.
6
1012
15 17 18 23
20 19 34
![Page 14: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/14.jpg)
14
Percolate Up
Insert 206
1012
15 17 18 23
21 19 34 20
![Page 15: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/15.jpg)
15
Percolate UpInsert 16
6
1012
15 18 23
21 19 34 17
![Page 16: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/16.jpg)
16
Percolate Up6
1012
15 18 23
21 19 34 17
16
Complexity of insertion: O(logN)
![Page 17: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/17.jpg)
17
Percolate Down – Delete a Node
1016
13 12 18 23
15 19 30 34
![Page 18: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/18.jpg)
18
Percolate Down – the wrong way
10
16
13 12 18 23
15 19 30 34
![Page 19: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/19.jpg)
19
Percolate Down – the wrong way
16
13 18 23
15 19 34
10
12
30
The empty hole violates the heap-structure property
![Page 20: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/20.jpg)
20
Percolate Down
Last element - 20. The hole at the root.
1016
12 13 18 23
15 19 30 20
We try to insert 20 in the hole by percolating the hole down
![Page 21: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/21.jpg)
21
Percolate Down
16
12 13 18 23
15 19 3020
10
![Page 22: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/22.jpg)
22
Percolate Down
16
13 18 23
15 19 3020
10
12
![Page 23: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/23.jpg)
23
Percolate Down
16
13 18 23
20 19 30
10
12
15
Complexity of deletion: O(logN)
![Page 24: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/24.jpg)
24
Other Heap Operations
1. DecreaseKey(p,d)
increase the priority of element p in the heap with a positive value d. percolate up.
2. IncreaseKey(p,d)
decrease the priority of element p in the heap with a positive value d. percolate down.
![Page 25: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/25.jpg)
25
Other Heap Operations
3. Remove(p) a. Assigning the highest priority to p - percolate p up to the root.
b. Deleting the element in the root and filling the hole by percolating down and trying to insert the last element in the queue.
4. BuildHeap input N elements
place them into an empty heap through successive inserts. The worst case running time is O(NlogN).
![Page 26: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/26.jpg)
26
Build Heap - O(N)
Given an array of elements to be inserted in the heap,
treat the array as a heap with order property violated,
and then do operations to fix the order property.
![Page 27: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/27.jpg)
27
Example:150 80 40 30 10 70 110 100 20 90 60 50 120 140 130
150
80 40
30 1070 110
100 20 90 60 50 120 140 130
![Page 28: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/28.jpg)
28
Example (cont)
150
80 40
20 1050 110
100 30 90 60 70 120 140 130
After processing height 1
![Page 29: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/29.jpg)
29
Example (cont)
150
10 40
20 6050 11
0
100
30 90 80 70 120
140
130
After processing height 2
![Page 30: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/30.jpg)
30
Example (cont)
10
20 40
30 6050 110
100 150 90 80 70 120 140 130
After processing height 3
![Page 31: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/31.jpg)
31
Theorem
For a perfect binary tree of height h containing N = 2 h+1 - 1 nodes,
the sum S of the heights of the nodes is S = 2 h+1 - 1 - (h + 1) = O(N)
![Page 32: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/32.jpg)
32
Proof
The tree has 1 node at height h, 2 nodes at height h-1, 4 nodes at height h -2, etc
1 (20) h2 (21) h - 14 (22) h - 28 (23) h - 3…….2 h 0
S = 2i (h - i), i = 0 to h
![Page 33: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/33.jpg)
33
S = h + 2(h - 1) + 4(h - 2) + 8 (h -
3) + …. 2(h-2).2 + 2(h-1).1 (1)
2S = 2h + 4(h - 1) + 8(h - 2) + 16 (h - 3)
+ …. 2(h-1).2 + 2(h).1 (2)
Subtract (1) from (2):
S = h -2h + 2 + 4 + 8 + …. 2 h = - h - 1 + (2 h+1 - 1) = (2 h+1 - 1) - (h + 1)
![Page 34: Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College](https://reader034.vdocument.in/reader034/viewer/2022042516/56649d4e5503460f94a2d50b/html5/thumbnails/34.jpg)
34
Proof (cont.)
Note: 1 + 2 + 4 + 8 + …. 2 h = (2 h+1 - 1)
Hence S = (2 h+1 - 1) - (h + 1)
Hence the complexity of building a heap with N nodes is linear O(N)