Escolar Documentos
Profissional Documentos
Cultura Documentos
1. [CLRS 22.1-1
Solution: Given an adja
en
y-list representation
of a dire
ted graph, the out-degree
of a vertex is equal to the length of [ , and the sum of the lengths of all the
adja
en
y lists in is j j. Thus the time to
ompute the out-degree of every vertex is
( + ). The in-degree of a vertex is equal to the number of times it appears in all the
lists in . If we sear
h all the lists for ea
h vertex, the time to
ompute the in-degree of
every vertex is ( ). Alternatively, we
an allo
ate an array of size j j and initialize
its entries to zero. Then we only need to s
an the lists in
on
e, in
rementing [
when we see in the lists. The values in will be the in-degrees of every vertex. This
an be done in ( + ) time with ( ) additional storage.
The adja
en
y-matrix of any graph has ( 2 ) entries, regardless of the number of
edges in the graph. For a dire
ted graph,
omputing the out-degree of a vertex is
equivalent to s
anning the row
orresponding to in and summing the ones, so that
omputing the out-degree of every vertex is equivalent to s
anning all entries of . Thus
the time required is ( 2 ). Similarly,
omputing the in-degree of a vertex is equivalent
to s
anning the
olumn
orresponding to in and summing the ones, thus the time
required is also ( 2).
2. [CLRS 22.1-5
2
Solution: To
ompute
from the adja
en
y-list representation
of , we perform
the following for ea
h [ :
for ea
h vertex
in
[
for ea
h vertex
in
[
( )2 2
insert
in
2( )
where 2 is the adja
en
y-list representation of 2. After we have
omputed 2, we
have to remove any dupli
ate edges from the lists (there may be more than one two-edge
path in between any two verti
es). For every edge in we s
an at most j j verti
es,
we
ompute 2 in time ( ). Removing dupli
ate edges is done in ( + ) as
shown in [CLRS 22.1-4. Thus the total running time is ( )+ ( + )= ( ).
Let denote the adja
en
y-matrix representation of . The adja
en
y-matrix representation of 2 is the square of . Computing 2
an be done in time ( 3) (and even
faster, theoreti
ally; Strassen's algorithm for example will
ompute 2 in ( lg 7)).
3. [CLRS 22.2-3
Solution: If the input graph for BFS is represented by an adja
en
y-matrix
and the
BFS algorithm is modied to handle this form of input, the the running time will be the
size of , whi
h is ( 2). This is be
ause we have to modify BFS to look at every entry
in in the for loop of the algorithm, whi
h may or may not be an edge.
Adj
Adj u
Adj
Adj
V E
Adj
T u
Adj
Adj u
Adj u
edge u; w
Adj v
Adj
Adj
Adj
Adj
Adj
O VE
O V
O VE
O V
O V
O V
O VE
4. [CLRS 22.3-7
Solution: Consider the following dire
ted graph
:
G
w
u
There is a path from to in . Suppose a DFS sear
h dis
overs verti
es in the order
. Then the depth-rst tree will have root and , are
hildren of . However,
is not a des
endant of .
This is just one possible
ounterexample.
5. [CLRS 22.4-5
Solution: We
an perform topologi
al sorting on a dire
ted a
y
li
graph
using the
following idea: repeatedly nd a vertex of in-degree 0, output it, and remove it and all
of its outgoing edges from the graph. To implement this idea, we rst
reate an array
of size j j and initialize its entries to zero, and
reate an initially empty sta
k . Let
denote the adja
en
y-list representation of . We s
an through all the edges in ,
in
rementing [ ea
h time we see a vertex . In a dire
ted a
y
li
graph there must be
at least one vertex of in-degree 0, so we know that there is at least one entry of that
is zero. We s
an through a se
ond time and for every vertex su
h that [ = 0, we
push on . Pop and output . When we output a vertex we do as follows: for ea
h
vertex in [ we de
rement [ by one. If any of these [ = 0, then push on .
To show our algorithm is
orre
t: At ea
h step there must be at least one vertex with
in-degree 0, so the sta
k is never empty, and every vertex will be pushed and popped from
the sta
k on
e, so we will output all the verti
es. For a vertex with in-degree 1,
there are verti
es 1 2
whi
h will appear before in the linear ordering of .
Then [ = , sin
e 2 [ for = 1
verti
es of , and will only be pushed
on the sta
k after all have already been popped (ea
h pop de
rements [ by one).
The running time is ( ) to initialize , (1) to initialize , and ( ) to s
an the edges
of and
ount in-degrees. The se
ond s
an of is ( ). Every vertex will be pushed
and popped from the sta
k exa
tly on
e. The j j edges are removed from the graph on
e
(whi
h
orresponds to de
rementing entries of ( ) times). This gives a total running
time of ( )+ (1)+( )+( )+( ) = ( + ).
If the graph has
y
les, then at some point there will be no zero entries in , the sta
k
will be empty, and our algorithm
annot
omplete the sort.
Note: The algorithm to solve this problem is also given in Le
ture 19, but you still need
to analyze the running time and prove it works.
u
w; u; v
Adj
T u
Adj
T u
Adj u
T v
T v
T v
u ; u ; : : : uk
Adj ui
;:::;k
ui
T v