This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: EWajhi 'itonUniversi «in tlouis
v s n5 t3 S Engineering Minimum Spanning Trees
Kruskal’s Algorithm and Partitions Jon Turner
Computer Science & Engineering
Washington University www.ar1.wustl.edu/~jst 55;: ' ~ ~ 3,  I: ‘
WashmgtonUrmersm mStlouls Engineering Kruskal’s Algorithm l Kruskal's algorithm applies following rule to the edges
in non—decreasing order of cost >> Coloring Rule 1: if the current edge e has both ends in the
same blue tree, color it red; otherwise color it blue IThe algorithm can also be expressed as procedure minspantree(graph G=(V,E), modifies set blue) vertex u,v; set edges;
blue := {}; edges 2: E;
Sort edges by cost;
for {u,v}E edges => if u and v are in different blue trees 2 blue := blue U {u,v}; fi; rof; // edges not added to blue are implicitly red
end; 55;: ' ‘ ~ 3,  I: "
WashmgtonUrmersm mStlouls Engineering Partition Data Structure ITO make Kruskal's algorithm fast, need a fast way to
determine if two vertices are in same blue tree >> use a data structure that maintains a partition on set of all
vertices, with a separate set for the vertices in each blue tree >> requires operations that allow us to merge two sets and to
determine whether or not two vertices are in the same set
IOperations on partition data structure >> partition(S): create a partition on the set S with each element
of S forming a separate set >> find(x): return the canonical element of the set containing X
>> link(x,y): merge the two sets with canonical elements x and y
0 original sets replaced with new set; returns new canonical element
l Efficient, easy to implement and has many applications
>> often referred to as unionﬁnd data structure 55;: ' ~ ~ 3,  I: ‘
WashmgtonUrmersm mStlouls Engineering Using Partitions in Kruskal’s Algorithm procedure minspantree(graph G =(V,E), modifies set blue)
vertex u,v; set edges;
blue := {}; edges 2: E;
Sort edges by cost; partition(V );
for {u,v}E edges 2
if find(u) 7t find(v) => ink(find(u),find(v));
blue := blue U {u,v}
fi;
rof;
end; ISorting can be done in O(m log m)=O(m log n) time
 Remaining time determined by the partition operations >> n—1 links and at most 4m finds
