Operations On Dynamic Sets
Operations On Dynamic Sets
UNION(v,u)
End
SAME_COMPONENT(u,v)
if FIND_SET(u) == FIND_SET(v)
then return TRUE
else return FALSE
End
Parameters for analyzing running
times of operations
The no of MAKE_SET operations is n
total no of operations is m
Each UNION operation reduces no of sets by one, so that
after n-1 such operations, only one set remains.
Therefore, m cannot exceed n-
operations are included in m.
P3- r nodes of
rank r exists.
P4- Every node has a rank at most floor (lg n)
Property on size of tree rooted at x
This can be proved by induction on no of LINK operations.
Before first LINK on x, this is TRUE since rank[x]=0.
Let rank, size before LINK be rank, size and after LINK it
becomes .
In operation LINK(x,y) let rank[x] < rank[y].
Node y is root of tree formed through LINK and we have
rank[x] + 2rank[y]
rank[y] [no rank changes
other than y]
rank[y] = 2rank[y]+1 =
2
rank[x]
Counting nodes within a rank r
When rank r is assigned to x, attach a label x to all
nodes of the tree rooted at x.
At least 2r nodes are labeled each time. When root
changes for x, rank of root is at least r+1. Hence no
new node is labeled x for this.
Each node is therefore labeled at most once. There
being n nodes in all, at most n labeled nodes with at
least 2r labels assigned for each node of rank r.
If there are more than n/2r nodes of rank r, then more
than (n/2r ) 2r i.e. more than n nodes would be labeled
by a node of rank r, which is a contradiction.
Maximum rank possible for a node
Let r > lg n, then there are at most n/2r < 1
node of rank r.
But this is impossible as rank is integer.
Every node has a rank at most floor (lg n)
Dividing ranks into rank groups
Rank 0, 1 rank group 1; Rank 2,2^2-1 rank group 2
Rank 4 to 2^2^2-1 (15) rank group 3
Rank 16 to 2^2^2^2-1 (255) rank group 4
Rank F(g) to 2F(g) - 1 rank group g
lg*(n) take lg
till n reduces to 1.
This puts rank r into group lg* lg n)
Highest group no will be lg*(lg n) = lg*(n) -1.
Then j-th group has ranks {F(j-1)+1, F(j-
Time complexity for transitions
Two cost types: within group and transition to
higher rank group.
In Transition cost, there can be lg*(n) +1
transitions in all for each FIND-SET operation.
Once a node has parent in a different group, it
can no longer come back to previous group
because of heuristics.
For m FIND-SET operations, total cost of
transitions is thus m(lg*(n) +1)
Cost within group
No of nodes are given by
r ) = (n/2F(g-1)+1 r )
The sum running from r=0 to F(g) F(g-1)+1 can
be changed to an infinite series sum for large g.
So, N(g) < n / F(g)
At each step, we can scan lowcost to find the vertex in V - U that is closest to U.
Then we update lowcost and closest taking into account the new addition to U.
Complexity: O(n2)
Proof of Correctness-Prim's Algorithm
Let G = (V,E) be a weighted, connected graph. Let T be the
edge set that is grown in Prim's algorithm. The proof is by
mathematical induction on the number of edges in T and
using the MST Lemma.
Basis: The empty set is promising since a connected,
weighted graph always has at least one MST.
Induction Step: Assume that T is promising just before the
algorithm adds a new edge e = (u,v). Let U be the set of nodes
grown in Prim's algorithm. Then all three conditions in the
MST Lemma are satisfied and therefore T U e is also
promising.
When the algorithm stops, U includes all vertices of the graph
and hence T is a spanning tree. Since T is also promising, it will
be a MST.
- illustration
Difference between Prim and Kruskal
algorithm
Initialize with a node Initialize with an edge
Graph has to be connected Work on disconnected
graph
Always keep a connected
component, look at all edges do not keep one connected
component but a forest. At
from the current component
each stage, look at the globally
to other vertices and find the smallest edge that does not
smallest among them - then create a cycle in the current
add the neighbouring vertex to forest. Such an edge has to
the component, increasing size necessarily merge two trees in
by 1. the current forest into one.
Difference between Prim and Kruskal
algorithm
In N-1 steps, every vertex Since you start with N single-
would be merged to the vertex trees, in N-1 steps, they
current one if we have a would all merge into one if the
connected graph. graph was connected.
Next edge shall be the cheapest Choose the cheapest edge, but it
edge in the current vertex. may not be in the current vertex.
Prim's algorithm is found to run Kruskal's algorithm is found to
faster in dense graphs with more run faster in sparse graphs.
number of edges than vertices