CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Similar documents
Data Structures and Algorithms

CMSC 341 Lecture 14: Priority Queues, Heaps

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

Sorting and Searching

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Sorting and Searching

Recall: Properties of B-Trees

Priority Queues Heaps Heapsort

Data Structures Lesson 9

Priority Queues (Heaps)

Priority Queues Heaps Heapsort

CSCI2100B Data Structures Heaps

Algorithms and Data Structures

Data Structures and Algorithms for Engineers

9. Heap : Priority Queue

Priority Queues (Heaps)

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Binary Heaps. CSE 373 Data Structures Lecture 11

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

CS350: Data Structures Heaps and Priority Queues

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Module 2: Priority Queues

Heap Model. specialized queue required heap (priority queue) provides at least

Chapter 6 Heap and Its Application

Basic Data Structures (Version 7) Name:

CSC263 Week 2. Larry Zhang

csci 210: Data Structures Priority Queues and Heaps

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

CMSC 341. Binary Heaps. Priority Queues

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Appendix A and B are also worth reading.

Heap sort. Carlos Moreno uwaterloo.ca EIT

Module 2: Priority Queues

Data Structures and Algorithms

UNIT III BALANCED SEARCH TREES AND INDEXING

Binary Search Trees Treesort

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Module 2: Priority Queues

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Programming II (CS300)

CS 234. Module 6. October 16, CS 234 Module 6 ADT Dictionary 1 / 33

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Properties of a heap (represented by an array A)

CS165: Priority Queues, Heaps

Heap sort. Carlos Moreno uwaterloo.ca EIT

Chapter 6 Heapsort 1

Programming Abstractions

Chapter 2: Basic Data Structures

Priority Queues and Binary Heaps

Heaps. Heaps. A heap is a complete binary tree.

COMP250: Priority queue ADT, Heaps. Lecture 23 Jérôme Waldispühl School of Computer Science McGill University

Priority Queues and Heaps

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

CS102 Binary Search Trees

Binary heaps (chapters ) Leftist heaps

DATA STRUCTURES AND ALGORITHMS

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

1 Interlude: Is keeping the data sorted worth it? 2 Tree Heap and Priority queue

Topic: Heaps and priority queues

Tables and Priority Queues

Data Structures and Algorithms

CH 8. HEAPS AND PRIORITY QUEUES

CS S-08 Priority Queues Heaps 1

Figure 1: A complete binary tree.

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

CH. 8 PRIORITY QUEUES AND HEAPS

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

The priority is indicated by a number, the lower the number - the higher the priority.

Balanced Binary Search Trees. Victor Gao

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

Binary Trees, Binary Search Trees

Figure 1: A complete binary tree.

Lower Bound on Comparison-based Sorting

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

Priority queues. Priority queues. Priority queue operations

COMP Analysis of Algorithms & Data Structures

CSE 214 Computer Science II Heaps and Priority Queues

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Stores a collection of elements each with an associated key value

Programming II (CS300)

CS 10: Problem solving via Object Oriented Programming. Priori9zing 2

CSE332: Data Abstractions Lecture 4: Priority Queues; Heaps. James Fogarty Winter 2012

Data Structures and Algorithms Chapter 4

CSE 373: Data Structures and Algorithms

COMP Analysis of Algorithms & Data Structures

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

Data Structures and Algorithms Week 4

Heaps and Priority Queues

Transcription:

CS 234 Module 8 November 15, 2018 CS 234 Module 8 ADT Priority Queue 1 / 22

ADT Priority Queue Data: (key, element pairs) where keys are orderable but not necessarily distinct, and elements are any data. Preconditions: For all P is a priority queue, k is a key, and e is an element; for LookUpMin and DeleteMin, P is not empty. Postconditions: Mutation by Add (add item with key k) and DeleteMin (delete an item with minimum key). Name Create() IsEmpty(P) LookUpMin(P) Add(P, k, e) DeleteMin(P) Returns a new empty priority queue true if empty, else false item with minimum key k item with minimum key k CS 234 Module 8 ADT Priority Queue 1 / 22

