CS2351 Data Structures. Tutorial 3: Data Structures for Disjoint Sets

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

CS 161: Design and Analysis of Algorithms

Data Structures for Disjoint Sets

Equivalence Relations. Disjoint Union / Find. Dynamic Equivalence Problem. Equivalence Classes

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

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

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

Connected-components. Summary of lecture 9. Algorithms and Data Structures Disjoint sets. Example: connected components in graphs

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

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

Disjoint-Set Forests

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

Chapter 8 The Disjoint Sets Class

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

CSE 202: Design and Analysis of Algorithms Lecture 3

CSE 332. Data Abstractions

Greedy Algorithms and Data Compression. Curs 2018

Maximum Flow. Reading: CLRS Chapter 26. CSE 6331 Algorithms Steve Lai

9. v > 7.3 mi/h x < 2.5 or x > x between 1350 and 5650 hot dogs

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Lecture 6 January 15, 2014

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Curve Sketching. The process of curve sketching can be performed in the following steps:

( x) f = where P and Q are polynomials.

Topics in Theoretical Computer Science April 08, Lecture 8

Branch-and-Bound for the Travelling Salesman Problem

Exact Inference: Variable Elimination

Transformations of Quadratic Functions

Finger Search in the Implicit Model

v( t) g 2 v 0 sin θ ( ) ( ) g t ( ) = 0

1 CHAPTER 7 PROJECTILES. 7.1 No Air Resistance

A Dichotomy. in in Probabilistic Databases. Joint work with Robert Fink. for Non-Repeating Queries with Negation Queries with Negation

Computing Congruences Efficiently

Greedy Algorithms and Data Compression. Curs 2017

Partitions and Covers

Lecture 12: Feb 16, 2017

9.1 The Square Root Function

1 Caveats of Parallel Algorithms

Data selection. Lower complexity bound for sorting

Generalized Tree Architecture with High-Radix Processing for an SC Polar Decoder

Disjoint set (Union-Find)

Strong Interference and Spectrum Warfare

Cuts & Metrics: Multiway Cut

PHY 133 Lab 1 - The Pendulum

CS 6110 S16 Lecture 33 Testing Equirecursive Equality 27 April 2016

Problem Solving as State Space Search

Algorithms And Programming I. Lecture 5 Quicksort

