Chapter 8: The Disjoint Set Class Equivalence Classes Disjoint Set ADT CS 340 Page 132 Kruskal’s Algorithm Disjoint Set Implementation.

Slides:



Advertisements
Similar presentations
Equivalence Relations
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
CSC401 – Analysis of Algorithms Lecture Notes 14 Graph Biconnectivity
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CMPS 2433 Discrete Structures Chapter 5 - Trees R. HALVERSON – MIDWESTERN STATE UNIVERSITY.
EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Disjoint Union / Find CSE 373 Data Structures Lecture 17.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
1 Chapter 8 The Disjoint Set ADT Concerns with equivalence problems Find and Union.
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.
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COMP 261 Lecture 12 Disjoint Sets. Menu Kruskal's minimum spanning tree algorithm Disjoint-set data structure and Union-Find algorithm Administrivia –Marking.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
 2004 SDU Lecture 7- Minimum Spanning Tree-- Extension 1.Properties of Minimum Spanning Tree 2.Secondary Minimum Spanning Tree 3.Bottleneck.
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Lauren Milne Spring 2015.
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions.
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,
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Disjoint Sets.
The Disjoint Set Class Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 24 © 2002 Addison Wesley.
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.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Nattee Niparnan. Greedy If solving problem is a series of steps Simply pick the one that “maximize” the immediate outcome Instead of looking for the long.
CHAPTER 8 THE DISJOINT SET ADT §1 Equivalence Relations 【 Definition 】 A relation R is defined on a set S if for every pair of elements (a, b), a, b 
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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:
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free.
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets and the Union-Find ADT Lauren Milne Summer 2015.
Chapter 8: Relations. 8.1 Relations and Their Properties Binary relations: Let A and B be any two sets. A binary relation R from A to B, written R : A.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
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.
Chapter8 Relations 8.1: Relations and their properties.
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Linda Shapiro Spring 2016.
Biconnectivity SEA PVD ORD FCO SNA MIA 11/16/2018 2:31 AM
Minimum-Cost Spanning Tree
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Linda Shapiro Winter 2015.
Minimum Spanning Tree.
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets & Union-Find Dan Grossman Fall 2013.
The UNION-FIND Abstract Data Type
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Minimum Spanning Trees
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Biconnectivity SEA PVD ORD FCO SNA MIA 5/6/2019 5:08 PM Biconnectivity
Lecture 12 Algorithm Analysis
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Biconnectivity SEA PVD ORD FCO SNA MIA 5/23/ :21 PM
Minimum Spanning Trees
Disjoint Sets Textbook Chapter 8
Minimum-Cost Spanning Tree
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

Chapter 8: The Disjoint Set Class Equivalence Classes Disjoint Set ADT CS 340 Page 132 Kruskal’s Algorithm Disjoint Set Implementation

CS 340 Page 133 Background: Equivalence Relations A relation R on a set S maps every pair of elements in S to either TRUE or FALSE. For example, the greater than relation, >, is TRUE for the integer pair (5,3) (i.e., 5 > 3) but not for the pair (6,8) (since 6>8 is FALSE). An equivalence relation R is a relation satisfying the following three properties: (Reflexive)  R  for every  in set S. (Symmetric) For every pair  and  in set S, if  R , then  R . (Transitive) For every triple , ,  in set S, if  R  and  R , then  R . The Disjoint Set Class For example… Modulo-10 equality is an equivalence relation for integers.Modulo-10 equality is an equivalence relation for integers. The greater than relation is not an equivalence relation for integers (since it lacks the symmetric property).The greater than relation is not an equivalence relation for integers (since it lacks the symmetric property). The does not equal relation is not an equivalence relation for integers (since it lacks both the reflexive and transitive properties).The does not equal relation is not an equivalence relation for integers (since it lacks both the reflexive and transitive properties). The electrical connectivity relation is an equivalence relation for network system components (i.e., endstations, servers, switches).The electrical connectivity relation is an equivalence relation for network system components (i.e., endstations, servers, switches).

CS 340 Page 134 Equivalence Classes The Equivalence Classes for Mod-3 Equality The Equivalence Classes for Electrical Connectivity An equivalence relation R splits the set S up into disjoint (i.e., non-intersecting) subsets called equivalence classes, such that: Every pair of elements in the same equivalence class is R-equivalent. Every pair of elements from different equivalence classes is not R-equivalent.

