0% found this document useful (0 votes)
85 views67 pages

The Disjoint Set ADT

The document discusses the disjoint set data structure. The disjoint set ADT represents sets as trees and supports three operations: MAKE-SET creates a new set with one element, FIND-SET returns the set containing a given element, and UNION combines two sets into one. Two heuristics improve the performance of the disjoint set ADT: the smart union algorithm joins sets based on size, and path compression optimizes FIND-SET to directly link each node to the root.

Uploaded by

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

The Disjoint Set ADT

The document discusses the disjoint set data structure. The disjoint set ADT represents sets as trees and supports three operations: MAKE-SET creates a new set with one element, FIND-SET returns the set containing a given element, and UNION combines two sets into one. Two heuristics improve the performance of the disjoint set ADT: the smart union algorithm joins sets based on size, and path compression optimizes FIND-SET to directly link each node to the root.

Uploaded by

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

The Disjoint Set ADT

 A disjoint set data structure keeps note of a set of non-


overlapping subsets. It is a data structure that helps us
solve the dynamic equivalence problem.
EQUIVALENCE RELATIONS

A relation is represented by ψ. Let a and b be the two elements of set S. We


say that a ψ b if a and b are related (i.e. if they belong to the same set).
 
An equivalence relation is one that satisfies the following three properties.
 i. Reflexive: a ψ a, for all a S
 ii. Symmetric: a ψ b if and only if b ψ a
 iii. Transitive: a ψ b and b ψ c => a ψ c

The  relationship is not an equivalence relationship.


Two cities are related if they are in the same country. This relation is an

equivalence relation if all the roads are two-way.


DYNAMIC EQUIVALENCE PROBLEM
 The dynamic equivalence problem expresses the problem of
deciding, given two elements a and b, if a ψ b.
 The problem is often because the relation is not explicitly, but
implicitly defined.
 To decide if a ψ b, we need only to check whether a and b are
in the same equivalence class.
Operations on disjoint sets
We are given an input of n sets each containing one element. Initially all
sets are represented in such a way that they are not related. This means
that all the sets are disjoint and can be represented as Si .
 
The three permissible operations on disjoint sets are:
 
i. MAKE-SET: It creates a set with only one member in each set.
ii.FIND-SET: Returns the name of the set (equivalence class) containing a

given element
iii.UNION: If we want to add the relation a ψ b, then we first see if a and b

are already related.


The Disjoint Set ADT
 Given an equivalence relation ~, the natural problem is
to decide, for any a and b, if a~b.

 The equivalence class of an element aS is the subset


of S that contains all the elements that are related to a.

 Notice that the equivalence classes from a partition of S:


Every member of S appears in exactly one equivalence
class.
The Disjoint Set ADT
 To decide if a~b, we need only to check whether a and b
are in the same equivalence class. This provides our
strategy to solve the equivalence problem.

 The input is initially a collection of N sets, each with


one element. Each set has a different element.

 There are two permissible operations.


The Disjoint Set ADT
 The first is Find, which returns the name of the set (that
is, the equivalence class) containing a given element.
 The second operation adds relations. If we want to add
the relation a~b, then

 we first see if a and b are already related. This is done


by performing Finds on both a and b and checking
whether they are in the same equivalence class.

 If they are not, then we apply Union. This operation


merges the two equivalence classes containing a and b
into a new equivalence class.
The Disjoint Set ADT
 We will represent each set by a tree. Initially, each set
contains one element.

 The representation of the trees is easy, because the


only information we will need is a parent pointer.

 Since only the name of the parent is required, we can


assume that this tree is stored implicitly in an array:
each entry P[i] in the array represents the parent of
element i. If i is a root, then P[i]=0.
The Disjoint Set ADT

1 2 3 4 5 6 7 8

Eight elements, initially in different sets


0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8
The Disjoint Set ADT
 To perform a Union of two sets, we merge the two trees by making the
root pointer of one node point to the root node of the other tree. We have
adopted the convention that the new root after the Union(X, Y) is X.

