CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
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)
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2010.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
CSE 326: Data Structures Lecture #7 Branching Out Steve Wolfman Winter Quarter 2000.
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.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
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.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Lecture 7 Heaps and Priority Queues. Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting.
Lecture 7 Heaps and Priority Queues. Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
Binary Trees Chapter 6.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
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.
CSE 326: Data Structures Lecture #13 Priority Queues and Binary Heaps Nick Deibel Winter Quarter 2002.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Chapter 21 Binary Heap.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2010W2 1.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
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.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
1 CSE 326 Data Structures Part 6: Priority Queues, AKA Heaps Henry Kautz Autumn 2002.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2012.
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.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
1 Chapter 6: Priority Queues, AKA Heaps. 2 Queues with special properties Consider applications –ordering CPU jobs –searching for the exit in a maze (or.
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
October 30th – Priority QUeues
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
CMSC 341: Data Structures Priority Queues – Binary Heaps
CSE 326: Data Structures Lecture #4 Mind Your Priority Queues
CSE332: Data Abstractions Lecture 4: Priority Queues
Heaps and the Heapsort Heaps and priority queues
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
CSE 326: Data Structures Priority Queues & Binary Heaps
Definition Applications Implementations Heap Comparison
CSC 380: Design and Analysis of Algorithms
Presentation transcript:

CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1

Learning Goals Provide examples of appropriate applications for priority queues. Describe efficient implementations of priority queues. Relate heaps to binary trees, binary search trees, and arrays. 2 It is not a goal for this term to be able to manipulate heaps by hand.

Today’s Outline Priority Queue ADT Solutions So Far? 10km View of Heaps 3

Back to Queues Some applications –ordering CPU jobs –simulating events –picking the next search site when searching {a maze, a game tree, a room, the WWW} Problems? –short jobs should go first –earliest (simulated time) events should go first –most promising sites should be searched first 4

Priority Queue ADT Priority Queue operations –create –destroy –insert –deleteMin –is_empty Priority Queue property: for two elements in the queue, x and y, if x has a lower priority value than y, x will be deleted before y F(7) E(5) D(100) A(4) B(6) insert deleteMin G(9)C(3) 5

Applications of the Priority Q Call queues for help lines (or don’t you think so?) Hold jobs for a printer in order of length Simulate events (hold in order of time) Sort numbers Store packets on network routers in order of urgency Select symbols for compression (hold in order of frequency) Anything greedy: an algorithm that makes the “locally best choice” at each step (hold in order of quality) 6 Your call will not be answered in the order it was received.

Naïve Priority Q Data Structures Unsorted array (or linked list): –insert: –deleteMin: Sorted array: –insert: –deleteMin: 7 a.O(lg n) b.O(n) c.O(n lg n) d.O(n 2 ) e.Something else

Today’s Outline Priority Queue ADT Solutions So Far? 10km View of Heaps 8

How Can We Efficiently Implement a PQ? Stack? Queue? Linked List –Singly, doubly, … Array –Circular, resizing, … BST AVL Tree Priority value is _______ insert is ________ delete_min is _______ 9

AVL Tree as PQ How well does this work in terms of the number of priority values/data in the tree, n? Runtime of insert ? Runtime of delete_min ? 10

Today’s Outline Priority Queue ADT Solutions So Far? 10km View of Heaps 11

Binary Heap Priority Q Data Structure Heap-order property –parent’s key is less than or equal to children’s keys –result: minimum is always at the top Structure property –“nearly complete tree” –result: depth is always O(log n); next open location always known WARNING: this has NO SIMILARITY to the “heap” you hear about when people say “things you create with new go on the heap”. And, this is a binary tree but is NOT a binary search tree. Look! Invariants! 12

Nifty Storage Trick 0 13 So, we get the speed and “spatial locality” of arrays. (Also the occasional expensive resizes of arrays.)

DeleteMin ? pqueue.deleteMin() Invariants violated! DOOOM!!! 14

Percolate Down ? ? ?

Finally… Algorithm intuition: move the rightmost, bottom element to the root; then, fix the invariant downward until stable.

Insert pqueue.insert(3) 3 Invariant violated! What will we do? Intuition: insert at the bottom-right; then, fix invariant upward until stable. 17

Insert Result

Closer Look at Creating Heaps To create a heap given a list of items: Create an empty heap. For each item: insert into heap. Time complexity? a.O(lg n) b.O(n) c.O(n lg n) d.O(n 2 ) e.None of these , 4, 8, 1, 7, 2 19

A Better BuildHeap Floyd’s Method. Thank you, Floyd pretend it’s a heap and fix the heap-order property! Invariant violated! Where can the order invariant be violated in general? a.Anywhere b.Non-leaves c.Non-roots 20

Build(this)Heap: fix from bottom to top

Finally… runtime: 22

Build(any)Heap This is as many violations as we can get. How do we fix them? Let’s play colouring games! 23 “amortized analysis!”

Other Uses for Trees (besides BSTs and heaps) Family Trees Organization Charts Classification trees –what kind of flower is this? –is this mushroom poisonous? File directory structure –folders, subfolders in Windows –directories, subdirectories in UNIX Function call tree (i.e., a record of everything that goes in the call stack) 24

To Do Read: KW Section

Coming Up Sorting, sorting, and more sorting! 26

Tree Terminology Reference A E B DF C G IH LJMKN root: the single node with no parent leaf: a node with no children child: a node pointed to by me parent: the node that points to me sibling: another child of my parent ancestor: my parent or my parent’s ancestor descendent: my child or my child’s descendent subtree: a node and its descendents We sometimes use degenerate versions of these definitions that allow NULL as the empty tree. (This can be very handy for recursive base cases!) 27