CPSC 311, Fall 2009 1 CPSC 311 Analysis of Algorithms Disjoint Sets Prof. Jennifer Welch Fall 2009.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
Union-Find structure Mikko Malinen School of Computing University of Eastern Finland.
1 Union-find. 2 Maintain a collection of disjoint sets under the following two operations S 3 = Union(S 1,S 2 ) Find(x) : returns the set containing x.
MA/CSSE 473 Day 38 Finish Disjoint Sets Dijkstra.
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 20 Prof. Erik Demaine.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
A Randomized Linear-Time Algorithm to Find Minimum Spanning Trees David R. Karger David R. Karger Philip N. Klein Philip N. Klein Robert E. Tarjan.
Andreas Klappenecker [Based on slides by Prof. Welch]
Disjoint-Set Operation
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
CPSC 411, Fall 2008: Set 7 1 CPSC 411 Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Jennifer Welch Fall 2008.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
Chapter 9: Union-Find Algorithms The Design and Analysis of Algorithms.
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Data Structures Week 9 Spanning Trees We will now consider another famous problem in graphs. Recall the bridges problem again. The purpose of the bridge.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
Chapter 6: Union-Find and Related Structures CS6310 ADVANCED DATA STRUCTURE SHADHA MUHI & HASNAA IMAD.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
CS 473Lecture X1 CS473-Algorithms I Lecture X1 Properties of Ranks.
Homework remarking requests BEFORE submitting a remarking request: a)read and understand our solution set (which is posted on the course web site) b)read.
Disjoint Sets Data Structure (Chap. 21) A disjoint-set is a collection  ={S 1, S 2,…, S k } of distinct dynamic sets. Each set is identified by a member.
CSC 252a: Algorithms Pallavi Moorthy 252a-av Smith College December 14, 2000.
Lecture X Disjoint Set Operations
Disjoint Sets Data Structure. Disjoint Sets Some applications require maintaining a collection of disjoint sets. A Disjoint set S is a collection of sets.
Union-find Algorithm Presented by Michael Cassarino.
Union Find ADT Data type for disjoint sets: makeSet(x): Given an element x create a singleton set that contains only this element. Return a locator/handle.
CSE373: Data Structures & Algorithms Lecture 10: Implementing Union-Find Dan Grossman Fall 2013.
CSCE 411H Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 7 1 * Slides adapted from.
Disjoint-Set Operation. p2. Disjoint Set Operations : MAKE-SET(x) : Create new set {x} with representative x. UNION(x,y) : x and y are elements of two.
Union-Find A data structure for maintaining a collection of disjoint sets Course: Data Structures Lecturers: Haim Kaplan and Uri Zwick January 2014.
Disjoint-sets Abstract Data Type (ADT) that contains nonempty pairwise disjoint sets: For all sets X and Y in the container, – X != ϴ and Y != ϴ – And.
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
0 Union-Find data structure. 1 Disjoint set ADT (also Dynamic Equivalence) The universe consists of n elements, named 1, 2, …, n n The ADT is a collection.
CMSC 341 Disjoint Sets. 2 Disjoint Set Definition Suppose we have N distinct items. We want to partition the items into a collection of sets such that:
Union By Rank Ackermann’s Function Graph Algorithms Rajee S Ramanikanthan Kavya Reddy Musani.
Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
CSE Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University.
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
CSE 373, Copyright S. Tanimoto, 2001 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case.
Data Structures for Disjoint Sets
Disjoint Sets Data Structure
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
Disjoint Sets Data Structure (Chap. 21)
Data Structures and Algorithms
Disjoint Sets with Arrays
Disjoint Set Neil Tang 02/23/2010
Disjoint Set Neil Tang 02/26/2008
CSCE 411 Design and Analysis of Algorithms
CSE 332: Data Abstractions Union/Find II
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Union-Find Partition Structures
Union-Find Partition Structures
Lecture 20: Disjoint Sets
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Disjoint Sets Data Structure (Chap. 21)
Presentation transcript:

CPSC 311, Fall CPSC 311 Analysis of Algorithms Disjoint Sets Prof. Jennifer Welch Fall 2009

CPSC 311, Fall Disjoint Sets Abstract Data Type State: collection of disjoint dynamic sets number of sets and composition can change, but must always be disjoint each set has representative element, serves as name of the set Operations: Make-Set(x): creates {x} and adds to collection of sets Union(x,y): replaces x's set S x and y's set S y with S x U S y Find-Set(x): returns (pointer to) the representative of the set containing x

CPSC 311, Fall Disjoint Sets Example Make-Set(a) Make-Set(b) Make-Set(c ) Make-Set(d) Union(a,b) Union(c,d) Find-Set(b) Find-Set(d) Union(b,d) b a c d returns a returns c

CPSC 311, Fall Sample Use of Disjoint Sets Algorithm to find all connected components in an undirected graph Initially make each graph vertex into its own disjoint set (use MakeSet) Consider each graph edge in turn: if the two endpoints are in different sets (use FindSet to test), then Union the two sets together End result is that there is one set in the disjoint sets data structure for each connected component of the graph, containing exactly the vertices of that component

