Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445

Similar documents
CSE 100: GRAPH ALGORITHMS

Chapter 5. Greedy algorithms

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4)

Kruskal s MST Algorithm

CSE 100 Disjoint Set, Union Find

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

CS 6783 (Applied Algorithms) Lecture 5

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

CS 310 Advanced Data Structures and Algorithms

6,8:15. MA/CSSE 473 Day 37. Student Questions. Kruskal data structures. Disjoint Set ADT. Complexity intro

CMSC 341 Lecture 20 Disjointed Sets

Disjoint Sets. The obvious data structure for disjoint sets looks like this.

Greedy Algorithms Part Three

COMP Analysis of Algorithms & Data Structures

Disjoint Sets. Based on slides from previous iterations of this course.

CMSC 341 Lecture 20 Disjointed Sets

Dijkstra s algorithm for shortest paths when no edges have negative weight.

CMSC 341 Lecture 20 Disjointed Sets

CSC 505, Fall 2000: Week 7. In which our discussion of Minimum Spanning Tree Algorithms and their data Structures is brought to a happy Conclusion.

Chapter 23. Minimum Spanning Trees

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Minimum Spanning Trees My T. UF

Minimum Spanning Trees

Lecture 13: Minimum Spanning Trees Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Outline Purpose Disjoint Sets Maze Generation. Disjoint Sets. Seth Long. March 24, 2010

Minimum Spanning Trees. COMPSCI 355 Fall 2016

Outline. Disjoint set data structure Applications Implementation

Advanced Set Representation Methods

CSC263 Week 11. Larry Zhang.

2 A Template for Minimum Spanning Tree Algorithms

Minimum spanning trees

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

looking ahead to see the optimum

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Lecture 13: Minimum Spanning Trees Steven Skiena

The minimum spanning tree problem

Optimization I : Brute force and Greedy strategy

Union-Find and Amortization

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Minimum Spanning Trees. Lecture II: Minimium Spanning Tree Algorithms. An Idea. Some Terminology. Dr Kieran T. Herley

22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets

Solutions to Exam Data structures (X and NV)

Lecture Summary CSC 263H. August 5, 2016

Algorithm Design and Analysis

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015

Disjoint Sets 3/22/2010

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

CSE 332: Data Structures & Parallelism Lecture 22: Minimum Spanning Trees. Ruth Anderson Winter 2018

Week 9 Student Responsibilities. Mat Example: Minimal Spanning Tree. 3.3 Spanning Trees. Prim s Minimal Spanning Tree.

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Minimum Spanning Trees and Union Find. CSE 101: Design and Analysis of Algorithms Lecture 7

CS 561, Lecture 9. Jared Saia University of New Mexico

CS 161: Design and Analysis of Algorithms

22 Elementary Graph Algorithms. There are two standard ways to represent a

1 Minimum Cut Problem

Minimum Spanning Trees

Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1

Week 12: Minimum Spanning trees and Shortest Paths

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees

What is a minimal spanning tree (MST) and how to find one

CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find. Lauren Milne Spring 2015

Data Structures for Disjoint Sets

N objects numbered 0, 1,, N 1, grouped into disjoint sets One member of a set chosen to be the label

Kruskal's MST Algorithm

6.1 Minimum Spanning Trees

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

CS261: A Second Course in Algorithms Lecture #16: The Traveling Salesman Problem

Amortized Analysis and Union-Find , Inge Li Gørtz

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Example of why greedy step is correct

Today s Outline. Motivation. Disjoint Sets. Disjoint Sets and Dynamic Equivalence Relations. Announcements. Today s Topics:

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Greedy algorithms. Given a problem, how do we design an algorithm that solves the problem? There are several strategies:

1 Minimum Spanning Trees (MST) b 2 3 a. 10 e h. j m

CS : Data Structures

COMP251: Disjoint sets

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

Disjoint Sets. (Disjoint Sets) Data Structures and Programming Spring / 50

Minimum Spanning Tree

22 Elementary Graph Algorithms. There are two standard ways to represent a

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

Greedy Algorithms 1. For large values of d, brute force search is not feasible because there are 2 d

In this chapter, we consider some of the interesting problems for which the greedy technique works, but we start with few simple examples.

Lecture 4 Greedy algorithms

Exercises: Disjoint Sets/ Union-find [updated Feb. 3]

Minimum-Cost Spanning Tree. Example

Introduction to Optimization

Transcription:

