Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Similar documents
Binary Heaps. CSE 373 Data Structures Lecture 11

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Recall: Properties of B-Trees

Administration CSE 326: Data Structures

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

CS350: Data Structures Heaps and Priority Queues

9. Heap : Priority Queue

Data Structures and Algorithms

CSCI2100B Data Structures Heaps

Priority Queues. Binary Heaps

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

CMSC 341 Lecture 14: Priority Queues, Heaps

CE 221 Data Structures and Algorithms

Priority queues. Priority queues. Priority queue operations

Priority Queues Heaps Heapsort

Algorithms and Data Structures

Priority Queues Heaps Heapsort

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

The priority is indicated by a number, the lower the number - the higher the priority.

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

Priority Queues (Heaps)

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Data Structures Lesson 9

CSE 373: Data Structures and Algorithms

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Priority Queues (Heaps)

Priority Queues and Heaps

Sorting and Searching

Heap Model. specialized queue required heap (priority queue) provides at least

Sorting and Searching

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Heaps Goodrich, Tamassia. Heaps 1

Programming II (CS300)

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

CSE332: Data Abstractions Lecture 4: Priority Queues; Heaps. James Fogarty Winter 2012

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

Priority Queues and Binary Heaps

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

CSE373: Data Structures & Algorithms Priority Queues

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Priority Queues. 04/10/03 Lecture 22 1

Figure 1: A complete binary tree.

Figure 1: A complete binary tree.

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Balanced Search Trees

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

UNIT III BALANCED SEARCH TREES AND INDEXING

Module 2: Priority Queues

Module 2: Priority Queues

CS 315 April 1. Goals: Heap (Chapter 6) continued review of Algorithms for Insert DeleteMin. algoritms for decrasekey increasekey Build-heap

Binary Heaps in Dynamic Arrays

Heaps, Heap Sort, and Priority Queues.

Heaps. Heaps. A heap is a complete binary tree.

CS350: Data Structures Binary Search Trees

Basic Data Structures (Version 7) Name:

8. Binary Search Tree

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

Heaps. 2/13/2006 Heaps 1

Stores a collection of elements each with an associated key value

Data Structures in Java

Programming II (CS300)

Today s Outline. The One Page Cheat Sheet. Simplifying Recurrences. Set Notation. Set Notation. Priority Queues. O( 2 n ) O( n 3 )

Data Structures Week #8. Heaps (Priority Queues)

Priority queues. Priority queues. Priority queue operations

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CS 350 : Data Structures Binary Search Trees

COMP : Trees. COMP20012 Trees 219

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Tables, Priority Queues, Heaps

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

Binary Trees, Binary Search Trees

CMSC 341. Binary Heaps. Priority Queues

csci 210: Data Structures Priority Queues and Heaps

Adam Blank Lecture 3 Winter 2017 CSE 332. Data Structures and Parallelism

Analysis of Algorithms

Heaps and Priority Queues

Design and Analysis of Algorithms

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Heaps and Priority Queues

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

Algorithms and Data Structures

Dictionaries. Priority Queues

Binary Search Trees (10.1) Dictionary ADT (9.5.1)

Section 4 SOLUTION: AVL Trees & B-Trees

Operations. Priority Queues & Heaps. Prio-Q as Array. Niave Solution: Prio-Q As Array. Prio-Q as Sorted Array. Prio-Q as Sorted Array

CSE 214 Computer Science II Heaps and Priority Queues

CMSC 341 Lecture 15 Leftist Heaps

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Transcription:

Readings Priority Queues & Binary Heaps Chapter Section.-. CSE Data Structures Winter 00 Binary Heaps FindMin Problem Quickly find the smallest (or highest priority) item in a set Applications: Operating system needs to schedule jobs according to priority instead of FIFO Event simulation (bank customers arriving and departing, ordered according to when the event happened) Find student with highest grade, employee with highest salary etc. Find most important customer waiting in line Priority Queue ADT Priority Queue can efficiently do: FindMin() Returns minimum but does not delete it DeleteMin( ) Returns minimum and deletes it Insert (k) In GT Insert (k,x) where k is the key and x the. In all algorithms the important part is the key, a comparable item. We ll skip the. size() and isempty() Binary Heaps Binary Heaps List implementation of a Priority Queue What if we use unsorted lists: FindMin and DeleteMin are O(n) In fact you have to go through the whole list Insert(k) is O() What if we used sorted lists FindMin and DeleteMin are O() Be careful if we want both Min and Max (circular array or doubly linked list) Insert(k) is O(n) Binary Heaps BST implementation of a Priority Queue Worst case (degenerate tree) FindMin, DeleteMin and Insert (k) are all O(n) Best case (completely balanced BST) FindMin, DeleteMin and Insert (k) are all O(logn) Balanced BSTs FindMin, DeleteMin and Insert (k) are all O(logn) Binary Heaps

