D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chapter 23 Minimum Spanning Tree
Greed is good. (Some of the time)
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.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum Spanning Tree (MST) form a tree that connects all the vertices (spanning tree). minimize the total edge weight of the spanning tree. Problem Select.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
Andreas Klappenecker [Based on slides by Prof. Welch]
Disjoint-Set Operation
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
CPSC 411, Fall 2008: Set 7 1 CPSC 411 Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Jennifer Welch Fall 2008.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Analysis of Algorithms CS 477/677
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.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
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.
Tirgul 13 Today we’ll solve two questions from last year’s exams.
Minimum Spanning Trees. a b d f g e c a b d f g e c.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State 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.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
Analysis of Algorithms
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
Definition: Given an undirected graph G = (V, E), a spanning tree of G is any subgraph of G that is a tree Minimum Spanning Trees (Ch. 23) abc d f e gh.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm Binary Search Trees1.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
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.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
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.
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.
MST – KRUSKAL UNIT IV. Disjoint-Set Union Problem Want a data structure to support disjoint sets – Collection of disjoint sets S = {S i }, S i ∩ S j =
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
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.
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.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
Tirgul 12 Solving T4 Q. 3,4 Rehearsal about MST and Union-Find
Lecture ? The Algorithms of Kruskal and Prim
Disjoint Sets Data Structure
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Disjoint Sets with Arrays
CS200: Algorithm Analysis
CSC 413/513: Intro to Algorithms
CSE 373: Data Structures and Algorithms
Minimum Spanning Trees
Lecture 20: Disjoint Sets
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Minimum Spanning Trees
Presentation transcript:

D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University

D ISJOINT S ETS Some applications involve grouping n distinct elements into a collection of disjoint sets Two important operations: Find which set a given element belongs to Uniting two sets

D ISJOINT S ETS D ATA S TRUCTURE Maintains a collection S = {S 1, S 2, …, S k } of disjoint dynamic sets. Each set is identified by a representative, which is some member of the set. Operations : MAKE-SET(x) UNION(x, y) FIND-SET(x)

E XAMPLE : G RAPH ’ S C ONNECTED C OMPONENT CONNECTED-COMPONENTS(G) for each vertex v  V MAKE-SET(v) for each edge (u,v)  E if FIND-SET(u) ≠ FIND-SET(v) UNION(u,v) SAME-COMPONENT(u,v) if FIND-SET(u) == FIND-SET(v) return TRUE else return FALSE Uses the disjoint-set operations to compute connected the components of a graph Answers queries about whether two vertices are in the same connected component

R EPRESENTATION #1 : L INKED L IST Each list represents one set The first element of the list is the set’s representative Each node points to the set’s representative (first element of the list)

R EPRESENTATION #1 : L INKED L IST MAKE-SET and FIND-SET are easy, requiring O(1) time UNION requires O(n) time for updating the pointer to set’s representative a headtail bc d headtail e

W EIGHTED U NION Maintains the size of each list UNION operation adds the shorter list to the longer list The number of pointers updated is minimized Still takes O(n) time if the number of elements of both lists are O(n) How do weighted- union improves the running time ?

R EPRESENTATION #2 : R OOTED T REES Each set represented as a tree Each node points only to its parent The root of each tree is the set’s representative, and is its own parent

R EPRESENTATION #2 : R OOTED T REES b c e fg h d a j k l i MAKE-SET creates a tree consisting of one node FIND-SET simply follows the parent pointer until it finds the root UNION makes the root of one tree to points to the other tree’s root

W HAT IS THE TIME COMPLEXITY ? MAKE-SET is clearly O(1) FIND-SET traces back to the root UNION needs to find the roots of the two trees, thus performing 2x FIND-SET → O(n) → also O(n) Why ?

C AN WE DO BETTER ? A B + = A B A B OR

U NION B Y R ANK Similar to WEIGHTED-UNION, we add the shorter tree as a child of the taller tree Keep track of rank of each tree, that is the upper bound of the tree’s height How?

P ATH C OMPRESSION For each node, we only need to know which node is the root During FIND-SET, change the parent pointer of each node in find path to directly point to the root Leave rank unchanged a b c d a b c d = How?

W HAT IS THE TIME COMPLEXITY ? How do Union By Rank and Path Compression improves the time complexity ?

I MPLEMENTATION Using two arrays: Array of parents Array of ranks If the elements are not integer, use another array to map each element into an integer.

E XAMPLE Apple Melon Shoes Banana Grape T-Shirt idxelement 1Apple 2Melon 3Shoes 4Banana 5Grape 6T-Shirt idx parent rank211000

A PPLICATION : K RUSKAL ’ S MST A Spanning Tree of a graph G is an acyclic subset T  E that connects all of G’s vertices The weight of T is the sum of all its edges’ weight Such T with minimum possible weight is called the Minimum Spanning Tree (MST) of G Kruskal’s MST algorithm is a greedy algorithm for finding the MST of graph G

K RUSKAL ’ S MST A LGORITHM MST-KRUSKAL(G, w) T = empty graph for each v  V MAKE-SET(v) sort the edges of E into non-decreasing order by weight w for each edge (u,v)  E, taken in non- decreasing order by weight if FIND-SET(u) ≠ FIND-SET(v) T = T  {(u,v)} UNION(u,v) return T

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e

E XAMPLE a bc i hgf d e