nov dec2008 key

13
 NOVEMBER/DECEMBER 2008    CS1201   Answer Key Page 1 of 13 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 hall’s 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 Euclid’s algorithm, the consecutive integer checking algorithm and the middle- school 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:

Upload: sivamspecial555

Post on 18-Oct-2015

21 views

Category:

Documents


0 download

DESCRIPTION

Univ Ques

TRANSCRIPT

  • 5/28/2018 Nov Dec2008 Key

    1/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 1of 13

    Question Paper Code: C 3194

    Regulation2004

    CS1201Design and Analysis of Algorithm

    NOV/DEC 2008

    PartA

    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.

    PartB

    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 caseII. In the Best case

    III. In the Average case(or)

    b) Specify the Euclids algorithm, the consecutive integer checking algorithm and the middle-

    school 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:

  • 5/28/2018 Nov Dec2008 Key

    2/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 2of 13

    ALGORITHM Secret (A[0,,n-1])

    //input : An array A[0,,n-1]

    Minval A[0]; maxval A[0];

    For i1to n-1 do

    if(a[i] < minval

    minvalA[i];

    If A[i] > maxval

    Maxval A[i];

    Return maxval-minval;

    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 basicoperation is executed. (8)

    ii). How does this algorithm compare with the straight forward non recursivealgorithm for computing this function (8)

    13. a) i) Describe Quick sort algorithm. (10)

  • 5/28/2018 Nov Dec2008 Key

    3/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 3of 13

    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.

    (or)

    b) For each of the following lists, construct an AVL tree by inserting their elements

    successfully, starting with an empty tree.

    i). 1, 2, 3, 4, 5, 6ii). 6, 5, 4, 3, 2, 1

    iii). 3, 6, 5, 1, 2, 415. a) Using backtracking enumerate how can you solve the following problems.

    i).

    8Queens problemii). Hamiltonian circuit problem

    (or)

    b) i) Solve the following instance of the Knapsack problem by branch and bound

    algorithm. (8)

    a b

    e

    C

    d

    5

    6

    4

    2 3

  • 5/28/2018 Nov Dec2008 Key

    4/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 4of 13

    Item Weight Values

    1 10 $100

    2 7 $63

    3 8 $56

    4 4 $12

    W = 16

    ii) Give an example for the best case input for the branch and bound algorithm for theassignment 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)

  • 5/28/2018 Nov Dec2008 Key

    5/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 5of 13

    Keys:

    PartA

    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

  • 5/28/2018 Nov Dec2008 Key

    6/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 6of 13

    }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 boundalgorithm?

    - 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.

  • 5/28/2018 Nov Dec2008 Key

    7/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 7of 13

    - We have nkinds of items, 1 through n. Each kind of itemjhas a valuepjand a weightwj. 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, whichrestricts the numberxjof copies of each kind of item to zero or one. Mathematically

    the 0-1-knapsack problem can be formulated as:

    minimize subject to

    PartB

    11.a) i) Asymptotic metrics used to evaluate the efficiency of the algorithm:-big Onotation (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) 0 O (g (n)): class of functionsf (n) that grow no faster thang (n)

    Big notation: (Asymptotic Tight Bound)

    T (n) = f (n) = (g (n))

    - If c1*g (n) 0.

    (g (n)): class of functionsf (n) that grow at same rate asg (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 n0are constants > 0 (g (n)): class of functions f (n) that grow at least as fast as g (n)

  • 5/28/2018 Nov Dec2008 Key

    8/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 8of 13

    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 isperformed through out the loop.

    - Thus compari son is the basic operationhere.iii) No. of times basic operation computed:

    - n-1 times basic operation will be performed, where n is the no. of elements inthe array.

    iv) Efficiency class:

    - No. of times the comparisons performed is,= 2 (n-1)

    = O (n)

    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 syllabusb) Compare DFS and BFS:

  • 5/28/2018 Nov Dec2008 Key

    9/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 9of 13

    DFS:

    - DFS implemented using stack- Applications of DFS are,

    Topological Sort Strongly Connected Components. Spanning Forest

    BFS

    - BFS implemented using Queue- Applications of BFS are,

    Compute the connected components Compute a spanning forest Find Shortest path.

    How they fit to Decrease and Conquer Strategy:

    - Not in Syllabus

  • 5/28/2018 Nov Dec2008 Key

    10/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 10of 13

    14.a) Prims Algorithm for minimum cost spanning tree:- Not in Syllabus

    b) AVL Tree:

    - Not in Syllabus15.i) 8Queens 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 xiis the column for ithqueen

    Identify explicit constraints- Explicit constraints using 8-tuple formulation are Si = {1, 2, 3, 4, 5, 6, 7, 8},

    1 i 8

    - Solution space of 88- 8-tuples Identify implicit constraints

    - No two xican 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 88to 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, 5Algorithms:

    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.

    {

  • 5/28/2018 Nov Dec2008 Key

    11/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 11of 13

    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

  • 5/28/2018 Nov Dec2008 Key

    12/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 12of 13

    ii) Hamiltonian circuit problem using Backtracking:

    - Let G = (V, E) be a connected graph with n vertices. A Hamiltonian cycle is a roundtrip path along n edges of G that visits every vertex once and returns to its starting

    position.

    - In other words if a Hamiltonian cycle begins at some vertex v 1 G and the vertices ofG 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 v1and vn+1, which are equal.

    Algori thm: Generating next ver tex

    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

  • 5/28/2018 Nov Dec2008 Key

    13/13

    NOVEMBER/DECEMBER 2008CS1201Answer Key

    Page 13of 13

    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