Lecture Note 05 EECS 4101/5101 Instructor: Andy Mirzaian. SKEW HEAPS: Self-Adjusting Heaps

Similar documents
Cpt S 223. School of EECS, WSU

CS711008Z Algorithm Design and Analysis

Converting a Number from Decimal to Binary

6 March Array Implementation of Binary Trees

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Algorithms Chapter 12 Binary Search Trees

From Last Time: Remove (Delete) Operation

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Data Structures Fibonacci Heaps, Amortized Analysis

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Analysis of Algorithms I: Binary Search Trees

Binary Heap Algorithms

Binary Heaps. CSE 373 Data Structures

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Full and Complete Binary Trees

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Rotation Operation for Binary Search Trees Idea:

Binary Search Trees CMPSC 122

An Evaluation of Self-adjusting Binary Search Tree Techniques

Big Data and Scripting. Part 4: Memory Hierarchies

Heaps & Priority Queues in the C++ STL 2-3 Trees

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:


S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

PES Institute of Technology-BSC QUESTION BANK

Persistent Data Structures and Planar Point Location

Lecture Notes on Binary Search Trees

Data Structure [Question Bank]

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

Data storage Tree indexes

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

How To Create A Tree From A Tree In Runtime (For A Tree)

Union-Find Problem. Using Arrays And Chains

Lecture Notes on Binary Search Trees

Ordered Lists and Binary Trees

Complexity of Union-Split-Find Problems. Katherine Jane Lai

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Data Structures and Algorithms

TREE BASIC TERMINOLOGIES

Sample Questions Csci 1112 A. Bellaachia

Lecture 2 February 12, 2003

Binary Trees and Huffman Encoding Binary Search Trees

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Lecture 1: Course overview, circuits, and formulas

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Persistent Data Structures

1/1 7/4 2/2 12/7 10/30 12/25

Lecture 4: Balanced Binary Search Trees

Introduction to Data Structures and Algorithms

CMSC 858T: Randomized Algorithms Spring 2003 Handout 8: The Local Lemma

Section IV.1: Recursive Algorithms and Recursion Trees

Persistent Binary Search Trees

The Cost of Offline Binary Search Tree Algorithms and the Complexity of the Request Sequence

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

DATA STRUCTURES USING C

Binary Coded Web Access Pattern Tree in Education Domain

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Union-find with deletions

Data Structure with C

8.1 Min Degree Spanning Tree

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

International Journal of Advanced Research in Computer Science and Software Engineering

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

6.852: Distributed Algorithms Fall, Class 2

Algorithms and Data Structures

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Randomized Binary Search Trees

Introduction to Data Structures and Algorithms

External Memory Geometric Data Structures

Fast nondeterministic recognition of context-free languages using two queues

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

The Goldberg Rao Algorithm for the Maximum Flow Problem

Binary Search Trees. Ric Glassey

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Exam study sheet for CS2711. List of topics

10CS35: Data Structures Using C

Fast Sequential Summation Algorithms Using Augmented Data Structures

Binary Search Trees (BST)

Algorithms and Data Structures

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

GENERATING THE FIBONACCI CHAIN IN O(log n) SPACE AND O(n) TIME J. Patera

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Lecture 5 - CPA security, Pseudorandom functions

Transcription:

Lecture Note 05 EECS 40/50 Instructor: Andy Mirzaian SKEW HEAPS: Self-Adjusting Heaps In this handout we describe the skew heap data structure, a self-adjusting form of heap related to the leftist heap of Crane and Knuth. Skew heaps, in contrast to leftist heaps, use less space and are easier to implement, and yet in the amortized sense they are as efficient, to within a constant factor, asleftist heaps. We will consider the following priority queue operations: makeheap(h): Create anew, empty heap, named h. findmin(h): insert(k, h): deletemin(h): Return a minimum key in h. If h is empty, return the special key "null". (This operation does not change the set of keys stored in h.) Insert key k in heap h. (Duplicate keys are allowed in the heap. That is, the key tobeinserted is treated as "new".) Delete a minimum key from heap h and return it. If the heap is initially empty, return null. union( h, h 2 ): Return the heap formed by joining (i.e., taking the union of) heaps h and h 2.This operation distroys h and h 2. THE DATA STRUCTURE There are several ways to implement heaps in a self-adjusting fashion. The one we shall discuss is called skew heaps as proposed by Sleator and Tarjan, and is analogous to leftist heaps. Askew heap is a heap-ordered binary tree. That is, it is a binary tree with one key ineach node so that for each node x other that the root, the key at node x is no less than the key at the parent of x. Torepresent such a tree we store in each node x its associated key, denoted key(x) and two pointers left(x) and right(x), to its left child and right child, respectively. If x has no left child we define left(x)=λ; if x has no right child we define right(x)=λ. Access to the tree is by a pointer to its root; we represent an empty tree by a pointer to Λ. With this representation we can carry out the various heap operations as follows. We perform makeheap(h) in O( ) time by initializing h to Λ. Since heap order implies that the root is a minimum key in the tree, we can carry out findmin(h) in O( ) time by returning the key at the root; returning null if the heap is empty. We perform insert and deletemin using union. Tocarry out insert(k, h), we make k into a one-node heap and Union it with h. To carry out deletemin(h), if h is not empty we replace h by the Union of its left and right subtrees and return the key at the original root. (If h is originally empty we return null.) To perform union( h, h 2 ), we form a single tree by traversing down the right paths of h and h 2,merging them into a single right path with keys innondecreasing order. First assume the left subtrees of nodes along the merge path do not change. (See Figure (a).) The time for the Union operation is bounded by a constant times the length of the merge path. To make Union efficient, we must keep right paths short. In leftist heaps this is done

