CSE 326: Data Structures Lecture #5 Political Heaps

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.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
1 CSE 326: Data Structures Priority Queues – Binary Heaps.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CSE 373 Data Structures Lecture 12
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
Heap: A Special Kind of Tree
CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
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.
1 CSE 326 Data Structures Part 6: Priority Queues, AKA Heaps Henry Kautz Autumn 2002.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
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.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
CSE332: Data Abstractions Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Priority Queues
CSE 326: Data Structures Priority Queues (Heaps)
CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE 332 Data Abstractions B-Trees
Binary Search Trees.
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
October 30th – Priority QUeues
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Hashing Exercises.
COMP 103 Binary Search Trees.
Cse 373 April 26th – Exam Review.
CMSC 341 Lecture 13 Leftist Heaps
ITEC 2620M Introduction to Data Structures
Binary Search Trees Why this is a useful data structure. Terminology
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
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
B+ Trees What are B+ Trees used for What is a B Tree What is a B+ Tree
David Kaplan Dept of Computer Science & Engineering Autumn 2001
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
Heaps and the Heapsort Heaps and priority queues
CSE 332: Data Abstractions Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
A Robust Data Structure
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
CSE 332: Data Abstractions AVL Trees
CSE373: Data Structures & Algorithms Implementing Union-Find
Binary and Binomial Heaps
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
CSE 326: Data Structures Lecture #8 Balanced Dendrology
CSE 326: Data Structures Lecture #9 AVL II
Priority Queues CSE 373 Data Structures.
Richard Anderson Spring 2016
CSE 326: Data Structures Splay Trees
CSE 326: Data Structures Lecture #5 Heaps More
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
CSE 373 Data Structures Lecture 12
CSE 326: Data Structures Lecture #10 Amazingly Vexing Letters
326 Lecture 9 Henry Kautz Winter Quarter 2002
CO 303 Algorithm Analysis and Design
Heaps.
Presentation transcript:

CSE 326: Data Structures Lecture #5 Political Heaps Bart Niswonger Summer Quarter 2001

Today’s Outline Project comments & questions Things Bart Didn’t Finish on Monday (Leftist Heaps) Skew Heaps Comparing Heaps Today we’ll cover everything I missed on Monday Recall the problem at hand – merging heaps Today we’ll cover leftist heaps and skew heaps today; those are two kinds of mergeable heaps. Then, if we have time, we’ll compare all those heap implementations.

Merging Heaps How can we make it fast? Array-based implementation: Pointer-based implementation: To make it fast we need to identify the bottleneck…. Seems like you have to copy which would be theta(n) for equal sized heaps, just to copy the array What if rather than using an array, we use a linked structure (linked list/tree)? We will see we can make it faster, but what about the other operations?

Leftist Heaps Idea: Leftist heap: almost all nodes are on the left all the merging work is on the right make it so that all the work you have to do in maintaining a heap is in one small part Leftist heaps are a solution to that. They put all the work on the right side of the tree and most of the nodes on the left (or at least not on the right)

Not-so Random Definition: Null Path Length the null path length (npl) of a node is the number of nodes between it and a null in the tree npl(null) = -1 npl(leaf) = 0 npl(single-child node) = 0 2 1 1 Before I make this more concrete, let’s get a totally unrelated random definition that we probably will never use. Note that I’m assuming a binary tree here. Null path length is the number of nodes between a given node and a null. Npl of null is -1 npl of a leaf or a node with just one child is 0 Notice that I’m putting the null path length, NOT the values in the nodes. 1 another way of looking at it: npl is the height of complete subtree rooted at this node

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 So, here are the formal properties of a leftist heap. Heap-order, of course. And, the leftist property; this means that the npl of the right child is never greater than that of the left child. Are leftist trees necessarily complete or balanced? NO. In fact, a _great_ leftist tree is a string of nodes all on the left! Are leftist trees complete? Balanced?

Leftist tree examples NOT leftist leftist leftist 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Here are some training examples to help you learn. The green nodes break the leftist property for the first tree. The other two trees are leftist. 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 2r-1 nodes Proof by induction Basis: r = 1. Tree has at least one node: 21 - 1 = 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 2r - 1 - 1 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 2r - 1 - 1 nodes. All told then, there are at least: 2r - 1 - 1 + 2r - 1 - 1 + 1 = 2r - 1 So, a leftist tree with at least n nodes has a right path of at most log n nodes 2 1 1 1 Why do we have this leftist property? Because it guarantees that the right path is really short compared to the number of nodes in the tree without being as stodgy as the heap structure property. Here’s a proof which boils down to the fact that if the left path is r long, the null path length must be at least r (otherwise somewhere along there, there’s a node with a larger right npl than left). Since the tree is complete at least to depth r, there are at least 2r - 1 nodes in the tree.