>> these operations can be done in O(m log n) time ISO, overall time is O(m log n) E Washington University in Stlouis C++ Version Engmeeﬁng void kruskal(wgraph& G, wgraph& T) {
edge e; vertex u,v,cu,cv; weight w;
class prtn P(G.n());
edge *elist = new edge[G.m()+l];
for (e = 1; e <= G.m(); e++) e1ist[e] = e;
sortEdges (e1ist,G) ; <50, G.w(eist[i])sG.w(eist[i+1])]
for (int i = 1; i <= G.m(); i++) {
e = e1ist[i]; ﬁ eis ith “lightest” edge
= G.left(e); v G.right(e); w = G.w(e);
cu = P.find(u); cv = P.find(v);
if (cu != cv) {
P.1ink(cu,cv); e = T.join(u,v); T.changeWt(e,w); _ 7 . u  1 u
«:4 ~ I .l ‘
WashmgtonUrmersm mStlouls Engineering Implementing Partition l Represent each set as a tree >> each tree node contains a set element x and a pointer to its
parent p(x) in the tree; the root points to itself I Link operation limits depth of trees using auxiliary variable rank(x) for each node x
procedure partition(set S) for X65 2 p(x) := X; rank(x) := O; rof; end;
int function find(integer X); do p(x)¢x => x := p(x) od; return x;
end;
int function ink(integer X,y); if rank(x)>rank(y) 2 p(y):=x; return X;
 rank(x)=rank(y) => rank(y):=rank(x) + 1;
fi; := y; return y; —
Engineering Basic Analysis of Partition E Washington University in Stlouis l Lemma 2.1. If X is any node, rank(X)srank(p(X)) with
inequality strict if p(X)¢X; rank(X)=O initially and
increases with time until p(X) is assigned value other
than X; after that, rank(X) does not change; rank(p(x))
is nondecreasing function of time Proof. By induction on number of ﬁnd and link ops l I Lemma 2.2. Number of nodes in tree with root X is
>2rank(x) Proof. By induction on number of link operations I rank( )= rank(b)=k+1 b rank(b)=/l: :> A _ 7 . u  1 u
«:4 ~ I .l ‘
WashmgtonUrmersm mStlouls Engineering I Lemma 2.3. For any integer kzo, the # of nodes with
rank k is at most n/Zk; so, every node has rank Sig n
Proof. Suppose that when a node x is assigned a rank of k, all
nodes in the tree with root x are labeled (X,k) By Lemma 2.2, at least 2" nodes are labeled when x gets rank k If root of tree containing x changes, new root must have rank at least k+1, so no node is labeled more than once with a label of the
form (y,k) for any particular value of k Since there are n nodes, there are Sn labels of form (y,k) for any
particular k and there are at least 2" for each node of rank k Hence, at most n/2" nodes can be assigned a rank of k. l
 Running time
>> initialization takes O(n) time and each link operation takes 0(1) >> by Lemmas 2.1—2.3, the height of any tree is Sig n; so ﬁnd
takes O(log n) time >> so any sequence of m ops takes at most O(m log n) time —
Engineering E Washington University in Stiouis Speeding Up Partition nSimple optimization makes partition significantly faster
Int function. find(nteger X); ﬁrst pass ﬁnds root
Integer r, r .= X, second pass redirects
do p(r);er 2 r := p(r); od; parent pointers
do p(X)¢r => x, p(X) := p(X), r; od;
return r;
end;
ICalled path compression — speeds up later finds. I Note: earlier lemmas remain true when path
compression is used
I Recursive version (used in analysis) int function find(integer X); each recursive if X¢P(X) => P(X) 1: ﬁnd( P(X)) ﬁ} call referred to
return p(X); as a “find step”
end; 55;: i ‘ ~ 3,  ,v "
WashmgtonUrmersm mStlouls Engineering Bounding Number of Find Steps I For a node X, let A(X)=rank(p(X))—rank(X)
>> A(X)=0 initially and may then increase but is always Sig n ICall X dominant if A(X)>2A(y) for all ancestors y of X
>> there can be <lg lg n dominant nodes along a “find path”
>> so, total number of find steps at dominant nodes is O(m lg lg n) l IfX not dominant, it has ancestor y, with A(y)2A(X)/2
>> a find involving X increases A(X) by a factor of at least 1.5
>> this can happen to X at most 2 lg lg n times
>> so O(n lg lg n) find steps at nodes that are not dominant My)=rank(p(y))rank(y)ZA(x)/2 ' A'(X)Z 1.5 A(X) A(X) =rank(p(X))—rank(x) ﬂ / l:> E Washington University in Stiouis Engineering Understanding Partition Analysis —
Engineering I 2 The figure below shows an instance of the partition data
X e I S e S Structure @0 0 1 1 1
l. The figure below shows an intermediate state in Kruskal’s
algorithm. The tree edges are shown in bold and the non 0 0 O O 0 0 Singleton sets in the Partition dam Strucmm are “5th “5 50L“ Show the data structure after the following operations are performed: link(i,n), link(d,e), liuk(i,v)t Washington University in Stlouis Show the state of the algorithm after the next three edges are Them Show the data Structure after the following Operations
considered for inclusion by the algorithm, are performed as well (show the effects of path
compression):ﬁndU),ﬁnd(n). 3. washington Universiw in Stlouis The height of a node in a tree is the length of a longest path
from the node to one of its descendants. Show that if we
leave out the path compression feature of the ﬁnd operation
from the partition data structure, then the rank of a node is
equal to its height. By induction on the number oflink operations Initially all nodes
have 0 rank and are leaves (which have height 0), so initially all
nodes have rank equal to their height, When a link operation is
performed on two nodes of different rank (height), the node with
the larger rank (height) becomes the tree root and neither its rank
nor its height changes. On the other hand, ifthe link operation
joins two nodes of equal rank, the new height of the new root node
increases by 1, as does the rank, The heights (and ranks) of other
nodes are unaffected by the link, so the link operation preserves the
equivalence between ranks and heights. Show that with path compression, the rank of a node is an
upper bound on the height. By induction on the number oflink andﬁnd operations. As before,
the claim is true initially because all nodes have zero rank and
height. For a link operation, there are two cases, the tree roots
have difﬂ’rent ranks, the height of the resulting tree can be no
larger than the height of the “higher” tree, which is bounded by the
larger rank, So making the node with the larger rank the tree root
preserves the property. If the nodes have equal rank, the height of
the new tree is at most one plus the old height which is at most one
plus the rank of the roots. Aﬁnd operation with path compression can only reduce the height
of any node in the tree and does not change the rank of any node.
So,for any node x, if rank(.\')2height(.\') before the operation, then
this remains true after the operation, —
Engineering Lemma 2.2 was proved for the partition data structure
without path compression. Explain why it remains true
when path compression is included. The lemma states that a tree whose root has rank k contains at least
2A nodes. l’ath compression does not change the value of any rank
and does not change the number ofnodes in any tree (although it
can change the number of nodes in certain subtrees), Therefore, find operations with path compression will preseree the property stated in the lemma. In the analysis of path compression, explain why A(x) cannot
decrease over time. Alx) is the difference between the rank ofa node's parent and its
rank. As long as a node is the root of its tree, this difference is zero.
Once a node acquires a parent it becomes nonzero and its own
rank can no longer change. If the parent of the node is a tree root,
link operations can cause the rank of the parent to increase,
causing A(.\') to increase along with it. If the parent is not a tree
root, then its parent's rank may increase as a result ofaﬁnd
operation that assigns it a new parent. But since its new parent
was an ancestor of its old parent, and since ranks increase as you
go up a tree, the rank of the new parent must be larger than the
rank of the old parent. So once again, A(.\') increases Can A(x) can decrease as you follow parent pointers in the
tree? Yes, While the ranks increase as you go up the tree, the differences
between successive rank values can both increase and decrease as
you go up the tree. ...
View
Full Document
 Spring '12
 JonTurner

Click to edit the document details