Disjoint-Set Forests

Similar documents
Outline for Today. Static Optimality. Splay Trees. Properties of Splay Trees. Dynamic Optimality (ITA) Balanced BSTs aren't necessarily optimal!

Computing Connected Components Given a graph G = (V; E) compute the connected components of G. Connected-Components(G) 1 for each vertex v 2 V [G] 2 d

CS 161: Design and Analysis of Algorithms

Data Structures for Disjoint Sets

CS4311 Design and Analysis of Algorithms. Analysis of Union-By-Rank with Path Compression

9 Union Find. 9 Union Find. List Implementation. 9 Union Find. Union Find Data Structure P: Maintains a partition of disjoint sets over elements.

Outline for Today. Static Optimality. Splay Trees. Properties of Splay Trees. Dynamic Optimality (ITA) Balanced BSTs aren't necessarily optimal!

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Divide-and-Conquer Algorithms Part Two

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Union-Find Structures

Disjoint Sets : ADT. Disjoint-Set : Find-Set

Outline for Today. Binary Relations. Equivalence Relations. Partial Orders. Quick Midterm Review

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Intractable Problems Part Two

Equivalence Relation. Equivalence Classes. Building Equivalence Classes. Disjoint Union Find 2016/2/9. Determining equivalence classes

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

1 Terminology and setup

Lecture 3. Big-O notation, more recurrences!!

The Inductive Proof Template

NP-Completeness Part II

Friday Four Square! Today at 4:15PM, Outside Gates

1 Some loose ends from last time

NP-Completeness Part One

On shredders and vertex connectivity augmentation

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Mathematical Induction Part Two

Chapter 8 The Disjoint Sets Class

Greedy Algorithms and Data Compression. Curs 2018

Checkpoint Questions Due Monday, October 1 at 2:15 PM Remaining Questions Due Friday, October 5 at 2:15 PM

Concrete models and tight upper/lower bounds

CSC236 Week 4. Larry Zhang

Outline for Today. Revisiting our first lecture with our new techniques! How do we know when two sets have the same size?

Equivalence Relations. Equivalence Classes. A new question. Building Equivalence Classes. CSE 326: Data Structures Disjoint Union/Find

IS 709/809: Computational Methods in IS Research Fall Exam Review

Complexity Theory Part I

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2013

2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

Notes on Logarithmic Lower Bounds in the Cell Probe Model

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

CS103 Handout 22 Fall 2012 November 30, 2012 Problem Set 9

Mathematical Induction

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

Biased Quantiles. Flip Korn Graham Cormode S. Muthukrishnan

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

Lecture 2: Divide and conquer and Dynamic programming

COMP 355 Advanced Algorithms

Recap from Last Time

Announcements. Problem Set 1 out. Checkpoint due Monday, September 30. Remaining problems due Friday, October 4.

CS 410/584, Algorithm Design & Analysis: Lecture Notes 3

NP-Completeness Part II

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

Midterm 1 for CS 170

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

PROFESSOR: WELCOME BACK TO THE LAST LECTURE OF THE SEMESTER. PLANNING TO DO TODAY WAS FINISH THE BOOK. FINISH SECTION 6.5

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

Divide and Conquer Algorithms

O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem.

MITOCW MITRES18_005S10_DerivOfSinXCosX_300k_512kb-mp4

Breadth-First Search of Graphs

CS 161 Summer 2009 Homework #2 Sample Solutions

Quiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b)

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

CS173 Lecture B, November 3, 2015

Kartsuba s Algorithm and Linear Time Selection

Announcements. Problem Set 6 due next Monday, February 25, at 12:50PM. Midterm graded, will be returned at end of lecture.

Turing Machines Part II

MITOCW ocw f99-lec01_300k

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Asymptotic Analysis and Recurrences

Recurrences COMP 215

Quick Sort Notes , Spring 2010

CS103 Handout 08 Spring 2012 April 20, 2012 Problem Set 3

COE428 Notes Week 4 (Week of Jan 30, 2017)

Complexity Theory Part I

Divide and Conquer Algorithms

Lower Bounds for Dynamic Connectivity (2004; Pǎtraşcu, Demaine)

Selection and Adversary Arguments. COMP 215 Lecture 19

Fall 2017 November 10, Written Homework 5

A Note on Turing Machine Design

NP-Completeness Part II

Math 391: Midterm 1.0 Spring 2016

CPSC 320 Sample Final Examination December 2013

Methods for solving recurrences

Lecture 8: Path Technology

NP-Completeness Part I

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

