David Stotts Computer Science Department UNC Chapel Hill.

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)
Trees Types and Operations
Heaps, Heap Sort, and Priority Queues. Sorting III / Slide 2 Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two.
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Heapsort Based off slides by: David Matuszek
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
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 Binary Heap.
data ordered along paths from root to leaf
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a 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.
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.
David Stotts Computer Science Department UNC Chapel Hill.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
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.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
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.
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.
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)
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
Priority Queues and Heaps
Data Structures and Analysis (COMP 410)
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
CMSC 341: Data Structures Priority Queues – Binary Heaps
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
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) A heap.
Data Structures and Analysis (COMP 410)
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Priority Queues & Heaps
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

David Stotts Computer Science Department UNC Chapel Hill

Binary Heaps and Priority Queues

Consider a Queue (FIFO) with extra info along with each element… a priority tail head

Consider a Queue (FIFO) with extra info along with each element… a priority tail head ins(6.4,1) tail head 6.4 1

How can we make a priority queue?  Linked List ◦ O(1) enq as normal as tail ◦ O(N) deq traverse list to find smallest  Sorted List ◦ O(N) enq sorted, find the right spot ◦ O(1) deq as normal, from head

How can we make a priority queue?  BST / AVLT / SPLT ◦ O(log N) insert as normal ◦ O(log N) findMin Trees are overkill, they have structure and complexity needed for other ops that PQUE doesn’t need We have a specialized structure that is faster than O(log N) a tree gives. Binary Heap

 Binary Heap is NOT same as Priority Queue ◦ BHEAP is so often used to implement PQUE that they get used interchangeably  Binary Heap is NOT the same thing as the Heap used in implementing programming languages ◦ PL Heap is a means of organizing memory so that objects can be allocated dynamically with “new” ◦ Not even related

BHEAP has 2 properties 1. Structure property: a binary tree, completely filled except for the last row of leaves; we fill that L to R ( called a complete binary tree ) 2. Heap-order property: ◦ minimum element in heap is at root ◦ every child >= parent ◦ each path is ordered list, root to leaf small to large * Every subtree of a BHEAP is also a BHEAP.

Structure property Complete binary tree Not complete

Heap-order property every child >= parent Min at root every path root to leaf is an ordered sequence small to large

Heap-order property Every subtree is a BHEAP

To fill array, breadth-first across tree root in slot 1: 3 then level 1 in slots 2,3: 7, 4 level 2 in slots 4,5,6,7: 16,12,11,9 next level in slots 8, 9, 10 …15: 31,18,21 Next item causes slot 11 to fill

To infer tree structure from array node in slot 1: 3 Parent: floor(1/2) is 0 (root, no parent) Lchild: 2*1 is 2, slot 2 has 7 Rchild: (2*1)+1 is 3, slot 3 has 4

To infer tree structure from array node in slot 4 is 16 Parent: floor(4/2) is 2, slot 2 has 7 Lchild: 4*2 is 8, slot 8 has 31 Rchild: (4*2)+1 is 9, slot 9 has 18

node in slot 4 is 16 Parent: floor(4/2) is 2, slot 2 has 7 Lchild: 4*2 is 8, slot 8 has 31 Rchild: (4*2)+1 is 9, slot 9 has 18 half back double forward parent L-child R-child

Next item causes slot 11 to fill this maintains structure property insert ( 17 ) still has heap-order property ? We are good ( by luck of it being 17, > parent 12 ) 17

insert ( 10 ) still has heap-order property ? We are not good ( since 10 < parent 12 ) swap 10 with parent 12 Good here And good here Slot 11 parent is floor(11/2) = 5

insert ( 2 ) Check for heap-order, swap upwards repeat until we get it In the array representation… Slot 11 parent is floor(11/2) = 5 Slot 5 parent is floor(5/2) = 2 Slot 2 parent is floor(2/2) = Slot 11 parent is floor(11/2) = 5

 Bubble up element ◦ Each swap takes 3 assignments ◦ O(log N) swaps ◦ O(3 log N) is O(log N)  Book mentions “Bubble down hole” ◦ Pull root element aside ◦ Move child up into the “hole” ◦ Repeat until hea-order achieved ◦ Put root val into hole

insert ( 2 ) Check for heap-order, if 2 were put in the hole Move parent down into hole, check 2 again, and again In the array representation Move values into the hole, hole moves to wards slot 1 12 temp 31 2

insert ◦ put new val in next open slot in array/tree ◦ swap/bubble up towards root until heap- order is achieved ◦ O(log N) as it follows path to root (height) getMin ◦ just read the value at root (array slot 1) ◦ O(1) complexity

delMin ◦ Remove root node val (it is min val) ◦ Leaves a “hole” at root node ◦ Pull out val in last leaf (eliminates a node) ◦ See if it fits in root hole  If so, put leaf val into root hole  If not, move hole down to smaller child  repeat

delMin ( ) Remove root value Save out last element Bubble hole down from root Stop when last element causes heap-order in the hole Array representation temp 12

 Remove root node: O(1)  Save out last element: O(1)  Bubble down the hole: O(log N) ◦ One copy per bubble move, +1 at end  Swap down method: O(log N), but… ◦ 3 assigns per swap, O(log N) swaps ◦ O(3 log N) is O(log N)

AverageWorst case searchO(n) insertO(1) *O(log n) deleteO(log n) findMinO(1) * ½ to ¾ of the nodes are in the bottom 2 layers. Avg insert time is 2.67

BHEAP is faster than BST for finding min O(1) to just get root val TANSTAAFL Can’t get a full sort out of a BHEAP directly like you can out of a BST O(N) to traverse a BST to get a sort How can we get a sort from a BHEAP ? Array is NOT sorted

There is a method for building an initial heap from a list of N values that is O(N) Structural Property 1. First load the N values into an array, from slot 1 to last (gives structural property) Heap-order 1. Start with parent of last node, and bubble down (like in delete) 2. Go backwards to root, sequentially, and do this for each node

 Build heap : O(N)  Retrieve sequence: ◦ N * delMin( ) ◦ is N * O(log N)  Final sort: O(N) + O(N log N)

Beyond this is just templates