daa anskey

Upload: kalaraiju

Post on 02-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Daa Anskey

    1/14

    BTECH DEGREE EXAMINATION,MAY2014

    CS010 601 DESIGN AND ANALYSIS OF ALGORITHMS

    ANSWER KEY

    PART-A

    1.

    A recursive algorithm is an algorithm which calls itself with "smaller (or

    simpler)" input values, and which obtains the result for the current input by

    applying simple operations to the returned value for the smaller (or

    simpler) input.

    int factorial(int n)

    {

    if (n == 0)

    return 1;

    else

    return (n * factorial(n-1));

    }

    2.

    Control abstraction means a procedures whose flowof control is clear but

    whose primary operations are specified by other procedures whose precise

    meanings are left defined.

    3. MONTE CARLO METHOD

    4. A minimum spanning tree is a least-cost subset of the edges of a graph that

    connects all the nodes

    Start by picking any node and adding it to the treeRepeatedly: Pick any least-costedge from a node in the tree to a node not in

    the tree, and add the edge and new node to the tree

    Stop when all nodes have been added to the tree

    5. An ordering of the vertices in a directed acyclic graph, such that:If there is a path from uto v, then vappears after uin the ordering.

  • 8/10/2019 Daa Anskey

    2/14

    1. Compute the indegrees of all vertices

    2. Find a vertex Uwith indegree 0 and print it (store it in the ordering)

    If there is no such vertex then there is a cycleand the vertices cannot be ordered. Stop.

    3. Remove Uand all its edges (U,V)from the graph.

    4. Update the indegrees of the remaining vertices.

    5. Repeat steps 2 through 4 while there are vertices to be processed.

    PART B

    6. Space Complexity:-Space complexity of an algorithm is the amount to

    memory needed by the program for its completion.

    The space requirement s(p) of any algorithm p may thereore be written

    as s(p) =c+Sp, where c is a constant

    Time Complexity:-Time complexity of an algorithm is the amount of

    time needed by the program for its completion

    Step count

    Step per execution or Build a table

    7.

    Finding the Maximum And Minimum.

    to find the minimum and maximum items in a list of numbers

    Algorithm StraightMaxMin(a,n,max,min)

    // Set max to the maximum and min to the minimum of a[1:n];

    {

    max=min=a[1];

    for i=2 to n do

    {

    if (a[i]>max) then max=a[i];

  • 8/10/2019 Daa Anskey

    3/14

    if(a[i]

  • 8/10/2019 Daa Anskey

    4/14

    Many decision sequences may be generated.

    9.

    10.

    DETERMINISTIC ALGORITHMS

    Algorithms with the property that the result of every operation is uniquely defined are termed

    deterministic.

    Such algorithms agree with the way programs are executed on a computer.

    Nondeterministic algorithms

    In a theoretical framework we can allow algorithms to contain operations whose outcome is

    not uniquely defined but is limited to a specified set of possibilities.

    The machine executing such operations are allowed to choose any one of these outcomes

    subject to a termination condition.

    This leads to the concept of non deterministic algorithms.

    PART C

    2. ASYMPTOTIC NOTATIONS

    Asymptotic efficiency of algorithms concerned with how the running time of

    an algorithm increases with he size of the input in the limit.The notations used

    to decribe the asymptotic efficiency of an algorithm is called asymptotic

    notations.Asymptotic complexity

    is a way of expressing the main component

    of the cost of an algorithm, using idealized units of computational work. Note

    that we have been speaking about boundson the performance of algorithms,

    rather than giving exact speeds.

    Different asymptotic Notations are,

    Big Oh

    Omega

    Theta

    Little oh

    Little omega

    BIG-OH NOTATION (O)

  • 8/10/2019 Daa Anskey

    5/14

    Big-O is the formal method of expressing the upper bound of an

    algorithm's running time. It's a measure of the longest amount of time it

    could possibly take for the algorithm to complete.

    Defenition

    Given functions f(n)and g(n), we say that f(n)= O(g(n)) if and only if

    there are positive constants cand n0such thatf(n) c g(n)for n n0for

    the algorithm to complete.

    Omega Notation ()

    Definition

    Given functions f(n)and g(n), we say thatf(n)= (g(n)) if and only if there are

    positive constants cand n0such thatf(n)c g(n)for n n0.

    It is the lower bound of any function. Hence it denotes the best case complexity of

    any algorithm. We can represent it graphically as

  • 8/10/2019 Daa Anskey

    6/14

    Theta Notation ()

    Definition

    Given functions f(n)and g(n), we say that f(n)= (g(n)) if and only if there arepositive constants c1,c2and n0such that c1g(n)f(n) c2g(n) for n n0.

    The theta notation is more precise than both the big oh and big omega notations.

    The function f(n)=theta(g(n)) iff g(n) is both lower and upper bound of f(n).

    13, Matrix Multiplication Algorithms:

    When multiplying two 2 2 matrices [Aij] and [Bij]

  • 8/10/2019 Daa Anskey

    7/14

    The resulting matrix [Cij] is computed by the equations

    C11=A11B11+A12B21

    C12=A11B12+A12B22

    C21=A21B11+A22B21

    C22=A21B12+A22B22

    When the size of the matrices are nn,the amount of computation is O(n3), because there are

    n2entries in the product matrix where each require O(n) time to compute.

    Strassens Matrix Multiplication

    Strassens Algorithm (two 2x2 matrices)

    c11 c12 a11 a12 b11 b12

    = *

    c21 c22 a21 a22 b21 b22

    P + S - T+ V R+ T

    =

    Q + S P + RQ + U

    P = (a11+ a22) * (b11+ b22)

    Q = (a21+ a22) * b11

    2221

    1211

    2221

    1211

    2221

    1211

    CC

    CC

    BB

    BB

    AA

    AA

  • 8/10/2019 Daa Anskey

    8/14

    R = a11* (b12- b22)

    S= a22* (b21- b11)

    T = (a11+ a12) * b22

    U= (a21- a11) * (b11+ b12)

    V = (a12- a22) * (b21+ b22)

    7 multiplications

    18 additions / subtractions

    The resulting recurrence relation for T(n) is

    Where

    n is a power of 2, n=2kfor some integer k.

    a and b are constant.

    14.

    Divide:divide array A[0..n-1] in two about equal halves and make copies of each half in arrays B

    and C

    Conquer:

    If number of elements in B and C is 1, directly solve it

    Sort arrays B and C recursively

    Combine:Merge sorted arrays B and C into array A

    Repeat the following until no elements remain in one of the arrays:

    compare the first elements in the remaining unprocessed portions of the arrays

    B and C

  • 8/10/2019 Daa Anskey

    9/14

    copy the smaller of the two into A, while incrementing the index indicating the

    unprocessed portion of that array

    Once all elements in one of the arrays are processed, copy the remaining unprocessed

    elements from the other array into A.

    Algorithm MergeSort(low,high)

    //a[low:high] is a global array to be sorted. Small(P) is true if there is only one element to sort.In this

    case the list is already sorted.

    {

    if(low

  • 8/10/2019 Daa Anskey

    10/14

    //Solve the subproblems

    MergeSort(low,mid);

    MergeSort(mid+1,high);

    // Combine the solutions

    Merge(low,mid,high)

    }

    }

    Algorithm Merge(low,mid,high)

    //a[low:high] is a global array containing two sorted subset in a[low:mid] and in a[mid+1:high]. The goal

    is to merge these two sets into a single set residing in a[low:high]. b[] is an auxilary global array.

    {

    h:=low; i:=low; j:=mid+1;

    while((hmid) and (j high)) do

    {

    if(a*h+ a*j+) then

    {

    b[i]:=a[h]; h=h+1;

    }

    else

    {

    b[i]=a[j];j=j+1;

    }

    i=i+1;

  • 8/10/2019 Daa Anskey

    11/14

    }

    if(h>mid)then

    for k:= j to high do

    {

    b[i]=a[k];i=i+1;

    }

    else

    for k:=h to mid do

    {

    b[i]:=a[k]; i=i+1;

    }

    for k:= low to high do

    a[k]:=b[k];

    }

    If T(n) represent the time for the merging operation is proportional to n then the computing time for

    merge sort is ,

    Where n is a power of 2, n=2k

    for some integer k.

    c and a are constant.

    15. Basics of Kruskals Algorithm

    Edges are initially sorted by increasing weight

    Start with an empty forest ,grow MST one edge at a time

  • 8/10/2019 Daa Anskey

    12/14

    intermediate stages usually have forest of trees (not connected)

    at each stage add minimum weight edge among those not yet used that does not create a cycle

    at each stage the edge may:

    expand an existing tree

    combine two existing trees into a single tree

    create a new tree

    need efficient way of detecting/avoiding cycles

    algorithm stops when all vertices are included

    Algorithm Kruskal(E,cost,n,t)

    // E is the set of edges in G . G has n vertices. Cost[u,v] is the// cost of edge(u,v). T is the set of edges in the minimum-cost

    //spanning tree. The finalcost is returned

    {

    construct a heap out of the edge costs using heapify;

    for i=1 to n do parent[i]=-1;

    // each vertex is in a different set.

    i=0; mincost=0.0;

    while((i

  • 8/10/2019 Daa Anskey

    13/14

    else return mincost;

    }

    The final set is

    {1 2 3 4 5 6 7}

    Analysis of Kruskal

  • 8/10/2019 Daa Anskey

    14/14

    (initialization): O(V)

    (sorting): O(E log E)

    (set-operation): O(E log E)

    Total: O(E log E)