Professional Documents
Culture Documents
Assignment #4, CS/531
Assignment #4, CS/531
a
6 c
1
3
5
e
2 d
9
b
7
6 3
8
4 f
g
4 2
h
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
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:
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:
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
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
m n o p
q r s
t
u
v w
x y z
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