Vous êtes sur la page 1sur 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key

Question Paper Code: C 3194


Regulation 2004 CS1201 Design and Analysis of Algorithm NOV/DEC 2008 Part A 1. Define an algorithm. 2. Design an algorithm for checking whether the given word is a palindrome or not, i.e., whether the word is the same even when reversed. E.g., MADAM is a palindrome. 3. List out the steps for empirical analysis of analysis of algorithm efficiency. 4. What is the significance of Fibonacci number of sequence? 5. Give an example problem that cannot be solved by a brute force attack. 6. Write a pseudo code for a divide and conquer algorithm for finding the position of the largest element in an array of n numbers. 7. Define a Heap. 8. Give pseudo code of the Wars halls algorithm. 9. When do you terminate the search path in a state space tree of a branch and bound algorithm? 10. Define a Knapsack problem. Part B 11. a) i) Elaborate the various asymptotic metrics used to evaluate the efficiency of the algorithm. (10) ii) Use the most appropriate notation to indicate the time efficiency class of sequential search. (6) I. In the Worst case II. In the Best case III. In the Average case (or) b) Specify the Euclids algorithm, the consecutive integer checking algorithm and the middleschool algorithm for computing the greatest common divisor of two integers. Which of them is simpler? Which is more efficient? Why? (16)

12. a) Consider the following algorithm:

Page 1 of 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key ALGORITHM Secret (A[0,,n-1]) //input : An array A[0,,n-1] Minval A[0]; For i1to n-1 do if(a[i] < minval minvalA[i]; If A[i] > maxval Maxval A[i]; Return maxval-minval; maxval A[0];

i). What does this algorithm compute? (2) ii). What is the basic operation? (2) iii). How many times is the basic operation computed? (4) iv). What is the efficiency class of this algorithm? (3) v). Suggest an improvement or a better algorithm altogether and indicate the efficiency class. If you cant do it, prove that in fact it cant be done. (5) (or) b) Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) = 13 + 23 + . + n3; ALGORITHM S(n) //Input: A +ve integer n; //Output: The sum of the first n cubes If n=1 return 1; else return S(n-1) + n*n*n i). Set up and solve a recurrence relation for the number of the algorithm basic operation is executed. (8) ii). How does this algorithm compare with the straight forward non recursive algorithm for computing this function (8) 13. a) i) Describe Quick sort algorithm. (10)
Page 2 of 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key ii) Apply quick sort to the list E, X, A, M, P, L, E in alphabetical order. Draw the tree of the recursive calls made. (6) (or) b) Compare and Contrast the Depth First Search and Breadth First Search algorithms. How do they fit to the decrease and conquer strategy? (16) 14. a) i) Describe the Prims algorithm for finding the minimum cost spanning tree. ii) Apply Prims algorithm to the following graph.
5

a
2 7 4 3

b
e
6 5

C
4

d
(or)

b) For each of the following lists, construct an AVL tree by inserting their elements successfully, starting with an empty tree. i). ii). iii). 1, 2, 3, 4, 5, 6 6, 5, 4, 3, 2, 1 3, 6, 5, 1, 2, 4

15. a) Using backtracking enumerate how can you solve the following problems. i). ii). 8 Queens problem Hamiltonian circuit problem (or) b) i) Solve the following instance of the Knapsack problem by branch and bound algorithm. (8)

Page 3 of 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key Item 1 2 3 4 W = 16 Weight 10 7 8 4 Values $100 $63 $56 $12

ii) Give an example for the best case input for the branch and bound algorithm for the assignment problem. (4)

iii) In the best case, how many nodes will be in the state space tree of the branch and bound algorithm for the assignment problem. (4)

Page 4 of 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key Keys: Part A 1. Algorithm: An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in finite amount of time. 2. Algorithm for Palindrome:
Algorithm : boolean isPalindrome(String testString){ int length=testString.length(); for(int i=0;i<length/2;i++) if(testString[i]!=testString[length-i+1]) { return false; } //else it's palindrome return true; }

3. Steps for Empirical analysis of algorithm Efficiency: Not in syllabus

4. Significance of Fibonacci series: 5. Examples that cannot be solves by Brute force attack: Not in syllabus 6. Algorithm for finding the position of the largest element in array of n numbers (divide and conquer): void maxposition(int i, int j) //a[ 1 : n ] is a global array. Parameters i & j are integers, 1 <= i <= j <= n. { int pos1, pos2, max1,max; if (i == j) { max = a [i]; //max position is i; } else if (i == j-1) { if (a [i] < a [j]) { max = a [j]; pos1=j;
Page 5 of 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key } else max = a [i]; pos1=i; } else { int mid = (i + j)/2; maxpos (i, mid, max); max1=max; pos2=pos1; maxpos ( mid+1, j, max1); if (max < max1) { max = max1; pos1=pos2; //max position is pos1; } else { //max position is pos1; } } } 7. Heap: Not in Syllabus 8. Pseudo code for Warshalls algorithm: Not is syllabus

9. When do you terminate the search path in a state space tree of a branch and bound algorithm? The algorithm always keeps the list of live nodes in a list When all the children of E have been generated, E becomes a dead node Happens only if none of Es children is an answer node

If there are no live nodes left, the algorithm terminates; otherwise, Least() correctly chooses the next E- node and the search continues.

10. Knap sack problem: Given items of different values and volumes, find the most valuable set of items that fit in a knapsack of fixed volume.

Page 6 of 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key We have n kinds of items, 1 through n. Each kind of item j has a value pj and a weight wj. We usually assume that all values and weights are nonnegative. The maximum weight that we can carry in the bag is W. The most common formulation of the problem is the 0-1 knapsack problem, which restricts the number xj of copies of each kind of item to zero or one. Mathematically the 0-1-knapsack problem can be formulated as:

minimize

subject to

Part B 11. a) i) Asymptotic metrics used to evaluate the efficiency of the algorithm: - big O notation (big Oh) - big notation (big theta) - big notation (big omega) Big Oh notation: (Asymptotic upper bound) T (n) = f (n) = O (g (n)) - If f (n) <= c*g (n) for all n n0, where c & n0 are constants > 0 O (g (n)): class of functions f (n) that grow no faster than g (n)

