Priority queue ADT Heaps. Lecture 21

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

Sorting and Searching

Sorting and Searching

Priority Queues & Heaps. CS16: Introduction to Data Structures & Algorithms Spring 2019

Heaps. 2/13/2006 Heaps 1

Priority queues. Priority queues. Priority queue operations

Heaps Goodrich, Tamassia. Heaps 1

CH 8. HEAPS AND PRIORITY QUEUES

CH. 8 PRIORITY QUEUES AND HEAPS

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Algorithms and Data Structures

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

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

Data Structures and Algorithms

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

Recall: Properties of B-Trees

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

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

Module 2: Priority Queues

Appendix A and B are also worth reading.

Module 2: Priority Queues

Priority Queues (Heaps)

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

csci 210: Data Structures Priority Queues and Heaps

Stores a collection of elements each with an associated key value

Heaps and Priority Queues

CSC263 Week 2. Larry Zhang

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

COMP251: Background. Jérôme Waldispühl School of Computer Science McGill University

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

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

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

Sorting and Selection

Chapter 6 Heapsort 1

Heaps and Priority Queues

Priority Queues and Heaps. Heaps and Priority Queues 1

CSCI2100B Data Structures Heaps

COMP251: Algorithms and Data Structures. Jérôme Waldispühl School of Computer Science McGill University

quiz heapsort intuition overview Is an algorithm with a worst-case time complexity in O(n) data structures and algorithms lecture 3

Binary Heaps. CSE 373 Data Structures Lecture 11

Heaps. Heaps Priority Queue Revisit HeapSort

CMSC 341 Lecture 14: Priority Queues, Heaps

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 20, March 24, 2016

Merge Sort Goodrich, Tamassia Merge Sort 1

CSE 373: Data Structures and Algorithms

Programming II (CS300)

Data Structures and Algorithms

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Merge Sort fi fi fi 4 9. Merge Sort Goodrich, Tamassia

Properties of a heap (represented by an array A)

Heaps and Priority Queues

COMP250: Dictionary ADT & Binary Search Trees. Lecture 22 Jérôme Waldispühl School of Computer Science McGill University

Priority Queues. 04/10/03 Lecture 22 1

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

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

Topic: Heaps and priority queues

9. Heap : Priority Queue

Priority Queues and Binary Heaps

Heapsort. Heap data structure

Lecture 19 Sorting Goodrich, Tamassia

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

Dictionaries. Priority Queues

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end

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

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

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

Module 2: Priority Queues

Data Structures Lecture 7

Priority Queues Heaps Heapsort

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

Binary heaps (chapters ) Leftist heaps

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Priority Queue ADT ( 7.1) Heaps and Priority Queues 2. Comparator ADT ( 7.1.4) Total Order Relation. Using Comparators in C++

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Priority Queues. Problem. Let S={(s1,p1), (s2,p2),,(sn,pn)} where s(i) is a key and p(i) is the priority of s(i).

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

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

Sorting. Outline. Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort

Lower Bound on Comparison-based Sorting

Heaps, Heap Sort, and Priority Queues.

Sorting Goodrich, Tamassia Sorting 1

The Heap Data Structure

Data Structures and Algorithms Week 4

Outline. Part 7. Tables and Priority Queues. Example: Table of Student Points. Value Oriented Data Structures. Table ADT. Search Keys 4/2/12

10/3/2018 Data Structure & Algorithm. Quiz (30 min, closed-book) Heaps: Array-based Heap Implementation

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

CSE 373: Data Structures and Algorithms More Asympto<c Analysis; More Heaps

Quick-Sort fi fi fi 7 9. Quick-Sort Goodrich, Tamassia

Teach A level Compu/ng: Algorithms and Data Structures

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

Data Structures Lesson 9

Priority Queues. Binary Heaps

Algorithms and Data Structures

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

Lecture: Analysis of Algorithms (CS )

Data Structures and Algorithms

Data Structures and Algorithms Chapter 4

Transcription:

Priority queue ADT Heaps Lecture 21

Priority queue ADT Like a dic9onary, a priority queue stores a set of pairs (key, info) The rank of an object depends on its priority (key) Rear of queue Front of queue key: 9 8 6 5 4 2 Allows only access to Object findmin() //returns info of smallest key Object removemin() // removes smallest key void insert(key k, info i) // inserts pair Applica9ons: customers in line, Data compression, Graph searching, Ar9ficial intelligence

Outline Priority queues Heaps Opera9ons on heaps Array-based implementa9on of heaps HeapSort

