Introduction To Algorithms CS 445: Discussion Session 4 Instructor: DR Alon Efrat TA: Pooja Vaswani 02/28/2005

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

Introduction To Algorithms

CS 445

Discussion Session 4
Instructor: Dr Alon Efrat
TA : Pooja Vaswani
02/28/2005
Topics

• Graphs

• Minimum Spanning Trees


– Kruskal
– Prim

2
Minimum Spanning Trees
• Undirected, connected graph
G = (V,E)
• Weight function W: E  R
(assigning cost or length or
other values to edges)

 Spanning tree: tree that connects all the vertices


(above?)
 Minimum spanning tree: tree that connects all
the vertices and minimizes w(T ) 
 w(u, v)
( u , v )T

3
Generic MST Algorithm
Generic-MST(G,
Generic-MST(G, w) w)
11 A//
A// Contains
Contains edges
edges that
that belong
belong toto aa MST
MST
22 while
while AA does
does not
not form
form aa spanning
spanning tree
tree dodo
33 Find
Find anan edge
edge (u,v)
(u,v) that
that is
is safe
safe for
for AA
44 AA{(u,v)}
AA{(u,v)}
55 return
return AA

Safe edge – edge that does not destroy A’s property


The
The algorithm
algorithm manages
manages aa set
set of
of edges
edges AA maintaining
maintaining
the
the following
following loop
loop invariant
invariant
Prior
Prior to each iteration, AA is
to each iteration, is aa subset
subset of
of some
some
minimum
minimum spanning
spanning tree.
tree.
At
At each
each step,
step, an
an edge
edge is
is determined
determined that
that can
can be
be added
added
to
to AA without
without violating
violating this
this invariant.
invariant. Such
Such an
an edge
edge
is
is called
called aa Safe
Safe Edge.
Edge.
4
Kruskal's Algorithm
• Edge based algorithm
• Add the edges one at a time, in increasing
weight order
• The algorithm maintains A – a forest of trees.
An edge is accepted it if connects vertices of
distinct trees
• We need a data structure that maintains a
partition, i.e.,a collection of disjoint sets
– MakeSet(S,x): S  S  {{x}}
– Union(Si,Sj): S  S – {Si,Sj}  {Si  Sj}
– FindSet(S, x): returns unique Si  S, where x  Si

5
Kruskal's Algorithm
• The algorithm adds the cheapest edge that
connects two trees of the forest

MST-Kruskal(G,w)
01 A  
02 for each vertex v  V[G] do
03 Make-Set(v)
04 sort the edges of E by non-decreasing weight w
05 for each edge (u,v)  E, in order by non-
decreasing weight do
06 if Find-Set(u)  Find-Set(v) then
07 A  A  {(u,v)}
08 Union(u,v)
09 return A

6
Kruskal Example

7
Kruskal Example (2)

8
Kruskal Example (3)

9
Kruskal Example (4)

10
Kruskal Running Time
• Initialization O(V) time
• Sorting the edges (E lg E) = (E lg V) (why?)
• O(E) calls to FindSet
• Union costs
– Let t(v) – the number of times v is moved to a new
cluster
– Each time a vertex is moved to a new cluster the size
of the cluster containing the vertex at least doubles:
t(v) log V
– Total time spent doing Union  t (v)  V log V
vV
• Total time: O(E lg V)
11
Prim-Jarnik Algorithm
• Vertex based algorithm
• Grows one tree T, one vertex at a time
• A cloud covering the portion of T already
computed
• Label the vertices v outside the cloud with key[v]
– the minimum weigth of an edge connecting v
to a vertex in the cloud, key[v] = , if no such
edge exists

12
Prim-Jarnik Algorithm (2)
MST-Prim(G,w,r)
01 Q  V[G] // Q – vertices out of T
02 for each u  Q
03 key[u]  
04 key[r]  0
05 [r]  NIL
06 while Q   do
07 u  ExtractMin(Q) // making u part of T
08 for each v  Adj[u] do
09 if v  Q and w(u,v) < key[v] then
10 [v]  u
11 key[v]  w(u,v)

13
Prim Example

14
Prim Example (2)

15
Prim Example (3)

16

You might also like