Big notation: (Asymptotic Tight Bound) T (n) = f (n) = (g (n)) - If c1*g (n) <= f (n) <= c2*g (n) for all n > n0, where c1, c2 and n0 are constants > 0. (g (n)): class of functions f (n) that grow at same rate as g (n)

Big notation: (Asymptotic lower bound) T (n) = f (n) = (g (n)) - If f (n) >= c*g (n) for all n > n0, where c and n0 are constants > 0 (g (n)): class of functions f (n) that grow at least as fast as g (n)

Page 7 of 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key ii) Time Efficiency of a sequential search of an array Best case: O (1) Worst case: O (n) Average case: O (n)

b) Euclids Algorithm: Not in syllabus

12. a) i) What does this algorithm compute? This algorithm computes the difference between the maximum and minimum element in an array having n elements. ii) Basic operation: Operations performed here are, Comparison of array element with min & max value. Assignment operation. Assignment is performed only when the condition satisfied. But comparison is performed through out the loop. Thus comparison is the basic operation here. n-1 times basic operation will be performed, where n is the no. of elements in the array. iv) Efficiency class: No. of times the comparisons performed is, = 2 (n-1) = O (n)

iii) No. of times basic operation computed: -

b)

- Not in syllabus

13. a) i) Quick sort algorithm: Not in syllabus

ii) Quick sort to sort E, X, A, M, P, L, E: Not in syllabus

b) Compare DFS and BFS:

Page 8 of 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key DFS: DFS implemented using stack Applications of DFS are, BFS BFS implemented using Queue Applications of BFS are, Compute the connected components Compute a spanning forest Find Shortest path. Topological Sort Strongly Connected Components. Spanning Forest

How they fit to Decrease and Conquer Strategy: Not in Syllabus


Page 9 of 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key 14. a) Prims Algorithm for minimum cost spanning tree: Not in Syllabus

b) AVL Tree: Not in Syllabus 15. i) 8 Queens problem using Backtracking: Place eight queens on an 8 8 chessboard so that no queen attacks another queen.

Identify data structures to solve the problem First pass: Define the chessboard to be an 8 8 array Second pass: Since each queen is in a different row, define the chessboard solution to be an 8-tuple (x1, . . . , x8), where xi is the column for ith queen Identify explicit constraints Explicit constraints using 8-tuple formulation are Si = {1, 2, 3, 4, 5, 6, 7, 8}, 1 i8 Solution space of 88 - 8-tuples

Identify implicit constraints No two xi can be the same, or all the queens must be in different columns All solutions are permutations of the 8-tuple (1, 2, 3, 4, 5, 6, 7, 8) Reduces the size of solution space from 88 to 8! tuples

No two queens can be on the same diagonal.

The solution above is expressed as an 8-tuple as 4, 6, 8, 2, 7, 1, 3, 5

Algorithms: boolean place( int k, int i) // Returns true if a queen can be placed in kth row and ith column. // otherwise it returns false. x [] is a global array whose first (k-1) values have been set. // abs returns the absolute value of r. {

Page 10 of 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key for ( int j=1; j < k; j++) { if ( (x [j] == i) || (abs (x [j] i) == abs (j-k))) { return false; } return true; } }

void Nqueen (int k, int n) /*using backtracking, this procedure prints all possible placements of a n queens on n x n chess board so that they are non attacking. */ { for (int i=1; i<=n; i++) { if (place (k,i)) { x [k] = i; if ( k==n) { for (int j=1; j<=n; j++) { cout << x [j]; } } else { Nqueen (k+1, n); } } }

Page 11 of 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key ii) Hamiltonian circuit problem using Backtracking: Let G = (V, E) be a connected graph with n vertices. A Hamiltonian cycle is a round trip path along n edges of G that visits every vertex once and returns to its sta rting position. In other words if a Hamiltonian cycle begins at some vertex v1 G and the vertices of G are visited in the order v1, v2., vn+1, then the edges (vi, vi+1) are in E, 1 i n, and the vi are distinct except for v1 and vn+1, which are equal. Algorithm: Generating next vertex void Nextvalue (int k) { do { x [k] = (x [k] + 1) % (n+1); // Next vertex. if (!x [k]) { return; } if (G [x [k-1]] [x [k]]) { // Is there an edge? for (int j=1; j<=k-1; j++) { if (x [j] == x [k]) { break; } if (j==k) // If true, then the vertex is distinct. if ((k<n) || ((k==n) && G [x [n]] [x [1])) { return; } } } } while (1); }
Page 12 of 13

NOVEMBER/DECEMBER 2008 CS1201 Answer Key void Hamiltonian (int k) // finding Hamiltonian cycle. { do { Nextvalue (k); // Assign a legal next value to x [k] if (!x [k]) return; if (k == n) { for (int i=1; i<=n; i++) { cout << x [i]; } } else { Hamiltonian(k+1); } }while (1); }

Page 13 of 13

Vous aimerez peut-être aussi