You are on page 1of 2

CPS 130 Homework 19 - Solutions

1. [CLRS 22.1-1]
Solution: Given an adjacency-list representation
of a directed graph, the out-degree
of a vertex is equal to the length of [ ], and the sum of the lengths of all the
adjacency lists in is j j. Thus the time to compute 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 search all the lists for each vertex, the time to compute the in-degree of
every vertex is ( ). Alternatively, we can allocate an array of size j j and initialize
its entries to zero. Then we only need to scan the lists in
once, incrementing [ ]
when we see in the lists. The values in will be the in-degrees of every vertex. This
can be done in ( + ) time with ( ) additional storage.
The adjacency-matrix of any graph has ( 2 ) entries, regardless of the number of
edges in the graph. For a directed graph, computing the out-degree of a vertex is
equivalent to scanning the row corresponding to in and summing the ones, so that
computing the out-degree of every vertex is equivalent to scanning all entries of . Thus
the time required is ( 2 ). Similarly, computing the in-degree of a vertex is equivalent
to scanning the column corresponding to in and summing the ones, thus the time
required is also ( 2).
2. [CLRS 22.1-5]
2
Solution: To compute
from the adjacency-list representation
of , we perform
the following for each [ ]:
for each vertex
in
[]
for each vertex
in
[]
2
( )2
insert
in
2( )
where 2 is the adjacency-list representation of 2. After we have computed 2, we
have to remove any duplicate edges from the lists (there may be more than one two-edge
path in between any two vertices). For every edge in we scan at most j j vertices,
we compute 2 in time ( ). Removing duplicate edges is done in ( + ) as
shown in [CLRS 22.1-4]. Thus the total running time is ( )+ ( + )= ( ).
Let denote the adjacency-matrix representation of . The adjacency-matrix representation of 2 is the square of . Computing 2 can be done in time ( 3) (and even
faster, theoretically; Strassen's algorithm for example will compute 2 in ( lg 7)).
3. [CLRS 22.2-3]
Solution: If the input graph for BFS is represented by an adjacency-matrix
and the
BFS algorithm is modi ed to handle this form of input, the the running time will be the
size of , which is ( 2). This is because we have to modify BFS to look at every entry
in in the for loop of the algorithm, which 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 VE

O V

O V

4. [CLRS 22.3-7]
Solution: Consider the following directed graph
:
G

w
u

There is a path from to in . Suppose a DFS search discovers vertices in the order
. Then the depth- rst tree will have root and , are children of . However,
is not a descendant of .
This is just one possible counterexample.
5. [CLRS 22.4-5]
Solution: We can perform topological sorting on a directed acyclic 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 create an array
of size j j and initialize its entries to zero, and create an initially empty stack . Let
denote the adjacency-list representation of . We scan through all the edges in ,
incrementing [ ] each time we see a vertex . In a directed acyclic 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 scan through a second time and for every vertex such that [ ] = 0, we
push on . Pop and output . When we output a vertex we do as follows: for each
vertex in [ ] we decrement [ ] by one. If any of these [ ] = 0, then push on .
To show our algorithm is correct: At each step there must be at least one vertex with
in-degree 0, so the stack is never empty, and every vertex will be pushed and popped from
the stack once, so we will output all the vertices. For a vertex with in-degree  1,
there are vertices 1 2
which will appear before in the linear ordering of .
Then [ ] = , since 2 [ ] for = 1
vertices of , and will only be pushed
on the stack after all have already been popped (each pop decrements [ ] by one).
The running time is ( ) to initialize , (1) to initialize , and ( ) to scan the edges
of and count in-degrees. The second scan of is ( ). Every vertex will be pushed
and popped from the stack exactly once. The j j edges are removed from the graph once
(which corresponds to decrementing entries of ( ) times). This gives a total running
time of ( )+ (1)+( )+( )+( ) = ( + ).
If the graph has cycles, then at some point there will be no zero entries in , the stack
will be empty, and our algorithm cannot complete the sort.
Note: The algorithm to solve this problem is also given in Lecture 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

You might also like