PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24, 2015 1.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Data Structures Lecture 7 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
The Priority Queue Abstract Data Type. Heaps. Adaptable Priority Queue. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich,
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
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.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
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.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
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.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 21 Binary Heap.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Priority Queues & Heaps Chapter 9. Iterable Collection Abstract Collection Queue List Abstract Queue Priority Queue Array List Abstract List Vector Stack.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Priority Queues and Heaps. Outline and Reading PriorityQueue ADT (§8.1) Total order relation (§8.1.1) Comparator ADT (§8.1.2) Sorting with a Priority.
Chapter 2.4: Priority Queues and Heaps PriorityQueue ADT (§2.4.1) Total order relation (§2.4.1) Comparator ADT (§2.4.1) Sorting with a priority queue (§2.4.2)
HEAPS • Heaps • Properties of Heaps • HeapSort
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
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.
CS 2468: Assignment 2 (Due Week 9, Tuesday. Drop a hard copy in Mail Box 75 or hand in during the lecture) Use array representation (double a[]) to implement.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
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.
Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank).  You only have one match; what.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
CSC 213 – Large Scale Programming. Priority Queue ADT  Prioritizes Entry s using their keys  For Entry s with equal priorities, order not specified.
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 (8.3) CSE 2011 Winter May 2018.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
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,
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Chapter 2: Basic Data Structures
Heaps and Priority Queues
Priority Queues and Heaps
Part-D1 Priority Queues
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
Heaps A heap is a binary tree.
Heaps and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Heaps and Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
Lecture 9 CS2013.
Heaps and Priority Queues
1 Lecture 10 CS2013.
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,

Outline Priority Queues Motivation Abstract Data Type Implementations Heaps insert and upheap removeMin and downheap Tuesday, February 24,

Priority Queue Motivation Many problems are solved by maintaining a collection of items and assigning each a priority Examples: Plane departures Different flights require the runway Some flights are higher priority than others Bandwidth management Highest priority data (real-time traffic like Skype, for example) is transmitted first Tuesday, February 24,

Priority Queue Abstract Data Type A priority queue stores a collection of items An item is a pair: (key, element) The key defines the element ’s position in the queue. Main methods: insert(key, element) inserts an item with the specified key and element removeMin() removes the item with the smallest key and returns the associated element Tuesday, February 24,

Priority Queue Implementations Tuesday, February 24, Implementation addremoveMin Unsorted Array O(1)O(n) Sorted Array O(n)O(1) Unsorted Linked List O(1)O(n) Sorted Linked List O(n)O(1) Hash Table O(1)O(n) Heap O(log n)

Heaps Data structure used to implement the priority queue ADT insert(key, element) removeMin() Can be implemented with a tree (link-based) or with an array Let’s look at tree first Tuesday, February 24,

Heap Properties Binary Each node has at most two children Key (or “priority”) at each node Heap order for min-heap: n.key ≥ n.parent.key for max-heap: n.key ≤ n.parent.key Left-complete Height O(log n) Tuesday, February 24,

Heaps and Priority Queues We can use a heap to implement a priority queue We store a (key, element) item at each node For simplicity, we will show only the keys in the following pictures Tuesday, February 24, (2, Nick) (6, David)(5, Marley) (9, Pat)(7, Sarah)

insert() We must keep track of the next spot where we will insert to keep the tree left- complete: the “insertion node” Tuesday, February 24, insertion node

insert() (2) Example: insert(1) Put the new node where the insertion node was Tuesday, February 24,

insert() (3) But now heap order is violated Tuesday, February 24,

Upheap Repair heap order by swapping the new item up the tree until all keys are properly sorted The first swap fixes everything underneath the new location Note that since all keys beneath the 6’s old location must have been greater or equal to 6, they must be greater than or equal to 1 Tuesday, February 24,

Upheap (2) One more swap, because the 1 is less than the 2 on top Now both the completeness and the heap order properties are satisfied Tuesday, February 24,

Upheap (3) After the insertion of a new key k, the heap order property may be violated The upheap algorithm restores the order by swapping k along an upward path from the insertion node Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k Since a heap has height O(log n), upheap runs in O(log n) time Tuesday, February 24,

Upheap (4) Tuesday, February 24,

removeMin() The minimum element of the heap is always the root due to heap order How do we remove the root of the heap without destroying the ordering? Tuesday, February 24, BAD

removeMin() (2) Instead swap the root (which we want to remove) with the last item in the heap Removing from the last position is easy But now heap order isn’t preserved… Tuesday, February 24, GOOD

Downheap Swap the root down as necessary Now the heap is in order What if 7 were a 20? Tuesday, February 24, GOOD

Downheap (2) Downheap restores the heap order property by swapping key k along a downward path from the root, swapping with the lesser of the two children Downheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k Since a heap has height O(log n), downheap runs in O(log n) time Tuesday, February 24,

Heap Implementation Recap insert(): Insert item into the “insertion node,” the next available spot (which you must keep track of) Upheap from the bottom as necessary removeMin(): Swap root with last item in heap Delete swapped root Downheap from the root as necessary Tuesday, February 24,

Finding the Insertion Node The insertion node can be found by traversing a path of O(log n) nodes Start with the last added node Go up until a left child or the root is reached If a left child is reached, go to its sibling (the corresponding right child) Go down left until a leaf is reached Tuesday, February 24,

Finding the Insertion Node (2) This is O(log n) but it can also be done in constant time using an additional data structure. Think about it – you’ll want to do it for your next project. Tuesday, February 24,

Array-based Implementation We can represent a heap with n keys by means of an array of length n + 1 Implementation For the node at index i the left child is at index 2i the right child is at index 2i + 1 Leaves and edges (links between nodes) are not represented The cell at index 0 is not used Operations insert corresponds to inserting at index n + 1 removeMin corresponds to swap with index n and remove Tuesday, February 24,

Priority Queue Analysis Recap Tuesday, February 24, Implementation addremoveMin Unsorted Array O(1)O(n) Sorted Array O(n)O(1) Unsorted Linked List O(1)O(n) Sorted Linked List O(n)O(1) Hash Table O(1)O(n) Heap O(log n) SUBLINEAR == AWESOME