Today s Outline. The One Page Cheat Sheet. Simplifying Recurrences. Set Notation. Set Notation. Priority Queues. O( 2 n ) O( n 3 )

Similar documents
Administration CSE 326: Data Structures

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

CSE 326: Data Structures Lecture #4 Heaps more Priority Qs. Today s Outline

CE 221 Data Structures and Algorithms

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

CSE332: Data Abstractions Lecture 4: Priority Queues; Heaps. James Fogarty Winter 2012

AMORTIZED ANALYSIS. Announcements. CSE 332 Data Abstractions: Priority Queues, Heaps, and a Small Town Barber. Today.

Unit #2: Priority Queues

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

CSCI2100B Data Structures Heaps

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

CSE 373: Data Structures and Algorithms

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

Heaps, Heap Sort, and Priority Queues.

Priority Queues Heaps Heapsort

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

Priority Queues (Heaps)

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues. Aaron Bauer Winter 2014

Priority Queues (Heaps)

Priority Queues Heaps Heapsort

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

CSE 373: Data Structures and Algorithms More Asympto<c Analysis; More Heaps

Data Structures and Algorithms

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

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

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

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

CS350: Data Structures Heaps and Priority Queues

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Adam Blank Lecture 3 Winter 2017 CSE 332. Data Structures and Parallelism

COMP : Trees. COMP20012 Trees 219

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

CMSC 341 Lecture 14: Priority Queues, Heaps

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

Recall: Properties of B-Trees

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Figure 1: A complete binary tree.

Binary Heaps. CSE 373 Data Structures Lecture 11

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

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

Priority Queues. Binary Heaps

CMSC 341. Binary Heaps. Priority Queues

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

Data Structures Lesson 9

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Priority Queues and Heaps

Binomial Queue deletemin ADTs Seen So Far

Sorting and Searching

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Sorting and Searching

Binary Trees

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

CSE 214 Computer Science II Heaps and Priority Queues

Priority Queues. Chapter 9

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

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

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Heaps. Heaps. A heap is a complete binary tree.

Course Review. Cpt S 223 Fall 2010

Algorithms and Data Structures

Binary Trees, Binary Search Trees

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

CSE 326: Data Structures Binary Search Trees

CSE373: Data Structures & Algorithms Priority Queues

Midterm solutions. n f 3 (n) = 3

Figure 1: A complete binary tree.

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Chapter 6 Heapsort 1

Heaps. 2/13/2006 Heaps 1

INF2220: algorithms and data structures Series 4

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

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

CSE 373 Data Structures and Algorithms. Lecture 15: Priority Queues (Heaps) III

Heaps Goodrich, Tamassia. Heaps 1

The heap is essentially an array-based binary tree with either the biggest or smallest element at the root.

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

UNIT III BALANCED SEARCH TREES AND INDEXING

Course Review. Cpt S 223 Fall 2009

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Binary heaps (chapters ) Leftist heaps

CMSC 341 Leftist Heaps

Data Structures in Java

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

CS 310: Priority Queues and Binary Heaps

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

Partha Sarathi Manal

The beautiful binary heap.

EECS 477: Introduction to algorithms. Lecture 10

Topic: Heaps and priority queues

csci 210: Data Structures Priority Queues and Heaps

Data Structures Week #8. Heaps (Priority Queues)

Text Compression through Huffman Coding. Terminology

CSE 373: Data Structures and Algorithms

Transcription:

Priority Queues (Today: inary Min Heaps) hapter in Weiss SE Data Structures Ruth Anderson Winter 0 //0 Today s Outline Announcements Project #, due pm Wed Jan. Written Assignment # posted, due at the beginning of class Friday Jan. Today s Topics: Asymptotic Analysis Priority Queues inary Min Heap //0 The One Page heat Sheet alculating series: e.g. n n( n + ) i = i=. rute force (Section..). Induction (Section..). Memorize simple ones! Solving recurrences: e.g. T(n) = T(n/) +. Expansion (example in class). Induction (Section..). Telescoping (later ) eneral proofs (Section..) e.g. How many edges in a tree with n nodes?. ounterexample. Induction. ontradiction //0 Simplifying Recurrences iven a recursive equation for the running time, can sometimes simplify it for analysis. For an upper-bound analysis, can optionally simplify to something larger, e.g. T(n) = T(floor(n/)) + to T(n) T(n/) + For a lower-bound analysis, can optionally simplify to something smaller, e.g. T(n) = T(n/ + ) + to T(n) T(n/) + //0 O( f(n) ) is a set of functions n + Set Notation O( n ) 0n log n n - n.00 n + n. n - 0 O( n ) Set Notation n - n.00 n + n So we say both Set notation allows us to 0n log n = O( n ) and formalize our intuition //0 0n log n O( n ) //0 O( n ) O( n ) n + O( n ) 0n log n n + n 00 n log n