Priority queue ADT (4,O 4 ) (5, O 5 ) (8,O 8 ) (4,O 4 ) (5, O 5 ) (8,O 8 ) (9,O 9 ) (5, O 5 ) (8,O 8 ) (9,O 9 ) (5, O 5 ) (6,O 6 ) (8,O 8 ) (9,O 9 ) (2, O 2 ) (5, O 5 ) (6,O 6 ) (8,O 8 ) (9,O 9 ) insert(9,o 9 ) remove() insert(6,o 6 ) insert(2,o 2 )

Implementa9on of priority queue Unsorted array of pairs (key, info) findmin(): insert(key, info): removemin(): Sorted array of pairs (key, info) findmin(): Need to scan array O(n) Put new object at the end First, findmin, then shiy array Return first element O(1) O(n) O(1) insert(key, info): Use binary-search to find posi9on of inser9on. O(log n) Then shiy array to make space. O(n)

Implementa9on of priority queue Using a sorted doubly-linked list of pairs (key, info) findmin(): Return first element O(1) insert(key, info): First, find loca9on of inser9on. Binary Search? No. Too slow on linked list. Instead, we scan an array O(n) Then inser9on is easy O(1) removemin(): Remove first element of list O(1)

Heap data structure A heap is a data structure that implements a priority queue: findmin(): O(1) removemin(): O(log n) insert(key, info): O(log n) A heap is based on a binary tree, but with a different property than a binary search tree heap binary search tree

Heap - Defini9on A heap is a binary tree such that: For any node n other than the root, key(n) key( parent(n) ) Let h be the height of the heap First h-1 levels are full: For i = 0,,h-1, there are 2 i nodes of depth i At depth h, the leaves are packed on the ley side of the tree

Heap - Example 0 1 2 3

Height of a heap What is the maximum number of nodes that fits in a heap of height h? h k=0 2 k What is the minimum number? = 2 h+1 1 (2 h 1)+1= 2 h Thus, the height of a heap with n nodes is:!" log(n) #$

Heaps: findmin() The minimum key is always at the root of the heap!

Heaps: Insert Insert(key k, info i). Two steps: 1. Find the ley-most unoccupied node and insert (k,i) there temporarily. 2. Restore the heap-order property (see next)

Heaps: Bubbling-up Restoring the heap-order property: Keep swapping new node with its parent as long as its key is smaller than its parent s key Running 9me? Ο(h) = Ο(log(n))

Insert pseudocode Algorithm insert(key k, info i) Input: Key k and info i to add to the heap Output: (k,i) is added lastnode nextavailablenode(lastnode) lastnode.key k, lastnode.info i n lastnode while (n.getparent()!=null and n.getparent().key > k) do swap (n.getparent(), n)

Heaps: RemoveMin() The minimum key is always at the root of the heap! Replace the root with last node Restore heap-order property (see next)

Heaps: Bubbling-down Restoring the heap-order property: Keep swapping the node with its smallest child as long as the node s key is larger than its child s key Running 9me? Ο(h) = Ο(log(n))

removemin pseudocode Algorithm removemin() Input: The heap Output: A new heap where the node at the top of the input heap has been removed. swap(lastnode, root) Update lastnode n root while (n.key > min(n.getleftchild().key, n.getrightchild().key)) do if (n.getleftchild().key < n.getrightchild().key) then swap(n, n.getleftchild) else swap(n, n.getrightchild)

Array representa9on of heaps A heap with n keys can be stored in an array of length n+1: 0 1 2 3 4 5 6 7 8 9 10-2 5 6 7 10 8 9 8 9 12 For a node at index i, The parent (if any) is at index i/2 The ley child is at index 2*i The right child is at index 2*i + 1 lastnode is the first empty cell of the array. To update it, either add or subtract one

Heaps as arrays 26 24 20 18 17 19 13 12 14 11 1 2 3 4 5 6 7 8 9 10 1 26 2 3 24 20 4 5 6 7 18 17 19 13 8 9 1 12 14 11 0 Max-heap as an array. Map from array elements to tree nodes and vice versa Root A[1] Left[i] A[2i] Right[i] A[2i+1] Parent[i] A[ i/2 ]

HeapSort Algorithm heapsort(array A[0 n-1]) Heap h new Heap() for i=0 to n-1 do h.insert(a[i]) for i=0 to n-1 do A[i] h.removemin() Running time: O(n log n) in worst-case Easy to do in-place: Just use the array A to store the heap Note: We can optimize the construction of the heap (See COMP251)

Supplement Implementa9ng nextavailablenode

NextAvailableNode - Example

Finding nextavailablenode nextavailablenode(lastnode) finds the loca9on where the next node should be inserted. It runs in 9me O(n). n = lastnode; while (n==(n.parent).rightchild && n.parent!=null) do n = n.parent if ( n.parent == null ) then return left child of the leftmost node of tree else n = n.parent // go up one more level if ( n has no right child) then return (right child of n) else n = n.rightchild // go to right child while (n has a left child ) do n = n.leftchild return (left child of n)