CS 5 Union/Find Aka: Disjoint-set forest Alon Efrat Problem definition Given: A set of atoms S={1, n E.g. each represents a commercial name of a drugs. This set consists of different disjoint subsets. Problem: suggest a data structures that efficiently supports two operations Find(i,j) reports if the atom i and atom j belong to the same set. Union(i,j) unify (merged) all elements of the set containing i with the set containing j. Example on the board. Naïve attempts Idea: Each element knows to which set it belongs (recall each atom belongs to exactly one set) Bad idea: once two sets are merged, we need to scan all elements of one set and tell them that they belong to a different set requires lots of work if the set is large. 1

A Promising Attempt Store a forest of trees. Each set is stored as a tree (each node is an atom) Every node points to the parent (different than standard trees) Only the root knows the name of the set. 1 1 So the `name of the set {,,,1 is. The name of the set of {5,6,, is. The name of the set of { is. The name of the set of {,1 is 1. To find if two atoms belong to the same set, just check if they belong to same tree: Follow the parent pointers from each of them up all the way to the root. Check if this is the same root. Disjoint sets forests - cont 1 Find_root(j){ If (p[j] j) return Find_root(p[j]); // p[j] - points to the parent Else return j ; Let r = Find_root(j) p[r]=i Find(i,j){ just check if Find_root(i) == Find_root(j) Example Union(5,) It this efficient? 1 Time per operation depends on the height of the tree. Might be Θ(n) in the worst case. (Prove) So n operations takes Θ(n )

Could we do better? 1 Example Union(5,) Improved union operation version 1 First improvement Let r = Find_root(j) p[r]=find_root(i) /* rather than p[r]= i ; */ Note that we can also do Let r = Find_root(i) p[r]=find_root(j) Keeping tracks of # nodes 1 1 1 Every root (only roots) stores the number of nodes in its tree Let r.n denote this field in the root r. Let r1 = Find_root(i); Let r = Find_root(j); /* connect the root of the small tree as a child of the root of the larger tree */ if (r1.n< r.n ) { p[r1]=r ; r.n += r1.n ; else { p[r]=r1 ; r1.n += r.n Example: Union(,) Proving bounds on the height Assume we start from a forest where each node is a singleton (a set of one element), and we perform a sequence of union operations. Lemma: The height of every tree is log n. (n number of atoms) Proof: Show by induction that every tree of height h has h nodes. Assume true for every tree of height h <h, and assume that after merging trees T 1, T, we created a tree of height exactly h. T has height is exactly h-1, so it has h-1 nodes. h T 1 T T 1 also has h-1 nodes, (why?) Together they have h-1 + h-1 = h nodes.

Further improvement: path compression So far we know that every tree has height O(log n), so this bounds the time for each operation. Path compression: during either union or find operation, we scan a sequence of nodes on our way from a node j to the root. Idea: set the parent pointer of all these node to points to the root. (Slightly more work to perform it, but pays off in next operations) Find_root( j ){ If p[j] j then p[j]=find_root(p[j]); return p[j] Make sense but how fast is it? Thm: Consider a set of n atoms Any sequence of m U/F operations takes O( m α(n) ). Here α(n) is the inverse function of Ackerman function, and is approaching infinity as n approaching infinity. However, it does it very slowly. α(n) < when n< 10 0. Connected Components in Undirected graphs Let G(V,E) be a graph. We say that a subset C of V is a connected component (CC) if 1. for every pair u,v C, there is a path connecting them, and all the vertices of this path belong to C. And in addition. For any vertex u C, and any vertex v that does not belong to C, there is no path in G(V,E) connecting u to v. Examples 1: If G(V,E) is connected then V is a CC. Example : If G(V,E) contains no edges, then every node is CC, which contains only itself. Example : If G(V,E) is a tree, and we deleted an edge from E, then in the resulting graph there are CCs.

Minimum Spanning Trees G(V,E) with positive weights on its edges. A Minimum spanning tree (MST) is any graph T such that 1. Every vertex of V appears in T, and. T is connected (has a path between every two vertices). T is a subset of E. Sum of weights of its edges are as small as possible Application: Kruskal algorithm Kruskal algorithm for finding a MST. Input: Graph G(V,E). Output: Minimal Spanning Tree for G. 1) Assume E={e 1,..e m is sorted from cheapest edge to most expensive edge. ) Set S=EmptySet. ) For i=1..m ) If e i S does not contain a cycle, add e i to S /* We use U/F structure to answer last test */ If E is sorted, then the time is O( E α( E ) ) 5