Presentation is loading. Please wait.

Presentation is loading. Please wait.

Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.

Similar presentations


Presentation on theme: "Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority."— Presentation transcript:

1 Priority Queue

2 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)

3 Max/Min Priority Queue 68 2 4 1  empty  Size  Push  Top  pop

4 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

5 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

6 Implementation of Priority Queue Array, linked list, heap and complete binary tree. Operations: empty size top insert (push) remove (pop)

7

8 C++ STL priority_queue (max priority queue)

9

10 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)

11 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.

12 After Inserting Into Max Priority Queue Sorted Array 68 2 4 1 Max Priority Queue

13 After First Remove Max Operation Sorted Array 6 2 4 1 8 Max Priority Queue

14 After Second Remove Max Operation Sorted Array 2 4 1 86 Max Priority Queue

15 After Third Remove Max Operation Sorted Array 2 1 864 Max Priority Queue

16 After Fourth Remove Max Operation Sorted Array 1 8642 Max Priority Queue

17 After Fifth Remove Max Operation Sorted Array 86421 Max Priority Queue

18 18 Implementing a priority queue Using a sorted Array Using a sorted Chain Using a heap

19 19 Heap properties heap: a tree with the following two properties: –1. completeness complete 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 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 21 Which are min-heaps? 1530 8020 10 996040 8020 10 50700 85 996040 8020 10 50 700 85 996040 8010 20 50700 85 6040 8020 10 996040 8020 10 wrong!

22 22 24 73 30 10 40 30 80 25 10 48 21 14 1017 33 9 1828 11 22 3530 50 30 10 20 wrong! Which are max-heaps?

23 Heap Operations Adding to a heap (Min) Remove from a heap Build a heap (turn any input into a heap)

24 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 50700 85 65 996040 8020 10 50700 85 65 15

25 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 50700 85 65 15 992040 8015 10 50700 85 65 60

26 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)

27 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 28 Adding to a max-heap 16 511 3 18 16 18 11 3 5 18 1611 3 5 same operations, but must bubble up larger values to top

29 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 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 50700 85 65

31 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 70050 85 65 996040 8020 65 70050 85 65

32 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 70050 85996050 8040 20 70065 85

33 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 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 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 32456021 1814 6 32

36 Initializing/building A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 8 4 7 67 89 3 7 10 1 11 5 2

37 Initializing A Max Heap Start at rightmost array position that has a child. 8 4 7 67 89 3 7 10 1 11 5 2 Index is n/2.

38 Initializing A Max Heap Move to next lower array position. 8 4 7 67 89 3 7 10 1 5 11 2

39 Initializing A Max Heap 8 4 7 67 89 3 7 10 1 5 11 2

40 Initializing A Max Heap 8 9 7 67 84 3 7 10 1 5 11 2

41 Initializing A Max Heap 8 9 7 67 84 3 7 10 1 5 11 2

42 Initializing A Max Heap 8 9 7 63 84 7 7 10 1 5 11 2

43 Initializing A Max Heap 8 9 7 63 84 7 7 10 1 5 11 2

44 Initializing A Max Heap 8 9 7 63 84 7 7 10 1 5 11 Find a home for 2.

45 Initializing A Max Heap 8 9 7 63 84 7 7 5 1 11 Find a home for 2. 10

46 Initializing A Max Heap 8 9 7 63 84 7 7 2 1 11 Done, move to next lower array position. 10 5

47 Initializing A Max Heap 8 9 7 63 84 7 7 2 1 11 10 5 Find home for 1.

48 11 Initializing A Max Heap 8 9 7 63 84 7 7 2 10 5 Find home for 1.

49 Initializing A Max Heap 8 9 7 63 84 7 7 2 11 10 5 Find home for 1.

50 Initializing A Max Heap 8 9 7 63 84 7 7 2 11 10 5 Find home for 1.

51 Initializing A Max Heap Done. 8 9 7 63 84 7 7 2 11 10 5 1

52 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

53 practice Turn the array : (6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2) to a min heap and a max heap

54 Implement a priority queue using heap Lab 5

55

56 constructor template maxHeap ::maxHeap(int initialCapacity) {// Constructor. if (initialCapacity < 1) {ostringstream s; s 0"; throw illegalParameterValue(s.str()); } arrayLength = initialCapacity + 1; heap = new T[arrayLength]; heapSize = 0; }

57 Initialize(T* theHeap, int theSize)

58 Push(theElement)

59 Pop()

60 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.


Download ppt "Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority."

Similar presentations


Ads by Google