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

33
DESIGN & ANALYSIS OF ALGORITHM 06 – DISJOINT SETS Informatics Department Parahyangan Catholic University

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

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

DESIGN & ANALYSIS OF ALGORITHM06 – DISJOINT SETS

Informatics Department

Parahyangan Catholic University

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

DISJOINT SETS

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

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

DISJOINT SETS DATA STRUCTURE

Maintains a collection S = {S1, S2, … , Sk} 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)

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

EXAMPLE : GRAPH’S CONNECTED COMPONENT

CONNECTED-COMPONENTS(G)

for each vertex v VMAKE-SET(v)

for each edge (u,v) Eif 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

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

REPRESENTATION #1 : LINKED LIST

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)

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

REPRESENTATION #1 : LINKED LIST

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

head tail

b c

d

head tail

e

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

WEIGHTED UNION

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 ?

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

REPRESENTATION #2 : ROOTED TREES

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

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

REPRESENTATION #2 : ROOTED TREES

b c

e f g

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

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

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

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

CAN WE DO BETTER ?

A

B

+ =

A

B

A

BOR

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

UNION BY RANK

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?

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

PATH COMPRESSION

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?

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

WHAT IS THE TIME COMPLEXITY ?

How do Union By Rank and Path Compression improves the time complexity ?

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

IMPLEMENTATION

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.

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

EXAMPLE

Apple

Melon Shoes

BananaGrapeT-Shirt

idx element

1 Apple

2 Melon

3 Shoes

4 Banana

5 Grape

6 T-Shirt

1

2 3

456

2

1

5

4

3

6

idx 1 2 3 4 5 6

parent 1 1 3 2 1 3

rank 2 1 1 0 0 0

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

APPLICATION : KRUSKAL’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

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

KRUSKAL’S MST ALGORITHM

MST-KRUSKAL(G, w)

T = empty graph

for each v VMAKE-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

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10

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

EXAMPLE

a

b c

i

h g f

d

e

8

8

11

4

7

4

2

7 6

1 2

14

9

10