CPSC 311, Fall Linked List Representation Store set elements in a linked list each list node has a pointer to the next list node First list node is set representative (rep) Each list node also has a pointer to the first list node (rep) Keep external pointers to first list node (rep) and last list node (tail)

CPSC 311, Fall Linked List Representation edafbc rep tailreptailreptail

CPSC 311, Fall Linked List Representation Make-Set(x): make a new linked list containing just a node for x O(1) time Find-Set(x): given (pointer to) linked list node containing x, follow rep pointer to head of list O(1) time Union(x,y): append x's list to end of y's list and update all rep pointers in x's old list to point to head of y's list O(size of x's old list) time

CPSC 311, Fall Time Analysis What is worst-case time for any sequence of Disjoint Set operations, using the linked list representation? Let m be number of ops in the sequence Let n be number of Make-Set ops (i.e., number of elements)

CPSC 311, Fall Expensive Case MS(x 1 ), MS(x 2 ), …, MS(x n ), U(x 1,x 2 ), U(x 2,x 3 ), …, U(x n-1,x n ) Total time is O(n 2 ), which is O(m 2 ) since m = 2n - 1 So amortized time per operation is O(m 2 )/m = O(m)

CPSC 311, Fall Linked List with Weighted Union Always append smaller list to larger list Need to keep count of number of elements in list (weight) in rep node Calculate worst-case time for a sequence of m operations. Make-Set and Find-Set operations contribute O(m) total

CPSC 311, Fall Analyzing Time for All Unions How many times can the rep pointer for an arbitrary node x be updated? First time x's rep pointer is updated, the new set has at least 2 elements Second time x's rep pointer is updated, the new set has at least 4 elements x's set has at least 2 and the other set is at least as large as x's set

CPSC 311, Fall Analyzing Time for All Unions The maximum size of a set is n (the number of Make-Set ops in the sequence) So x's rep pointer can be updated at most log n times. Thus total time for all unions is O(n log n). Note style of counting - focus on one element and how it fares over all the Unions

CPSC 311, Fall Amortized Time Grand total for sequence is O(m+n log n) Amortized cost per Make-Set and Find-Set is O(1) Amortized cost per Union is O(log n) since there can be at most n - 1 Union ops.

CPSC 311, Fall Tree Representation Can we improve on the linked list with weighted union representation? Use a collection of trees, one per set The rep is the root Each node has a pointer to its parent in the tree

CPSC 311, Fall Tree Representation a de b f c

CPSC 311, Fall Analysis of Tree Implementation Make-Set: make a tree with one node O(1) time Find-Set: follow parent pointers to root O(h) time where h is height of tree Union(x,y): make the root of x's tree a child of the root of y's tree O(1) time So far, no better than original linked list implementation

CPSC 311, Fall Improved Tree Implementation Use a weighted union, so that smaller tree becomes child of larger tree prevents long chains from developing can show this gives O(m log n) time for a sequence of m ops with n Make-Sets Also do path compression during Find-Set flattens out trees even more can show this gives O(m log*n) time!

CPSC 311, Fall What is log*n ? The number of times you can successively take the log, starting with n, before reaching a number that is at most 1 More formally: log*n = min{i ≥ 0 : log (i) n ≤ 1} where log (i) n = n, if i = 0, and otherwise log (i) n = log(log (i-1) n)

CPSC 311, Fall Examples of log*n nlog*nwhy 000 < ≤ 1 21log 2 = 1 32log 3 > 1, log(log 3) < 1 42log(log 4) = log 2 = 1 53log(log 5) > 1, log(log(log 5)) < 1 … 163log(log(log 16) = log(log 4) = log 2 = 1 174log(log(log 17) > 1, log(log(log(log 17))) < 1 … 65,5364log(log(log(log 65,536) = log(log(log 16) = log(log 4) = log 2 = 1 65,5375 … 2 65,537 5

CPSC 311, Fall log*n Grows Slowly For all practical values of n, log*n is never more than 5.

CPSC 311, Fall Make-Set Make-Set(x): parent(x) := x rank(x) := 0 // used for weighted union

CPSC 311, Fall Union Union(x,y): r := Find-Set(x); s := Find-Set(y) if rank(r) > rank(s) then parent(s) := r else parent(r) := s if rank(r ) = rank(s) then rank(s)++

CPSC 311, Fall Rank gives upper bound on height of tree is approximately the log of the number of nodes in the tree Example: MS(a), MS(b), MS(c), MS(d), MS(e), MS(f), U(a,b), U(c,d), U(e,f), U(a,c), U(a,e)

CPSC 311, Fall End Result of Rank Example d c b a f e

CPSC 311, Fall Find-Set Find-Set(x): if x  parent(x) then parent(x) := Find-Set(parent(x)) return parent(x) Unroll recursion: first, follow parent pointers up the tree then go back down the path, making every node on the path a child of the root

CPSC 311, Fall Find-Set(a) a b c d e e dcba

CPSC 311, Fall Time Analysis It can be shown that any sequence of m Disjoint Set operations, n of which are Make-Sets, takes O(m log*n) time with the improved tree implementation.

CPSC 311, Fall Even Better Bound By working even harder, it can be shown that the worst-case time is O(m  (n)), where  is a function that grows even more slowly than log*n: for all practical values of n,  (n) is never more than 4.