Priority Queues (Chapter 6):

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
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.
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.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
FALL 2005CENG 213 Data Structures1 Review. FALL 2005CENG 213 Data Structures2 Collection of items Problems that must manage data by value. Some important.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
CSE373: Data Structures & Algorithms Priority Queues
Partially Ordered Data ,Heap,Binary Heap
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
BCA-II Data Structure Using C
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Chapter 11 Heap.
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Red Black Trees
Binary Search Tree (BST)
Lecture 16 Multiway Search Trees
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
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Hashing Exercises.
Source: Muangsin / Weiss
March 31 – Priority Queues and the heap
Bohyung Han CSE, POSTECH
ITEC 2620M Introduction to Data Structures
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
7/23/2009 Many thanks to David Sun for some of the included slides!
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CMSC 341: Data Structures Priority Queues – Binary Heaps
Chapter 6 Transform and Conquer.
Priority Queue & Heap CSCI 3110 Nan Chen.
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Search Sorted Array: Binary Search Linked List: Linear Search
CMSC 341 Lecture 14 Priority Queues & Heaps
Priority Queue and Binary Heap Neil Tang 02/12/2008
ITEC 2620M Introduction to Data Structures
Heaps and the Heapsort Heaps and priority queues
Computer Science 2 Heaps.
CSE 214 – Computer Science II B-Trees
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
CE 221 Data Structures and Algorithms
Priority Queues (Chapter 6.6):
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Data Structures and Analysis (COMP 410)
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
การวิเคราะห์และออกแบบขั้นตอนวิธี
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Presentation transcript:

Priority Queues (Chapter 6): Abstract model of Priority Queues Priority Queue Get the one with the highest priority insert the minimum one (or equivalently, maximum) a black box 5/5/2019 IT 279

Let’s cleaning up some confusion. Is Binary Search Tree a priority queue? Binary Search Tree Get the one with the highest priority insert No! It is not, but nothing wrong to use it in the black box. the minimum one (or equivalently, maximum) 5/5/2019 IT 279

Items with higher priority will be treated first. Stacks (FILO), queues (FIFO) are Priority Queues Arrays are not, since we can randomly access the items in an array The priority of the item may be fixed or changed over the time. 5/5/2019 IT 279

Using a Binary Search Tree is overkilled. Why? 17 Get the one with the highest priority insert 14 19 12 15 18 11 13 the minimum one (or equivalently, maximum) Items are totally ordered, but we don’t really this information (nothing comes free) 5/5/2019 IT 279

Our goals: 1. Can get the minimum one efficiently 2. and low maintenance 11 9 16 12 11 12 Get the minimum one 9 13 insert 13 12 14 17 15 19 18 13 14 17 15 19 18 16 14 17 15 19 18 Binary Heap 5/5/2019 IT 279

Binary Heap Using (abstractly) binary tree structure For every node in the tree, its key is smaller than (or equal to) the keys of its two children. Therefore, for every node in the tree, its data is the smallest one among data stored in the sub-trees (the two children). Different from BST, not every key in the left-sub-tree is less than every key in the right-sub-tree. Loosing this requirement, we gain some benefits. 5/5/2019 IT 279

A Binary Heap but we can organize it in a better way without too much extra cost. 1 4 6 5 9 23 10 25 8 14 11 25 12 29 28 12 17 20 15 22 29 30 19 36 13 29 23 30 37 5/5/2019 IT 279

A Binary Heap in complete binary tree 10 12 13 15 14 16 17 23 22 21 18 19 24 25 26 27 32 29 30 25 26 37 pack 5/5/2019 IT 279

A Binary Heap implemented in an array 10 1 12 13 2 3 15 14 16 17 4 5 6 7 23 22 21 28 39 24 25 42 10 11 15 8 9 12 13 14 27 32 29 40 35 36 37 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1st child (left-child) of i is 2*i 2nd child (right-child) of i is 2*i+1 The parent of i is i / 2 double the size of the array if the space ran out 5/5/2019 IT 279

Heap operation: insert an item 11 10 12 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 37 11 5/5/2019 IT 279

Heap operation: insert an item 11 10 12 13 15 14 16 17 23 22 21 39 24 25 42 11 27 32 29 40 35 36 37 28 5/5/2019 IT 279

Heap operation: insert an item 11 10 12 13 15 11 16 17 23 22 21 39 24 25 42 14 27 32 29 40 35 36 37 28 5/5/2019 IT 279

Heap operation: insert an item 11 10 11 13 15 12 16 17 23 22 21 39 24 25 42 14 27 32 29 40 35 36 37 28 5/5/2019 IT 279

Heap operation: delete the minimum 10 12 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 37 5/5/2019 IT 279

Heap operation: delete the minimum 10 12 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 37 5/5/2019 IT 279

Heap operation: delete the minimum 10 37 12 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 5/5/2019 IT 279

Heap operation: delete the minimum 10 12 37 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 5/5/2019 IT 279

Heap operation: delete the minimum 10 12 14 13 15 37 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 5/5/2019 IT 279

Heap operation: delete the minimum 10 12 14 13 15 16 21 17 23 22 37 28 39 24 25 42 27 32 29 40 35 36 5/5/2019 IT 279

Heap operation: delete the minimum 10 12 14 13 15 16 21 17 23 22 35 28 39 24 25 42 27 32 29 40 37 36 5/5/2019 IT 279

Time complexity for insertion best case: O(1) worst case: O(log n) 14 log n 15 21 23 22 37 25 27 32 29 40 35 36 30 27 average case: 5/5/2019 IT 279

Time complexity of Building a heap Insert them one by one into the empty heap What is the cost? best case: O(n) average case: O(n) worst case: O(n log n) Can we improve the worst case? 34 15 30 2 23 9 25 6 32 5 40 11 25 21 7 5/5/2019 IT 279

Percolating a non-heap Worst case: O(n) 34 2 15 2 5 30 7 7 2 15 6 5 23 9 25 7 30 21 6 23 9 21 15 6 32 5 23 40 11 31 30 21 7 25 15 32 34 40 11 31 30 25 Binary Heap 5/5/2019 IT 279

Percolating a non-heap Worst case: O(S) S = Sum of heights of all nodes h O(S) = O(n) 5/5/2019 IT 279