Priority Queues. 04/10/03 Lecture 22 1

Similar documents
CE 221 Data Structures and Algorithms

COMP : Trees. COMP20012 Trees 219

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

Priority Queues (Heaps)

Priority Queues (Heaps)

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

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

Data Structures in Java

protected BinaryNode root; } 02/17/04 Lecture 11 1

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

Priority Queue. 03/09/04 Lecture 17 1

CSCI2100B Data Structures Heaps

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

CMSC 341 Lecture 14: Priority Queues, Heaps

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

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

Recall: Properties of B-Trees

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Binary Heaps. CSE 373 Data Structures Lecture 11

Analysis of Algorithms

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

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

Binary Node. private Object element; private BinaryNode left; private BinaryNode right; 02/18/03 Lecture 12 1

Priority Queues Heaps Heapsort

lecture29: Shortest Path Algorithms

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

Course Review for Finals. Cpt S 223 Fall 2008

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

CSE 214 Computer Science II Heaps and Priority Queues

Trees. A tree is a directed graph with the property

Data Structures Brett Bernstein

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

CMSC 341 Lecture 15 Leftist Heaps

CSE373 Fall 2013, Midterm Examination October 18, 2013

CMSC 341 Lecture 15 Leftist Heaps

CS-301 Data Structure. Tariq Hanif

CS350: Data Structures Binary Search Trees

Exam Data structures DIT960/DAT036

Data Structures and Algorithms

Heaps, Heap Sort, and Priority Queues.

Lecture Summary CSC 263H. August 5, 2016

CS 350 : Data Structures Binary Search Trees

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

Final Examination. Algorithms & Data Structures II ( )

CMSC 341. Binary Search Trees CMSC 341 BST

CS102 Binary Search Trees

CS 315 Data Structures mid-term 2

DATA STRUCTURES AND ALGORITHMS

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

CS : Data Structures

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

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

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

Solved by: Syed Zain Ali Bukhari (BSCS)

Review of course COMP-251B winter 2010

CMSC 341. Binary Heaps. Priority Queues

Course Review. Cpt S 223 Fall 2009

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CMSC 341 Lecture 10 Binary Search Trees

Administration CSE 326: Data Structures

Sorting and Searching

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

9. Heap : Priority Queue

COMP Data Structures

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

COMP250: Priority queue ADT, Heaps. Lecture 23 Jérôme Waldispühl School of Computer Science McGill University

Priority Queues Heaps Heapsort

Lecture 13: AVL Trees and Binary Heaps

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

Trees. (Trees) Data Structures and Programming Spring / 28

Priority queues. Priority queues. Priority queue operations

Course Review. Cpt S 223 Fall 2010

1. AVL Trees (10 Points)

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Sorting and Searching

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

CS 261 Data Structures. AVL Trees

Midterm solutions. n f 3 (n) = 3

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

UNIT III BALANCED SEARCH TREES AND INDEXING

Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees

Chapter 2: Basic Data Structures

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

Advanced Set Representation Methods

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

Chapter 9. Priority Queue

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

Data Structures in Java

CMSC 341 Leftist Heaps

Transcription:

Priority Queues It is a variant of queues Each item has an associated priority value. When inserting an item in the queue, the priority value is also provided for it. The data structure provides a method to delete the item with the highest priority. 04/10/03 Lecture 22 1

package weiss.nonstandard; Priority Queues // PriorityQueue interface // // ******************PUBLIC OPERATIONS********************* // Position insert( x ) --> Insert x // Comparable deletemin( )--> Return and remove smallest item // Comparable findmin( ) --> Return smallest item // boolean isempty( ) --> Return true if empty; else false // void makeempty( ) --> Remove all items // int size( ) --> Return size // void decreasekey( p, v)--> Decrease value in p to v // ******************ERRORS******************************** // Throws UnderflowException for findmin and deletemin when empty 04/10/03 Lecture 22 2

Applications of Priority Queues Implementing job queues in computer systems, or queues in an emergency room in a hospital. Implementing Dijkstra s shortest path algorithm 04/10/03 Lecture 22 3

