Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Assignment #4, CS/531

Due Date: Sat., Nov. 7, 2020


Solution

UNSUPPORTED SOLUTIONS RECEIVE NO CREDIT.


Total points: 48
1. (3 pts) Consider the graph G = (V, E) shown in Figure 1. The integers near an edge is its
weight. Using Kruskal’s algorithm, compute the minimum spanning tree T1 of G. List the edges
of T1 in the order they are added into T1 .
Answer:

a
6 c
1
3
5
e
2 d
9
b
7
6 3
8
4 f
g
4 2
h

Figure 1: Kruskal’s Algorithm.

The MST is {ae, hf, eb, cd, eh, hg, dh}.


2. (6 pts) For a given undirected, edge weighted graph G = (V, E), there might be more than
one MST of G. This can happen if G has two edges e0 and e00 such that w(e0 ) = w(e00 ). In this
case, when we run Kruskal algorithm on G, the edges e0 and e00 may be processed in different
order, and this might results in two different MSTs of G.
Suppose that all edge weights of G are different. (Namely for any two edges e0 and e00 , we
have w(e0 ) 6= w(e00 ).) Under this condition, show that G has a unique MST.
Note: You might want to “prove” this as follows: Run Kruskal’s algorithm on G. Since all
edge weights w(e) are different, Kruskal’s algorithm can only consider and select one edge at
each step. So the MST is unique.
This “proof” is invalid: It only shows the MST of G constructed by Kruskal’s algorithm is
unique. When you run other MST algorithm (for example, Prim’s algorithm), you might get a
different MST.
Hint: Assume there are two different MST T1 and T2 . Assume the edges in T1 are {e1 , e2 , . . . , ek }
with increasing edge weights. Assume the edges in T2 are {e01 , e02 , . . . , e0k } with increasing edge
weights. Try to get a contradiction.
Answer:

1
Toward a contradiction, assume G has two different MSTs T1 = {e1 , · · · , en−1 } and T2 =
{e01 , · · · , e0n−1 }. We also assume (re-number the edges of T1 and T2 if necessary) that w(e1 ) <
w(e2 ) · · · < w(en−1 ), and w(e01 ) < w(e02 ) · · · < w(e0n−1 ). Let i = min{j|ej 6= e0j }, without loss of
generality, we assume w(ei ) > w(e0i ). Then we know that there must exist a cycle C in T1 {e0i }.
S
S 0
First, we claim there is only one cycle in T1 {ei }. Toward a contradiction, suppose there
are two such cycles C1 , C2 . Because there is no cycle in T1 , both C1 and C2 must contain e0i .
Then {C1 C2 }/{e0i } ⊂ T1 is also a cycle. This contradicts to the fact that T1 has no cycle.
S

6 ∅. Otherwise, C ⊂ {e01 , · · · , e0i } ⊆ T2 , this contradicts the


Next we claim C {ei , · · · , en−1 } =
T

fact that T2 has no cycle.


If we move one edge ei0 from C {ei , · · · , en−1 }, then T3 = (T1 {e0i })/{ei0 } is a spanning
T S

tree of G. Clearly, w(T3 ) = w(T1 ) + w(e0i ) − w(ei0 ). Since w(e0i ) < w(ei ) ≤ w(ei0 ), we have
w(T3 ) < w(T1 ). This contradicts the fact that T1 is a MSTs of G. Thus, the original assumption
(that G has two different MST) must be false.
3. (8 pts) Consider a long, quite country road with houses scattered very sparsely along it. You
may picture the road as a long straight line segment, with the western endpoint (mile stone
0), and the eastern endpoint (mile stone L). Each house is identified by its distance to the
western endpoint. A cell phone company wants to set up cell phone services along the road.
The company can place a base station at any house. (The monthly charge will be waived if a
base station is located in a house, so the house owners are eager to accommodate base stations).
The power of base stations are limited that can only cover a distance of 5 miles. The goal for
the company is to select a minimum number of base stations so that every house on the road
is within 5 miles of a base station. (The bases stations are connected by other means, say by
Satellite. So the distance between them can be more than 5 miles).
A formal description of the problems: The input is a set X of n points: X = {x1 < x2 <
. . . < xn }, where each xi (1 ≤ i ≤ n) represents a house. We need to select a subset Y ⊆ X
such that: (1) for every point xi ∈ X, there is a point xj ∈ Y with |xi − xj | ≤ 5, and (2) the
size of Y is minimum, subject to condition (1).
Describe a greedy algorithm for solving this problem. Argue why your algorithm produces
an optimal solution and analyze it’s runtime.
Answer: A greedy algorithm works as in Algorithm 1.

