# CISC 235: Topic 7 Priority Queues and Binary Heaps.

## Presentation on theme: "CISC 235: Topic 7 Priority Queues and Binary Heaps."— Presentation transcript:

CISC 235: Topic 7 Priority Queues and Binary Heaps

CISC 235 Topic 72 Outline Priority Queues Binary Heaps –Ordering Properties –Structural Property Array Representation Algorithms and Analysis of Complexity insert minimin extractMin buildHeap

CISC 235 Topic 73 Priority Queues A queue that is ordered according to some priority value Standard Queue Priority Queue enqueue dequeueenqueue dequeue Add at RemoveAdd Remove end from frontaccording from front to priority value

CISC 235 Topic 74 Example Applications Line-up of Incoming Planes at Airport Possible Criteria for Priority? Operating Systems Priority Queues? Several criteria could be mapped to a priority status

CISC 235 Topic 75 Min-Priority Queue Operations insert( S, x ) – Inserts element x into set S, according to its priority minimum( S ) – Returns, but does not remove, element of S with the smallest key extractMin( S ) – Removes and returns the element of S with the smallest key

CISC 235 Topic 76 Possible Implementations?

CISC 235 Topic 77 Binary Heaps A binary tree with an ordering property and a structural property Ordering Property: Min Heap –The element in the root is less than or equal to all elements in both its sub-trees –Both of its sub-trees are Min Heaps Ordering Property: Max Heap –The element in the root is greater than or equal to all elements in both its sub-trees –Both of its sub-trees are Max Heaps

CISC 235 Topic 78 Binary Heap Structural Property A binary heap is a binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right with no missing nodes.

CISC 235 Topic 79 Which Tree is a Min Heap? 68 16 21 13 14 65 31 19 2632 68 16 21 13 24 65 31 19 26 32

CISC 235 Topic 710 A Min Heap and its Array Representation 68 16 21 13 24 65 31 19 2632 13211624311968652632 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 8109 Note: Only the array exists in memory heap

CISC 235 Topic 711 Locating Parents & Children parent( i ) return  i / 2  left( i ) return 2i right( i ) return 2i + 1

CISC 235 Topic 712 Min Heap Operations: Insert 1.Find the next position at which to insert – after the “last” element in the heap – and create a “hole” at that postion 2.“Bubble” the hole up the tree by moving its parent element into it until the hole is at a position where the new element ≤ its children and ≥ its parent: the percolateUp() operation 3.Insert the new element in the hole

CISC 235 Topic 713 Insert Step 1: Create hole at next insert position 68 16 21 13 24 65 31 19 2632 13211624311968652632 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 8109 Insert 14 14 11 heap

CISC 235 Topic 714 Insert Step 2 : Bubble hole up to where element ≤ its children and ≥ its parent 68 16 21 13 24 65 31 19 2632 13211624196865263231 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 8109 Insert 14 14 11 heap

CISC 235 Topic 715 Insert Step 2 : Bubble hole up to where element ≤ its children and ≥ its parent 68 16 21 13 24 65 31 19 2632 13162421196865263231 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 8109 Insert 14 14 11 heap

CISC 235 Topic 716 Insert Step 3 : Insert the new element in the hole 68 16 21 13 24 65 31 19 2632 1314162421196865263231 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 8109 Insert 14 11 14 heap 14

CISC 235 Topic 717 Analysis of Insert? Worst-case complexity?

CISC 235 Topic 718 Min Heap Operations: Minimum Return minimum at top of heap: O(1) 68 16 21 13 24 65 31 19 2632 13211624311968652632 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 8109 13 heap return heap[1];

CISC 235 Topic 719 Min Heap Operations: ExtractMin 1.Copy the root (the minimum) to a temporary variable, thus creating a “hole” at the root. 2.Delete the last item in the heap by copying its element to a temporary location. 3.Find a place for that element by bubbling the hole down by moving its smallest child up until the element ≤ its children and ≥ its parent: the percolateDown() operation. 4.Place the former last element in that hole and return the former root.

CISC 235 Topic 720 ExtractMin Step 1: Copy the root to a temporary variable 68 16 21 13 24 65 31 19 2632 13211624311968652632 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 8109 13 minimum heap minimum = heap[1];

CISC 235 Topic 721 ExtractMin Step 1: thus creating a “hole” at the root 68 16 21 24 65 31 19 2632 211624311968652632 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 8109 13 minimum heap minimum = heap[1];

