CE 221 Data Structures and Algorithms

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.
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.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
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.
Priority queues CS310 – Data Structures Professor Roch Weiss, Chapter 6.9, 21 All figures marked with a chapter and section number are copyrighted © 2006.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
BST Data Structure A BST node contains: A BST contains
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
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. 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.
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.
§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.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues (Heaps)
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
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.
CE 221 Data Structures and Algorithms
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
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.
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.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
1 Priority Queues (Heaps)  Sections 6.1 to Priority Queues  Regular queues which supports –First In, First Out –Enqueue(): add a new element.
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 Binary Heaps Fundamental Data Structures and Algorithms Peter Lee February 4, 2003.
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)
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues (Heaps)
October 30th – Priority QUeues
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
CSCI2100 Data Structures Tutorial 7
Heaps, Heap Sort, and Priority Queues
CMSC 341: Data Structures Priority Queues – Binary Heaps
Chapter 8 – Binary Search Tree
Priority Queue & Heap CSCI 3110 Nan Chen.
CMSC 341 Lecture 14 Priority Queues & Heaps
Priority Queue and Binary Heap Neil Tang 02/12/2008
Heaps and the Heapsort Heaps and priority queues
Priority Queues (Chapter 6.6):
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Priority Queues & Heaps
CSC 380: Design and Analysis of Algorithms
Priority Queues (Heaps)
Priority Queues (Chapter 6):
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Priority Queues (Heaps)
Presentation transcript:

CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 Izmir University of Economics 1

Source: Muangsin / Weiss Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value (key) of each object (smaller value higher priority, or higher value higher priority) Example Applications: printer -> print (dequeue) the shortest document first operating system -> run (dequeue) the shortest job first normal queue -> dequeue the first enqueued element first Source: Muangsin / Weiss

Priority Queue (Heap) Operations deleteMin insert insert (enqueue) deleteMin (dequeue) smaller value higher priority Find / save the minimum element, delete it from structure and return it Source: Muangsin / Weiss

Implementation using Linked List Unsorted linked list insert takes O(1) time deleteMin takes O(N) time Sorted linked list insert takes O(N) time deleteMin takes O(1) time Source: Muangsin / Weiss

Implementation using Binary Search Tree insert takes O(log N) time on the average deleteMin takes O(log N) time on the average support other operations that are not required by priority queue (for example, findMax) deleteMin operations make the tree unbalanced Source: Muangsin / Weiss

Binary Heap Implementation Property 1: Structure Property Binary tree & completely filled (bottom level is filled from left to right) (complete binary tree) if height is h, size between 2h (bottom level has only one node) and 2h+1-1 A C G F B E J D H I Source: Muangsin / Weiss

Array Implementation of Binary Heap C G F B E J D H I left child is in position 2i right child is in position (2i+1) parent is in position floor(i/2) or integer division in C A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 Source: Muangsin / Weiss

Property 2: Heap Order Property (for Minimum Heap) Any node is smaller than (or equal to) all of its children (any subtree is a heap) Smallest element is at the root (findMin take O(1) time) 13 16 68 19 21 31 32 24 65 26 Source: Muangsin / Weiss

Source: Muangsin / Weiss Insert Create a hole in the next available location Move the hole up (swap with its parent) until data can be placed in the hole without violating the heap order property (called percolate up) 13 16 68 19 21 31 32 24 65 26 13 16 68 19 21 32 24 65 26 31 Source: Muangsin / Weiss

Source: Muangsin / Weiss Insert insert 14 13 16 68 19 21 31 32 24 65 26 13 16 68 19 21 32 24 65 26 31 Percolate Up -> move the place to put 14 up (move its parent down) until its parent <= 14 Source: Muangsin / Weiss

Source: Muangsin / Weiss Insert 13 16 68 19 21 31 32 24 65 26 13 16 68 19 21 31 32 24 65 26 14 Source: Muangsin / Weiss

Source: Muangsin / Weiss deleteMin Create a hole at the root Move the hole down (swap with the smaller one of its children) until the last element of the heap can be placed in the hole without violating the heap order property (called percolate down) 13 16 68 19 21 31 32 65 26 14 16 68 19 21 32 65 26 14 31 Source: Muangsin / Weiss

Source: Muangsin / Weiss deleteMin 13 16 68 19 21 31 32 65 26 14 16 68 19 21 32 65 26 14 31 Percolate Down -> move the place to put 31 down (move its smaller child up) until its children >= 31 Source: Muangsin / Weiss

Source: Muangsin / Weiss deleteMin 16 68 19 21 32 65 26 14 31 16 68 19 21 32 65 26 14 31 Source: Muangsin / Weiss

Source: Muangsin / Weiss deleteMin 16 68 19 21 32 65 14 31 26 16 68 19 21 32 65 26 14 31 Source: Muangsin / Weiss

Source: Muangsin / Weiss Running Time insert worst case: takes O(log N) time, moves an element from the bottom to the top on average: takes a constant time (2.607 comparisons), moves an element up 1.607 levels deleteMin worst case: takes O(log N) time on average: takes O(log N) time (element that is placed at the root is large, so it is percolated almost to the bottom) Source: Muangsin / Weiss

Implementation in C - HeapStruct #define MinPQSize (10) #define MinData (-32767) typedef int ElementType; struct HeapStruct { int Capacity; int Size; ElementType *Elements; }; typedef struct HeapStruct *PriorityQueue;

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

Implementation in C – IsEmpty, IsFull int IsEmpty( PriorityQueue H ) { return H->Size == 0; } int IsFull( PriorityQueue H ) return H->Size == H->Capacity;

Implementation in C – Insert /* 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;

Implementation in C – DeleteMin ElementType DeleteMin( PriorityQueue H ) { ElementType MinElement; if( IsEmpty( H ) ) Error( "Priority queue is empty" ); return H->Elements[0]; } MinElement = H->Elements[ 1 ]; H->Elements[ 1 ] = H->Elements[ H->Size-- ]; percolateDown( H, 1 ); return MinElement;

Implementation in C – percolateDown void percolateDown( PriorityQueue H, int hole ) { int child; ElementType tmp = H->Elements[ hole ]; for( ; hole * 2 <= H->Size; hole = child ) /* Find smaller child */ child = hole * 2; if (child != H->Size && H->Elements[child+1]<H->Elements[child]) child++; /* Percolate one level */ if( tmp > H->Elements[child] ) H->Elements[ hole ] = H->Elements[ child ]; else break; } H->Elements[ hole ] = tmp;

Building a Heap Sometimes it is required to construct it from an initial collection of items O(NlogN) in the worst case. But insertions take O(1) on the average. Hence the question: is it possible to do any better?

buildHeap Algorithm General Algorithm Place the N items into the tree in any order, maintaining the structure property. Call buildHeap void buildHeap( PriorityQueue H, int N ) { int i; for( i = N / 2; i > 0; i-- ) percolateDown( H, i ); }

buildHeap Example - I after percolateDown(7) initial heap after

buildHeap Example - II after percolateDown(4) after percolateDown(3)

Complexity of buildHeap The number of dashed lines must be bounded which can simply be done by computing the sum of the heights of all the nodes in the heap. Theorem: For a perfect binary tree of height h with N=2h+1-1 nodes, this sum is 2h+1-1-(h+1). Proof: number of nodes in a complete tree of height h is less than or equal to the the number of nodes in a perfect binary tree of the same height. Therefore, O(N)

Homework Assignments 6.2, 6.3, 6.4, 6.10.a, 6.38 You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me.