



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




George Washington University Department of Computer Science Csci 212 - Solution to Homework 4
(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|).
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.