CS 4349 Lecture August 30th, 2017

Since the logs have the same base, I can set the arguments equal and solve: x 2 30 = x x 2 x 30 = 0

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

MITOCW MIT18_01SCF10Rec_24_300k

CSC236 Week 3. Larry Zhang

MITOCW MITRES18_005S10_DiffEqnsGrowth_300k_512kb-mp4

data structures and algorithms lecture 2

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Minimum spanning tree

Transcription:

Disjoint-Set Forests

Thanks for Showing Up!

Outline for Today Incremental Connectivity Maintaining connectivity as edges are added to a graph. Disjoint-Set Forests A simple data structure for incremental connectivity. Union-by-Rank and Path Compression Two improvements over the basic data structure. Forest Slicing A technique for analyzing these structures. The Ackermann Inverse Function An unbelievably slowly-growing function.

The Dynamic Connectivity Problem

The Connectivity Problem The graph connectivity problem is the following: Given an undirected graph G, preprocess the graph so that queries of the form are nodes u and v connected? Using Θ(m + n) preprocessing, can preprocess the graph to answer queries in time O(1).

Dynamic Connectivity The dynamic connectivity problem is the following: Maintain an undirected graph G so that edges may be inserted an deleted and connectivity queries may be answered efficiently. This is a much harder problem!

Dynamic Connectivity Euler tour trees solve dynamic connectivity in forests. Today, we'll focus on the incremental dynamic connectivity problem: maintaining connectivity when edges can only be added, not deleted. Applications to Kruskal's MST algorithm. Next Monday, we'll see how to achieve full dynamic connectivity in polylogarithmic amortized time.

Incremental Connectivity and Partitions

Set Partitions The incremental connectivity problem is equivalent to maintaining a partition of a set. Initially, each node belongs to its own set. As edges are added, the sets at the endpoints become connected and are merged together. Querying for connectivity is equivalent to querying for whether two elements belong to the same set. Goal: Maintain a set partition while supporting the union and in-same-set operation.

Representatives Given a partition of a set S, we can choose one representative from each of the sets in the partition. Representatives give a simple proxy for which set an element belongs to: two elements are in the same set in the partition iff their set has the same representative.

Union-Find Structures A union-find structure is a data structure supporting the following operations: find(x), which returns the representative of node x, and union(x, y), which merges the sets containing x and y into a single set. We'll focus on these sorts of structures as a solution to incremental connectivity.

Data Structure Idea Idea: Associate each element in a set with a representative from that set. To determine if two nodes are in the same set, check if they have the same representative. To link two sets together, change all elements of the two sets so they reference a single representative.

Using Representatives

Using Representatives If we update all the representative pointers in a set when doing a union, we may spend time O(n) per union operation. Can we avoid paying this cost?

Hierarchical Representatives

Hierarchical Representatives In a degenerate case, a hierarchical representative approach will require time Θ(n) for some find operations. Therefore, some union operations will take time Θ(n) as well. Can we avoid these degenerate cases?

Union by Rank 0 0 1 0 1 2 0 0 0 0

Union by Rank Assign to each node a rank that is initially zero. To link two trees, link the tree of the smaller rank to the tree of the larger rank. If both trees have the same rank, link one to the other and increase the rank of the other tree by one.

Union by Rank Claim: The number of nodes in a tree of rank r is at least 2 r. Proof is by induction; intuitively, need to double the size to get to a tree of the next order. Claim: Maximum rank of a node in a graph with n nodes is O(log n). Runtime for union and find is now O(log n).

Path Compression 0 0 1 0 1 2 0 0 0 0

Path Compression 0 0 1 0 1 2 0 0 0 0

Path Compression Path compression is an optimization to the standard disjoint-set forest. When performing a find, change the parent pointers of each node found along the way to point to the representative. When combined with union-by-rank, the runtime is O(log n). Intuitively, it seems like this shouldn't be tight, since repeated find operations will end up taking less time.

The Claim Claim: The runtime of union and find when using path compression and union-by-rank is amortized O(α(n)), where α is an extremely slowly-growing function. The original proof of this result (which is included in CLRS) is due to Tarjan and uses a complex amortized charging scheme. Today, we'll use a proof due to Seidel and Sharir based on a forest-slicing approach.

Where We're Going This analysis is nontrivial. First, we're going to define our cost model so we know how to analyze the structure. Next, we'll introduce the forest-slicing approach and use it to prove a key lemma. Finally, we'll use that lemma to build recurrence relations that analyze the runtime.