Binary Search Trees // BinarySearchTree class // // **************PUBLIC OPERATIONS***************** // void insert( x ) --> Insert x O(h) // void remove( x ) --> Remove x O(h) // void removemin( ) --> Remove minimum item O(h) // Comparable find( x ) --> Return item that matches x O(h) // Comparable findmin( ) --> Return smallest item O(h) // Comparable findmax( ) --> Return largest item O(h) // boolean isempty( ) --> Return true if empty; else false // void makeempty( ) --> Remove all items The height of the tree =? O(log n) on the average, and O(n) on the worst case. 04/10/03 Lecture 22 4

Balanced Binary Search Trees There can be many binary search trees for the same data. Not all of them have the same characteristics. Some are better than the others. Worst case height of the binary search tree is O(log n) More work when you insert or delete, because you try to fix any imbalances in the tree caused by the change. No change when you search, because the tree is still a binary search tree. 04/10/03 Lecture 22 5

Figure 19.19 (a) The balanced tree has a depth of log N; (b) the unbalanced tree has a depth of N 1. 04/10/03 Lecture 22 6

Figure 19.20 Binary search trees that can result from inserting a permutation 1, 2, and 3; the balanced tree shown in part (c) is twice as likely to result as any of the others. 04/10/03 Lecture 22 7

Figure 19.22 Minimum tree of height H 04/10/03 Lecture 22 8

Figure 19.23 Single rotation to fix case 1 04/10/03 Lecture 22 9

Figure 19.25 Single rotation fixes an AVL tree after insertion of 1. 04/10/03 Lecture 22 10

Applications of Balanced BSTs Anywhere where you want to store a dynamic set of items. Static means that the items in the set are fixed at the start and do not change. To implement static sets, a sorted array would do well. Dynamic means that the items in the set are changing (inserts and deletes). Balanced BSTs guarantee the worst-case performance of the data structure. 04/10/03 Lecture 22 11

Disjoint Set Union-Find Data Structure Maintains disjoint sets. Main operations: Union: unions two given sets Find: finds set containing given item 04/10/03 Lecture 22 12

Disjoint Set Union-Find Data Structure public class DisjointSets { public DisjointSets( int numelements ) public void union( int root1, int root2 ) public int find( int x ) private int [ ] s; } 04/10/03 Lecture 22 13

Figure 24.12 A forest and its eight elements, initially in different sets 04/10/03 Lecture 22 14

Figure 24.13 The forest after the union of trees with roots 4 and 5 04/10/03 Lecture 22 15

Figure 24.14 The forest after the union of trees with roots 6 and 7 04/10/03 Lecture 22 16

Figure 24.15 The forest after the union of trees with roots 4 and 6 04/10/03 Lecture 22 17

Figure 24.16 The forest formed by union-by-size, with the sizes encoded as negative numbers 04/10/03 Lecture 22 18

Applications: Disjoint Set Union- Find Data Structure Minimum Spanning Tree problem 04/10/03 Lecture 22 19

Figure 24.6 (a) A graph G and (b) its minimum spanning tree 04/10/03 Lecture 22 20

Figure 24.7 (A) Kruskal s algorithm after each edge has been considered. The stages proceed left-to-right, top-to-bottom, as numbered. (continued) 04/10/03 Lecture 22 21

Figure 24.7 (B) Kruskal s algorithm after each edge has been considered. The stages proceed left-to-right, top-to-bottom, as numbered. 04/10/03 Lecture 22 22

Figure 24.1 A 50 x 88 maze 04/10/03 Lecture 22 23

Figure 24.2 Initial state: All walls are up, and all cells are in their own sets. 04/10/03 Lecture 22 24

Figure 24.3 At some point in the algorithm, several walls have been knocked down and sets have been merged. At this point, if we randomly select the wall between 8 and 13, this wall is not knocked down because 8 and 13 are already connected. 04/10/03 Lecture 22 25

Figure 24.4 We randomly select the wall between squares 18 and 13 in Figure 24.3; this wall has been knocked down because 18 and 13 were not already connected, and their sets have been merged. 04/10/03 Lecture 22 26

Figure 24.5 Eventually, 24 walls have been knocked down, and all the elements are in the same set. 04/10/03 Lecture 22 27