priority queue. priority queues queue (fifo). priority queue. deletion from a priority queue is...
TRANSCRIPT
Priority Queue
Priority Queues
• Queue (FIFO).• Priority queue.
• Deletion from a priority queue is determined by the element priority.
• Two kinds of priority queues:• Min priority queue. (delete min element first)• Max priority queue. (delete max element first)
Max/Min Priority Queue
68
2
41 Max/Min
Priority Queue
empty Size Push Top pop
Min Priority Queue
• Collection of elements.• Each element has a priority or key.• Supports following operations:
empty size insert an element into the priority queue (push) get element with min priority (top) remove element with min priority (pop)
five elements whose keys are 6, 8, 2, 4, 1
Max Priority Queue
• Collection of elements.• Each element has a priority or key.• Supports following operations:
empty size insert an element into the priority queue (push) get element with max priority (top) remove element with max priority (pop)
five elements whose keys are 6, 8, 2, 4, 1
Implementation of Priority Queue
• Array, linked list, heap and complete binary tree.
• Operations:• empty • size• top• insert (push)• remove (pop)
C++ STL priority_queue(max priority queue)
Applications
Sorting• use element key as priority• insert elements to be sorted into a priority queue• remove/pop elements in priority order
if a min priority queue is used, elements are extracted in ascending order of priority (or key)
if a max priority queue is used, elements are extracted in descending order of priority (or key)
Sorting Example
Sort five elements whose keys are 6, 8, 2, 4, 1 using a max priority queue. Insert the five elements into a max priority queue. Do five remove max operations placing removed
elements into the sorted array from right to left.
After Inserting Into Max Priority Queue
Sorted Array
68
2
41 Max Priority
Queue
After First Remove Max Operation
Sorted Array
6
2
41
8
Max Priority Queue
After Second Remove Max Operation
Sorted Array
2
41
86
Max Priority Queue
After Third Remove Max Operation
Sorted Array
21
864
Max Priority Queue
After Fourth Remove Max Operation
Sorted Array
1
8642
Max Priority Queue
After Fifth Remove Max Operation
Sorted Array
86421
Max Priority Queue
18
Implementing a priority queue
•Using a sorted Array•Using a sorted Chain•Using a heap
19
Heap properties• heap: a tree with the following two properties:
– 1. completenesscomplete tree: every level is full except possibly the lowest level, which must be filled from left to right with no leaves to the right of a missing node (i.e., a node may not have any children until all of its possible siblings exist)
Heap shape:
20
Heap properties 2– 2. heap ordering
a tree has heap ordering (min heap) if P < X for every element X with parent P• in other words, in heaps, parents' element values are
always smaller than those of their children• implies that minimum element is always the root
21
Which are min-heaps?
1530
8020
10
996040
8020
10
50 700
85
996040
8020
10
50 700
85 996040
8010
20
50 700
85
6040
8020
10
996040
8020
10
wrong! wrong!
wrong!
wrong!
22
24
7 3
30
10 40
30
80
2510
48
21
14
10 17
33
91828
11
22
3530
50
30
10 20
wrong!
wrong!
Which are max-heaps?
Heap Operations
• Adding to a heap (Min)• Remove from a heap• Build a heap (turn any input into a heap)
24
Adding to a heap• when an element is added to a heap, it
should be initially placed as the rightmost leaf (to maintain the completeness property)– heap ordering property becomes broken!
996040
8020
10
50 700
85
65
996040
8020
10
50 700
85
65 15
25
Adding to a heap, cont'd.• to restore heap ordering property, the newly added
element must be shifted upward ("bubbled up") until it reaches its proper place– bubble up by swapping with parent
996040
8020
10
50 700
85
65 15
992040
8015
10
50 700
85
65 60
• The new element is added and placed as the rightmost leaf (completeness property)
• The new element is bubbled up by swapping with parent (heap ordering property)
• Draw the state of the min-heap tree after adding the following elements to it:
6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2
28
Adding to a max-heap
16
5 11
3 18
16
18 11
3 5
18
16 11
3 5
• same operations, but must bubble up larger values to top
• Draw the state of the max-heap tree after adding the following elements to it:
6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2
30
The top operation• Top on a min-heap is trivial; because of the heap
properties, the minimum element is always the root– Top on a max-heap, but would return you the maximum
element and not the minimum one
996040
8020
10
50 700
85
65
31
Removing from a min-heap• min-heaps only support remove of the min element (the
root)– must remove the root while maintaining heap completeness and
ordering properties– intuitively, the last leaf must disappear to keep it a heap– initially, just swap root with last leaf (we'll fix it)
996040
8020
10
700 50
85
65
996040
8020
65
700 50
85
65
32
Removing from heap, cont'd.• must fix heap-ordering property; root is out of order
– shift the root downward ("bubble down") until it's in place– swap it with its smaller child each time
• What happens if we don't always swap with the smaller child?
996040
8020
65
700 50
85 996050
8040
20
700 65
85
• Swap the last leaf with root and Remove the root (completeness property)
• The new root is bubbled down by swapping with his min/max child (heap ordering property)
34
Heap practice problem
• Draw the state of the min-heap tree after adding the following elements to it:
6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2
• Show the state of the heap after remove() has been executed on it 3 times, and state which elements are returned by the removal.
35
Turning any input into a heap• we can quickly turn any complete tree of comparable
elements into a heap with a buildHeap algorithm• simply perform a "bubble down" operation on every node
that is not a leaf, starting from the rightmost internal node and working back to the root
66014
1821
45
32 456021
1814
6
32
Initializing/building A Max Heap
input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
8
4
7
6 7
8 9
3
710
1
11
5
2
Initializing A Max Heap
Start at rightmost array position that has a child.
8
4
7
6 7
8 9
3
710
1
11
5
2
Index is n/2.
Initializing A Max Heap
Move to next lower array position.
8
4
7
6 7
8 9
3
710
1
5
11
2
Initializing A Max Heap
8
4
7
6 7
8 9
3
710
1
5
11
2
Initializing A Max Heap
8
9
7
6 7
8 4
3
710
1
5
11
2
Initializing A Max Heap
8
9
7
6 7
8 4
3
710
1
5
11
2
Initializing A Max Heap
8
9
7
6 3
8 4
7
710
1
5
11
2
Initializing A Max Heap
8
9
7
6 3
8 4
7
710
1
5
11
2
Initializing A Max Heap
8
9
7
6 3
8 4
7
710
1
5
11
Find a home for 2.
Initializing A Max Heap
8
9
7
6 3
8 4
7
75
1
11
Find a home for 2.
10
Initializing A Max Heap
8
9
7
6 3
8 4
7
72
1
11
Done, move to next lower array position.
10
5
Initializing A Max Heap
8
9
7
6 3
8 4
7
72
1
11
10
5
Find home for 1.
11
Initializing A Max Heap
8
9
7
6 3
8 4
7
72
10
5
Find home for 1.
Initializing A Max Heap
8
9
7
6 3
8 4
7
72
11
10
5
Find home for 1.
Initializing A Max Heap
8
9
7
6 3
8 4
7
72
11
10
5
Find home for 1.
Initializing A Max Heap
Done.
8
9
7
6 3
8 4
7
72
11
10
5
1
• Put input array to an complete binary tree• Start at rightmost array position that has a
child. (index = n/2), bubbled down until it reaches the proper position
• Repeat until root
practice
• Turn the array : (6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2) to a min heap and a max heap
Implement a priority queue using heap
Lab 5
constructor
template<class T>
maxHeap<T>::maxHeap(int initialCapacity)
{// Constructor.
if (initialCapacity < 1)
{ostringstream s;
s << "Initial capacity = " << initialCapacity << " Must be > 0";
throw illegalParameterValue(s.str());
}
arrayLength = initialCapacity + 1;
heap = new T[arrayLength];
heapSize = 0;
}
Initialize(T* theHeap, int theSize)
Push(theElement)
Pop()
Lab 5• Implement
– initialize(); Push(); Pop()
• Test your code– Push the following element to a maxHeap in order: 6,
50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2– Show the state of the maxheap after each push
operation– Show the state of the maxheap after each Pop
operation.– turn the array to a maxheap and show the state of the
maxheap.