After
Union(5,6)
0 0 0 0 0 5 0 0
1 2 3 4 5 7 8
1 2 3 4 5 6 7 8

6
The Disjoint Set ADT

After Union(7,8)

0 0 0 0 0 5 0 7
1 2 3 4 5 7
1 2 3 4 5 6 7 8
6 8

After Union(5,7)

1 2 3 4 5 0 0 0 0 0 5 5 7
1 2 3 4 5 6 7 8

6 7 8
The Disjoint Set ADT
typedef int DisjSet[NumSets+1];
 A Find(X) on element X
is performed by void Initialize(DisjSet S)
{
returning the root of the int i;
for(i=NumSets; i>0; i--)
tree containing X. The }
S[i]=0;

implementation of the
basic algorithm is as
follows: void SetUnion(DisjSet S, int Root1, int Root2)
{
S[Root2]=Root1;
}
int Find(int X, DisjSet S)
{
if (S[X]<=0)
return X;
else
return Find(S[X], S);
}
Two heuristics to achieve asymptotically fastest disjoint set.
The two heuristics are:
 
i.Smart Union Algorithm

ii.Path Compression

 
SMART UNION ALGORITHM
 Smart Union Algorithms:

The unions above were performed rather arbitrarily,


by making the second tree a subtree of the first.

 A simple improvement is always to make the smaller tree a


subtree of the larger, breaking ties by any method; we call
this approach union-by-size. Had the size heuristic not
been used, a deeper tree would have been formed.
The Disjoint Set ADT
1 2 3 4 5

6 7 8
Original Trees

1 2 3 5 1 2 3 4

4 6 7 5

8 6 7

Union(4,5): Union-by-size Union(4,5): Arbitrary Union


8
The Disjoint Set ADT
 An alternative implementation is union-by-height. We
keep track of the height, instead of the size, of each tree
and perform Unions by making the shallow tree a
subtree of the deeper tree.

 This is an easy algorithm, since the height of a tree


increases only when two equally deep trees are joined
(and then the height goes up by one). Thus, union-by-
height is a trivial modification of union-by-size.
 Path compression and ranks :

These are refinements of the forest representation which make it


significantly faster.
FIND-SET: Do path compression
UNION: Use ranks

“Path compression” means that when we do FIND-SET(X), we make


all nodes encountered point directly to the representative element
for x. Initially, all elements have rank 0. The ranks of representative
elements are updated so that if two sets with representatives of
the same rank are unioned, then the new representative is
incremented by one.
PATH COMPRESSION

Path compression does not change any ranks


 Example :
We show a sequence of operations and what the forest would
look like.
MAKE-SET(1) … MAKE-SET(6)

1 2 3 4 5 6 RANKS : 0
UNION(1,2) UNION(4,5)
2 3 5 6 RANK(2)=1
RANK(5)=1
1 4
UNION(1,3)
2 5 6 RANK(2)=1
RANK(5)=1
1 3 4

UNION(5,6)
2 5 RANK(2)=1
RANK(5)=1
1 3 4 6
UNION(4,3)

2 RANK(2)=2
RANK(5)=1
1 3 5

4 6

FIND(4)

2

1 3 5 4

FIND(3) no change
FIND(6) (path compression)
2
 
1 3 5 4 6
Path compression
The Find-set procedure is a two-pass method:
it makes one pass up the find path to find the root,
a second pass back down the find path to update each node so that it
points directly to the root.

Find-set function using path compression:


 Find-set(x)
if x ≠ p[x]
then p[x] = Find-set(p[x])
return p[x]
Basic Concepts
A graph is an ordered pair (V, E).
V is the set of vertices. (You can think of

them as integers 1, 2, …, n.)


E is the set of edges. 2 adjacent vertices

are joined by edges.An edge is a pair of


vertices: (u, v).
Edges can be labeled with a weight10 :
Concepts: Directedness
In a directed graph, the edges are “one-
way.” So an edge (u, v) means you can go
from u to v, but not vice versa. a self-loop

