HW 3 Ans
HW 3 Ans
1 1 2 3/
2 4 5/
2 3 3 6 7/
4 /
4 5 6 7 5 /
6 /
7 /
Adjacency Matrix:
1 2 3 4 5 6 7
1 0 1 1 0 0 0 0
2 0 0 0 1 1 0 0
3 0 0 0 0 0 1 1
4 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0
2. 22.1-5 The square of a directed graph G = (V, E) is the graph G2 = (V, E2) such that
(u,w) ∈ E2 if and only if for some v ∈ V, both (u,v) ∈ E and (v, w) ∈ E. That is, G2 contains
an edge between u and w whenever G contains a path with exactly two edges between u and
w. 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.
3. 22.2-1 Show the d and Π values that result from running breadth-first search on the
directed graph of Figure 22.2(a), using vertex 3 as the source.
-/-/- 5/3/4
1 2 3 1/0/-
4 5 6
4/2/5 2/1/3 3/1/3
4. 22.2-4 What is the running time of BFS if its input graph is represented by an adjacency
matrix and the algorithm is modified to handle this form of input?
Each vertex can be explored once and its adjacent vertices must be determined too. This takes
Θ(V2) time.
5. Do a DFS on figure 22.6 (p.611). Classify each edge based on the DFS tree you determine.
1/16 B 17/20
q r
T T
2/7 T
C
s t
F T
8/15 C u
T B T
y 18/19
v T w 9/12 x 13/14
3/6 4/5 T
B
z
10/11
z
12/13
Strongly connected components are {s, w, v}, {q, y, t}, {x, z}, {r}, {u}
T T
T
x y z
15/16 9/18 12/13
p n o s m r y v x w z u q t
8. 22.5-1 How can the number of strongly connected components of a graph change if a
new edge is added?
1 0 2
Consider
For this algorithm, the first DFS will give a list 1 2 0 for the second DFS. All vertices will be
incorrectly reported to be in the same SCC.
For the CLRS algorithm, the first DFS will give a list 0 2 1 for the second DFS. After reversing
edges, the correct SCCs {0, 1} and {2} will be reported.
10. 22.5-4 Prove that for any directed graph G, we have ((GT)SCC)T = GSCC. That is, the
transpose of the component graph of GT is the same as the component graph of G.
Since the strongly connected relationship is an equivalence relation, G and GT will always have
the same strongly connected components. If two vertices X and Y are not strongly connected,
then there is a unique path from X to Y in G iff there is a unique path from Y to X in GT.
A similar property will also hold for the component graph and transpose of the component graph,
i.e. If two vertices X and Y are not strongly connected in G, then here is a unique path from
SCC(G,X) to SCC(G,Y) in (G)SCC iff there is a unique path from SCC(GT,Y) to SCC(GT,X) in
(GT)SCC).
11. 23.1-1
Let (u, v) be a minimum-weight edge in a graph G. Show that (u, v) belongs to some
minimum spanning tree of G.
Let A be a subset of some MST T such that (u, v) ∉ A. To choose an edge to be added to A, all
the edges on the cut are considered and an edge with lowest weight is selected. Since (u, v) is the
minimum weight edge in the graph G, it gets selected on some cut.
12. 24.3-1 Run Dijkstra’s algorithm on the directed graph of Figure 24.2, first using vertex
s as the source and then using vertex z as the source. In the style of Figure 24.6, show the d
and Π values and the vertices in set S after each iteration of the while loop.
Black vertices are in the set S. The black, thick arrows are the values of Π and the values of d are
included are listed inside each node.
T X
A. B. T X
6
∞ ∞ 6
3 3 ∞
3
4
0 2 1 2 4
7 0 2 1 2
7
S 5
6 S 5
∞ ∞ 6
5 ∞
Y 3 Z
Y 3 Z
T X
C. D. T X
6
3 9 6
3 3 9
3
4
0 2 1 2 4
7 0 2 1 2
7
S 5
6 S 5
5 11 6
5 11
Y 3 Z
Y 3 Z
E. F.
T X T X
6 6
3 9 3 9
3 3
4 4
0 2 1 2 0 2 1 2
7 7
S 5 S 5
6 6
5 11 5 11
Y 3 Z Y 3 Z
Using Vertex Z as the source.
A. T X B.
T X
6
∞ ∞ 6
3 ∞ 7
3
4
∞ 2 1 2 4
7 3 2 1 2
7
S 5 5
6 S
∞ 0 6
∞ 0
Y 3 Z
Y 3 Z
C. D.
T X T X
6 6
6 7 6 7
3 3
4 4
3 2 1 2 3 2 1 2
7 7
5 5
S S
6 6
8 0 8 0
Y 3 Z Y 3 Z
E. T X F. T X
6
6 7 6
3 6 7
3
4
3 2 1 2 4
7 3 2 1 2
5 7
S 5
6 S
8 0 6
8 0
Y 3 Z
Y 3 Z
G. T X
6
6 7
3
4
3 2 1 2
7
5
S
6
8 0
Y 3 Z
13. a. Determine the transitive closure of the following Boolean matrix by using Warshall’s
algorithm.
1 0 1 1 0
0 0 1 1 0
1 0 0 0 0
1 0 1 1 1
0 0 0 1 0
T(1) = T(2)=
1 0 1 1 0 1 0 1 1 0
0 0 1 1 0 0 0 1 1 0
1 0 1 1 0 1 0 1 1 0
1 0 1 1 1 1 0 1 1 1
0 0 0 1 0 0 0 0 1 0
T(3)= T(4)=
1 0 1 1 0 1 0 1 1 1
1 0 1 1 0 1 0 1 1 1
1 0 1 1 0 1 0 1 1 1
1 0 1 1 1 1 0 1 1 1
0 0 0 1 0 1 0 1 1 1
T(5)= 1 0 1 1 1
1 0 1 1 1
1 0 1 1 1
1 0 1 1 1
1 0 1 1 1
b. Convert the matrix to indicate successors and use the version of Warshall’s algorithm
that allows path tracing.
0 -1 2 3 3
2 -1 2 3 3
0 -1 0 0 0
0 -1 2 3 4
3 -1 3 3 3
14. 26.2-2
In Figure 26.1(b), which is the flow across the cut ({s, v2, v4}, {v1, v3, t})? What is the
capacity of this cut?
12/12
v1 v3
11/16 15/20
s 10 1/14 7/7 t
4/9
8/13
v2 v4 4/4
11/14
Net flow across the cut is f(s, v1) + f(v2,v1) + f(v2,v3) + f(v3,v4) + f(v4,t) = 11 + 1 + -4 + 7 + 4
= 19 and its capacity is = 16 + 14 +7 + 4 = 41
16 26.2-3
12 12 / 12
v1 v3 v1 v3
16 20 12 / 16 12/ 20
10 4 7 10 4
s t s 7 t
9 9
13 13
4 4
v2 v4 v2 v4
14 14
12 12 / 12
v1 v3 8 v1 v3
12 12 / 16 12 / 20
4 12
10 4 10 4 7
s 7 t s t
9 9
13 4 / 13
4 4/4
v2 v4 v2 v4
14 4 / 14
12 12 / 12
v1 v3 v1 v3
8 19 / 20
12 12 / 16
4 12
10 4 10 4 7/7
7 s t
s t 9
9
9 11 / 13
10 4/4
4 4 v2 v4
v2 v4 11 / 14
4
12
v1 v3 1
12
4 19
10 4 7
s t
9
2
3
11 4
v2 v4
11
17 26.3-1
1 1
1 6
1 1
1 2
1
1 7
1 Augmenting path
s 1 3 t
1
1
1
1 S ? 1 ? 6 ? t
8
1 4 1 1
1 9
5
Flow N/W:
1
1/1
1/1 6
1 1/1
1 2
1 7
1
1
s 1 3 t
1
1 1
1 8
1 4 1 1
1 9
5
Residual N/W:
1
1
1 1 6 1
1 2
1
1 7 1 Augmenting path
s
1 3 t
1 1
1
1
S ? 2 ? 8 ? t
8
4 1
1
1
1 9
5
1 1
1 1 6 1
1 2
1
1 7
1 Augmenting path
s 1 3 t
1 1
1
1
S ? 3 ? 7? t
8
1 4 1 1
1 9
5