Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University."— Presentation transcript:

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

2 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

3 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)

4 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

5 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)

6 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

7 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 ?

8 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

9 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

10 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 ?

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

12 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?

13 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?

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

15 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.

16 E XAMPLE Apple Melon Shoes Banana Grape T-Shirt idxelement 1Apple 2Melon 3Shoes 4Banana 5Grape 6T-Shirt 1 2 3 4 5 6 2 1 5 4 3 6 idx123456 parent113213 rank211000

17 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

18 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

19 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

20 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

21 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

22 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

23 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

24 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

25 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

26 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

27 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

28 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

29 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

30 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

31 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

32 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10

33 E XAMPLE a bc i hgf d e 8 8 11 4 7 4 2 7 6 12 14 9 10


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

Similar presentations


Ads by Google