Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1. [CLRS 23.2-4 Suppose that all edge weights in a graph are integers in the range 1 to j j.
How fast
an you make Kruskal's algorithm run? What if the edge weights are integers
in the range from 1 to for some
onstant ?
Solution: Kruskal's algorithm sorts edges in nonde
reasing order by weight. If the edge
weights are integers in the range 1 to j j, we
an use Counting-Sort to sort the edges
in ( + ) time (re
all Counting-Sort
orre
tly sorts integers in the range 0 to in
( + ) time). Then Kruskal's algorithm will run in ( + + log ) = ( + log )
time.
If the edge weights are integers in the range from 1 to for some
onstant , we
an
use Counting-Sort to sort the edges in ( + ) time and Kruskal's algorithm will
run in ( + + log ) time.
2. [CLRS 23-4
V
O V
O E
O W
Solution:
(a)
Maybe-MST-A removes edges in non-in
reasing order as long as the graph remains
onne
ted, and the resulting is a minimum spanning tree. To show
orre
tness, let
be a MST of . Ea
h time we remove an edge , either 2 or 2 . If 2 ,
then we
an just remove it. If 2 , removing dis
onne
ts (but not the graph)
into two trees. There
annot be another edge
onne
ting these trees with smaller
weight than , be
ause by assumption is a MST, and if a larger edge existed that
onne
ted the trees the algorithm would have removed it before removing . We
know a path exists sin
e the graph is still
onne
ted, so it must be there is another
edge with equal weight that hasn't been dis
overed yet, so we
an remove .
Implementation: Use an adja
en
y list representation for . We
an sort the edges
in ( log ) using Merge-Sort. BFS or DFS
an be used in ( + ) to
he
k if
f g is a
onne
ted graph. The edges are sorted on
e and BFS/DFS is performed
times, on
e for ea
h edge. The total running time is ( log + ( + )) =
( 2).
(b) Maybe-MST-B will not give a minimum spanning tree. We prove this by
ounterexample. Consider the following graph :
T
e = S
e = S
O E
O V
O E
E V
O E
w
1
The MST of would have edges ( ) and ( ) with weight 3. Sin
e MaybeMST-B takes edges in arbitrary order, it
ould add edges (
) and ( ) to ,
then try to add ( ) whi
h forms a
y
le, then return (weight 5).
G
w; u
v; w
u; v
w; u
v; w
u; v
u v
O V
O E
O V
O V
O V
O V
T
O V
O V
O EV