CS 340 Page 135 The Disjoint Set ADT AA BBCCDD EEFFGGHHIIJJKK LLMMNNOO PPQQ Let’s define an abstract data type to implement equivalence relations. The data will consist of the set S, divided into equivalence classes. The desired operations will be as follows: A Find operation that determines which equivalence class contains a particular set element. A Union operation that merges two previously separate equivalence classes. Example: Finding Nearest Common Ancestors in a Tree To find the nearest common ancestor for two nodes U and V in a tree: Originally, set up each node as its own equivalence class. Then traverse the tree in postorder… Every time you return from traversing offspring Y of parent X, perform Union(Find(X),Find(Y)) and mark X as the “anchor” node for Find(X). Continue until Find(U) and Find(V) are the same. At that point, their common anchor is their nearest common ancestor.

CS 340 Page 136 Example: Kruskal’s Algorithm A spanning tree for a graph is a tree formed from edges of the graph that connects all of the graph vertices. A minimum spanning tree is one in which the sum of the costs in the tree is minimized (where every edge is assumed to have an associated “cost”). Kruskal’s Algorithm finds a minimum spanning tree by repeatedly adding the previously excluded edge with the least cost without creating a loop. (Minimum spanning trees are particularly useful in such applications as network routing). Originally, each node is its own equivalence class; a Union operation is performed between distinct equivalence classes Find(X) and Find(Y) whenever X and Y have a minimum cost edge between them.

CS 340 Page 137 Applying Kruskal’s Algorithm ORIGINAL GRAPH BB AA DD CC EE FFGG HH BB AA DD CC EE FFGG HH BBCC EE FFGG HH 33 AA DD BB AADD CC EE FFGG HH BB AADD CC EE FFGG HH BB AADD CC EE FFGG HH BB AADD CC EE FFGG HH BB AADD CC EE FFGG HH BB AADD CC EE FFGG HH MINIMUM SPANNING TREE BB AADD CC EE FFGG HH

CS 340 Page 138 Disjoint Set Union Operations Seven elements, originally in distinct sets After Union(C,D) and Union(E,F) 1 1 Element A 2 2 Element B 3 3 Element C 4 4 Element D 5 5 Element E 6 6 Element F 7 7 Element G After Union(C,E) 1 1 Element A 2 2 Element B 3 3 Element C 4 4 Element D 5 5 Element E 6 6 Element F 7 7 Element G 1 1 Element A 2 2 Element B 3 3 Element C 4 4 Element D 5 5 Element E 6 6 Element F 7 7 Element G

CS 340 Page 139 Disjoint Set Union Operation Choices After Union(B,C) with random union (Depth can become linear.) After Union(B,C) with union-by-size or union-by-height (Depth remains logarithmic.) 1 1 Element A 2 2 Element B 3 3 Element C 4 4 Element D 5 5 Element E 6 6 Element F 7 7 Element G 1 1 Element A 2 2 Element B 3 3 Element C 4 4 Element D 5 5 Element E 6 6 Element F 7 7 Element G

CS 340 Page 140 Disjoint Set Implementation When using random union, merely use an array filled with the slot numbers of the parent nodes (with zero for nodes with no parent) When using union-by-size, merely use an array filled with the slot numbers of the parent nodes, and the negation of the size of the tree for nodes with no parent When using union-by-height, merely use an array filled with the slot numbers of the parent nodes, the negation of the height of the tree for nodes with offspring but no parent, and zero for the nodes with no offspring and no parent Element A 2 2 Element B 3 3 Element C 4 4 Element D 5 5 Element E 6 6 Element F 7 7 Element G 1 1 Element A 2 2 Element B 3 3 Element C 4 4 Element D 5 5 Element E 6 6 Element F 7 7 Element G

CS 340 Page 141 Disjoint Set Application: Maze Generation Starting with a complete grid of walls, remove the two representing the entrance and the exit, and consider each cell in the grid as a disjoint set. Randomly remove walls that separate two disconnected cells, performing a union of the cells. Continue until there is only one cell left.

CS 340 Page 142 Disjoint Set Application: Colorization A monochrome image is analyzed one scanline at a time, with white pixels grouped in equivalence classes whenever it is determined that their respective regions are separated by black border pixels. Union operations occur whenever a non-border path is discovered between two regions.