CS165: Priority Queues, Heaps

Similar documents
CS200: Tables and Priority Queues

CS350: Data Structures Heaps and Priority Queues

4/3/13. Outline. Part 7. Tables and Priority Queues. Value Oriented Data Structures. Example: Table of Student Points. Table ADT.

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

ADT Priority Queue. Heaps. A Heap Implementation of the ADT Priority Queue. Heapsort

Priority Queues Heaps Heapsort

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

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

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

CH 8. HEAPS AND PRIORITY QUEUES

CH. 8 PRIORITY QUEUES AND HEAPS

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

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

Heaps and Priority Queues

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

Algorithms and Data Structures

CSE 214 Computer Science II Heaps and Priority Queues

Sorting and Searching

BM267 - Introduction to Data Structures

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

Heaps & Priority Queues. (Walls & Mirrors - Remainder of Chapter 11)

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

Heaps. Heaps Priority Queue Revisit HeapSort

Programming II (CS300)

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

Sorting and Searching

" Which data Structure to extend to create a heap? " Two binary tree extensions are needed in a heap. n it is a binary tree (a complete one)

CSCI2100B Data Structures Heaps

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

Priority Queue. How to implement a Priority Queue? queue. priority queue

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Priority Queues and Heaps. Heaps of fun, for everyone!

CS 10: Problem solving via Object Oriented Programming Winter 2017

Heaps. 2/13/2006 Heaps 1

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

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

Tables, Priority Queues, Heaps

Heapsort. Heap data structure

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

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

9. Heap : Priority Queue

Properties of a heap (represented by an array A)

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

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Design and Analysis of Algorithms

Priority Queues and Binary Heaps

Heaps Goodrich, Tamassia. Heaps 1

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

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

CS 241 Analysis of Algorithms

Data Structures Lesson 9

Stores a collection of elements each with an associated key value

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

Priority Queues Heaps Heapsort

Partha Sarathi Manal

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?

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

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

EST Solutions. Ans 1(a): KMP Algorithm for Preprocessing: KMP Algorithm for Searching:

Collection of priority-job pairs; priorities are comparable.

Topic: Heaps and priority queues

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

Chapter 6 Heapsort 1

Heaps, Heap Sort, and Priority Queues.

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

Tables and Priority Queues

Module 2: Priority Queues

Programming II (CS300)

The Heap Data Structure

Module 2: Priority Queues

Tables and Priority Queues!

Chapter 6 Heap and Its Application

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Priority Queues (Heaps)

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

Data Structures and Algorithms

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

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

Module 2: Priority Queues

Data Structures and Algorithms for Engineers

Maintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater

Leftist Heaps and Skew Heaps. (Leftist Heaps and Skew Heaps) Data Structures and Programming Spring / 41

CMSC 341 Lecture 14: Priority Queues, Heaps

Appendix A and B are also worth reading.

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

PRIORITY QUEUES AND HEAPS

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

CS200: Trees. Rosen Ch & 11.3 Prichard Ch. 11. CS200 - Trees

Tables and Priority Queues

Figure 1: A complete binary tree.

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Data Structures Lecture 7

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

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

Section 4 SOLUTION: AVL Trees & B-Trees

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Priority queues. Priority queues. Priority queue operations

CS302 - Data Structures using C++

CSCI 104 Binary Trees / Priority Queues / Heaps. Mark Redekopp Michael Crowley

Transcription:

CS1: Priority Queues, Heaps Prichard Ch. 12 Priority Queues Characteristics Items are associated with a Comparable value: priority Provide access to one element at a time - the one with the highest priority offer(e e) and add(e e) inserts the element into the priority queue based on the priority order remove() and poll() removes the head of the queue (which is the highest priority) and returns it PQ Reference-based Implementation pqhead.2.8 3 Reference-based implementation Sorted in descending order Highest priority value is at the beginning of the linked list remove() returns the item that pqhead references and changes pqhead to reference the next item. offer(e e) must traverse the list to find the correct position for insertion. CS1 - Priority Queues 1 CS1 - Priority Queues 2 CS1 - Priority Queues 3 1