Better than a speeding BST Can we do better than Balanced Binary Search Trees Very limited requirements: Insert, FindMin, DeleteMin. The goals are: FindMin is O() Insert is O(log N) DeleteMin is O(log N) Binary Heaps Binary Heaps A binary heap is a binary tree (NOT a BST) that is: Complete: the tree is completely filled except possibly the bottom level, which is filled from left to right Satisfies the heap order property every node is less than or equal to its children or every node is greater than or equal to its children The root node is always the smallest node or the largest, depending on the heap order Binary Heaps Heap order property A heap provides limited ordering information Each path is sorted, but the subtrees are not sorted relative to each other A binary heap is NOT a binary search tree 0-0 These are all valid binary heaps (minimum) Binary Heaps Binary Heap vs Binary Search Tree Binary Heap 0 min Parent is less than both left and right children min Binary Search Tree 0 Parent is greater than left child, less than right child Binary Heaps 0 Structure property Examples A binary heap is a complete tree All nodes are in use except for possibly the right end of the bottom row complete tree, heap order is "max" not complete complete tree, heap order is "min" complete tree, but min heap order is broken Binary Heaps Binary Heaps

Array Implementation of Heaps Root node = A[] Children of A[i] = A[i], A[i + ] Keep track of current size N (number of nodes) index - 0 N = Binary Heaps FindMin and DeleteMin FindMin: Easy! Return root A[] Run time = DeleteMin: Delete (and return) at root node Binary Heaps 0 DeleteMin Delete (and return) at root node Binary Heaps 0 Maintain the Structure Property We now have a Hole at the root Need to fill the hole with another When we get done, the tree will have one less node and must still be complete Binary Heaps 0 0 Maintain the Heap Property The last has lost its node we need to find a new place for it We can do a simple insertion sort - like operation to find the correct place for it in the tree Binary Heaps 0 DeleteMin: Percolate Down 0 Keep comparing with children A[i] and A[i + ] Copy smaller child up and go down one level Done if both children are item or reached a leaf node What is the run time 0 Binary Heaps 0

Percolate Down DeleteMin: Run Time Analysis PercDown(i:integer, x :integer): { // N is the number of entries in heap// j : integer; Case{ i > N : A[i] := x; //at bottom// i = N : if A[i] < x then A[i] := A[i]; A[i] := x; else A[i] := x; i < N : if A[i] < A[i+] then j := i; else j := i+; if A[j] < x then A[i] := A[j]; PercDown(j,x); else A[i] := x; }} Binary Heaps Run time is O(depth of heap) A heap is a complete binary tree Depth of a complete binary tree of N nodes depth = log (N) Run time of DeleteMin is O(log N) Binary Heaps 0 Insert Add a to the tree Structure and heap order properties must still be correct when we are done 0 Maintain the Structure Property The only valid place for a new node in a complete tree is at the end of the array We need to decide on the correct for the new node, and adjust the heap accordingly 0 Binary Heaps Binary Heaps Maintain the Heap Property The new goes where We can do a simple insertion sort operation on the path from the new place to the root to find the correct place for it in the tree 0 Binary Heaps Insert: Percolate Up 0 Start at last node and keep comparing with parent A[i/] If parent larger, copy parent down and go up one level Done if parent item or reached top node A[] Run time 0 Binary Heaps 0

Insert: Done PercUp 0 Run time PercUp(i : integer, x : integer): { if i = then A[] := x else if A[i/] < x then A[i] := x; else A[i] := A[i/]; Percup(i/,x); } Binary Heaps Binary Heaps Sentinel Values Every iteration of Insert needs to test: - if it has reached the top node A[] if parent item Can avoid first test if A[0] contains a very large negative sentinel - < item, for all items 0 Second test alone always stops at top Binary Heap Analysis Space needed for heap of N nodes: O(MaxN) An array of size MaxN, plus a variable to store the size N, plus an array slot to hold the sentinel Time FindMin: O() DeleteMin and Insert: O(log N) BuildHeap from N inputs : O(N) (forthcoming) index - 0 0 0 Binary Heaps Binary Heaps