Docsity

Assignment 4 Solution - Design and Analysis of Algorithm | CSCI 212, Assignments of Computer Science

Toggle sidebar
bg1
George Washington University
Department of Computer Science
Csci 212 - Solution to Homework 4
1. Let A[n, n] denote an n×ninput matrix.
(a) The first letter can be anywhere in the matrix. In our algorithm, we are looking for legal
words letter by letter. If we have found a word with iletters, we try to check whether
we can extend the current word by adding an additional letter chosen from the adjacent
letters. If we can, we continue to find the next letter. If we can’t, we print out the
current word with iletters and backtract to the previous letter.
The algorithm F indW ord(A) starts at any one of A[i, j]. To avoid using one letter more
than once, we use an array coordinate[k] to record the coordinate of the kth letter in
the word. After we fix the first letter, say A[i, j], we call the algorithm Extend(i, j, 2) to
extend the word. Each time we have 8 possible directions to extend a word, (i−1, j −
1),(i−1, j),(i−1, j + 1),(i, j −1),(i, j + 1),(i+ 1, j −1),(i+ 1, j ) and (i+ 1, j + 1).
We call Next(i, j) to check whether the new letter is legal or not.
F indW ord(A)
1: {
2: for i= 1 to ndo
3: {
4: for j= 1 to ndo
5: {//The first letter could be A[i, j]
6: x[1] = A[i, j];
7: coordinate[1] = [i, j];
8: Extend(i, j, 2);
9: }
10: }
11: }
Extend(i, j, k)
1: {
2: if Next(i−1, j −1) then
3: {
4: x[k] = A[i−1, j −1];
5: coordinate[k] = [i−1, j −1];
6: Extend(i−1, j −1, k + 1);
7: }
8: if Next(i−1, j) then
9: {
10: x[k] = A[i−1, j];
11: coordinate[k] = [i−1, j];
12: Extend(i−1, j, k + 1);
pf3
pf4
pf5

Partial preview of the text

Download Assignment 4 Solution - Design and Analysis of Algorithm | CSCI 212 and more Assignments Computer Science in PDF only on Docsity!

George Washington University Department of Computer Science Csci 212 - Solution to Homework 4

  1. Let A[n, n] denote an n × n input matrix.

(a) The first letter can be anywhere in the matrix. In our algorithm, we are looking for legal words letter by letter. If we have found a word with i letters, we try to check whether we can extend the current word by adding an additional letter chosen from the adjacent letters. If we can, we continue to find the next letter. If we can’t, we print out the current word with i letters and backtract to the previous letter. The algorithm F indW ord(A) starts at any one of A[i, j]. To avoid using one letter more than once, we use an array coordinate[k] to record the coordinate of the kth letter in the word. After we fix the first letter, say A[i, j], we call the algorithm Extend(i, j, 2) to extend the word. Each time we have 8 possible directions to extend a word, (i − 1 , j − 1), (i − 1 , j), (i − 1 , j + 1), (i, j − 1), (i, j + 1), (i + 1, j − 1), (i + 1, j) and (i + 1, j + 1). We call N ext(i, j) to check whether the new letter is legal or not. F indW ord(A) 1: { 2: for i = 1 to n do 3: { 4: for j = 1 to n do 5: { //The first letter could be A[i, j] 6: x[1] = A[i, j]; 7: coordinate[1] = [i, j]; 8: Extend(i, j, 2); 9: } 10: } 11: } Extend(i, j, k) 1: { 2: if N ext(i − 1 , j − 1) then 3: { 4: x[k] = A[i − 1 , j − 1]; 5: coordinate[k] = [i − 1 , j − 1]; 6: Extend(i − 1 , j − 1 , k + 1); 7: } 8: if N ext(i − 1 , j) then 9: { 10: x[k] = A[i − 1 , j]; 11: coordinate[k] = [i − 1 , j]; 12: Extend(i − 1 , j, k + 1);

14: if N ext(i − 1 , j + 1) then 15: { 16: x[k] = A[i − 1 , j + 1]; 17: coordinate[k] = [i − 1 , j + 1]; 18: Extend(i − 1 , j + 1, k + 1); 19: } 20: if N ext(i, j − 1) then 21: { 22: x[k] = A[i, j − 1]; 23: coordinate[k] = [i, j − 1]; 24: Extend(i, j − 1 , k + 1); 25: } 26: if N ext(i, j + 1) then 27: { 28: x[k] = A[i, j + 1]; 29: coordinate[k] = [i, j + 1]; 30: Extend(i, j + 1, k + 1); 31: } 32: if N ext(i + 1, j − 1) then 33: { 34: x[k] = A[i + 1, j − 1]; 35: coordinate[k] = [i + 1, j − 1]; 36: Extend(i + 1, j − 1 , k + 1); 37: } 38: if N ext(i + 1, j) then 39: { 40: x[k] = A[i + 1, j]; 41: coordinate[k] = [i + 1, j]; 42: Extend(i + 1, j, k + 1); 43: } 44: if N ext(i + 1, j + 1) then 45: { 46: x[k] = A[i + 1, j + 1]; 47: coordinate[k] = [i + 1, j + 1]; 48: Extend(i + 1, j + 1, k + 1); 49: } 50: write(x[1 : k]); 51: }

N ext(i, j)

1: { 2: if i < 1 or i > n or j < 1 or j > n then return f alse; // Outside the matrix 3: for l = 1 to k − 1 do

CheckDFS(T, G, v) 1: { 2: visitedT (v) = visitedG(v) = 1; 3: for each vertex w adjacent from v in T do 4: { 5: if visitedT [w] = 0 then CheckDF S(T, G, w); 6: } 7: if there is a vertex w′^ adjacent from v in G and vistitedG[w] = 0 then return f alse; 8: return true; 9: } 10: We do DFS on both T and G. The time complexity is then O(|E|).

  1. Let the BFS tree rooted at v be TB. Suppose there is DFS tree TD rooted at v and it is identical to TB. For any edge of G, say (u, w), in the TB , there are only two cases. (1) u and w are at the same depth. (2) u and w are at two consecutive depths. We discuss case by case. For the case (1) that u and w are at the same depth in the TB , we claim this case is impossible if there is a DFS tree TD identical to TB. If u and w at the same depth in the TB , which implies that u is reachable from w or w is reachable from u, then in the TD, it is either that u is a descendant of w or that w is descendant of u. In other words, in this case, u and w can’t be at the same depth in the TD. So this case is not possible. For the case (2) that u and w are at two consecutive depths in the TB , without loss of generality, we assume u is at depth i and w is at depth i + 1. Under this case, there are also two cases. (i) u is the parent of w in the TB. (ii) u is not the parent of w, i.e., there is another node, say x, is the parent of w in the TB. For subcase (i) that u is the parent of w, which implies that the edge (u, w) is also present in the TB , it is possible that TD contains such an edge. For subcase (ii) that u is not the parent of w but another node x is the parent of w in the TB , this implies that u is reachable from x or x is reachable from u. Then in any DFS tree, if u is visited first, then w will be a descendant of u and vice versa. u and x cannot be at the same depth in TD. So this subcase is impossible. We conclude what we get now. For any edge (u, w) of G, if TB is identical to TD, (u, w) must present in the TB. Since TB is a spanning tree, we can conclude that G is a tree. If G is a tree, then its spanning tree is unique and both TB and TD are spanning tree of G. So TB is identical to TD. So if and only if G is a tree, the BFS tree is identical to its DFS tree.
  2. Solution: The problem is asking whether there exists a cycle that contains both (u 1 , v 1 ) and (u 2 , v 2 ) but not (u 3 , v 3 ). If there exits such a cycle, after we delete the edge (u 3 , v 3 ), (u 1 , v 1 ) and (u 2 , v 2 ) are still on the same cycle. Then we collapse the edge (u 1 , v 1 ) into a new node denoted as wu 1 v 1 as shown in the Figure 1.

u 1

v 1

Wu1v

Figure 1: Collapse (u 1 , v 1 ) into wu 1 v 1

We also collapse the edge (u 2 , v 2 ) into another new node denoted as wu 2 v 2 , as shown in the Figure 2.

u 2

v 2 Wu2v

Figure 2: Collapse (u 2 , v 2 ) into wu 2 v 2

Now we start a DFS rooted at wu 1 v 1. If we can visit wu 2 v 2 during this search and either wu 2 v 2 has a back edge connected with wu 1 v 1 or some descendant of wu 2 v 2 has a back edge connected with wu 1 v 1 , we know that the two nodes wu 1 v 1 and wu 2 v 2 are on the same cycle, i.e., in the original graph the edges (u 1 , v 1 ) and (u 2 , v 2 ) are on the same cycle. Since we have deleted the edge (u 3 , v 3 ) at the first beginning, such a cycle doesn’t include the edge (u 3 , v 3 ). Whether wu 2 v 2 or its descendant has a back edge connected to wu 1 v 1 can be verified by checking whether the low value L(wu 2 v 2 ) is equal to 1 after the DFS.

So the algorithm CommonCycle() is as follows.

CommonCycle()

1: Delete the edge (u 3 , v 3 ); 2: Collapse the edge (u 1 , v 1 ) into a new node wu 1 v 1 ; 3: Collapse the edge (u 1 , v 1 ) into a new node wu 2 v 2 ; 4: Call the algorithm Art(wu 1 v 1 , φ); 5: If df n(wu 2 v 2 ) = 0 then return f alse. // (The two nodes wu 1 v 1 and wu 2 v 2 are not con- nected). 6: If L(wu 2 v 2 ) 6 = 1 then return f alse, otherwise return true.

Art(u, v)

1: // num is initialized to 1. The array df n is initialized to 0. 2: { 3: df n(u) = num; L(u) = num; num = num + 1; 4: for each vertex w adjacent from u do 5: {

(b) In the state space tree, each node corresponding to a vertex in the original graph G. The path from the root to a node i corresponds to a clique whose vertices are all the nodes on the path from the root to the node i except the root node (the root node is a dummy node and is not present in G). It is possible to extend the current clique to a bigger clique. In the state space tree, for each live node i, which corresponds to a vertex u in the G, we need to find an estimator, say c(ˆi), to speed up the search for the solution. One possible estimator is chosen as follows. Suppose along the path from the root to a node i, a node j corresponding to a vertex v in G, has the minimum degree d(v). Suppose along the path from the root to a node i, there is l(i) nodes on the path except the root node. Then the maximum number of new vertices we can add to extend the current clique is d(v) − (l(v) − 1). Then the maximum possible clique we can get in the subtree of i is bounded by

W (i) + sum of [d(v) − (l(v) − 1)] largest vertices that are not included in the current clique,

where W (i) is the weight of the current clique. Thus, for node i in the state space tree, the cost function is set as

c(ˆi) = W (i) + sum of [d(v) − (l(v) − 1)] largest vertices that are not included in the current clique,

The algorithm M axClique(G) returns the weight of the maximum clique. M axClique(G) 1: E = the root node; 2: Q is an array. Initially it has only one node, E; 3: Expand the node E with each vertex of G being a child node of E. Thus, E has n children. Add all the new children of E into Q. The node E is not live any more. 4: For each child node i of E, calculate c(ˆi); 5: Find the node i with maximum c(ˆi) among all the live nodes and set this node to be E. 6: If E is a leaf, meaning we can’t expand it any longer, and the weight W (i) is maximum among all live nodes, then print out the path from E to the root and return; 7: Expand the node E with children nodes and calculate c(ˆi) for each child node. Add all the new children nodes of E into the Q. The node E is not live any more. 8: Find the node i with maximum c(ˆi) among all the live nodes and set this node to be E. Delete this node from Q and go back to step 6.