Sei sulla pagina 1di 2

CPS 130 Homework 20 - Solutions

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

This implementation is similar to Kruskal's algorithm. Use a Union-Find data


stru ture to maintain . For ea h vertex we need to Make-Set( ). For ea h
edge = ( ), if Find-Set( ) 6= Find-Set( ) then there is no y le in [ f g,
and we Union-Set( , ).
In total there are Make-Set operations, 2 Find-Set operations, and
1
Union-Set operations. Using an improved Union-Find data stru ture we an
perform Find-Set in (1) and Union-Set in (log ) time amortized to give a
running time of ( ) + ( ) + ( log ) = ( log + ).
( ) Maybe-MST-C gives a minimum spanning tree. Maybe-MST-C adds edges in
arbitrary order to and if a y le is dete ted removes the maximum-weight edge
on . Ea h time we add an edge , either we form a y le or we do not. Suppose
is added to some tree in and a y le is formed. Then we remove a maximumweight edge from and
+ is of less or equal weight than (be ause
is of greater or equal weight than ). If a y le is not formed we either just add
to some tree in or onne ts two trees in . In the se ond ase if is not the
smallest weight edge that onne ts the trees then we haven't dis overed it yet, and
when we eventually form a y le we have already shown Maybe-MST-C performs
orre tly.
Implementation: Use an adja en y list representation for . For ea h edge, we add
it to and then he k [ f g for y les. There an be at most one y le, so we an
use DFS to dete t the y le and output it. If there are no y les, then we are done
with this edge. Otherwise, we need to output the y le, nd the maximum-weight
edge, and delete it from .
Adding an edge takes (1). DFS takes ( + ) = ( ) in this ase (as soon as
has a y le we break it, so the number of edges in at any point is no greater
than ). Finding the maximum-weight edge on the y le takes ( ) and deleting
an edge is ( ). For ea h edge we add it to , perform DFS, and possibly nd a
y le, so the total running time is ( ).
T

u; v

u v

O V

O E

O V

O V

O V

O V
T

O V

O V

O EV

Potrebbero piacerti anche