Priority Queues (Heaps)

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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.
Heaps, Heap Sort, and Priority Queues
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.
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.
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.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Lecture 7 Heaps and Priority Queues. Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting.
Lecture 7 Heaps and Priority Queues. Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
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.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
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.
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 (Heaps)
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.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
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.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
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)  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.
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)
"Teachers open the door, but you must enter by yourself. "
Partially Ordered Data ,Heap,Binary Heap
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues (Heaps)
Source: Muangsin / Weiss
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heapsort.
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Priority Queues Sections 6.1 to 6.5.
Binary Heaps What is a Binary Heap?
CSCI2100 Data Structures Tutorial 7
Heaps, Heap Sort, and Priority Queues
CMSC 341: Data Structures Priority Queues – Binary Heaps
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Priority Queue & Heap CSCI 3110 Nan Chen.
CMSC 341 Lecture 14 Priority Queues & Heaps
Tree Representation Heap.
"Teachers open the door, but you must enter by yourself. "
CE 221 Data Structures and Algorithms
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Sorting Dr. Yingwu Zhu.
Heapsort.
Priority Queues (Heaps)
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Data Structures for Shaping and Scheduling
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Presentation transcript:

Priority Queues (Heaps) Sections 6.1 to 6.5

The Priority Queue ADT DeleteMin log N time Insert Other operations FindMin Constant time Initialize N time 2 2

Applications of Priority Queues Any event/job management that assign priority to events/jobs In Operating Systems Scheduling jobs In Simulators Scheduling the next event (smallest event time)

Priority Queue Implementation Implemented as adaptor class around Linked lists O(N) worst-case time on either insert() or deleteMin() Binary Search Trees O(log(N)) average time on insert() and delete() Overkill: all elements are sorted However, we only need the minimum element Heaps This is what we’ll study and use to implement Priority Queues O(logN) worst case for both insertion and delete operations

Partially Ordered Trees A partially ordered tree (POT) is a tree T such that: There is an order relation <= defined for the vertices of T For any vertex p and any child c of p, p <= c Consequences: The smallest element in a POT is the root No conclusion can be drawn about the order of children

Binary Heaps A binary heap is a partially ordered complete binary tree The tree is completely filled on all levels except possibly the lowest. In a more general d-Heap A parent node can have d children We simply refer to binary heaps as heaps root 3 2 4 5

Vector Representation of Complete Binary Tree Storing elements in vector in level-order Parent of v[k] = v[k/2] Left child of v[k] = v[2*k] Right child of v[k] = v[2*k + 1] root R l r ll lr rl rr 7 6 5 4 3 2 1 R l r ll lr rl rr

Heap example Parent of v[k] = v[k/2] Left child of v[k] = v[2*k] Right child of v[k] = v[2*k + 1]

Examples Which one is a heap?

Implementation of Priority Queue (heap)

Insertion Example: insert(14)

Basic Heap Operations: insert(x) Maintain the complete binary tree property and fix any problem with the partially ordered tree property Create a leaf at the end Repeat Locate parent if POT not satisfied Swap with parent else Stop Insert x into its final location

Implementation of insert

deleteMin() example 31 13 14 16 19 21 68 65 26 32 31

deleteMin() Example (Cont’d) 31 31 31

Basic Heap Operations: deleteMin() Replace root with the last leaf ( last element in the array representation This maintains the complete binary tree property but may violate the partially ordered tree property Repeat Find the smaller child of the “hole” If POT not satisfied Swap hole and smaller child else Stop

Implementation of deleteMin()

Implementation of deleteMin()

Constructor Construct heap from a collection of item How to? Naïve methods Insert() each element Worst-case time: O(N(logN)) We show an approach taking O(N) worst-case Basic idea First insert all elements into the tree without worrying about POT Then, adjust the tree to satisfy POT, starting from the bottom

Constructor

Example percolateDown(7) percolateDown(6) percolateDown(5)

percolateDown(4) percolateDown(3) percolateDown(2) percolateDown(1)

Complexity Analysis Consider a tree of height h with 2h-1 nodes Time = 1•(h) + 2•(h-1) + 4•(h-2) + ... + 2h-1•1 = i=1h 2h-i i = 2h i=1h i/2i = 2h O(1) = O(2h) = O(N) Proof for i=1h i/2i = O(1) i/2i ≤ ∫i-1i (x+1)/2x dx i=1h i/2i ≤ i=1∞ i/2i ≤ ∫0∞ (x+1)/2x dx Note: ∫u dv = uv - ∫v du, with dv = 2x and u = x and ∫2-x dx = -2-x/ln 2 ∫0∞ (x+1)/2x dx = -x 2-x/ln 2|0∞ + 1/ln 2 ∫0∞ 2-x dx + ∫0∞ 2-x dx = -2-x/ln2 2|0∞ - 2-x/ln 2|0∞ = (1 + 1/ln 2)/ln 2 = O(1) 23 23

Alternate Proof Prove i=1h i/2i = O(1) i=0∞ xi = 1/(1-x) when |x| < 1 Differentiating both sides with respect to x we get i=0∞ i xi-1 = 1/(1-x)2 So, i=0∞ i xi = x/(1-x)2 Substituting x = 1/2 above gives i=0∞ i 2-i = 0.5/(1-0.5)2 = 2 = O(1) 24 24

C++ STL Priority Queues priority_queue class template Implements deleteMax instead of deleteMin in default MaxHeap instead of MinHeap Template Item type container type (default vector) comparator (default less) Associative queue operations Void push(t) void pop() T& top() void clear() bool empty()