-2- by maintaining the invariant that, for any node x, the right path descending from x is a shortest path down to a missing node. Maintaining this invariant requires storing at every node the length of a shortest path down to a missing node; after the merge we walk back up the merge path, updating the shortest path lengths and swapping left and right children as necessary to maintain the leftist property. The length of the right path in a leftist heap of n nodes is at most log n, implying an O(log n) worst-case time bound for each of the heap operations, where n is the number of nodes in the heap or heaps involved. 6 5 30 4 (a) 5 6 30 4 (b) 6 5 4 30 Figure : AUnion of two skew heaps. (a) Merge of the right paths. (b) Swapping of children along the path formed by the merge. In our self-adjusting version of this data structure, we perform the Union operation by merging the right paths of the two trees and then swapping the left and right children of every node on the merge path except the lowest. (See Figure (b).) This makes the potentially long right path formed by the merge into a left path. We call the resulting data structure a skew heap.

-3- Exercise : Write a simple recursive algorithm for the Union operation on skew heaps. THE AMORTIZED ANALYSIS In our analysis of skew heaps we shall use the following general approach. We associate with each collection S of skew heaps a real number Φ(S) called the potential of S. For any sequence of m operations with running times t, t 2,..., t m,wedefine the amortized time a i of the i th operation to be a i = t i +Φ i Φ i,where, Φ i,for i =,2,..., m, isthe potential of the skew heaps after the i th operation and Φ 0 is the potential of the skew heaps before the first operation. The total running time of the sequence of the operations is then m Σ t i = Σ m ( a i Φ i +Φ i )=Φ 0 Φ m + Σ m a i That is, the total running time equals the total amortized time plus the net decrease in potential from the initial to the final collection of the skew heaps. In our analysis of skew heaps the potential will be zero initially and will remain nonnegative. This implies that the total amortized time is an upper bound on the actual running time over the entire sequence of operations. We shall define the potential of a single skew heap; the potential of a collection of skew heaps is the sum of the potentials of its members. Intuitively, a heap with high potential is one subject to unusually time-consuming operations; the extra time spent corresponds to a drop in potential. Definition : For any node x in a binary tree, we define the weight of x, denoted wt(x), to be the number of descendents of x, including x itself. We use weights to partition nonroot nodes into two classes: Definition 2: Anonroot node x is heavy if wt(x)> wt( parent(x)) /2and light otherwise. Aroot node is considered neither heavy nor light. The following facts immediately follow from the above definitions. (Try to prove them yourself.) Fact : Of the children of any node, at most one is heavy. Fact 2: On any path from a node x down to a descendent y, there are at most log ( wt(x)/ wt(y)) light nodes, not counting x. In particular, any path in an n-node tree contains at most log n light nodes. Definition 3: left otherwise. Anonroot node is called right if it is the right child of its parent; it is called Definition 4: We define the potential of a skew heap to be the total number of right heavy nodes it contains. Suppose we begin with no heaps and carry out a sequence of skew heap operations. The initial potential is zero and the final potential is nonnegative, so the total amortized time is an upper bound on the total actual time of the operations. The amortized time of a makeheap or findmin operation is O( ), since these operations require O( ) time and do not

-4- change the potential. The amortized time of the other operations depend on the amortized time for the union operation. Consider a Union of two heaps h and h 2,containing n and n 2 nodes, respectively. Let n = n + n 2 be the total number of nodes in the two heaps. As ameasure of the time for Union, we shall charge one per node on the merge path. Thus the amortized time of Union is the number of nodes on the merge path plus the change in potential. By Fact 2, the number of light nodes on the right paths of h and h 2 is at most log n and log n 2, respectively. Thus the total number of light nodes on the two paths is at most 2 log n. (Check this.) (See Figure 2.) # LIGHT lg n # LIGHT lg n 2 # HEAVY = k # HEAVY = k 2 # LIGHT lg n # HEAVY = k 3 lg n Figure 2: Analysis of right heavy nodes in union. Let k and k 2 be the number of heavy nodes on the right paths of h and h 2,respectively, and let k 3 be the number of nodes that become right heavy children of nodes on the merge path. By Fact, every node counted by k 3 corresponds to a light node on the merge path. Thus Fact 2 implies that k 3 log n. The number of nodes on the merge path is at most 2 + log n + k + log n 2 + k 2 + 2 log n + k + k 2. (The first "2" counts the roots of h and h 2.) Therefore the running time of the Union operation is t + 2 log n + k + k 2. The change in potential caused by the Union is Φ = k 3 k k 2 log n k k 2. Thus the amortized time of the Union, t + Φ, isatmost 3 log n +. We summarize the above result in the following theorem. Theorem: The amortized time of an insert, deletemin, or union skew heap operation is O(log n), where n is the number of keys inthe heap or heaps involved in the operation. The amortized time of a makeheap or findmin operation is O( ). Proof: The analysis above gives the bound for findmin, makeheap, and union; the bound for insert and deletemin follows immediately from that of union.

-5- Bibliography: To see further improvements to the basic structure described above, the interested reader is referred to: Sleator, Tarjan, "Self-Adjusting Heaps," SIAM J. Computing, (), 86, pp. 52-6. Bernard Chazelle s papers sited below describe a novel self adjusting priority queue that allows a controled measure of error in order to accelerate performance! He shows many applications, including the fastest deterministic minimum spanning tree algorithm todate: "The Soft Heap: An Approximate Priority Queue with Optimal Error Rate", JACM 47(6), Nov. 2000, pp. 0-027. "A Minimum Spanning Tree Algorithm with inverse-ackermann Type Complexity" JACM 47(6), Nov. 2000, pp. 028-047.