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

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Heaps, Heap Sort, and Priority Queues
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Tyler Robison Summer
1 CSE 326: Data Structures Priority Queues – Binary Heaps.
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
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.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
CS 315 Lecture 18 Nov 15 Goals Complete heap operations review insert, deletemin decreaseKey, increaseKey heap building Heap sorting Some applications.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
Priority Queues, Heaps & Leftist Trees
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
CSE 326: Data Structures Lecture #13 Priority Queues and Binary Heaps Nick Deibel Winter Quarter 2002.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
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.
data ordered along paths from root to leaf
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2010W2 1.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
Lecture1 introductions and Tree Data Structures 11/12/20151.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
1 CSE 326 Data Structures Part 6: Priority Queues, AKA Heaps Henry Kautz Autumn 2002.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1.
Priority Queues (Heaps)
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.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Dan Grossman Spring 2012.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
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.
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 5: Dictionary ADTs; Binary Trees Lauren Milne Summer 2015.
CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps Linda Shapiro Spring 2016.
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.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
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.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
CS 201 Data Structures and Algorithms
October 30th – Priority QUeues
Source: Muangsin / Weiss
CMSC 341: Data Structures Priority Queues – Binary Heaps
CSE 326 Heaps and the Priority Queue ADT
CSE 326: Data Structures Lecture #4 Heaps more Priority Qs
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued
Priority Queue & Heap CSCI 3110 Nan Chen.
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 326: Data Structures Lecture #4 Mind Your Priority Queues
CSE 332: Data Structures Priority Queues – Binary Heaps
Instructor: Lilian de Greef Quarter: Summer 2017
Heaps and the Heapsort Heaps and priority queues
CE 221 Data Structures and Algorithms
CSE 326: Data Structures Priority Queues & Binary Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
CSE 326: Data Structures Lecture #3 Mind your Priority Qs
CSE 326: Data Structures Lecture #5 Heaps More
Presentation transcript:

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

Today’s Outline Trees, Briefly Priority Queue ADT Heaps –Implementing Priority Queue ADT –Focus on Create: Heapify –Brief introduction to d-Heaps 2

Trees 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 structure (i.e., a record of everything that goes in the call stack) 3

Tree Terminology A E B DF C G IH LJMKN root: leaf: child: parent: sibling: ancestor: descendent: subtree: 4

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!) 5

More Tree Terminology A E B DF C G IH LJMKN depth: # of edges along path from root to node depth of H? a.0 b.1 c.2 d.3 e.4 6

More Tree Terminology A E B DF C G IH LJMKN height: # of edges along longest path from node to leaf or, for whole tree, from root to leaf height of tree? a.0 b.1 c.2 d.3 e.4 7

More Tree Terminology A E B DF C G IH LJMKN degree: # of children of a node degree of B? a.0 b.1 c.2 d.3 e.4 8

More Tree Terminology A E B DF C G IH LJMKN branching factor: maximum degree of any node in the tree 2 for binary trees, our usual concern; 5 for this weird tree 9

One More Tree Terminology Slide JIH GFED CB A binary: branching factor of 2 (each child has at most 2 children) n-ary: branching factor of n complete: “packed” binary tree; as many nodes as possible for its height nearly complete: complete plus some nodes on the left at the bottom 10

Tree Calculations Find the longest undirected path in a tree A tree is an empty tree or a node with some number of subtrees Path might be: – t 11

Tree Calculations Example A E B DF C G IH LJMKN 12

Today’s Outline Trees, Briefly Priority Queue ADT Heaps –Implementing Priority Queue ADT –Focus on Create: Heapify –Brief introduction to d-Heaps 13

Back to Queues Some applications –ordering CPU jobs –simulating events –picking the next search site Problems? –short jobs should go first –earliest (simulated time) events should go first –most promising sites should be searched first 14

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) 15

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 Sort numbers Store packets on network routers in order of urgency Select symbols for compression Anything greedy: an algorithm that makes the “locally best choice” at each step 16 Your call will not be answered in the order it was received.

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

Today’s Outline Trees, Briefly Priority Queue ADT Heaps –Implementing Priority Queue ADT –Focus on Create: Heapify –Brief introduction to d-Heaps 18

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”. Look! Invariants! 19

Nifty Storage Trick Calculations: –child: –parent: –root: –next free: 0 20

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

Percolate Down ? ? ?

Finally…

DeleteMin Code Object deleteMin() { assert(!isEmpty()); returnVal = Heap[0]; size--; newPos = percolateDown(0, Heap[size]); Heap[newPos] = Heap[size]; return returnVal; } int percolateDown(int hole, Object val) { while (2*hole + 1 < size) { left = 2*hole + 1; right = left + 1; if (right < size && Heap[right] < Heap[left]) target = right; else target = left; if (Heap[target] < val) { Heap[hole] = Heap[target]; hole = target; } else break; } return hole; } runtime: 24

DeleteMin Code (Better!) Do yours this way!!! Object deleteMin() { assert(!isEmpty()); returnVal = Heap[0]; size--; newPos = percolateDown(0, Heap[size]); Heap[newPos] = Heap[size]; return returnVal; } int percolateDown(int hole, Object val) { while (numChildren(hole) > 0) { left = getLeftChild(hole); right = getRightChild(hole); if (numChildren(hole) > 1 && Heap[right] < Heap[left]) target = right; else target = left; if (Heap[target] < val) { Heap[hole] = Heap[target]; hole = target; } else break; } return hole; } runtime: 25

Insert pqueue.insert(3) 3 Invariant violated! What will we do? 26

Percolate Up

Insert Code void insert(Object o) { assert(!isFull()); size++; newPos = percolateUp(size-1,o); Heap[newPos] = o; } int percolateUp(int hole, Object val) { while (!isRoot(hole) && val < Heap[parent(hole)] Heap[hole] = Heap[parent(hole)]; hole = parent(hole); } return hole; } runtime: 28 Note: parent(hole) == (hole-1)/2

Today’s Outline Trees, Briefly Priority Queue ADT Heaps –Implementing Priority Queue ADT –Focus on Create: Heapify –Brief introduction to d-Heaps 29

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 30

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 31

Build(this)Heap

Finally… runtime: 33

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

Today’s Outline Trees, Briefly Priority Queue ADT Heaps –Implementing Priority Queue ADT –Focus on Create: Heapify –Brief introduction to d-Heaps 35

Thinking about Binary Heaps Observations –finding a child/parent index is a multiply/divide by two –operations jump widely through the heap –deleteMins look at all (two) children of some nodes –inserts only care about parents of some nodes –inserts are at least as common as deleteMins Realities –division and multiplication by powers of two are fast –looking at one new piece of data sucks in a cache line –with huge data sets, disk accesses dominate 36 BUT… don’t solve a performance problem until you know you have one!

Solution: d-Heaps Nodes have (up to) d children Still representable by array Good choices for d: –optimize (non-asymptotic) performance based on ratio of inserts/removes –make d a power of two for efficiency –fit one set of children in a cache line –fit one set of children on a memory page/disk block d-heap mnemonic: d is for degree! 37

To Do Read: KW Section 8.5 Keep working on homework! Start preparing for the exam… it’s not that far away! 38

Coming Up Sorting, sorting, and more sorting! Midterm (Wed Feb 15, 5-7PM, loc’n TBD) 39