In an undirected graph, there is no


direction on the edges: you can go either
way. (Also, no self-loops.)

Concepts: Adjacency
Two vertices are adjacent if there is an
edge between them.
For a directed graph, u is adjacent to v iff

there is an edge (v, u).


v v
u w u w

u is adjacent to v. u is adjacent to v.
v is adjacent to u and w. v is adjacent to w.
w is adjacent to v.
Concepts: Degree
Undirected graph: The degree of a vertex
is the number of edges touching it.
degree 4

For a directed graph, the in-degree is the


number of edges entering the vertex, and
the out-degree is the number leaving it. The
degree is the in-degree + the out-degree.
in-degree 2, out-degree 1
Concepts: Path
 A path is a sequence of adjacent vertices.
The length of a path is the number of edges
it contains, i.e. one less than the number of
vertices. 2

1 3

We write u  v if there is path from u to


v. We say v is reachable from u.
Concepts: Cycle
•A cycle is a path of length at least 1 from a
vertex to itself.
•A graph with no cycles is acyclic.

•A path with no cycles is a simple path.


2

1 3

• The path <2, 3, 4, 2> is a cycle.


Concepts: Connectedness
An undirected graph is connected iff there
is a path between any two vertices.
An unconnected graph with three
connected components.
Properties of graph
 Complete graph: if an undirected graph
of n vertices consists of n(n-1)/2 no. of
edges
 Sub graph
 Connected graph
Representation of Graphs
 Adjacency Matrix
In a directed graph, if there is an edge
present between vertices Vi & Vj then
M[i][j]=1 else M[i][j]=0
In a undirected graph, if there is an edge
present between vertices Vi & Vj then
M[i][j]=1 then M[j][i]is also 1
Creating a graph using adjacency
matrix

 Declare an array of M[size][size] which will


store the graph.
 Enter how many nodes you want in a graph
 Enter the edges of the graphs by 2 vertices
each say Vi, Vj indicates some edge
 If the graph is directed set M[i][j]=1, if graph
is undirected set M[i][j] =1 & m[j][i]=1.
Representation of Graphs
Adjacency List
The type in which a graph is created with
the linked list is called adjacency list
Applications
 In computer networking such as LAN,
WAN, Internetworking
 In telephone cabling graph theory is
effectively used
 In job scheduling algorithms
 Artificial Intelligence
 Computer graphics
 Compiler writing
Definition
A topological sort of a Directed Acylic Graph
(DAG) is a linear ordering of all its vertices
such that if there is a path from vi to vj then vi
appears before vj in the ordering.
Algorithm
 From the given graph, find a vertex with no
incoming edges.
 Print this vertex and remove it , along with its
edges from the graph.
 If there are more than one such vertices then
break the tie randomly
 Note the vertices that are deleted
 All these recorded vertices give topologically
sorted list
Algorithm for finding a topological sort
Initialize TSArray to null

for (i=1; i<=numofvertices; i++)


v = find vertex of degree zero
if v is null
Graph has a cycle. Exit
else
add v to TSArray
remove v and all its outgoing vertices

Print TSArray
Algorithm for enumerating all topological sorts
TopologicalSorts()
compute the indegree count array, Indegree[ ]
initialize TSArray[ ] to null

for each vertex v


if indegree[v] == zero
push Indegree[ ], TSArray[ ], v

while (stack not empty)


pop Indegree[ ], TSArray[ ], v
add v to TSArray
for each x adjacent to v, Indegree[x]--
for each vertex v
if indegree[v] == zero
push Indegree[ ], TSArray[ ], v
Find out the ordering

1 2

3 4 5

6 7
Solution
1 2 5 4 3 7 6
1 2 5 4 7 3
Shortest Path Algorithms
Dijkstra’s Algorithm
INTRODUCTION
Dijkstra’s algorithm calculates the least distance
from a starting vertex to a destination vertex from
the
A ggiven weighted graph

