Docsity

Algorithm Design and Analysis: ECS122A Homework Assignment #5 Solutions - Prof. Zhaojun Ba, Assignments of Computer Science

Toggle sidebar
bg1
ECS122A: Algorithm Design and Analysis
Homework Assignment #5 Solutions
1. Show that determining whether a directed graph contains a sink – a vertex with in-degree |V| − 1
and out-degree 0 – can be determined in time O(V).
Solution: Let us first show that there is an O(V) algorithm for determining whether a directed
graph contains a sink. The algorithm begins with testing whether there is an edge from the vertex
uto vertex v.
•If there is an edge from uto v, then uis NOT sink.
•If there is no edge from uto v, then vis NOT sink.
Therefore, by |V| − 1 tests, we can eliminate all but one vertex.
The second step of the algorithm is to make sure this one is a sink. It takes |V| − 1 tests for edges
coming in (in-degree), and |V| − 1 tests for edges going out (out-degree).
The run time of the algorithm is then |V| − 1 + |V| − 1 + |V| − 1 = 3|V| − 3 = O(|V|).
2. Do Exercise 22.1-7
Solution: BBTis called the Laplacian of the graph E. For the incidence matrix B, note that the
(i, j) element of BB Tis
BBT(i, j) = X
e∈E
biebT
ej =X
e∈E
biebje .
Then,
•If i=j, then biebje = 1 (It is 1 ·1 or (−1) ·(−1) whenever eenters or leaves vertex i, and 0
otherwise.
•if i6=j, then biebje =−1 when e= (i, j ) or e= (j, i), and 0 otherwise.
Here is what BBTcontains
BBT(i, j) = (degree of i= in-degree + out-degree if i=j
−(# edges connecting iand j) if i6=j
The count for the number of edges between the two vertices includes in either direction, so it can
be 0, 1 or 2.
Here is an interesting property of the Laplacian BBT. Let Gube an undirected graph, and let G
be a directed graph gotten from Guby putting arrows on Gu’s edges in any way you like. Let BBT
be the Laplacian matrix of G. Then the null of connected components of Guis the dimension of
the null space of the matrix BBT, i.e., the number of linearly independent vectors xsatisfying
BBTx= 0.
1
pf3

Partial preview of the text

Download Algorithm Design and Analysis: ECS122A Homework Assignment #5 Solutions - Prof. Zhaojun Ba and more Assignments Computer Science in PDF only on Docsity!

ECS122A: Algorithm Design and Analysis Homework Assignment #5 Solutions

  1. Show that determining whether a directed graph contains a sink – a vertex with in-degree |V | − 1 and out-degree 0 – can be determined in time O(V ). Solution: Let us first show that there is an O(V ) algorithm for determining whether a directed graph contains a sink. The algorithm begins with testing whether there is an edge from the vertex u to vertex v. - If there is an edge from u to v, then u is NOT sink. - If there is no edge from u to v, then v is NOT sink.

Therefore, by |V | − 1 tests, we can eliminate all but one vertex. The second step of the algorithm is to make sure this one is a sink. It takes |V | − 1 tests for edges coming in (in-degree), and |V | − 1 tests for edges going out (out-degree). The run time of the algorithm is then |V | − 1 + |V | − 1 + |V | − 1 = 3|V | − 3 = O(|V |).

  1. Do Exercise 22.1- Solution: BBT^ is called the Laplacian of the graph E. For the incidence matrix B, note that the (i, j) element of BBT^ is BBT^ (i, j) =

∑

e∈E

biebTej =

∑

e∈E

biebje.

Then,

  • If i = j, then biebje = 1 (It is 1 · 1 or (−1) · (−1) whenever e enters or leaves vertex i, and 0 otherwise.
  • if i 6 = j, then biebje = −1 when e = (i, j) or e = (j, i), and 0 otherwise.

Here is what BBT^ contains

BBT^ (i, j) =

{ degree of i = in-degree + out-degree if i = j −(# edges connecting i and j) if i 6 = j

The count for the number of edges between the two vertices includes in either direction, so it can be 0, 1 or 2. Here is an interesting property of the Laplacian BBT^. Let Gu be an undirected graph, and let G be a directed graph gotten from Gu by putting arrows on Gu’s edges in any way you like. Let BBT be the Laplacian matrix of G. Then the null of connected components of Gu is the dimension of the null space of the matrix BBT^ , i.e., the number of linearly independent vectors x satisfying BBT^ x = 0.

  1. Solution: the result of running breadth-first search.

1 2 3

4 5 6

Q: 3 5 6 4 2

  1. Solution: the result of running breadth-first search.

r s t u

v w x y

Q: u t y w x s r v

  1. Do Exercise 22.3-

Solution: the result of running DFS. See Fig ??

q

s

v w

t

x

z

r

y

u

T

T

T

F

T

T

T B

T

B

C

C

T