Processor Scheduling Priority Queue ADT heckout line at the supermarket??? Printer queues??? operations: insert, deletemin insert deletemin //0 //0 Priority Queue ADT. PQueue data : collection of data with priority. PQueue operations insert deletemin (also: create, destroy, is_empty). PQueue 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 //0 Applications of the Priority Q Select print jobs in order of decreasing length Forward packets on network routers in order of urgency Select most frequent symbols for compression Sort numbers, picking minimum first Anything greedy //0 Implementations of Priority Queue ADT insert deletemin Unsorted list (Array) Unsorted list (Linked-List) Sorted list (Array) Sorted list (Linked-List) inary Search Tree (ST) root(t): leaves(t): children(): parent(h): siblings(e): ancestors(f): descendents(): subtree(): Tree Review A D E F H J K L Tree T I M N //0 //0

depth(t): More Tree Terminology A Tree T Some More Tree Terminology T is binary if A Tree T height(): degree(): branching factor(t): D E F H J K L M N I T is n-ary if T is complete if D H I E J F //0 How deep is a complete tree with n nodes? //0 rief interlude: Some Definitions: A Perfect binary tree A binary tree with all leaf nodes at the same depth. All internal nodes have children. height h h+ nodes h non-leaves h leaves 0 Full inary Tree A binary tree in which each node has exactly zero or two children. (also known as a proper binary tree) (we will use this later for Huffman trees) //0 //0 inary Heap Properties. Structure Property. Ordering Property Heap Structure Property A binary heap is a complete binary tree. omplete binary tree binary tree that is completely filled, with the possible exception of the bottom level, which is filled left to right. Examples: //0 //0

Representing omplete inary Trees in an Array Why better than tree with pointers? A D E F H I J K L From node i: left child: right child: parent: implicit (array) implementation: 0 A D E F H I J K L //0 //0 0 Heap Order Property Heap order property: For every non-root node X, the value in the parent of X is less than (or equal to) the value in X. Heap Operations findmin: insert(val): percolate up. deletemin: percolate down. 0 0 not a heap 0 0 0 0 0 00 0 0 0 00 0 0 0 //0 //0 Heap Insert(val) asic Idea:. Put val at next leaf position. Repeatedly exchange node with its parent if needed Insert pseudo ode (optimized) void insert(object o) { assert(!isfull()); size++; newpos = percolateup(size,o); Heap[newPos] = o; runtime: int percolateup(int hole, Object val) { while (hole > && val < Heap[hole/]) Heap[hole] = Heap[hole/]; hole /= ; return hole; (Java code in book) //0 //0

Insert: percolate up 0 0 Heap Deletemin 0 0 00 0 0 00 0 0 0 0 asic Idea:. Remove root (that is always the min!). Put last leaf node at root. Find smallest child of node. Swap node with its smallest child if needed.. Repeat steps & until no swaps needed. //0 //0 DeleteMin pseudo ode (Optimized) Object deletemin() { assert(!isempty()); returnval = Heap[]; size--; newpos = percolatedown(, Heap[size+]); Heap[newPos] = Heap[size + ]; return returnval; runtime: (Java code in book) int percolatedown(int hole, Object val) { while (*hole <= size) { left = *hole; right = left + ; 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; //0 DeleteMin: percolate down 0 0 0 0 00 0 0 0 0 00 //0 Insert:,,,,,, 0 Other Priority Queue Operations decreasekey given a pointer to an object in the queue, reduce its priority value Solution: change priority and increasekey given a pointer to an object in the queue, increase its priority value Solution: change priority and Why do we need a pointer? Why not simply data value? //0 //0 0

Other Heap Operations decreasekey(objptr, amount): raise the priority of a object, percolate up increasekey(objptr, amount): lower the priority of a object, percolate down remove(objptr): remove a object, move to top, them delete. ) decreasekey(objptr, ) ) deletemin() Worst case Running time for all of these: FindMax? ExpandHeap when heap fills, copy into new space. inary Min Heaps (summary) insert: percolate up. Θ(log N) time. deletemin: percolate down. Θ(log N) time. uild Heap? //0 //0 uildheap: Floyd s Method Add elements arbitrarily to form a complete tree. Pretend it s a heap and fix the heap-order property! uildheap pseudocode private void buildheap() { for ( int i = currentsize/; i > 0; i-- ) percolatedown( i ); runtime: //0 //0 uildheap: Floyd s Method Finally runtime: //0 //0