Array implementations of priority queues Unsorted array: store full then empty (plus variable to locate first empty) O(n) LookUpMin O(1) Add (put in first empty, update variable) O(n) DeleteMin (look through all elements, swap with last full, update variable) Unsorted array with a variable storing the location of the min: store full then empty (plus variable to locate first empty) O(1) LookUpMin O(1) Add (like above but also compare to min variable) O(n) DeleteMin (modify constant but linear to update min variable) Sorted array: Store index of first empty in variable. O(1) LookUpMin O(n) Add (find location by binary search or linear, shift all) O(1) DeleteMin (end of array) CS 234 Module 8 ADT Priority Queue 2 / 22

Linked implementations of priority queues Unsorted linked list: store items in arbitrary order O(n) LookUpMin O(1) Add (put front of linked list) O(n) DeleteMin (look through all elements to find smallest) Unsorted linked list with a pointer to min: O(1) LookUpMin O(1) Add (put at beginning of list, compare to min, update if needed) O(n) DeleteMin (modify constant but linear to update min variable) Sorted linked list: O(1) LookUpMin O(n) Add (scan in linked list) O(1) DeleteMin CS 234 Module 8 ADT Priority Queue 3 / 22

An improved implementation Goal: Maybe not as fast as Θ(1) for all operations Never as slow as Θ(n) Using a tree: Modify BST to allow duplicate keys (e.g. values in left subtree value at node). Where is minimum element? What is cost of Add in general? Θ(logn) time for balanced tree Keeping FindMin cheap Store smallest at root. Bound on height of tree. Come up with less stringest condition than binary search order in order to make the condition cheap to maintain. CS 234 Module 8 ADT Priority Queue 4 / 22

Data structure: heap A binary tree satisfies the heap-order property if for each node, the value stored at the node is no greater than that stored in either child (if any). A heap is a complete binary tree that satisfies the heap-order property. 3 5 10 8 5 13 11 9 14 20 15 18 Using a heap to implement the ADT Dictionary: store (key, element) pairs at each node, ensuring that the keys satisfy the heap-order property. CS 234 Module 8 ADT Priority Queue 5 / 22

Observations about example of previous slide Consequences of heap-order property: Path from root to leaf in nondecreasing order (remember that equal values are possible.) Relative values of left and right children unknown. Where are biggest and smallest? Consequences of complete: Nice array implementation Logarithmic bound on height CS 234 Module 8 ADT Priority Queue 6 / 22

Customizing ADT Binary Tree to implement a heap Additional data: Store both keys and elements at nodes. Note: Only keys are ordered using the heap order property. Modified operations: AddNode(B, parent, key, element, side) Additional operations (also for BST): KeyAtNode(B, n) ElementAtNode(B, n) StoreInNode(B, n, key, element) - changes what is stored in a node Additional operations (just for heap): LastLeaf(B) - node that is the last leaf in complete tree PreviousLeaf(B) - leaf before last leaf in complete tree NextLeaf(B) - node that will be the next leaf in complete tree SwapValues(B, node1, node2) - exchanges both key and element Store af variable lastleaf in either implementation. CS 234 Module 8 ADT Priority Queue 7 / 22

Implementing a heap using an array implementation of ADT Binary Tree 3 5 10 8 5 13 11 9 14 20 15 18 3 5 10 8 5 13 11 9 14 20 15 18 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 11 LastLeaf Illustrations show keys only, not elements. lastleaf is a pointer for a linked implementation of ADT Binary Tree. CS 234 Module 8 ADT Priority Queue 8 / 22

Implementing ADT Priority Queue using a heap LookUpMin(P) Return the data item in the root of tree. Θ(1) array implementation of ADT Binary Tree Θ(1) linked implementation of ADT Binary Tree Add(P, k, e) To preserve completeness, add at next leaf position. NextLeaf can be found in time Θ(1) for the array implementation of ADT Binary Tree. NextLeaf can be found in time Θ(logn) for the linked implementation of ADT Binary Tree. Problem: Heap-order property violated. DeleteMin(P) To preserve heap-order property, remove the root. Problem: What remains is not a tree. CS 234 Module 8 ADT Priority Queue 9 / 22

Implementing Add(P, k, e) in a heap Bubble up, fixing heap-order property on path from leaf to root by using Swap operation. Here 18 has just been added. 10 10 30 35 30 18 15 25 38 45 15 25 35 45 20 30 40 50 18 20 30 40 50 38 CS 234 Module 8 ADT Priority Queue 10 / 22

