CS 361 – Chapter 5 Priority Queue ADT Heap data structure –Properties –Internal representation –Insertion –Deletion.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Priority queues CS310 – Data Structures Professor Roch Weiss, Chapter 6.9, 21 All figures marked with a chapter and section number are copyrighted © 2006.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Week 10: Heap and Priority queue. Any feature here?
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Heapsort Based off slides by: David Matuszek
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Binary Heap.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Heaps & Priority Queues
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
CSC 213 – Large Scale Programming. Priority Queue ADT  Prioritizes Entry s using their keys  For Entry s with equal priorities, order not specified.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
"Teachers open the door, but you must enter by yourself. "
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues An abstract data type (ADT) Similar to a queue
Heaps, Heap Sort and Priority Queues
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
October 30th – Priority QUeues
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heaps and Priority Queues
Part-D1 Priority Queues
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
Heaps and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Heaps and Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues Binary Heaps
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

CS 361 – Chapter 5 Priority Queue ADT Heap data structure –Properties –Internal representation –Insertion –Deletion

Priority Queue An ADT where each item has a special value called its “key” or “priority” (in addition to its contents) Not really a queue It’s important to be able to find/extract smallest element –Could just as easily be defined with “largest” Application: –Scheduling a set of tasks. We could use “Earliest Deadline First” or “Shortest Job Next”. At all times, we need to know the winner. Desired operations: –insert (element, keyValue) –removeNext ( ) –findNext ( )

Implementation One approach is a sorted list (array). –Insert: O(n), to find the place in the list to insert, and possibly shift over other elements  –Remove and find smallest: O(1), since it’s at the front Can we do better than O(n) insertion? Heap implementation –This d.s. is a special type of binary tree –Complete or almost complete: the lowest level may have gap along the right side, but nowhere else –Heap property: For all nodes i in the heap, value(parent(i))  value(i) with the exception of the root which has no parent.

Why a heap? Designed so that the PQ insert function can run in time proportional to the height of the tree: O(log(n)), rather than O(n). Because of the heap property, finding the min element is O(1). On the other hand, searching the heap for an arbitrary value is not a priority. That would still take O(n). –How do you solve that problem?

Insert & heapify up To insert a node, make it the last child. But now, we have probably violated the heap property, which we can restore by doing a “heapify up”. Heapify up: Starting with c, the child we just inserted: If value(c)  value(parent(c)), we’re done. Else: swap c with its parent, and continue up the heap at the new location of c. Example p. 169 What is the complexity?

Delete & heapify down How to remove smallest element, which is at the root. Remove the root, and immediately replace it with the last child. But, we may have just violated the heap property, so… Heapify down: starting with the root node r: If value(r)  values of both children, we’re done. Else: swap r with its smaller child, and continue down the heap at the new location of r. –(Why swap with smaller child; does it matter?)

Array, take 2 Earlier we saw that we didn’t want a sorted array representation. No need to keep all elements sorted to maintain heap property. Why is array attractive? –Insert/remove operations need to quickly find last child, which would be at end of array. O(1) –“Almost complete” binary tree: all elements in array contiguous from A[1..n]. Although internally represented as 1-D array, we still conceive of heap logically as a tree structure.

More on heaps Finish heap d.s. –Heap sort –How to build heap: from scratch more efficiently

Heap sort The desired ops for a PQ are enough to allow us to sort some list of items. –Insert(item) –removeMin() How to sort: –Insert items one at a time –Remove items one by one Analysis: (n inserts) + (n removeMin’s) –And we know that insert & remove both take O(log n) time. –(Recall that just finding an element is O(1) but need heapify.) –Total time is O(n log n). –More on sorting in Chapters 8 and 9.

Optimizing heap sort Doesn’t mean we’re perfecting it. Actually, it will still be O(n log n). The major improvement is in the insertion: we can bring this part down to O(n). Algorithm p. 176: bottomUpHeap(S) –Given a “sequence” of values, create a heap. 1.If size(S) < 2, return trivial heap. 2.Remove S[0] 3.H1 = bottomUpHeap (firstHalf(S)) 4.H2 = bottomUpHeap (secondHalf(S)) 5.H = tree with S[0] at root, and subtrees H1 and H2. 6.Heapify down H starting at S[0], and return H.

Build heap example Use the algorithm to build a heap out of: 5,4,7,3,2,6,1 –Not a base case –Remove S[0] = 5 –H1 = bottomUpHeap(4,7,3) Eventually creates a heap with 3 nodes. –H2 = bottomUpHeap(2,6,1) Eventually creates a heap with 3 nodes. –H = tree with 7 nodes with 5 at the root. –Need to heapify down. Try another example. Sometimes the 2 “halves” that we use in recursive call are not exactly the same size. No big deal.

Analysis of bUH Background: the classic way of creating a heap is to insert n elements one by one: O(n log n). We hope to show bottomUpHeap can do it in O(n). In the best case, no heapify down is needed. We create n heaps. Creating 1 takes constant time (just an assignment stmt and function call). n * const  Ω(n) What is different about worst case? –Need to heapify down. Count the number of swaps. –Consider case of 15 nodes. (height = 3) 1 node needs 3 swaps 2 nodes each need 2 swaps 4 nodes each need 1 swap –Consider case of 31 nodes. (height = 4)

Analysis (2) If we have n nodes, h = floor(log n). Total maximum # swap operations depends on h. –The formula is: the sum i = 1 to h of: (2 h – i nodes * i swaps per node) –Let’s work out the sum from i = 1 to h of… Sum (i 2 h – i ) = 2 h Sum (i 2 – i ) = 2 h Sum (i / 2 i ) –What is this summation? Consider this: Let S = 1/ / / /2 4 + … Then, S/2 = 1/ / /2 4 + … Subtracting, we obtain S/2 = 1/2 + 1/4 + 1/8 + 1/16 + … = 1. Therefore, S = 2. –Then, the # of swaps is  2 h * 2 = O(2 h ) = O(n).