Merging Two Leftist Heaps merge(T1,T2) returns one leftist heap containing all elements of the two (distinct) leftist heaps T1 and T2 merge T1 a a merge L1 R1 L1 R1 How do we merge two leftist heaps? We put the smaller root as the new root, hang its left subtree on the left. Recursively merge its right subtree and the other tree. Are we done? No. Because we haven’t ensured that the leftist property is intact. a < b T2 b b L2 R2 L2 R2

Merge Continued a a npl(R’) > npl(L1) L1 R’ R’ L1 runtime: So, we check if the leftist property is intact. If it isn’t we switch the right and left children. How long does this take? How much work do we do at each step? CONSTANT And, we recursively traverse the whole right path of both trees. How long are those paths? At most log n. So how much work? O(log n) Pretty cool, huh? R’ = Merge(R1, T2) 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 Now that we have that wicked merge op. Let’s just use it to implement all the other ops. Is one node a size one leftist heap? merge

Example merge 3 5 merge 7 10 12 5 5 merge 14 3 10 10 12 12 7 8 8 8 14 ? 1 3 5 merge ? 7 10 12 1 5 5 merge 1 14 3 10 10 12 12 Alright, let’s try an example. I’m unwinding the recursion here. I pulled a fast one merging the last two nodes. I could have reduced that to merging a node with null (which is clearly the original node) but I didn’t for convenience. Don’t _you_ do that on tests! 7 8 8 8 14 8 12

Sewing Up the Example ? ? 2 3 3 3 7 ? 7 1 7 5 1 5 5 14 14 14 10 8 10 10 8 8 No, we need to put the example back together, fixing the leftist property as we go. Are we done here? NO! The root’s right child has a higher npl than its left child! 12 12 12 Done?

Finally… 2 2 3 3 7 5 1 5 1 7 14 10 8 10 8 14 So, we swap the root’s children. Is the final tree a leftist heap? 12 12

Iterative Leftist Merging downward pass: merge right paths merge 1 5 2 3 10 12 7 5 1 It turns out that we can do an iterative version of this. First, we traverse down the right side, picking up the lesser node of each tree as we go. 1 3 14 10 8 7 8 12 14

Iterative Leftist Merging upward pass: fix right path 2 2 2 2 3 3 3 3 1 1 1 1 7 5 7 5 7 5 5 7 14 10 8 14 10 8 10 8 14 14 10 8 Then, we go back up and fix the leftist property from bottom to top. What do we need to do this iteratively? A STACK of the nodes on the right path! 12 12 12 12 What do we need to do this iteratively?

Random Definition: Amortized Time 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. Now, we’re done with leftist heaps for the moment, and we’re going to move on to another type of heap. But, before we do, here’s a totally random definition. If M operations take total O(M log N) time, amortized time per operation is O(log N)

Skew Heaps Problems with leftist heaps Solution: skew 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 There are some problems with leftist heaps. We’ll use skew heaps to fix those problems. Motivation? Remember that we “often” get heavy right sides when we merge. So, why not just assume the right side is heavy and move it over automatically?

Merging Two Skew Heaps merge T1 a a merge L1 R1 L1 R1 a < b T2 b b This merge should look very similar but simpler than the previous algorithm. L2 R2 L2 R2

Example merge 3 3 5 merge 5 7 7 10 12 5 merge 10 14 3 14 12 10 12 8 3 This happens to end up with a leftist tree and start out with leftist trees. Is that guaranteed? No – an example? 8 8 5 7 14 8 10 14 12

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); } Here’s the code. I won’t go into it except to say that it is _easy_. The iterative version is a touch more intricate but also fairly easy and requires NO STACK!

Comparing Heaps Leftist Heaps Skew Heaps Binary Heaps d-Heaps Binomial Queues Leftist Heaps Skew Heaps OK, I said you were being cheated unless you got a comparison of data structures. Who can tell me some good things about each of these? Binary: simple, memory efficient, fast, can run buildHeap. cannot merge quickly. d: Faster than binary heaps. Possibly much faster in some applications. Slightly more complicated. Otherwise, all binary advantages. Leftist: supports merge quickly. Supports all other operations asymptotically quickly. Practically, extra storage, link traversals and recursion make it bigger and slower. Skew: slightly less storage, somewhat faster and simpler than leftist. NOT guaranteed fast on EVERY operation (but guaranteed amortized speed. Binomial queues? Never heard of ‘em. Read up on them in your book!

To Do Project I due tonight @ 10pm Please email me if you are going to be late Groups were due Monday Homework due tomorrow @ START of section

Coming Up Section Project II Asymptotic Complexity! tomorrow: The Ashish Experience Project II Asymptotic Complexity! Ashish is back from an intensive Karate training – maybe he’ll show you some moves, esp if your homework is late