Complete tree definition Complete Binary Tree Heap - Definition Complete binary tree of height h zero or more rightmost leaves not present at level h A binary tree T of height h is complete if All nodes at level h 2 and above have two children each, and When a node at level h 1 has children, all nodes to its left at the same level have two children each, and When a node at level h - 1 has one child, it is a left child So the leaves at level h go from left to right h-2: h-1: h: Level-by-level numbering of a complete binary tree, NOTE 0 based! 3:Alan 1:Bob 4:Ellen 0:Jane 2:Tom :Nancy What is the parent child index relationship? left child i: at 2*i+1 right child i: at 2*(i+1) lparent i: at (i-1)/2 So we can store a complete binary tree in an array!! A maximum heap (maxheap) is a complete binary tree that satisfies the following: It is a leaf, or it has the heap property: Its root contains a key greater or equal to the keys of its children Its left and right sub-trees are also maxheaps A minheap has the root less or equal children, and left and right sub trees are also minheaps CS1 - Priority Queues 4 CS1 - Priority Queues CS1 - Priority Queues 2

maxheap Property Implications Examples Array(List) Implementation Implications of the heap property: The root holds the maximum value (global property) Values in descending order on every path from root to leaf A Heap is NOT a binary search tree, as in a BST the nodes in the right sub tree of the root are larger than the root 20 0 10 1 2 Satisfies heap property AND Complete 30 2 20 10 1 Satisfies heap property BUT Not complete 1 30 20 10 2 Does not satisfy heap property AND Not complete 20 0 10 1 2 0 1 2 3 4 0 20 2 10 1 CS1 - Priority Queues 7 CS1 - Priority Queues 8 CS1 - Priority Queues 3

Array(List) Implementation Traversal: Root at position 0 Left child of position i at position 2*i+1 Right child of position i at position 2*(i+1) Parent of position i at position (i-1)/2 (int arithmetic truncates) Heap Operations - heapinsert Step 1: put a new value into first open position (maintaining completeness), i.e. at the end but now we potentially violated the heap property, so: Step 2: bubble values up Re-enforcing the heap property Swap with parent, if new value > parent, until in the right place. The heap property holds for the tree below the new value, when swapping up Swapping up Swapping up enforces heap property for sub tree below the new, inserted value: y x new if (new > x) swap(x,new) y new x x>y, therefore new > y CS1 - Priority Queues 10 CS1 - Priority Queues 11 CS1 - Priority Queues 12 4

Insertion into a heap (Insert 1) Insertion into a heap (Insert 1) Insertion into a heap (Insert 1) 1 1 1 Insert 1 bubble up bubble up CS1 - Priority Queues 13 CS1 - Priority Queues 14 CS1 - Priority Queues 1

Heap operations heapdelete Step 1: remove value at root (Why?) Step 2: substitute with rightmost leaf of bottom level (Why?) Step 3: bubble down Swap with maximum child as necessary, until in place each bubble down restores the heap property for the max child this is called HEAPIFY Swapping down Swapping down enforces heap property at the swap location: y new x new<x and y<x: swap(x,new) y x new x>y and x>new Deletion from a heap 10 Delete 10 Place last node in root CS1 - Priority Queues 1 CS1 - Priority Queues 17 CS1 - Priority Queues 18

bubble down heapify draw the heap delete again draw the heap 2 2 3 3 CS1 - Priority Queues 1 CS1 - Priority Queues 20 CS1 - Priority Queues 21 7

HeapSort Algorithm Insert all elements (one at a time) to a heap Iteratively delete them Removes minimum/maximum value at each step HeapSort Alternative method (in-place): buildheap: create a heap out of the input array: Consider the input array as a complete binary tree Create a heap by iteratively expanding the portion of the tree that is a heap Leaves are already heaps Start at last internal node Go backwards calling heapify with each internal node Iteratively swap the root item with last item in unsorted portion and rebuild Building the heap buildheap(n){ for (i = (n-2)/2 down to 0) //pre: the tree rooted at index is a semiheap //i.e., the sub trees are heaps heapify(i); // bubble down //post: the tree rooted at index is a heap } WHY start at (n-2)/2? WHY go backwards? The whole method is called buildheap One bubble down is called heapify CS1 - Priority Queues 22 CS1 - Priority Queues 23 CS1 - Priority Queues 24 8

3 7 2 10 Draw as a Complete Binary Tree: 3 7 3 10 10 2 10 2 7 7 Repeatedly heapify, starting at last internal node, going backwards CS1 - Priority Queues 2 CS1 - Priority Queues 2 CS1 - Priority Queues 27

10 7 10 7 In place heapsort using an array First build a heap out of an input array using buildheap(). See previous slides. Then partition the array into two regions; starting with the full heap and an empty sorted and stepwise growing sorted and shrinking heap. HEAP Sorted (Largest elements in array) Do it, do it HEAP 10 10 2 3 10 10 10 2 3 10 2 3 10 SORTED CS1 - Priority Queues 28 CS1 - Priority Queues 2 CS1 - Priority Queues 30 10