CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.

Slides:



Advertisements
Similar presentations
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COL 106 Shweta Agrawal and Amit Kumar
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.
Leftist Heaps Text Read Weiss, §23.1 (Skew Heaps) Leftist Heap Definition of null path length Definition of leftist heap Building a Leftist Heap Sequence.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Tyler Robison Summer
1 CSE 326: Data Structures Priority Queues – Binary Heaps.
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.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
Nick Harvey & Kevin Zatloukal
CSE 373 Data Structures Lecture 12
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
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Heap: A Special Kind of Tree
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.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity.  insert  remove min (or max)  initialize Can.
CSE 326: Data Structures Lecture #13 Priority Queues and Binary Heaps Nick Deibel Winter Quarter 2002.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Chapter 21 Binary Heap.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Cpt S 223 – Advanced Data Structures Priority Queues
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
CSE 326: Data Structures Lecture #12 Splay It Again, Sam Steve Wolfman Winter Quarter 2000.
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.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Nicki Dell Spring 2014.
Priority Queues (Heaps)
Change Keys in heaps Fibonacci heap Zhao Xiaobin.
CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000.
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.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
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.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
CSE 326: Data Structures Priority Queues (Heaps)
CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)
Binary Search Trees.
Hashing Exercises.
CMSC 341 Lecture 13 Leftist Heaps
CSE 326 Heaps and the Priority Queue ADT
CSE 326: Data Structures Lecture #4 Heaps more Priority Qs
CSE 326: Data Structures Lecture #5 Political Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
Leftist Heaps Text Leftist Heap Building a Leftist Heap
CSE 326: Data Structures Lecture #5 Heaps More
CS 6310 Advanced Data Structure Wei-Shian Wang
CSE 373 Data Structures Lecture 12
Presentation transcript:

CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000

Today’s Outline Turn in Homework #2 Things Steve Didn’t Finish on Monday (d-Heaps) Leftist Heaps Skew Heaps Comparing Heaps Hand Out Project #2

New Operation: Merge Given two heaps, merge them into one heap –first attempt: insert each element of the smaller heap into the larger. runtime: –second attempt: concatenate heaps’ arrays and run buildHeap. runtime: How about O(log n) time?

Idea: Hang a New Tree = ? 10 Now, just percolate down!

Idea: Hang a New Tree = Problem?

Leftist Heaps Idea: make it so that all the work you have to do in maintaining a heap is in one small part Leftist heap: –almost all nodes are on the left –all the merging work is on the right

the null path length (npl) of a node is the number of nodes between it and a null in the tree Random Definition: Null Path Length npl(null) = -1 npl(leaf) = 0 npl(single-child node) = another way of looking at it: npl is the height of complete subtree rooted at this node 0

Leftist Heap Properties Heap-order property –parent’s priority value is  to childrens’ priority values –result: minimum element is at the root Leftist property –null path length of left subtree is  npl of right subtree –result: tree is at least as “heavy” on the left as the right Are leftist trees complete? Balanced?

Leftist tree examples NOT leftistleftist every subtree of a leftist tree is leftist, comrade!

Right Path in a Leftist Tree is Short If the right path has length at least r, the tree has at least 2 r - 1 nodes Proof by induction Basis: r = 1. Tree has at least one node: = 1 Inductive step: assume true for r’ < r. The right subtree has a right path of at least r - 1 nodes, so it has at least 2 r nodes. The left subtree must also have a right path of at least r - 1 (otherwise, there is a null path of r - 3, less than the right subtree). Again, the left has 2 r nodes. All told then, there are at least: 2 r r = 2 r - 1 So, a leftist tree with at least n nodes has a right path of at most log n nodes

Whew!

Merging Two Leftist Heaps merge(T 1,T 2 ) returns one leftist heap containing all elements of the two (distinct) leftist heaps T 1 and T 2 a L1L1 R1R1 b L2L2 R2R2 merge T1T1 T2T2 a < b a L1L1 merge b L2L2 R2R2 R1R1

Merge Continued a L1L1 R’R’ R ’ = Merge(R 1, T 2 ) a R’R’ L1L1 npl(R ’ ) > npl(L 1 ) runtime:

Operations on Leftist Heaps merge with two trees of total size n: O(log n) insert with heap size n: O(log n) –pretend node is a size 1 leftist heap –insert by merging original heap with one node heap deleteMin with heap size n: O(log n) –remove and return root –merge left and right subtrees merge

Example merge ? merge 10 5 ? 0 merge

Sewing Up the Example ? ? ? Done?

Finally…

Iterative Leftist Merging merge downward pass: merge right paths

Iterative Leftist Merging upward pass: fix right path What do we need to do this iteratively?

Random Definition: Amortized Time am·or·tize To write off an expenditure for (office equipment, for example) by prorating over a certain period. time A nonspatial continuum in which events occur in apparently irreversible succession from the past through the present to the future. am·or·tized time Running time limit resulting from writing off expensive runs of an algorithm over multiple cheap runs of the algorithm, usually resulting in a lower overall running time than indicated by the worst possible case. If M operations take total O(M log N) time, amortized time per operation is O(log N)

Skew Heaps Problems with leftist heaps –extra storage for npl –two pass merge (with stack!) –extra complexity/logic to maintain and check npl Solution: skew heaps –blind adjusting version of leftist heaps –amortized time for merge, insert, and deleteMin is O(log n) –worst case time for all three is O(n) –merge always switches children when fixing right path –iterative method has only one pass

Merging Two Skew Heaps a L1L1 R1R1 b L2L2 R2R2 merge T1T1 T2T2 a < b a L1L1 merge b L2L2 R2R2 R1R1

Example merge merge merge

Skew Heap Code void merge(heap1, heap2) { case { heap1 == NULL: return heap2; heap2 == NULL: return heap1; heap1.findMin() < heap2.findMin(): temp = heap1.right; heap1.right = heap1.left; heap1.left = merge(heap2, temp); return heap1; otherwise: return merge(heap2, heap1); }

Comparing Heaps Binary Heaps d-Heaps Binomial Queues Leftist Heaps Skew Heaps

To Do Start looking at Project II Find a group for Project II Read chapter 4 in the book

Coming Up Dictionary ADT Self-Balancing Trees Quiz #3 (January 20 th )