Input:
• A weighted graph
• Starting vertex
• Destination vertex

Output:raph which connects all the vertices with least


distance
Dijkstra's Algorithm
Input:
a weighted digraph G=(V,E) with positive edge weights
a source node s ∈ V
Initialization:
d[s]=0
for each vertex x ∈ V-s
d[x]=infinity
Mark all the vertices as unprocessed
Iteration:
for i=1 to |V|
Choose an unprocessed vertex x from V with minimum d[x]
Mark x as processed
for all y ∈ adj(x)
if d[y] > d[x]+w(x,y)
d[y] = d[x]+w(x,y)
Dijkstra's Algorithm With PATH
Input:
a weighted digraph G=(V,E) with positive edge weights
a source node s ∈ V
Initialization:
d[s]=0 predecessor [ s ] = undefined
for each vertex x ∈ V-s
d[x]=infinity
Mark all the vertices as unprocessed
Iteration:
for i=1 to |V|
Choose an unprocessed vertex x from V with minimum d[x]
Mark x as processed
for all y ∈ adj(x)
if d[y] > d[x]+w(x,y)
d[y] = d[x]+w(x,y)
predecessor [ y ] = x
function Dijkstra(Graph, source):
for each vertex v in Graph: // Initialization
dist[v] := infinity // initial distance from source to vertex v is set to infinite
previous[v] := undefined // Previous node in optimal path from source
dist[source] := 0 // Distance from source to source
Q := the set of all nodes in Graph // all nodes in the graph are unoptimized - thus
are in Q
while Q is not empty: // main loop
u := node in Q with smallest dist[ ]
remove u from Q
for each neighbor v of u: // where v has not yet been removed from Q.
alt := dist[u] + dist_between(u, v)
if alt < dist[v] // Relax (u,v)
dist[v] := alt
previous[v] := u 15: return previous[ ]
Problem: Laying Telephone
Wire

Central office

47
Wiring: Naïve Approach

Central office

Expensive!

48
Wiring: Better Approach

Central office

Minimize the total length of wire connecting the customers

49
Contd..
A Spanning tree S is a subset of a tree T in which all the
vertices of tree T are present but it may not contain all the edges.
 
A spanning tree of a graph is just a sub-graph that contains all
the vertices and is a tree.

A graph may have many spanning trees.


 
 
Graph A Some Spanning Trees
from Graph A
Contd..
The Minimum Spanning Tree for a given graph is the Spanning Tree of
minimum cost for that graph.
MST
 A minimum spanning tree connects all
nodes in a given graph
 A MST must be a connected and
undirected graph
 A MST can have weighted edges.
Minimum Spanning Tree
(MST)
A minimum spanning tree is a subgraph of an
undirected weighted graph G, such that

• it is a tree (i.e., it is acyclic)


• it covers all the vertices V
– contains |V| - 1 edges
• the total cost associated with tree edges is the
minimum among all possible spanning trees
• not necessarily unique

53
Algorithm
Prim’s algorithm
Kruskal’s algorithm

Prim’s algorithm
 
•In this algorithm the pair with the minimum weight is to
be chosen.
•Then adjacent to these vertices whichever is the edge

having minimum weight is selected.


•This process will be continued till all the vertices are not

be covered.
•The necessary condition is this case is that the circuit

should not be formed.


Prim’s Algorithm
 Initially discovered in 1930 by Vojtěch
Jarník, then rediscovered in 1957 by
Robert C. Prim
 Similar to Dijkstra’s Algorithm regarding
a connected graph
 Starts off by picking any node within
the graph and growing from there
Prim’s Algorithm Cont.
 Label the starting node, A, with a 0 and all
others with infinite
 Starting from A, update all the connected
nodes’ labels to A with their weighted edges
if it less than the labeled value
 Find the next smallest label and update the
corresponding connecting nodes
 Repeat until all the nodes have been visited
 Similar to Dijkstra’s Algorithm except
that dv records edge weights, not path
lengths
Prim's Algorithm