Pseudocode for Add(P, k, e) l NextLeaf(B) lastleaf NextLeaf(B) StoreInNode(B, l, k, e) curr l par Parent(B, curr) while par false and KeyAtNode(B, curr) < KeyAtNode(B, par) SwapValues(B, curr, par) curr par par Parent(B, curr) CS 234 Module 8 ADT Priority Queue 11 / 22

Observations about Add Q: Why can we stop tracing up the path once we find a child that has a greater key value than its parent? A: We verify that the heap property is satisfied everywhere. When we swap a child C with its parent P: C P P O (for O the other child) Thus C O (heap property OK at C) Running time of Add: Number of iterations is height of heap in worst case. Θ(logn) time due to height. Various names: bubble-up sift percolate CS 234 Module 8 ADT Priority Queue 12 / 22

Implementing DeleteMin(P) in a heap Delete value in root, move value in last leaf to root. Bubble down, fixing heap-order property on path from root to leaf using Swap operation 1 2 2 5 4 5 6 4 10 8 6 7 10 8 7 CS 234 Module 8 ADT Priority Queue 13 / 22

DeleteMin pseudocode min Root(B) SwapValues(B, Root(B), lastleaf) DeleteNode(B, lastleaf) lastleaf PreviousLeaf(B) curr Root(B) left LeftChild(B, curr) right Rightchild(B, curr) key KeyAtNode(curr) stop false if left false and right = false lkey KeyAtNode(B, left) if lkey < key SwapValues(B, curr, left) stop true CS 234 Module 8 ADT Priority Queue 14 / 22

DeleteMin pseudocode, continued while left false and right false and not stop) lkey KeyAtNode(B, left) rkey KeyAtNode(B, right) if key lkey and key rkey stop true else if lkey < rkey SwapValues(B, curr, left) curr left else SwapValues(B, curr, right) curr right left LeftChild(B, curr) right RightChild(B, curr) return min CS 234 Module 8 ADT Priority Queue 15 / 22

Observations about DeleteMin PreviousLeaf: array-based implementation: Θ(1) time linked implementation: Θ(log n) time Running time of DeleteMin: Number of iterations is height of heap in worst case. Θ(logn) time due to height. Various names: bubble-down trickle percolate (again!) Moral of story: careful about names Why do we need to look at both children? Did we need to look at both siblings in bubble-up? CS 234 Module 8 ADT Priority Queue 16 / 22

Sorting using a priority queue User view Algorithm: Repeatedly use Add until all values entered. Repeatedly use DeleteMin. Analysis: Θ(n logn) for n Add operations Θ(n logn) for n DeleteMin operations Provider view Can we find a faster way to make a heap out of n elements? CS 234 Module 8 ADT Priority Queue 17 / 22

Customizing ADT Priority Queue Form heap out of a bunch of (key, element) pairs. The heapify operation forms a heap out of an array of items. Idea: Place all items into the structure. Fix heap-order property from bottom up. Observe that leaves are heaps of height 0. At phase i, form heaps of height at most i from two heaps of height at most i 1. CS 234 Module 8 ADT Priority Queue 18 / 22

Heapify example, phase 1 11 11 7 1 7 1 6 5 12 9 2 5 3 9 2 4 10 8 3 6 4 10 8 12 CS 234 Module 8 ADT Priority Queue 19 / 22

Heapify example, phase 2 11 11 7 1 2 1 2 5 3 9 4 5 3 9 6 4 10 8 12 6 7 10 8 12 CS 234 Module 8 ADT Priority Queue 20 / 22

Heapify example, phase 3 11 1 2 1 2 3 4 5 3 9 4 5 11 9 6 7 10 8 12 6 7 10 8 12 CS 234 Module 8 ADT Priority Queue 21 / 22

Linear-time heapify analysis Placement into structure takes Θ(n) time total. Logarithmic number of phases, in phase i forming at most n/2 i+1 heaps of height i each. Cost of making one heap of height i is in Θ(i), bounded above by some ci. log n n Total cost of phases is at most i=1 ci cn( 1 2 i+1 4 + 2 8 + 3 16 + ), which is linear in n. Total cost in O(n). CS 234 Module 8 ADT Priority Queue 22 / 22