Data Structures for representative member. Disjoint Sets ! - - PDF document

data structures for
SMART_READER_LITE
LIVE PREVIEW

Data Structures for representative member. Disjoint Sets ! - - PDF document

Disjoint Sets Data Structure ! A dynamic collection S = {S 1 ,S 2 ,,S k } of disjoint sets. ! Each set S i is identified by a Data Structures for representative member. Disjoint Sets ! Operations: Make-Set(x): create a new set in S,


slide-1
SLIDE 1

1

Data Structures for Disjoint Sets

2

Disjoint Sets Data Structure

! A dynamic collection S = {S1,S2,…,Sk}

  • f disjoint sets.

! Each set Si is identified by a

representative member.

! Operations:

– Make-Set(x): create a new set in S, whose

  • nly member is x (assuming x is not

already in one of the sets). – Union(x, y): replace the two sets Sx and Sy that contain x and y, by their union (assuming they are disjoint). – Find-Set(x): find and return the representative of the set containing x.

3

Application: connected components

! Given a graph G=(V,E) compute its

partitioning into connected components. Connected-Components(G=(V,E)): for each vertex v in V Make-Set(v); for each edge e in E if (Find-Set(u) != Find-Set(v)) Union(u, v); Same-Component(u, v): return (Find-Set(u) == Find-Set(v));

4

Example

! S = {{a},{b},{c},{d},{e},{f},{g},{h},{i},{j}} ! S = {{a},{b,d},{c},{e},{f},{g},{h},{i},{j}} ! S = {{a},{b,d},{c},{e,g},{f},{h},{i},{j}} ! S = {{a,c},{b,d},{e,g},{f},{h},{i},{j}} ! S = {{a,c},{b,d},{e,g},{f},{h,i},{j}} ! S = {{a,c,b,d},{e,g},{f},{h,i},{j}} ! S = {{a,c,b,d},{e,f,g},{h,i},{j}} ! S = {{a,c,b,d},{e,f,g},{h,i},{j}}

slide-2
SLIDE 2

5

A linked lists representation

! Each set in the collection is

represented by a linked list.

! First element in each list is the

representative of the set.

! Each element holds a pointer to the

representative.

! Make-Set and Find-Set take O(1) time. ! Union of two sets S1 and S2 takes time

O(min(|S1|,|S2|)) using the weighted-union heuristic.

6

Example

! S={S1, S2}, S1={c,h,e,b}, S2={f,g,d} ! The result of Union(e,g):

7

Analysis

! Amortized analysis ! Theorem (22.1): Using the linked-list

representation of disjoint sets and the weighted-union heuristic, a sequence

  • f m MakeSet, Union, and FindSet
  • perations, n of which are MakeSet
  • perations, takes

time.

! Proof idea: show that the pointer to

representative cannot be updated more than times.

( )

lg O m n n +

lgn    

8

Disjoint-Set Forests

! Each set is a represented by a tree,

and the representative is the root.

! Each element points to its parent in

the tree (the root points to itself).

! How long do the different operations

take?

– Make-Set – Find-Set – Union

slide-3
SLIDE 3

9

Example

! S={S1, S2}, S1={c,h,e,b}, S2={f,g,d} ! The result of Union(e,g):

10

Heuristics

! Union by rank: always make the root

  • f the smaller tree become the child of

the root of the larger tree.

! Path compression: during a Find-Set

  • peration, make each node on the find

path point directly to the root.

11

Path Compression

! Before: ! After performing Find-Set(a):

12

Pseudocode

makeSet(x) { x.parent = x; x.rank = 0; } findSet(x) { if (x != x.parent) x.parent = findSet(x.parent); return x.parent; } Union(x, y) { Link(findSet(x), findSet(y)); } Link(x, y) { if (x.rank > y.rank) y.parent = x; else { x.parent = y; if (x.rank == y.rank) y.rank++; } }

slide-4
SLIDE 4

13

Analysis

! A very quickly growing function: ! For example, ! A very slowly growing inverse: ! For all practical purposes:

( ) ( )

( 1) 1

1 if if 1

k j k

j k A j A j k

+ −

+ =  =  ≥ 

80 4(1)

10 A !

( )

{ }

( ) min : 1

k

n k A n α = ≥ ( ) 4 n α ≤

14

Analysis

! Theorem: A sequence of m MakeSet,

Union, and FindSet operations, n of which are FindSet operations, can be performed on a disjoint-set forest with union by rank and path compression in worst case time

! For all practical purposes this time is

linear in m, so each operation has constant amortized cost.

( )

( ) O m n α