CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.

Slides:



Advertisements
Similar presentations
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COL 106 Shweta Agrawal and Amit Kumar
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
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
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.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Nick Harvey & Kevin Zatloukal
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
1 CS211, Lecture 20 Priority queues and Heaps Readings: Weiss, sec. 6.9, secs When they've got two queues going, there's never any queue!
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.
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.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
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 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
§3 Binary Heap 1. Structure Property: 【 Definition 】 A binary tree with n nodes and height h is complete iff its nodes correspond to the nodes numbered.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Priority Queues and Disjoint Sets CSCI 2720 Spring 2005.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Chapter 21 Binary Heap.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Cpt S 223 – Advanced Data Structures Priority Queues
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
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:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
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.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
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.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
Heaps & Priority Queues
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
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.
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.
§3 Binary Tree Traversals —— visit each node exactly once L ::= move left ; R ::= move right ; V ::= visit the node. We will always traverse the left.
CS 367 Introduction to Data Structures Lecture 8.
1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003.
CSC 2300 Data Structures & Algorithms March 13, 2007 Chapter 6. Priority Queues.
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)
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:
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Priority Queues (Heaps)
CSCI2100 Data Structures Tutorial 7
CMSC 341: Data Structures Priority Queues – Binary Heaps
Priority Queue and Binary Heap Neil Tang 02/12/2008
CE 221 Data Structures and Algorithms
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Priority Queues.
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Priority Queues Binary Heaps
Presentation transcript:

CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int MaxElements );  void Insert( ElementType X, PriorityQueue H );  ElementType DeleteMin( PriorityQueue H );  ElementType FindMin( PriorityQueue H ); —— delete the element with the highest \ lowest priority 1/15

§2 Simple Implementations  Array : Insertion — add one item at the end ~  ( 1 ) Deletion — find the largest \ smallest key ~  ( n ) remove the item and shift array ~ O( n )  Linked List : Insertion — add to the front of the chain ~  ( 1 ) Deletion — find the largest \ smallest key ~  ( n ) remove the item ~  ( 1 )  Ordered Array : Insertion — find the proper position ~ O( n ) shift array and add the item ~ O( n ) Deletion — remove the first \ last item ~  ( 1 )  Ordered Linked List : Insertion — find the proper position ~ O( n ) add the item ~  ( 1 ) Deletion — remove the first \ last item ~  ( 1 ) Better since there are never more deletions than insertions 2/15

 Binary Search Tree : §2 Simple Implementations Ah! That’s a good idea! Both insertion and deletion will take O(log N) only. Well, insertions are random, but deletions are NOT. We are supposed to delete The minimum element only. Oh, right, then we must always delete from the left subtrees…. But hey, what if we keep a balanced tree? Hey you are getting smarter! Yes a balanced tree such as AVL tree is not a bad idea since only a constant factor will be added to the run time. However… Oh no… what’s wrong? There are many operations related to AVL tree that we don’t really need for a priority queue. Besides, pointers are always dangerous. I bet you have a better option? Now you begin to know me 3/15

§3 Binary Heap 1. Structure Property: 【 Definition 】 A binary tree with n nodes and height h is complete iff its nodes correspond to the nodes numbered from 1 to n in the perfect binary tree of height h A complete binary tree of height h has between andnodes. 2h2h 2 h+1  1h =  log N   Array Representation : BT [ n + 1 ] ( BT [ 0 ] is not used) D HI E J FG BC A BT 01 A 2 B 3 C 4 D 5 E 6 F 7 G 8 H 9 I 10 J /15

§3 Binary Heap 【 Lemma 】 If a complete binary tree with n nodes is represented sequentially, then for any node with index i, 1  i  n, we have: 5/15

§3 Binary Heap PriorityQueue Initialize( int MaxElements ) { PriorityQueue H; if ( MaxElements < MinPQSize ) return Error( "Priority queue size is too small" ); H = malloc( sizeof ( struct HeapStruct ) ); if ( H ==NULL ) return FatalError( "Out of space!!!" ); /* Allocate the array plus one extra for sentinel */ H->Elements = malloc(( MaxElements + 1 ) * sizeof( ElementType )); if ( H->Elements == NULL ) return FatalError( "Out of space!!!" ); H->Capacity = MaxElements; H->Size = 0; H->Elements[ 0 ] = MinData; /* set the sentinel */ return H; } 6/15

§3 Binary Heap 2. Heap Order Property: 【 Definition 】 A min tree is a tree in which the key value in each node is no larger than the key values in its children (if any). A min heap is a complete binary tree that is also a min tree. Note: Analogously, we can declare a max heap by changing the heap order property [1] [2][3] [4] A max heap [1] [2][3] [4] A min heap The largest keyThe smallest key 7/15