Algorithm 1 BaseStation
1: Y = ∅
2: l = 1 {// xl is the leftmost point to be covered by xi }
3: Find the largest i such that xi − xl ≤ 5.
4: Y ← Y ∪ {xi }
5: Find the largest r such that xr − xi ≤ 5. {// xr is the rightmost point covered by xi }
6: Set l = r + 1
7: Repeat Steps 3 - 6 until r ≥ n {// all points are covered}

The greedy choice property is: Select a point that is as far away as possible to the leftmost
point uncovered. We have to show that there exists an optimal solution that contains this greedy
choice. Let i be the first point selected by the algorithm BaseStation, and i0 be the first point in
an optimal solution. By the algorithm, we have i0 ≤ i. Then if we move the base station from
i0 to i, all the points covered by i0 are still be covered. We get another optimal solution which
contains the greedy choice.
The optimal substructure property is: If we get a subproblem P1 by removing all the points
covered by xi (the first base station), then an optimal solution S contains the optimal solution

2
of P1 . If not, we replace all the points except xi by the optimal solution of P1 , we get another
solution S 0 with less base stations which contradict to the fact that S is an optimal solution.
Thus our algorithm produces an optimal solution. Since each point is visited once, the
running time of the algorithm is O(n).
2
4. (4+4 = 8 pts) Let G = (V, E) be a directed graph. The square graph G2 = (V, E 2 ) of G is
defined as follows:
• The vertex set of G2 is V , (the same vertex set of G).

• u → v ∈ E 2 if and only if G contains a path with at most two edges from u to v. More
precisely:

E 2 = {u → v | either u → v ∈ E or there exists w ∈ V such that u → w ∈ E and w → v ∈ E}

Describe efficient algorithms for computing G2 from G.


(a) Using Adjacent-list representation.
(b) Using Adjacent matrix representation.
Analyze the running time in both cases.

The square of a directed graph G = (V, E) is the graph G2 = (V, E 2 ) such that (u, v) ∈ E 2 if
and only G contains a path with at most two edges between u and v. Describe efficient algorithms
for computing G2 from G for both the adjacency-list and adjacency-matrix representations of
G. Analyze the running times of your algorithms.
Answer: (a) G is represented by a matrix A. Let B denote the adjacency matrix of G2 .
Consider any two vertices i and j. There is a path of length 1 between i and j in G if and only
if A[i, j] = 1. There is a path of length 2 between i and j in G if and only if there is a vertex k
in G such that (i, k) ∈ E and (k, j) ∈ E. Thus we have:

B[i, j] = A[i, j] ∨ (∨nk=1 A[i, k] ∧ A[k, j])

So B[∗, ∗] can be computed by using this formula. There are n2 entries in B. Each entry
takes Θ(n) time. So the total run time is Θ(n3 ).
(b) G is represented by adjacency-list A. First copy the whole adjacency-list A of G into the
adjacency-list B of G2 . Then travel the whole adjacency-list A of G. For each entry in A that
corresponds to an edge i → k in G, do the following operation:
• Go to A[k], (the adjacency list of the vertex k.)

• For each entry in A[k] that corresponds to an edge k → j, add one entry in B[i] that
corresponds to the edge i → j.
There are m = |E| entries in A[∗]. For each entry in A[∗], we travel the adjacency list A[k],
which contains at most O(n) entries. So the total run time is at most O(|E||V |) = O(mn). 2
5. (3 pts) Show the d(∗) and π(∗) values that result from running BFS (bread-first search)
algorithm on the graph shown in Figure 2, using vertex a as the starting vertex. (Assuming the
adjacency lists are arranged in alphabetical order).
Answer:

3
a
b
e

g c

f
d

Figure 2: BFS algorithm.

a b c d e f g
d 0 3 3 2 1 2 1
π N IL d d g a e a
2
6. (3 pts) Show how the DFS (depth-first search) algorithm works on the directed graph shown
in Figure 3. Assume that the for loop of DFS procedure considers the vertices in alphabetical
order and that the adjacency-lists are ordered by alphabetical order. Show the discovery and
finishing time of each vertex, and show the classification of each edge.
a
b
h
e

g c

f
i
d

Figure 3: DFS

Answer:
a b c d e f g h i
d 1 2 3 4 9 11 8 15 17
f 14 7 6 5 10 12 13 16 18
π N IL a b c g g a N IL N IL
Tree edges: (a, b), (b, c), (c, d), (a, g), (g, e), (g, f )
Backward edges: (e, a)
Forward edges: (b, d)
Cross edges: (f, e), (g, d), (f, d), (h, c), (i, c), (i, h)
2
7. (3 pts) Show the ordering of vertices produced by TOPOLOGICAL-SORT when it is run on
the graph shown in Figure 4. Assume that the for loop of DFS procedure considers the vertices
in alphabetical order and that the adjacency-lists are ordered by alphabetical order.
Answer: The result is shown in Figure 4. The two black numbers near a vertex are its starting
and finishing time number. The red number near a vertex is its topological-order number.

4
[1,14] 3
[2,7] 7
a [15,16] 2
b
h
[8,9] 6 e
4
g [10,13] c [3,6] 8

f
i
[11,12] 5 d
[17,18] 1
[4,5] 9

Figure 4: Topological Sort.

The ordering of vertices produced by TOPOLOGICAL-SORT is: i, h, a, g, f, e, b, c, d.


2
8. (8 pts) Give a linear-time algorithm that takes as input a directed acyclic graph G = (V, E)
and two vertices s and t, and returns the number of paths from s to t in G. For example, in the
directed acyclic graph G shown in Fig 5, there are exactly four paths from vertex p to vertex v:
pov, poryv, posryv, and psryv. (Your algorithm needs only to count the simple paths, not list
them).
Hint: Start your algorithm by ordering the vertices of G by topological sort.

m n o p

q r s

t
u
v w

x y z

Figure 5: Kruskal’s Algorithm.

Answer:
We first do the topological-sort for graph G and get a vertex list L. Then associate a number
p[i] for each vertex i. p[i] indicates the number of paths from s to i found by the algorithm so
far. We start the search at the vertex s. We initialize p[i] = 0 if i 6= s; p[s] = 1. (This is because
that, at the beginning, there is one path from s to s, consisting of a single vertex s, and we have
not found any path from s to i for any other vertex i.) Then we repeatedly remove the vertices
in topologically sorted order. When a vertex i is removed, for each immediate neighbor u of
the vertex i, we update p[u] = p[u] + p[i]. The correctness of this approach is justified by the
following facts. Since we are dealing with directed acyclic graph, the number of path from s to
any vertex v is the sum of the numbers of the paths from s to v’s immediate predecessors.
Note: When the algorithms ends, for every vertex x, p[x] is the number of distinct paths
from s to x.

5
Algorithm 2 CountPath
1: for i ← 1 to n do
2: p[i] ← 0
3: end for
4: p[s] ← 1
5: i ← L.remove()
6: while i 6= N IL do
7: for u ∈ Adj[i] do
8: p[u] ← p[u] + p[i]
9: end for
10: i ← L.remove()
11: end while

2
9. (6 pts) Consider an undirected connected graph G = (V, E). If we run the BFS algorithm on
G, a BFS-tree Tb results. If we run the DFS algorithm on G, a DFS-tree Td results. If Tb = Td ,
what can you say about G. Justify your answer.

Answer:
We can conclude that G = Tb = Td is a tree.
First of all, G is connected since BFS algorithm only traverse a connected component of G
while DFS will traverse all the vertices and Tb = Td .
By the property of BFS, there are no back edges in G with respect to Tb . By the property
of DFS, there are no cross edges in G with respect to Td . Since any edge in G is either a tree
edge, or a back edge, or a cross edge, and since there are no back edges and cross edges with
respect to Tb = Td , we can conclude that all edges in G are tree edges. Thus G = Tb = Td .
2

You might also like