The Disjoint Set ADT
The Disjoint Set ADT
given element
iii.UNION: If we want to add the relation a ψ b, then we first see if a and b
1 2 3 4 5 6 7 8
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:
6 7 8
Original Trees
1 2 3 5 1 2 3 4
4 6 7 5
8 6 7
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.
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
1 3
1 3
Print TSArray
Algorithm for enumerating all topological sorts
TopologicalSorts()
compute the indegree count array, Indegree[ ]
initialize TSArray[ ] to null
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
Central office
47
Wiring: Naïve Approach
Central office
Expensive!
48
Wiring: Better Approach
Central office
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.
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
be covered.
•The necessary condition is this case is that the circuit
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
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