0% found this document useful (0 votes)
16 views

11.DisjointSets

The document discusses the disjoint-set data structure, which consists of distinct dynamic sets identified by representatives, and outlines operations such as MAKE-SET, UNION, and FIND-SET. It explains the implementation of disjoint sets using linked lists and forests, highlighting the inefficiencies of basic UNION operations and introducing optimizations like the weighted-union heuristic and path compression to improve performance. The worst-case running time for a sequence of operations is O(mα(n)), where α(n) is the inverse Ackermann function, making it nearly linear.

Uploaded by

radhakrishn1088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

11.DisjointSets

The document discusses the disjoint-set data structure, which consists of distinct dynamic sets identified by representatives, and outlines operations such as MAKE-SET, UNION, and FIND-SET. It explains the implementation of disjoint sets using linked lists and forests, highlighting the inefficiencies of basic UNION operations and introducing optimizations like the weighted-union heuristic and path compression to improve performance. The worst-case running time for a sequence of operations is O(mα(n)), where α(n) is the inverse Ackermann function, making it nearly linear.

Uploaded by

radhakrishn1088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Disjoint Sets Data Structure

• A disjoint-set is a collection s={S1, S2,…, Sk} of


distinct dynamic sets.
• Each set is identified by a member of the set,
called representative.
• Disjoint set operations:
– MAKE-SET(x): create a new set with only x. assume x
is not already in some other set.
– UNION(x,y): combine the two sets containing x and y
into one new set. A new representative is selected.
– FIND-SET(x): return the representative of the set
containing x.
Multiple Operations
• Suppose multiple operations:
– n: #MAKE-SET operations (executed at beginning).
– m: #MAKE-SET, UNION, FIND-SET operations.
– mn, #UNION operation is at most n-1.
An Application of Disjoint-Set
• Determine the connected components of an
undirected graph.

CONNECTED-COMPONENTS(G) SAME-COMPONENT(u,v)
1. for each vertex v V[G] 1. if FIND-SET(u)=FIND-SET(v)
2. do MAKE-SET(v) 2. then return TRUE
3. for each edge (u,v) E[G] 3. else return FALSE
4. do if FIND-SET(u)  FIND-SET(v)
5. then UNION(u,v)
Linked-List Implementation
• Each set as a linked-list, with head and tail, and
each node contains value, next node pointer and
back-to-representative pointer.
• Example:
• MAKE-SET costs O(1): just create a single
element list.
• FIND-SET costs O(1): just return back-to-
representative pointer.
Linked-lists for two sets
Set {c,h,e}
head c h e
tail

Set {f, g} head


f g
tail
UNION of
two Sets
head f g c h e
tail
UNION Implementation
• A simple implementation: UNION(x,y) just appends
x to the end of y, updates all back-to-representative
pointers in x to the head of y.
• Each UNION takes time linear in the x’s length.
• Suppose n MAKE-SET(xi) operations (O(1) each)
followed by n-1 UNION
– UNION(x1, x2), O(1),
– UNION(x2, x3), O(2),
– …..
– UNION(xn-1, xn), O(n-1)
• The UNIONs cost 1+2+…+n-1=(n2)
• So 2n-1 operations cost (n2), average (n) each.
• Not good!! How to solve it ???
Weighted-Union Heuristic
• Instead appending x to y, appending the shorter list to the
longer list.
• Associated a length with each list, which indicates how
many elements in the list.
• Result: a sequence of m MAKE-SET, UNION, FIND-SET
operations, n of which are MAKE-SET operations, the
running time is O(m+nlg n). Why???

•Hints: Count the number of updates to back-to-representative pointer


for any x in a set of n elements. Consider that each time, the UNION
will at least double the length of united set, it will take at most lg n
UNIONS to unite n elements. So each x’s back-to-representative pointer
can be updated at most lg n times.
Disjoint-set Implementation: Forests
• Rooted trees, each tree is a set, root is the
representative. Each node points to its
parent. Root points to itself.

c cf cf
h e d c d

Set {c,h,e} Set {f,d} h e UNION


Straightforward Solution
• Three operations
– MAKE-SET(x): create a tree containing x. O(1)
– FIND-SET(x): follow the chain of parent pointers until to the
root. O(height of x’s tree)
– UNION(x,y): let the root of one tree point to the root of the
other. O(1)
• It is possible that n-1 UNIONs results in a tree of
height n-1. (just a linear chain of n nodes).
• So n FIND-SET operations will cost O(n2).
Union by Rank & Path Compression
• Union by Rank: Each node is associated with a rank,
which is the upper bound on the height of the node (i.e.,
the height of subtree rooted at the node), then when
UNION, let the root with smaller rank point to the root
with larger rank.
• Path Compression: used in FIND-SET(x) operation, make
each node in the path from x to the root directly point to
the root. Thus reduce the tree height.
Path Compression
f f
e c d e
d

c
Algorithm for Disjoint-Set Forest
UNION(x,y)
MAKE-SET(x) 1. LINK(FIND-SET(x),FIND-SET(y))
1. p[x]x FIND-SET(x)
2. rank[x]0 LINK(x,y) 1. if x p[x]
1. if rank[x]>rank[y]
2. then p[y] x 2. then p[x] FIND-SET(p[x])
3. else p[x] y 3. return p[x]
4. if rank[x]=rank[y]
5. then rank[y]++

Worst case running time for m MAKE-SET, UNION, FIND-SET operations is:
O(m(n)) where (n)4. So nearly linear in m.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy