Priority Queues and Heaps

Slides:



Advertisements
Similar presentations
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.
Advertisements

CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
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.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Dr. Andrew Wallace PhD BEng(hons) EurIng
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Binary Heap.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Heaps & Priority Queues
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
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.
CS 367 Introduction to Data Structures Lecture 8.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
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). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
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)
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
October 30th – Priority QUeues
Source: Muangsin / Weiss
March 31 – Priority Queues and the heap
Bohyung Han CSE, POSTECH
Heap Sort Example Qamar Abbas.
Heapsort.
Heaps.
Heaps, Heap Sort, and Priority Queues
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CMSC 341: Data Structures Priority Queues – Binary Heaps
Chapter 8 – Binary Search Tree
Priority Queues.
Section 10 Questions Heaps.
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
Priority Queue and Binary Heap Neil Tang 02/12/2008
Priority Queues.
Heaps and the Heapsort Heaps and priority queues
Priority Queues.
Computer Science 2 Heaps.
"Teachers open the door, but you must enter by yourself. "
Priority Queues (Chapter 6.6):
A Robust Data Structure
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) A heap.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Priority Queues & Heaps
CSE 12 – Basic Data Structures
HEAPS.
Priority Queues.
Algorithms: Design and Analysis
Heapsort.
Priority Queues (Heaps)
Priority Queues.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Priority Queues (Chapter 6):
Heaps By JJ Shepherd.
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Computer Algorithms CISC4080 CIS, Fordham Univ.
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
A Heap Is Efficiently Represented As An Array
Heaps.
Presentation transcript:

Priority Queues and Heaps

Priority Queues We already know a ”standard” queue: Elements can only be added to the start of the queue Elements can only be removed from the end of the queue Order of removal is FIFO (first in, first out) RHS – SWC

Priority Queues In a Priority Queue, each element is assigned a priority as well A priority is a numeric value – the lower the number, the higher the priority Elements are removed in order of their priority – no longer FIFO Can still add elements in any order RHS – SWC

Priority Queues public interface PriorityQueue<T> { void add(T element); T remove(); T peek(); } RHS – SWC

Priority Queues The interface to a priority queue is almost the same as to a regular queue However, elements in the queue must now be of the type Comparable (i.e. implement the interface) The remove method always returns the element with highest priority (lowest numeric value…) RHS – SWC

Priority Queues A Priority Queue is an abstract data structure, which can be implemented in various ways Linked list, removal will be O(n) Binary search tree, removal will usually be O(log(n)), but not always Heap, removal will always be O(log(n)) RHS – SWC

Heaps A Heap is a Binary Tree, but not a Binary Search Tree In order for a Binary Tree to be a Heap, two conditions must be fulfilled: A heap is almost complete; only nodes missing in bottom layer All nodes store values which are at most as large as the values stored in any descendant RHS – SWC

Heaps RHS – SWC

Heaps On a heap, only two operations are of interest: Insert a new element Remove the element with lowest value, i.e. the root element However, both of these operations must preserve the heap property of the tree RHS – SWC

Heaps - insertion 32 23 29 42 37 56 Find an empty slot in the tree RHS – SWC

Heaps - insertion 32 23 29 42 If the value in the parent is larger than the new value, swap the parent and the new slot (repeat this) 37 56 RHS – SWC

Heaps - insertion 32 23 29 32 37 56 42 Now insert the new value RHS – SWC

Heaps - insertion Since the heap is ”almost complete”, the number of layers in a tree with n nodes is at most (log(n) + 1) Each swap operation can be done in constant time, so insertion of an element has the run-time complexity O(log(n)) RHS – SWC

Heaps - removal Remove the root node (always smallest) 23 29 32 37 56 42 RHS – SWC

Heaps - removal 29 32 Move the last element into the root position 37 56 42 RHS – SWC

Heaps - removal If any child has a lower value, swap position with child with lowest value (repeat this) 42 29 32 37 56 RHS – SWC

Heaps - removal If any child has a lower value, swap position with child with lowest value (repeat this) 29 42 32 37 56 RHS – SWC

Heaps - removal The heap property has now been reestablished This is known as ”fixing the heap” 29 37 32 42 56 RHS – SWC

Heaps - removal Since the heap is ”almost complete”, the number of layers in a tree with n nodes is at most (log(n) + 1) Each swap operation can be done in constant time, so deletion of an element has the run-time complexity O(log(n)) RHS – SWC

Heaps - removal The important point is that for a heap, we are guaranteed O(log(n)) run time for insertion and deletion This cannot be guaranteed for a binary search tree A binary search tree can ”degenerate” into a linked list; a heap cannot RHS – SWC

Heaps - representation Due to the regularity of a heap, it can efficiently be stored in an array Root node is stored in position 1 (not 0) A node in position i has its children stored in position 2i and (2i + 1) A node in position i has its parent stored in position i/2 (integer division) When running out of space, double the size RHS – SWC

Heapsort A heap can be used for a quite efficient way of sorting an array of n objects Run-time complexity of O(nlog(n)) Does not use extra space Main steps Turn the array into a heap Repeatedly remove the root element (which has the smallest value) RHS – SWC

Heapsort In order to turn the array into a heap, we could just insert all the elements into a new heap However, we can do this without using an extra heap! We use the ”fix heap” procedure from the bottom in the tree and upwards RHS – SWC

Heapsort Why will this work…? Remember that the ”fix heap” procedure takes two ”subheaps” as input, plus a root node with a ”wrong” value If we work from the bottom and up, the input will always be like above RHS – SWC

Heapsort Fix heap here Fix heaps here Fix heaps here Trivially a heap RHS – SWC

Heapsort Now the tree is a heap Repeatedly remove the root from the heap, and fix the remaining heap We ”remove” the root by placing it at the end of the array, beyond the last element in the remaining heap RHS – SWC

Heapsort 23 29 32 37 56 42 23 29 32 37 56 42 RHS – SWC

Heapsort 29 32 37 56 42 29 32 37 56 42 23 RHS – SWC

Heapsort 29 37 32 42 56 29 37 32 42 56 23 RHS – SWC

Heapsort 37 32 42 56 37 32 42 56 29 23 RHS – SWC

Heapsort 32 37 56 42 32 37 56 42 29 23 RHS – SWC

Heapsort 37 56 42 37 56 42 32 29 23 RHS – SWC

Heapsort 37 42 56 37 42 56 32 29 23 RHS – SWC

Heapsort 42 56 42 56 37 32 29 23 RHS – SWC

Heapsort 42 56 42 56 37 32 29 23 RHS – SWC

Heapsort 56 56 42 37 32 29 23 RHS – SWC

Heapsort 56 56 42 37 32 29 23 RHS – SWC

Heapsort 56 42 37 32 29 23 RHS – SWC

Heapsort One minor issue – numbers are sorted in wrong order Could just reverse order, takes O(n) Or use max-heap Min-heap: All nodes store values at most as large as the values stored in any descendant Max-heap: All nodes store values at least as large as the values stored in any descendant RHS – SWC