CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.

Slides:



Advertisements
Similar presentations
Lecture 10 Disjoint Set ADT.
Advertisements

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.
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
Union-Find: A Data Structure for Disjoint Set Operations
Andreas Klappenecker [Based on slides by Prof. Welch]
CS2420: Lecture 19 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 43 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
CPSC 411, Fall 2008: Set 7 1 CPSC 411 Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Jennifer Welch Fall 2008.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Disjoint Sets Prof. Jennifer Welch Fall 2009.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Vladimir Kulyukin Computer Science Department Utah State University
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.
CS2420: Lecture 15 Vladimir Kulyukin Computer Science Department Utah State University.
Lecture 9 Disjoint Set ADT. Preliminary Definitions A set is a collection of objects. Set A is a subset of set B if all elements of A are in B. Subsets.
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
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.
Data Structures & Algorithms Union-Find Example Richard Newman.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Trees Featuring Minimum Spanning Trees HKOI Training 2005 Advanced Group Presented by Liu Chi Man (cx)
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
COMP 261 Lecture 12 Disjoint Sets. Menu Kruskal's minimum spanning tree algorithm Disjoint-set data structure and Union-Find algorithm Administrivia –Marking.
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.
CMSC 341 Disjoint Sets Textbook Chapter 8. Equivalence Relations A relation R is defined on a set S if for every pair of elements (a, b) with a,b  S,
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.
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 11: Implementing Union-Find Nicki Dell Spring 2014.
CSE373: Data Structures & Algorithms Lecture 10: Implementing Union-Find Dan Grossman Fall 2013.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Disjoint Sets.
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.
1 The Disjoint Set ADT CS146 Chapter 8 Yan Qing Lei.
1 Today’s Material The dynamic equivalence problem –a.k.a. Disjoint Sets/Union-Find ADT –Covered in Chapter 8 of the textbook.
Minimum Spanning Trees Featuring Disjoint Sets HKOI Training 2006 Liu Chi Man (cx) 25 Mar 2006.
Union-Find A data structure for maintaining a collection of disjoint sets Course: Data Structures Lecturers: Haim Kaplan and Uri Zwick January 2014.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
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:
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.
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.
CSE 373: Data Structures and Algorithms
Disjoint Sets Data Structure
CMSC 341 Disjoint Sets Based on slides from previous iterations of this course.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
Disjoint Set Neil Tang 02/23/2010
Disjoint Set Neil Tang 02/26/2008
CS200: Algorithm Analysis
CSE 373 Data Structures and Algorithms
ICS 353: Design and Analysis of Algorithms
CSE 373: Data Structures and Algorithms
CSE 326 Union Find Richard Anderson Text Chapter CSE 326.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Running Time Analysis Union is clearly a constant time operation.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
CSE 373: Data Structures and Algorithms
Presentation transcript:

CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University

Outline Graph Algorithms (Chapter 9)

Initial Graph BC A D F E

Kruskal’s Algorithm: Basic Insight A cycle is created if and only if the new edge connects two vertices already connected by a path, i.e., belonging to the same tree.

Kruskal’s: Basic Ideas Initially, all vertices are in separate trees. Subsequently, when an edge (u, v) is examined, we look for the tree containing u and the tree containing v. If the trees are NOT the same, we unite them into a larger tree containing both u, v and (u, v).

Kruskal’s Algorithm: UnionFind Kruskal’s Algorithm can be implemented with the UnionFind data structure. The UnionFind data structure was designed to solve the problem of partitioning a given set into a collection of disjoint subsets. The set partitioning problem has many applications in data mining, information retrieval, molecular biology, bioinformatics, etc.

Disjoint Subsets of a Set Let S be a set of n elements. Partition S into a collection of k disjoint subsets S 1, S 2, S 3,..., S k.

Disjoint Subsets of a Set Set S of n elements

Disjoint Subsets of a Set Partitioning of S into 5 disjoint subsets

Example: Partition S into 6 Disjoint Subsets S = {1, 2, 3, 4, 5, 6}, k = 6 –S 1 = {1} –S 2 = {2} –S 3 = {3} –S 4 = {4} –S 5 = {5} –S 6 = {6}

Example: Partition S into 5 Disjoint Subsets S = {1, 2, 3, 4, 5, 6}, k = 5 –S 1 = {1, 2} –S 2 = {3} –S 3 = {4} –S 4 = {5} –S 5 = {6}

UnionFind Data Structure: Operations The UnionFind data structure has the following operations: –make a set out of a single element (singleton). –find a subset that contains some element x. –compute the union of two disjoint subsets, the first of which contains x and the second of which contains y.

