© 2013 Goodrich, Tamassia, Goldwasser

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

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)
Priority Queue and Heap 1. 2 Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the.
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,
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.
© 2004 Goodrich, Tamassia Priority Queues1. © 2004 Goodrich, Tamassia Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection.
Chapter 8: Priority Queues
© 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 Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Chapter 8: Priority Queues and Heaps Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided.
Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
© 2004 Goodrich, Tamassia Priority Queues1. © 2004 Goodrich, Tamassia Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection.
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.
COMP20010: Algorithms and Imperative Programming Lecture 2 Data structures for binary trees Priority queues.
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)
CH 8. HEAPS AND PRIORITY QUEUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
12/23/2015 2:18 AMPriority Queues1 Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
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.
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.
Priority Queues Last Update: Oct 23, 2014 EECS2011: Priority Queues1.
1 COMP9024: Data Structures and Algorithms Week Seven: Priority Queues Hui Wu Session 1, 2016
Binary Trees Priority Queues, and Heaps CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer.
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Binary Search Trees < > =
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,
Priority Queues Chuan-Ming Liu
Data Structures Review Session 2
Chapter 10.1 Binary Search Trees
COMP9024: Data Structures and Algorithms
09 Priority Queues Hongfei Yan Apr. 27, 2016.
Part-D1 Priority Queues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Priority Queues © 2014 Goodrich, Tamassia, Goldwasser Priority Queues
Heaps and Priority Queues
Priority Queues and Heaps
Part-D1 Priority Queues
Binary Search Trees < > =
Heaps and Priority Queues
CMSC 341 Lecture 14 Priority Queues & Heaps
Priority Queues 4/6/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
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
Copyright © Aiman Hanna All rights reserved
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Priority Queues Sell 100 IBM $ $120 Buy 500 $ $118
Heaps and Priority Queues
Copyright © Aiman Hanna All rights reserved
Priority Queues Sell 100 IBM $ $120 Buy 500 $ $118
Adaptable Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Lecture 9 CS2013.
Heaps and Priority Queues
Priority Queues © 2010 Goodrich, Tamassia Priority Queues
1 Lecture 10 CS2013.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

© 2013 Goodrich, Tamassia, Goldwasser Priority Queues 1/17/2019 5:46 AM Priority Queues © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

Assignment #8 R-8.20 Draw a binary tree T that simultaneously satisfies the following: Each internal node of T stores a single character. A preorder traversal of T yields EXAMFUN. An inorder traversal of T yields MAFXUEN.

C-8.50 Design algorithms for the following operations for a binary tree T: preorder_next(p): Return the position visited after p in a preorder traversal of T (or None if p is the last node visited). inorder_next(p): Return the position visited after p in an inorder traversal of T (or None if p is the last node visited). postorder_next(p): Return the position visited after p in a ostorder traversal of T (or None if p is the last node visited). What are the worst-case running times of your algorithms? The worst-case running times for these algorithms are all O(h) where h is the height of the tree T.

preorder_next(p) Algorithm preorder next(v): if v is internal then return v’s left child else p = parent of v if v is left child of p then return right child of p while v is not left child of p do v = p p = p.parent

inorder_next(p) Algorithm inorder next(v): if v is internal then p = right child of v while p has left child do p = left child of p return p else p = parent of v if v is left child of p then while v is not left child of p do v = p p = p.parent

postorder_next(p) Algorithm postorder next(v): if v is internal then p = parent of v if v is right child of p then return p else v = right child of p while v is not external do v = left child of v return v if v is left child of p then return right child of p

C-8. 58 Let T be a tree with n positions C-8.58 Let T be a tree with n positions. Define the lowest common ancestor (LCA) between two positions p and q as the lowest position in T that has both p and q as descendants (where we allow a position to be a descendant of itself ). Given two positions p and q, describe an efficient algorithm for finding the LCA of p and q. What is the running time of your algorithm?

lowest common ancestor (LCA) Algorithm LCA(v, w): int vdepth = v.depth int wdepth = w.depth while vdepth > wdepth do v = v.parent while wdepth > vdepth do w = w.parent while v != w do return v

This choice may be influenced by factors such as each plane’s an air-traffic control center that has to decide which flight to clear for landing This choice may be influenced by factors such as each plane’s distance from the runway, time spent waiting in a holding pattern, or amount of remaining fuel. It is unlikely that the landing decisions are based purely on a FIFO policy.