Practice Final Solutions. 1. Consider the following algorithm. Assume that n 1. line code 1 alg(n) { 2 j = 0 3 if (n = 0) { 4 return j

Appendix lecture 9: Extra terms.

(Nearly)-tight bounds on the linearity and contiguity of cographs

directed weighted graphs as flow networks the Ford-Fulkerson algorithm termination and running time

Relaxed Multiplication Using the Middle Product

Learning Decision Trees

On-Line Trajectory Optimization Including Moving Threats and Targets

CS5371 Theory of Computation. Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL)

Combinatorial Optimization

Functions. Introduction

Approximate Voronoi Diagrams

Lecture 2: Network Flows 1

5. Solve graphically. Express your answer in interval notation.

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Analysis of Uninformed Search Methods

Figure 1: Bayesian network for problem 1. P (A = t) = 0.3 (1) P (C = t) = 0.6 (2) Table 1: CPTs for problem 1. (c) P (E B) C P (D = t) f 0.9 t 0.

Mathematical Notation Math Calculus & Analytic Geometry III

CS 61B Asymptotic Analysis Fall 2017

Worst Case Instances for Maximum Clique Algorithms and How to Avoid Them

5.1 Learning using Polynomial Threshold Functions

5. Network Analysis. 5.1 Introduction

Supplementary material for Continuous-action planning for discounted infinite-horizon nonlinear optimal control with Lipschitz values

5. Partitions and Relations Ch.22 of PJE.

(K2) For sum of the potential differences around any cycle is equal to zero. r uv resistance of {u, v}. [In other words, V ir.]

Aditya Bhaskara CS 5968/6968, Lecture 1: Introduction and Review 12 January 2016

On shredders and vertex connectivity augmentation

Divide-and-Conquer. Reading: CLRS Sections 2.3, 4.1, 4.2, 4.3, 28.2, CSE 6331 Algorithms Steve Lai

Introduction to Algorithms

CS21 Decidability and Tractability

Energizing Math with Engineering Applications

CSE 326: Data Structures NP Completeness

Tutorial 4. Dynamic Set: Amortized Analysis

1999 AAPT PHYSICS OLYMPIAD

Exponential and Logarithmic. Functions CHAPTER The Algebra of Functions; Composite

Categories and Quantum Informatics: Monoidal categories

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Physics 5153 Classical Mechanics. Solution by Quadrature-1

1 Matroid intersection

4.3. Solving Friction Problems. Static Friction Problems. Tutorial 1 Static Friction Acting on Several Objects. Sample Problem 1.

Get the frictional force from the normal force. Use dynamics to get the normal force.

Complexity Theory: The P vs NP question

More Approximation Algorithms

APPROXIMATION ALGORITHMS RESOLUTION OF SELECTED PROBLEMS 1

Factorized Relational Databases Olteanu and Závodný, University of Oxford

Causal Bayesian Networks

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

23. Cutting planes and branch & bound

ICS 252 Introduction to Computer Design

This relationship is known as the ideal gas law and is mathematically described with the formula below:

REVIEW: Going from ONE to TWO Dimensions with Kinematics. Review of one dimension, constant acceleration kinematics. v x (t) = v x0 + a x t

The Ideal thermodynamic cycle for gas liquefaction is impractical and hence modified cycles are proposed.

Preliminaries. Graphs. E : set of edges (arcs) (Undirected) Graph : (i, j) = (j, i) (edges) V = {1, 2, 3, 4, 5}, E = {(1, 3), (3, 2), (2, 4)}

Transcription:

CS2351 Data Structures Tutorial 3: Data Structures or Disjoint Sets 1

Aout this lecture Data Structure or Disjoint Sets Support Union and Find operations Various Methods: 1. Union y Size 2. Union y Rank 3. Union y Rank + Path Compression 2

Maintainin Disjoint Set In some applications, especially in alorithms relatin to raphs, we oten have a set o elements, and want to maintain a dynamic partition o them I.e., the partition chanes over time Our taret corresponds to maintainin dynamic disjoint sets o the elements 3

Maintainin Disjoint Set Let Σ = { S 1, S 2,, S k } e a collection o dynamic disjoint sets o the elements Let x and y e any two elements We want to support: Make-Set(x): create a set containin x Find(x) : return which set x elons Union(x,y) : mere the sets containin x and containin y into one 4

Example Application: Findin Connected Components Step 0: Bein with the input raph a e h c d 5

Example Application: Findin Connected Components Step 1: Make-Set(v) or each vertex v a e h c d current Σ: { {a}, {}, {c}, {d}, {e}, {}, {}, {h} } 6

Example Application: Findin Connected Components Step 2: Visit each ede (u,v), perorm Union(u,v) a e h c d current Σ: { {a}, {}, {c}, {d}, {e}, {}, {}, {h} } 7

Step 2: Visit (a,) a e h ede visited c d current Σ: { {a,}, {c}, {d}, {e}, {}, {}, {h} } Step 2: Visit (c,d) a e h c d current Σ: { {a,}, {c,d}, {e}, {}, {}, {h} } 8

Step 2: Visit (e,) a e h ede visited c d current Σ: { {a,}, {c,d}, {e,}, {}, {h} } Step 2: Visit (,c) a e h c d current Σ: { {a,,c,d}, {e,}, {}, {h} } 9

Step 2: Visit (,) a e h ede visited c d current Σ: { {a,,c,d}, {e,,}, {h} } Step 2: Visit (,d) a e h c d current Σ: { {a,,c,d}, {e,,}, {h} } 10

Example Application: Findin Connected Components Ater Step 2 (when all edes visited) : Each Disjoint Set Connected Component a e h c d current Σ: { {a,,c,d}, {e,,}, {h} } 11

Remarks To acilitate Find(x), each set usually chooses one o its element as a representative Find(x) returns the representative element o the set where x elons To check i x and y elon to the same set, we can just check i Find(x) == Find(y) 12

Disjoint-Set Forest One popular method to maintain disjoint sets is y a orest Each set a separate rooted tree Representative root o tree 13

Example Current dynamic sets : { {a,,c,d}, {e,,}, {h} } h a c d e 14

Disjoint-Set Forest To perorm Union(x,y), we join the trees containin x and containin y, y linkin their roots E.. Union(,h) in previous example ives: h e 15

Disjoint-Set Forest Let H max = max heiht o all trees In the worst-case: Make-Set : Find or Union : Θ(1) time Ο(H max ) time m operations on n elements : worst-case Θ(mn) time 16

Union By Size Let us apply a union-y-size heuristic : To perorm Union, we link root o the smaller tree to root o the larer tree H max = Ο(lo n) (how to prove??) m operations : Θ(m lo n) time 17

Union By Rank A similar heuristic is called union-y-rank Each node keeps track o its rank an upper ound on the heiht o the node In a sinle-node tree (created y Make-Set) rank o root = 0 To perorm Union, we link root with smaller rank to root with larer rank 18

Union By Rank Rank needs not e very accurate as lon as it always ives an upper ound o heiht is enouh When Union is perormed, only the rank o the roots may chane : I oth roots have same rank rank o new root increases y 1 Else, no chane 19

Example o Union y Rank Beore Union Ater Union(c,) 1 1 2 0 0 0 0 0 a c d e 1 0 0 0 a c d 0 e 0? = rank 20

Union By Rank Let H max = max heiht o all trees H max = Ο(lo n) (how to prove??) m operations : Θ(m lo n) time So, union y rank is no etter than union y size, ut 21

Path Compression The closer a node to its root, the aster the Find or Union operation When we perorm Find(x), we will need to ind the root o the tree containin x will access every ancestor o x why don t we make all these ancestors o x closer to the root now? ( Because no increase in asymptotic perormance!!! ) 22

Example o Path Compression Beore Find(x) Ater Find(x) w v u x w v u x 23

Union y Rank + Path Compression I Union(x,y) is always perormed y irst Find(x), Find(y), and then linkin the roots, then y cominin union-y-rank (at Union) and path compression (at Find and Union) : m operations: Θ(m α(n)) time Inverse Ackermann (in practice, at most 4) 24

Findin Connected Components Recall: To ind connected components o a raph G with n vertices and m edes there are n Make-Set and m Find or Union operations Which scheme or dynamic disjoint sets ives the est runnin time (theoretically)? Ans. Depends on m (why?) 25