UnionFind: Operations MakeSet(x) - creates a one-element set {x}. Find(x) - returns a subset containing x. Union(x, y) - constructs the union of the disjoint sets S x and S y such that S x contains x and S y contains y. S x U S y replaces both S x and S y in the collection of subsets.

MakeSet: Example S = {1, 2, 3, 4, 5, 6}. MakeSet(1); MakeSet(2); MakeSet(3); MakeSet(4); MakeSet(5); MakeSet(6); The above sequence of MakeSet operations gives us: {{1}, {2}, {3}, {4}, {5}, {6}}.

Union: Examples {{1}, {2}, {3}, {4}, {5}, {6}} Union(1, 4)  {{1, 4}, {2}, {3}, {5}, {6}} Union(5, 2)  {{1, 4}, {5, 2}, {3}, {6}}

Union: Examples {{1, 4}, {5, 2}, {3}, {6}} Union(4, 5)  {{1, 4, 5, 2}, {3}, {6}} Union(3, 6)  {{1, 4, 5, 2}, {3, 6}}

UnionFind: Implementation Use one element of each disjoint set as a representative. Two implementation alternatives: –QuickFind: Find = O(1); Union = O(N). –QuickUnion: Union = O(1); Find = O(N).

QuickFind Two data structures: –An array of representatives; this array maps each element into its representative. –An array of subsets; this array contains each subset implemented as a linked list.

{{1}, {2}, {3}, {4}, {5}, {6}} Representatives Subsets

Union(1, 4) => {{1, 4}, {2}, {3}, {5}, {6}} Representatives Subsets Null

Union(5, 2) => {{1, 4}, {5, 2}, {3}, {6}} Representatives Subsets Null

Union(4, 5) => {{1, 4, 5, 2}, {3}, {6}} Representatives Subsets Null

union(3, 6) => {{1, 4, 5, 2}, {3, 6}} Representatives Subsets Null

QuickFind: Asymptotic Analysis MakeSet(x) = O(1) Why? All we need to do is to create a list and add x to it.

QuickFind: Asymptotic Analysis Find(x) = O(1) Why? All we need to do is look up x’s representative and then return the list to which the representative points.

QuickFind: Asymptotic Analysis Union(x, y) = O(n) Here is what we need to do to compute Union(x, y): –Do Find(x) and Find(y); // O(1) –Append Find(y) to the end of Find(x); // O(1) –Set the y-subset in the subsets array to NULL; // O(1) –Update the representatives for each element in Find(y); // O(N).

QuickFind: Asymptotic Analysis Consider the following sequence of unions: –union(2, 1), union(3, 2), union(4, 3),..., union(n, n-1). The first union requires 1 step, the second - 2 steps, the third - 3 steps,..., the n-th - n-1 steps. The sequence of n unions is asymptotically quadratic: (n - 1) = O(n 2 ).

QuickFind: Optimizations When performing the union operation, always append the shorter list to the longer one (union by size). The worst case run time of any legitimate sequence of unions is O(nlogn). The worst case run time of a sequence of n-1 unions and m finds is O(nlogn + m).

QuickUnion Two data structures: –An array of nodes, each containing exactly one element. –An array of trees, each containing one subset. –The root of each tree is the representative for the subset represented by that tree. –Each node has a pointer to its parent so that we can get to the root.

{{1}, {2}, {3}, {4}, {5}, {6}} Nodes Trees

Union(1, 4) => {{1, 4}, {2}, {3}, {5}, {6}} Nodes Trees Null

Union(5, 2) => {{1, 4}, {5, 2}, {3}, {6}} Nodes Trees Null

Union(4, 5) => {{1, 4, 5, 2}, {3}, {6}} Nodes Trees Null

Union(3, 6) => {{1, 4, 5, 2}, {3, 6}} Nodes Trees Null

Union(5, 6) => {1, 4, 5, 2, 3, 6} Nodes Trees Null

Quick Union: Asymptotic Analysis MakeSet(x) = O(1) Why? All we need to do is to create a tree node and add x to it.

QuickUnion: Asymptotic Analysis Find(x) = O(n) Why? We need to look up x’s node and then chase the upward pointers to the root of the tree that contains x.

QuickUnion: Asymptotic Analysis Union(x, y) = O(1) Why? –Do Find(x) and Find(y); // O(1) –Attach the root of Find(y) to the root of Find(x); // O(1) –Set the pointer in the tree array that used to point to Find(y) to NULL; // O(1)

QuickUnion: Optimization We can optimize the union operation by always attaching the root of a larger tree to the root of the smaller tree. Two alternatives: –Union by size - the size of the tree is the number of nodes in the tree. –Union by rank - the rank of the tree is the tree’s height.

QuickUnion: Optimization If union by size or union by rank is used, the height of the tree is logn. The time efficiency of a sequence of n-1 unions and m finds is O(n + mlogn).