§3 Binary Heap 3. Basic Heap Operations:  insertion [1] [2][3] [4] 18 [5][6]  Sketch of the idea: The only possible position for a new node since a heap must be a complete binary tree. Case 1 : new_item = < Case 2 : new_item = > < Case 3 : new_item = > > 9 8/15

§3 Binary Heap /* H->Element[ 0 ] is a sentinel */ void Insert( ElementType X, PriorityQueue H ) { int i; if ( IsFull( H ) ) { Error( "Priority queue is full" ); return; } for ( i = ++H->Size; H->Elements[ i / 2 ] > X; i /= 2 ) H->Elements[ i ] = H->Elements[ i / 2 ]; H->Elements[ i ] = X; } Percolate up Faster than swap H->Element[ 0 ] is a sentinel that is no larger than the minimum element in the heap. T (N) = O ( log N ) 9/15

§3 Binary Heap  DeleteMin  Sketch of the idea: [1] [2][3] [4] 18 [5] The node which must be removed to keep a complete binary tree.  move 18 up to the root 18  find the smaller child of < < 15 Ah! That’s simple -- we only have to delete the root node... And re-arrange the rest of the tree so that it’s still a min heap. T (N) = O ( log N ) 10/15

§3 Binary Heap ElementType DeleteMin( PriorityQueue H ) { int i, Child; ElementType MinElement, LastElement; if ( IsEmpty( H ) ) { Error( "Priority queue is empty" ); return H->Elements[ 0 ]; } MinElement = H->Elements[ 1 ]; /* save the min element */ LastElement = H->Elements[ H->Size-- ]; /* take last and reset size */ for ( i = 1; i * 2 Size; i = Child ) { /* Find smaller child */ Child = i * 2; if (Child != H->Size && H->Elements[Child+1] Elements[Child]) Child++; if ( LastElement > H->Elements[ Child ] ) /* Percolate one level */ H->Elements[ i ] = H->Elements[ Child ]; else break; /* find the proper position */ } H->Elements[ i ] = LastElement; return MinElement; } Percolate down What if this condition is omitted? Can we remove it by adding another sentinel? 11/15

§3 Binary Heap 4. Other Heap Operations: Note: Finding any key except the minimum one will have to take a linear scan through the entire heap.  DecreaseKey ( P, , H ) Lower the value of the key in the heap H at position P by a positive amount of  ……so my programs can run with highest priority. sys. admin.  IncreaseKey ( P, , H ) Percolate up sys. admin. Increases the value of the key in the heap H at position P by a positive amount of  ……drop the priority of a process that is consuming excessive CPU time. Percolate down 12/15

§3 Binary Heap  Delete ( P, H ) Remove the node at position P from the heap H …… delete the process that is terminated (abnormally) by a user. sys. admin. DecreaseKey(P, , H); DeleteMin(H)  BuildHeap ( H ) Place N input keys into an empty heap H. sys. admin. N successive Insertions ? Nehhhhh that would be toooo slow ! , 80, 40, 30, 10, 70, 110, 100, 20, 90, 60, 50, 120, 140, 130 PercolateDown (7) PercolateDown (6) PercolateDown (5) PercolateDown (4) PercolateDown (3) PercolateDown (2) PercolateDown (1) T (N) = ? 13/15

§3 Binary Heap 【 Theorem 】 For the perfect binary tree of height h containing 2 h+1  1 nodes, the sum of the heights of the nodes is 2 h+1  1  (h + 1). T ( N ) = O ( N ) §4 Applications of Priority Queues 〖 Example 〗 Given a list of N elements and an integer k. Find the kth largest element. How many methods can you think of to solve this problem? What are their complexities? Home work: p , 5.3, 5.4 A test case for operations on a heap 14/15

§5 d-Heaps ---- All nodes have d children heap Question: Shall we make d as large as possible? Note:  DeleteMin will take d  1 comparisons to find the smallest child. Hence the total time complexity would be O(d log d N).  *2 or /2 is merely a bit shift, but *d or /d is not.  When the priority queue is too large to fit entirely in main memory, a d-heap will become interesting. Note:  DeleteMin will take d  1 comparisons to find the smallest child. Hence the total time complexity would be O(d log d N).  *2 or /2 is merely a bit shift, but *d or /d is not.  When the priority queue is too large to fit entirely in main memory, a d-heap will become interesting. Home work: p Index computations for parents and children in a d-heap 15/15