© 2013 Goodrich, Tamassia, Goldwasser Priority Queue ADT A priority queue stores a collection of items Each item is a pair (key, value) Main methods of the Priority Queue ADT add (k, x) inserts an item with key k and value x remove_min() removes and returns the item with smallest key Additional methods min() returns, but does not remove, an item with smallest key len(P), is_empty() Applications: Standby flyers Auctions Stock market © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

Priority Queue Example © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

© 2013 Goodrich, Tamassia, Goldwasser Total Order Relations Keys in a priority queue can be arbitrary objects on which an order is defined Two distinct entries in a priority queue can have the same key Mathematical concept of total order relation  Reflexive property: x  x Antisymmetric property: x  y  y  x  x = y Transitive property: x  y  y  z  x  z © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

Composition Design Pattern An item in a priority queue is simply a key-value pair Priority queues store items to allow for efficient insertion and removal based on keys © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

Sequence-based Priority Queue Implementation with an unsorted list Performance: add takes O(1) time since we can insert the item at the beginning or end of the sequence Remove_min and min take O(n) time since we have to traverse the entire sequence to find the smallest key Implementation with a sorted list Performance: add takes O(n) time since we have to find the place where to insert the item remove_min and min take O(1) time, since the smallest key is at the beginning 4 5 2 3 1 1 2 3 4 5 © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

Unsorted List Implementation © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

Sorted List Implementation © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

Priority Queue Sorting We can use a priority queue to sort a set of comparable elements Insert the elements one by one with a series of add operations Remove the elements in sorted order with a series of remove_min operations The running time of this sorting method depends on the priority queue implementation Algorithm PQ-Sort(S, C) Input sequence S, comparator C for the elements of S Output sequence S sorted in increasing order according to C P  priority queue with comparator C while S.is_empty () e  S.remove_first () P.add (e, ) while P.is_empty() e  P.removeMin().key() S.add_last(e) © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

© 2013 Goodrich, Tamassia, Goldwasser Priority Queues 1/17/2019 5:46 AM Selection-Sort Selection-sort is the variation of PQ-sort where the priority queue is implemented with an unsorted sequence Running time of Selection-sort: Inserting the elements into the priority queue with n insert operations takes O(n) time Removing the elements in sorted order from the priority queue with n removeMin operations takes time proportional to 1 + 2 + …+ n Selection-sort runs in O(n2) time the bottleneck computation is the repeated “selection” of the minimum element in Phase 2. For this reason, this algorithm is better known as selection-sort. (See Figure 9.7.) © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

Selection-Sort Example Sequence S Priority Queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (7,4) .. .. .. (g) () (7,4,8,2,5,3,9) Phase 2 (a) (2) (7,4,8,5,3,9) (b) (2,3) (7,4,8,5,9) (c) (2,3,4) (7,8,5,9) (d) (2,3,4,5) (7,8,9) (e) (2,3,4,5,7) (8,9) (f) (2,3,4,5,7,8) (9) (g) (2,3,4,5,7,8,9) () © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

© 2013 Goodrich, Tamassia, Goldwasser Priority Queues 1/17/2019 5:46 AM Insertion-Sort Insertion-sort is the variation of PQ-sort where the priority queue is implemented with a sorted sequence Running time of Insertion-sort: Inserting the elements into the priority queue with n insert operations takes time proportional to 1 + 2 + …+ n Removing the elements in sorted order from the priority queue with a series of n removeMin operations takes O(n) time Insertion-sort runs in O(n2) time © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

Insertion-Sort Example Sequence S Priority queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (4,7) (c) (2,5,3,9) (4,7,8) (d) (5,3,9) (2,4,7,8) (e) (3,9) (2,4,5,7,8) (f) (9) (2,3,4,5,7,8) (g) () (2,3,4,5,7,8,9) Phase 2 (a) (2) (3,4,5,7,8,9) (b) (2,3) (4,5,7,8,9) .. .. .. (g) (2,3,4,5,7,8,9) () © 2013 Goodrich, Tamassia, Goldwasser Priority Queues

In-place Insertion-Sort Instead of using an external data structure, we can implement selection-sort and insertion-sort in-place A portion of the input sequence itself serves as the priority queue For in-place insertion-sort We keep sorted the initial portion of the sequence We can use swaps instead of modifying the sequence 5 4 2 3 1 5 4 2 3 1 4 5 2 3 1 2 4 5 3 1 2 3 4 5 1 1 2 3 4 5 1 2 3 4 5 © 2013 Goodrich, Tamassia, Goldwasser Priority Queues