Our Cost Model The cost of a union or find is O(1) plus Θ(#ptr-changes-made) Therefore, the cost of m operations is Θ(m + #ptr-changes-made) We will analyze the number of pointers changed across the life of the data structure to bound the overall cost.

Some Accounting Tricks To perform a union operation, we need to first perform two finds. After that, only O(1) time is required to perform the union operation. Therefore, we can replace each union(x, y) with three operations: A call to find(x). A call to find(y). A linking step between the nodes found this way. Going forward, we will assume that each union operation will take worst-case time O(1).

A Slight Simplification Currently, find(x) compresses from x up to its ancestor. For mathematical simplicity, we'll introduce an operation compress(x, y) that compresses from x upward to y, assuming that y is an ancestor of x. Our analysis will then try to bound the total cost of the compress operations.

Removing the Interleaving We will run into some trouble in our analysis because unions and compresses can be interleaved. To address this, we will will remove the interleaving by pretending that all compresses come before all compresses. This does not change the overall work being done.

Removing the Interleaving e a b h c d f j g i compress(j, b) union(b, a) compress(h, a) f b h b j b b a h a union(b, a) compress(j, b) compress(h, a) b a f b h b j b h a

Recap: The Setup Transform any sequence of unions and finds as follows: Replace all union operations with two finds and a union on the ancestors. Replace each find operation with a compress operation indicating its start and end nodes. Move all union operations to the front. Since all unions are at the front, we build the entire forest before we begin compressing. Can analyze compress assuming the forest has already been created for us.

The Forest-Slicing Approach

Forest-Slicing a b c d e f g h i j k l

Forest-Slicing Let F be a disjoint-set forest. Consider splitting F into two forests F+ and F with the following properties: F is upward-closed: if x F+, then any ancestor of x is also in F+. + F is downward-closed: if x F, then any descendant of x is also in F. We'll call F+ the top forest and F the bottom forest.

Forest-Slicing a b c d e f g h i j k l

Forest-Slicing a b c j g d e f l i h k Nodes Nodes from from F never never move move into into F + + or or vice-versa. vice-versa. We We retain retain the the original original cut cut after after doing doing compressions. compressions.

Why Slice Forests?

Forest-Slicing Key insight: Each compress operation is either purely in F+, purely in F, or crosses from F into F+. Analyze the runtime of a series of compressions using a divide-and-conquer approach: Analyze the compressions purely in F+ and F recursively. Bound the cost of the compressions crossing from F+ to F separately.

a b c d e f g a b c d e f g

a a b c b c d e f g d e f g

a a b c b c d e f g Observation Observation 1: 1: The The portion portion of of the the compression compression in in F + + is is equivalent equivalent to to a compression compression of of the the first first node node in in F + + on on the the compression compression path path to to the the last last node node in in F + + on on the the compression compression path. path. d e f g

a a b c b c d e f g d e f g Observation 2: 2: The The effect effect of of the the compression on on F is is not not the the same same as as the the effect effect of of compressing from from the the first first node node in in F to to the the last last node node in in F..

a a b c b c d e f g d e f g Observation 3: 3: The The cost cost of of the the compress in in F is is the the number of of nodes nodes in in F that that got got a parent in in F +, +, plus plus (possibly) one one more more for for the the topmost node node in in F on on the the compression path. path.

The Cost of Crossing Compressions Suppose we do k compressions crossing from F into F+. We can upper bound the cost of these compressions as the sum of the following: The cost of all the tops of those compressions, which occur purely in F+. k (one per compression). The number of nodes in F, since each node in F gets a parent in F+ for the first time at most once.

Theorem: Let F be a disjoint-set forest and let F+ and F be a partition of F into top and bottom forests. Then for any series of m compressions C, there exist compression sequences C+ in F+ and C in F such that cost(c) cost(c+) + cost(c ) + n + m+ m+ + m = m Here, m+ = C+ and m = C. Paths Paths purely purely in in F + or or F, F, plus plus the the tops tops of of paths paths crossing crossing them. them. Nodes Nodes in in F getting getting their their first first parent parent in in F + Nodes Nodes in in F having having their their parent parent in in F + change. change.

Time-Out for Announcements!

Midterms Graded Midterms are graded and available for pickup. Solutions and statistics released in hardcopy up front. Regrades accepted until next Monday at 3:15PM; just let us know what problem(s) you'd like us to review.

Presentation Schedule We've posted the presentation schedule to the course website. You're welcome to attend any presentations you'd like as long as they're not on the same data structure that you chose.

Writeup Logistics Writeup will be due electronically as a PDF exactly 24 hours before you present. Your writeup should include background on the data structure, how the data structure works, a correctness and runtime analysis, and what your interesting addition is.

Presentation Logistics Presentation should be 15 20 minutes; we'll cut you off after 20 minutes. We'll have up to five minutes of questions afterwards. Please arrive five minutes early so you have time to get set up. Don't try to present everything you won't have time!

Your Questions

Can we have some opportunity to practice our presentations in whatever room they're being presented in? Absolutely! The The room room assignments are are posted posted online. online. Feel Feel free free to to stop stop by by those those locations locations to to try try out out your your presentation, and and feel feel free free to to invite invite friends friends along along with! with!

How long do you plan on teaching? Do you think you'll ever want to leave Stanford, either to pursue options at other universities or to do research for a company? Do you have any desire to start your own company? Wow, Wow, that's that's a hard hard one. one.

Back to CS166!

The Main Analysis

Where We Are We now have a divide-and-conquer-style result for evaluating the runtime of a series of compresses. We'll now combine that analysis with some Clever Math to get the overall runtime bound.

Rank Forests The result we proved about compression costs works even if we don't use union-by-rank. If we do use union-by-rank, the following results hold: The maximum rank of any node is O(log n). For any rank r, there are at most n / 2 r nodes of rank greater than r.

Some Terminology Let's denote by T(m, n) the maximum possible cost of performing m compresses in a rank forest of n nodes. Define T(m, n, r) to be the maximum possible cost of performing m compresses in a rank forest of at most n nodes and with maximum rank r. Note that T(m, n) = T(m, n, O(log n)).

Functional Iteration Let f : N N be a nondecreasing function where f(0) = 0 and f(n) < n for n > 0. The iterated function of f, denoted f*, is defined as follows: f *(n)={ 0 if f (n) 2 1+ f *( f (n)) otherwise Intuitively, f*(n) is the number of times that f has to be applied to n for n to drop down to 2. (The choice of 2 here is arbitrary; we just need a nice, small constant.)

Functional Iteration As an example, consider the function lg n, assuming that we round down. Notice that lg 137 = 7 lg 7 = 2 Therefore, lg* 137 = 2.

Iterated Logarithms For any k, define log* (k) n = log***...* n (k times) These functions are extremely slowly-growing. log* n 4 for all n < 2 65,536, for example. Fun exercise: What is the inverse function of log* n? How about log** n?

Where We're Going We're going to show that T(m, n) = O(n lg* (k) lg n + m) for any constant k 1 From there, we'll define a function α(n) that grows slower than lg* (k) n for any k and prove that T(m, n) = O(mα(n) + n)

Our Approach Our result will rely on a feedback technique used to build stronger results out of weaker ones. We'll find an initial proof that T(m, n) = O(n lg* lg n + m). Then, we'll prove that if we know that T(m, n) = O(n lg* (k) lg n + m), then we can prove T(m, n) = O(n lg* (k+1) lg n + m)

Proving T(m, n) = O(n lg* lg n + m)

A Starting Point Lemma: T(m, n, r) nr. Proof: Since the maximum possible rank is r, each node can have its parent change at most r times. Therefore, the number of pointer changes made is at most nr. (Remember that we've defined the cost to be the number of pointer changes.)

Getting a Recurrence Let F be a rank forest of maximum rank r and let C be a worst-case series of m compresses performed in F. Split F into F and F+ by putting all nodes of rank at most lg r into F and all other nodes into F+. By our earlier theorem, there exist C+ and C such that Therefore cost(c) cost(c+) + cost(c ) + n + m+ T(m, n, r) cost(c+) + cost(c ) + n + m+ Let's see if we can simplify this expression, starting with cost(c+).

An Observation The forest F+ consists of all nodes whose rank is greater than lg r. Therefore, the ranks go from lg r + 1 up through and including r. By our earlier result, the number of nodes in F+ is at most n / 2 lg r = n / r. If we subtract lg r + 1 from the ranks of all of the nodes, we end up with a rank forest whose maximum rank is at most r. Therefore, by our earlier lemma, we get that cost(c+) r (n / r) = n.

The Recurrence We had T(m, n, r) cost(c+) + cost(c ) + n + m+ We now have T(m, n, r) cost(c ) + 2n + m+ Notice that C is a set of compressions in a rank forest of maximum rank lg r. There are at most n nodes in F and the number of compresses in C is m. Therefore, we have T(m, n, r) T(m, n, lg r) + 2n + m+

Solving the Recurrence We have T(m, n, r) T(m, n, lg r) + 2n + m+ As our base cases: T(0, n, r) = 0 T(m, n, 2) 2n As the recursion unwinds: The 2n term gets multiplied by the number of layers in the recursion. The m+ term sums across the layers to at most m. The solution is T(m, n, r) 2nL + m, where L is the total number of layers in the recursion.

Solving the Recurrence The solution is T(m, n, r) 2nL + m, where L is the total number of layers in the recursion. At each layer, we shrink r from r to lg r. The maximum number of times you can do this before r gets to 2 is at most lg* r. Therefore, T(m, n, r) 2n lg* r + m. Since r = O(log n), this is O(n lg* lg n + m).

Adding Extra Stars

The Feedback Lemma Lemma: If T(m, n, r) 2n log* (k) r + km then T(m, n, r) 2n log* (k+1) r + (k + 1)m This will enable us to place as many stars as we'd like on the runtime.

What We'll Prove Lemma: If T(m, n, r) 2n log* r + m then T(m, n, r) 2n log** r + 2m This is a special case of the theorem with k = 1, but uses the same basic approach. Fun exercise: Update the proof to the general case.

The Recurrence Let F be a rank forest of maximum rank r and let C be a worst-case series of m compressions performed in F. Split F into F and F+ by putting all nodes of depth at most lg* r into F and all other nodes F+. There exist C+ and C such that Therefore cost(c) cost(c+) + cost(c ) + n + m+ T(m, n, r) cost(c+) + cost(c ) + n + m+ Let's see if we can simplify this expression. into

An Observation The forest F+ consists of all nodes whose rank is at least lg* r. Therefore, the ranks go from lg* r + 1 up through and including r. The number of nodes in F + is at most n / 2 lg* r If we subtract lg* r + 1 from the ranks of all of the nodes, we end up with a rank forest with ranks going up to at most r. Then cost(c+) 2(n / 2 lg* r ) lg* r + m+. Therefore, cost(c+) 2n + m+.

The Recurrence We had T(m, n, r) cost(c+) + cost(c ) + n + m+ We now have T(m, n, r) cost(c ) + 2n + 2m+ Notice that C is a set of compressions in a rank forest of maximum rank lg* r. There are at most n nodes in F and the number of compresses in C is m. Therefore, we have T(m, n, r) T(m, n, lg* r) + 2n + 2m+

Solving the Recurrence We have T(m, n, r) T(m, n, lg* r) + 2n + 2m+ As our base cases: T(0, n, r) = 0 T(m, n, 2) 2n As the recursion unwinds: The 2n term gets multiplied by the number of layers in the recursion. The 2m+ term sums across the layers to 2m. The solution is T(m, n, r) 2nL + 2m, where L is the total number of layers in the recursion.

Solving the Recurrence The solution is T(m, n, r) 2nL + 2m, where L is the total number of layers in the recursion. At each layer, we shrink r from r to lg* r. The maximum number of times you can do this before r gets to 2 is lg** r. Thus T(m, n, r) 2n lg** r + 2m.

The Optimal Approach We know that for any k > 0, that T(m, n, r) 2n lg* (k) r + km Since r = O(log n), this means that for any k > 0, we have T(m, n) = O(n lg* (k) lg n + km) What is the optimal value of k? The Ackermann inverse function α(n) is defined as follows: Therefore: α(m, n) = min { k lg* (k) lg n 1 + m / n } T(m, n) = O(n + m + α(m, n)) = O(n + mα(m, n))

Completing the Analysis In a forest of n nodes, if we do m union and find operations, the total runtime will be O(m + mα(m, n)) = O(n + mα(m, n)). Assuming that m n, the amortized cost per operation is O(α(m, n)).

For Perspective Consider 2 65,536. Then lg 2 65,536 = 65,536 = 2 16 lg 2 16 = 16 = 2 4 lg 2 4 = 4 = 2 2 lg 2 2 = 2 So lg* 2 65,536 = 4.

For Perspective Recall that lg* 2 65,656 = 4. Let z be 2 raised to the 2 65,656 th power. Then lg* z = 5. If you let z' = 2z, then lg* z' = 6. Since lg** z' counts the number of times you have to apply lg* to z' to drop it down to two, this means that lg** z' is about three. Therefore, if m n, then α(m, n) 3 as long as n z'.

Next Time Fully-Dynamic Connectivity How to maintain full connectivity information in a dynamic graph.