MST=NULL;
Select an edge of min weight and add it to MST

Iteration:
repeat till n-1 edges are added to MST
1. select an edge (v1,v2) such that v1 is in MST and v2 is not in MST
2. add it to MST

 Initialization:
Prim’s Algorithm
Input:
A connected weighted graph G = {V, E}

Initialization:
VMST = EMST = null
Select an arbitrary vertex, x, from V
add x to VMST

Iteration:
for i = 1 to |V|-1
select an edge v1,v2 with minimum weight such that
v1 ∈ VMST and v2 ∈ V \ VMST
Add v1 to VMST
Add (v1,v2) to EMST
return EMST
Kruskal’s Algorithm- Introduction
 To select the edges in order of smallest weight and
accept an edge if it does not cause a cycle.
 It maintains a forest – a collection of trees
 Minimum weight is obtained
 Circuit should not be formed
 Each time the edge of minimum weight has to be
selected from the graph
 It is not necessary in this algorithm to have edges of
minimum weight to be adjacent.
Kruskal’s Algorithm
 Created in 1957 by Joseph Kruskal
 Finds the MST by taking the smallest weight in the
graph and connecting the two nodes and repeating
until all nodes are connected to just one tree
 This is done by creating a priority queue using the
weights as keys
 Each node starts off as it’s own tree
 While the queue is not empty, if the edge retrieved
connects two trees, connect them, if not, discard it
 Once the queue is empty, you are left with the
minimum spanning tree
Kruskal's Algorithm

This algorithm creates a forest of trees. Initially the forest consists


of n single node trees (and no edges). At each step, we add one
edge (the cheapest one) so that it joins two trees together. If it
were to form a cycle, it would simply link two nodes that were
already part of a single connected tree, so that this edge would
not be needed.
The steps are:

1. The forest is constructed - with each node in a separate tree.


2. The edges are placed in a priority queue.
3. Until we've added n-1 edges,
1. Extract the cheapest edge from the queue,
2. If it forms a cycle, reject it,
3. Else add it to the forest. Adding it to the forest will join two trees together.

Every step will have joined two trees in the forest together, so that at the end, there
will only be one tree in T.
Kruskals Algorithm
Input:
A connected weighted graph G = {V, E}

Initialization:
VMST = EMST = null

Iteration:
for i = 1 to |V|
select an edge v1,v2 with minimum weight such that it does not form a cycle with
existing selected edges.
Add (v1,v2) to EMST
return EMST
8
Kruskal’s
1 14
Method
1 3 5 7 1 3 5 7
0
7 4 12 6 3
2
2 4 6 8 2 4 6 8
9

 Start with a forest that has no edges.


• Consider edges in ascending order of cost.
• Edge (1,2) is considered first and added to the forest.
8
Kruskal’s
1 14
Method
1 3 5 7 1 3 5 7
0 7
7 12 6 3 4 6 3
2 4 2
2 4 6 8 2 4 6 8
9

 Edge (7,8) is considered next and added.


• Edge (3,4) is considered next and added.

• Edge (5,6) is considered next and added.

• Edge (2,3) is considered next and added.

• Edge (1,3) is considered next and rejected because it creates a cycle.


8
Kruskal’s
1 14
Method 1 14
1 3 5 7 1 3 0 5 7
0 7
7 12 6 3 4 6 3
2 4 2
2 4 6 8 2 4 6 8
9

 Edge (2,4) is considered next and rejected because it


creates a cycle.

• Edge (3,5) is considered next and added.

• Edge (3,6) is considered next and rejected.

• Edge (5,7) is considered next and added.


FA Questions
1. A graph may have ___________ spanning
trees.
a. One b. Two c. Many d. Null

2. A minimum spanning tree for a weighted graph


is a spanning tree with _______weight.
a. Minimum b. Maximum c. single d. null

3. A Minimum Spanning Tree must be a


_________ and ___________ graph.
a. Connected, directed b. Connected, Undirected
c. Completed, directed d. Completed, undirected

You might also like