CISC 235 Topic 722 ExtractMin Step 2: Delete last item in heap by copying it to a temporary location 68 16 21 24 65 31 19 2632 211624311968652632 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 8109 13 minimum heap 32 temp temp = heap[10];

CISC 235 Topic 723 68 16 21 24 65 31 19 26 2116243119686526 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 89 13 minimum heap 32 temp ExtractMin Step 2: Delete last item in heap by copying it to a temporary location

CISC 235 Topic 724 68 16 21 24 65 31 19 26 1621243119686526 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 89 13 minimum heap 32 temp ExtractMin Step 3: Bubble hole down until temp ≤ its children and ≥ its parent

CISC 235 Topic 725 68 16 21 24 65 31 19 26 1621192431686526 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 89 13 minimum heap 32 temp ExtractMin Step 3: Bubble hole down until temp ≤ its children and ≥ its parent

CISC 235 Topic 726 68 16 21 24 65 31 19 26 162119243132686526 0 1 2 3 4 5 6 7 8 9101112 1 3 2 7564 89 13 minimum heap 32 temp ExtractMin Step 4: Copy temp to the hole and return the minimum 32 heap[6] = temp; return minimum;

CISC 235 Topic 727 Analysis of ExtractMin? Worst-case complexity?

CISC 235 Topic 728 Insert n Elements into an Initially Empty Heap Worst-case complexity?

CISC 235 Topic 729 Bottom-Up Heap Construction: BuildHeap If all the keys are given in advance and stored in an array, we can build a heap in worst-case O(n) time. Note: For simplicity, we’ll describe bottom-up heap construction for a full tree Algorithm: Call percolateDown() on nodes in non- heap tree in reverse level-order traversal to convert tree to a heap.

CISC 235 Topic 730 BuildHeap Algorithm 1.Build (n+1)/2 elementary heaps, with one key each, of the leaves of the tree (no work) 2.Build (n+1)/4 heaps, each with 3 keys, by joining pairs of elementary heaps with their parent as the root. Call percolateDown() 3.Build (n+1)/8 heaps, each with 7 keys, by joining pairs of 3-key heaps with their parent as the root. Call percolateDown() 4.Continue until reach root of entire tree.

CISC 235 Topic 731 BuildHeap at Start: Elements are Unordered: Not in Heap Order 14 9 825 511271615 412 6 72320 0 1 2 3 4 5 6 7 8 9101112131415 14 9 25 16 5 15 4 8 11 6 27 723 12 20

CISC 235 Topic 732 BuildHeap Step 1: Build (n+1)/2 elementary heaps, with one key each 14 9 825 511271615 412 6 72320 0 1 2 3 4 5 6 7 8 9101112131415 14 9 25 16 5 15 4 8 11 6 27 723 12 20

CISC 235 Topic 733 BuildHeap Step 2: Build (n+1)/4 heaps, with 3 keys each 14 9 825 511271615 412 6 72320 0 1 2 3 4 5 6 7 8 9101112131415 14 9 25 16 5 15 4 8 11 6 27 723 12 20

CISC 235 Topic 734 BuildHeap Step 2: Call percolateDown( ) for each 3-key heap 14 9 825 511271615 412 6 72320 0 1 2 3 4 5 6 7 8 9101112131415 14 9 25 16 5 15 4 8 11 6 27 723 12 20 27 25 511

CISC 235 Topic 735 BuildHeap Step 2: Call percolateDown( ) for each 3-key heap 14 9 8 1615 412 6 72320 0 1 2 3 4 5 6 7 8 9101112131415 14 9 16 15 4 8 6 723 12 20 27 25 511

CISC 235 Topic 736 BuildHeap Step 2: Call percolateDown( ) for each 3-key heap 14 9 815 4 62016 12 723 0 1 2 3 4 5 6 7 8 9101112131415 14 9 16 15 4 8 6 723 12 20 27 25 511

CISC 235 Topic 737 BuildHeap Step 2: Call percolateDown( ) for each 3-key heap 14 9 815 4 62016 12 723 0 1 2 3 4 5 6 7 8 9101112131415 14 9 16 15 4 8 6 723 12 20 27 25 511

CISC 235 Topic 738 BuildHeap Step 2: Now we have (n+1)/4 heaps, with 3 keys each 14 9 815 4 6201625 51211 72327 0 1 2 3 4 5 6 7 8 9101112131415 14 9 16 15 4 8 6 723 12 20 25 27 11 5

CISC 235 Topic 739 BuildHeap Step 3: Build (n+1)/8 heaps, with 7 keys each 14 9 815 4 6201625 51211 72327 0 1 2 3 4 5 6 7 8 9101112131415 14 9 16 15 4 8 6 723 12 20 25 27 11 5

CISC 235 Topic 740 BuildHeap Step 3: Call percolateDown( ) for each 7-key heap 14 9 815 4 6201625 51211 72327 0 1 2 3 4 5 6 7 8 9101112131415 14 9 16 15 4 8 6 723 12 20 25 27 11 5 8 9

CISC 235 Topic 741 BuildHeap Step 3: Call percolateDown( ) for each 7-key heap 14 15 4 6201625 51211 72327 0 1 2 3 4 5 6 7 8 9101112131415 14 16 15 4 6 723 12 20 25 27 11 5 8 9

CISC 235 Topic 742 BuildHeap Step 3: Call percolateDown( ) for each 7-key heap 14 4 615 201625 51211 72327 0 1 2 3 4 5 6 7 8 9101112131415 14 16 15 4 6 723 12 20 25 27 11 5 8 9

CISC 235 Topic 743 BuildHeap Step 3: Call percolateDown( ) for each 7-key heap 14 4 615 5 7201625 1211 2327 0 1 2 3 4 5 6 7 8 9101112131415 14 16 15 4 6 7 23 12 20 25 27 11 5 8 9

CISC 235 Topic 744 BuildHeap Step 3: Call percolateDown( ) for each 7-key heap 14 4 615 5 7201625 1211 2327 0 1 2 3 4 5 6 7 8 9101112131415 14 16 15 4 6 7 23 12 20 25 27 11 5 8 9

CISC 235 Topic 745 BuildHeap Step 3: Now we have (n+1)/8 heaps, with 7 keys each 14 4 615 5 7201625 91211 82327 0 1 2 3 4 5 6 7 8 9101112131415 14 16 15 4 6 7 23 12 20 25 27 11 5 8 9

CISC 235 Topic 746 BuildHeap Step 4: Build (n+1)/16 heaps, with 15 keys each (only one here) 14 4 615 5 7201625 91211 82327 0 1 2 3 4 5 6 7 8 9101112131415 14 16 15 4 6 7 23 12 20 25 27 11 5 8 9

CISC 235 Topic 747 BuildHeap Step 4: Call percolateDown( ) for each 15-key heap 14 4 615 5 7201625 91211 82327 0 1 2 3 4 5 6 7 8 9101112131415 14 16 15 4 6 7 23 12 20 25 27 11 5 8 9 14

CISC 235 Topic 748 BuildHeap Step 4: Call percolateDown( ) for each 15-key heap 4 615 5 7201625 91211 82327 0 1 2 3 4 5 6 7 8 9101112131415 16 15 4 6 7 23 12 20 25 27 11 5 8 9 14

CISC 235 Topic 749 BuildHeap Step 4: Call percolateDown( ) for each 15-key heap 4 615 5 7201625 91211 82327 0 1 2 3 4 5 6 7 8 9101112131415 16 15 4 6 7 23 12 20 25 27 11 5 8 9 14

CISC 235 Topic 750 BuildHeap Step 4: Call percolateDown( ) for each 15-key heap 4 5 615 7201625 91211 82327 0 1 2 3 4 5 6 7 8 9101112131415 16 15 4 6 7 23 12 20 25 27 11 5 8 9 14

CISC 235 Topic 751 BuildHeap Step 4: Call percolateDown( ) for each 15-key heap 4 5 615 9 7201625 1211 82327 0 1 2 3 4 5 6 7 8 9101112131415 16 15 4 6 7 23 12 20 25 27 11 5 8 9 14

CISC 235 Topic 752 BuildHeap Step 4: Call percolateDown( ) for each 15-key heap 4 5 615 9 7201625 1211 82327 0 1 2 3 4 5 6 7 8 9101112131415 16 15 4 6 7 23 12 20 25 27 11 5 8 9 14

CISC 235 Topic 753 BuildHeap Step 4: The Entire Tree is Now a Heap! 4 5 615 9 7201625141211 82327 0 1 2 3 4 5 6 7 8 9101112131415 16 15 4 6 7 23 12 20 25 27 11 5 8 9 14

CISC 235 Topic 754 Heapsort How could we use a heap to sort an array?