3.mathematical analysis of nonrecursive algorithms

Upload: ik-surya-negara

Post on 14-Apr-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    1/22

    CS3024-FAZ 1

    Mathematical Analysis ofNonrecursive Algorithms

    Design and Analysis of Algorithms(CS3024)

    23/02/2006

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    2/22

    CS3024-FAZ 2

    Learning by Examples

    We systematically apply the general

    framework outlined before to analyzing the

    efficiency of nonrecursive algorithms.

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    3/22

    CS3024-FAZ 3

    Important Sum Manipulations (1)

    )(2

    1

    2

    )1(...21

    limitsintegerupper&lower:;11

    )(

    22

    10

    111

    11

    nnnn

    nii

    ullu

    baba

    acca

    n

    i

    n

    i

    u

    li

    u

    i

    i

    u

    i

    i

    u

    i

    ii

    u

    i

    i

    u

    i

    i

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    4/22

    CS3024-FAZ 4

    Important Sum Manipulations (2)

    n

    i

    n

    n

    i

    i

    nn

    n

    i

    i

    kkkkn

    i

    k

    n

    i

    nni

    n

    aa

    aaaaa

    n

    k

    ni

    nnnnni

    1

    12

    1

    1

    1

    12

    0

    1

    1

    3222

    1

    2

    lglg

    ...5772.0;ln...1

    )1(1

    1...1

    1

    1...21

    31

    6)12)(1(...21

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    5/22

    CS3024-FAZ 5

    Example 1

    Algorithm MaxElement(A[0..n-1])

    // determine the value of the largest element in a

    // given array

    // input: An array A[0..n-1] of real number// output: the value of the largest element in A

    maxval A[0]

    fori 1 to n-1 do

    ifA[i] > maxval

    maxval A[i]

    return maxval

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    6/22

    CS3024-FAZ 6

    Exp1: Analysis (1)

    Input size = number of elements = n

    Most often executed inside the forloop

    The comparison A[i] > maxvalThe assignment maxval A[i]

    Basic operation = the comparison

    Executed on each repetition of the loopThe assignment are not

    The number of comparison will be the

    same for all array of size n

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    7/22CS3024-FAZ 7

    Exp1: Analysis (2)

    C(n) = the number of times this comparison

    is executed

    The algorithm makes one comparison on

    each execution of the loop within the

    bounds between 1 and n-1 (inclusively)

    Thus, we have:

    )(11)(1

    1

    nnnCn

    i

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    8/22CS3024-FAZ 8

    Analyzing Efficiency of

    Nonrecursive Algorithms (1)

    1. Decide on a parameter(s) indicating an inputs

    size

    2. Identify the algorithms basic operation

    (typically, it is located in its inner most loop)3. Check whether the number of times the basic

    operation is executed depends only on the size

    of an input. If it also depend on some

    additional property, the worst-case, average-

    case, and, if necessary, the best-case

    efficiencies have to be investigated separately

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    9/22CS3024-FAZ 9

    Analyzing Efficiency of

    Nonrecursive Algorithms (2)

    4. Set up a sum expressing the number of

    times the algorithms basic operation is

    executed

    5. Using standard formulas and rules of

    sum manipulation, either find a closed-

    form formula for the count or, at the very

    least, establish its order of growth

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    10/22CS3024-FAZ 10

    Example 2

    Algorithm UniqueElements(A[0..n-1])

    //checks whether all the elements in a given

    // array are distinct

    //input: an array A[0..n-1]//output: returns true if all elements in A are

    // distinct, and false otherwise

    fori 0 to n 2 doforji+1 to n 1 do

    ifA[i] = A[j] return false

    return true

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    11/22CS3024-FAZ 11

    Exp2: Analysis (1)

    Inputs size = n

    Innermost loop contains a single operation

    Basic operation = comparison

    The number of comparison will depend not

    only on n but also on whether there are

    equal elements in the array and, if there

    are, which array positions they occupy

    We will limit our investigation on the worst-

    case only

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    12/22CS3024-FAZ 12

    Exp2: Analysis (2)

    The worst-case input is an array for which

    Cworst(n) is the largest among all arrays of

    size n

    There are two kinds of worst-case inputs:

    Array with no equal elements

    Array in which the last two elements are the

    only pair of equal elements

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    13/22CS3024-FAZ 13

    Exp2: Analysis (3)

    )(21

    2)1(1...)2()1(

    )1(]1)1()1[(1)(

    22

    2

    0

    2

    0

    2

    0

    1

    1

    nnnnnn

    ininnCn

    i

    n

    i

    n

    i

    n

    ij

    worst

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    14/22CS3024-FAZ 14

    Example 3

    AlgorithmMatrixMultiplication(A[0..n-1, 0..n-1],B[0..n-1, 0..n-1])//multiplies two square matrices of order n by the

    // definition-based algorithm

    //input: two n-by-n matrices A and B

    //output: matrix C = AB

    fori 0 to n-1 do

    forj 0 to n-1 do

    C[i,j] 0,0

    fork 0 to n-1 do

    C[i,j] C[i,j] + A[i,k]* B[k,j]

    return C

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    15/22CS3024-FAZ 15

    Exp3: Analysis (1)

    Inputs size = matrix order n

    In the innermost loop: multiplication &

    addition basic operation candidates

    MUL & ADD executed exactly once on each

    repetition on innermost loop we dont have

    to choose between these two operations

    Sum of total number of multiplication

    1

    0

    321

    0

    1

    0

    1

    0

    1

    0

    1

    0

    1)(n

    i

    n

    i

    n

    j

    n

    i

    n

    j

    n

    k

    nnnnM

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    16/22CS3024-FAZ 16

    Exp3: Analysis (2)

    Estimate the running time of the algorithmon a particular machine

    More accurate estimation (include addition)

    cm: the time of one multiplication

    ca: the time of one addition

    3)()( ncnMcnTmm

    333 )()()()( nccncncnAcnMcnT amamam

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    17/22CS3024-FAZ 17

    Example 4

    Algorithm Binary(n)

    //input: a positive decimal integern

    //output: the number of binary digits in nsbinary representation

    count 1

    while n > 1 do

    countcount+ 1n n/2

    return count

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    18/22CS3024-FAZ 18

    Exp4: Analysis (1)

    The most frequent executed operation is

    the comparison n > 1

    The number of times the comparison will

    be executed is larger than the number of

    repetition of the loops body by exactly 1

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    19/22CS3024-FAZ 19

    Exp4: Analysis (2)

    The value of n is about halved on each

    repetition of the loop about log2 n

    The exact formula: log2

    n + 1

    Another approach analysis techniques

    based on recurrence relation

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    20/22CS3024-FAZ 20

    Exercises (1)

    1. Compute the following sums:

    1 + 3 + 5 + 7 ++ 999

    2 + 4 + 6 + 8 ++ 1024

    2. Compute order of growth of the following

    sums

    n

    i

    n

    j

    n

    i

    n

    j

    jn

    i

    n

    i

    ijiii1 1

    1

    0 1

    11

    3

    1

    3

    ;3);1(;;1

    ;2)1(;lg;)1(1

    11

    2

    21

    0

    22

    n

    i

    in

    i

    n

    i

    iii

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    21/22CS3024-FAZ 21

    Exercises (2)

    3. Algorithm Mystery(n)

    //input: a nonnegative integer n

    S 0

    fori 1 to n do

    S S + i * i

    return S

    a. What does this algorithm compute?

    b. What is its basic operation?

    c. How many times is the basic op executed?

    d. What is the efficiency class of this algorithm?

    e. Can you make any improvement?

  • 7/30/2019 3.Mathematical Analysis of Nonrecursive Algorithms

    22/22

    CS3024 FAZ 22

    Exercises (3)

    4. Algorithm Secret(A[0..n-1])

    //input: an array A[0..n-1] of n real number

    mi A[0]; ma A[0]fori 1 to n-1 do

    ifA[i] < mi then mi A[i]ifA[i] > ma then ma A[i]

    return ma - mi

    a. What does this algorithm compute?b. What is its basic operation?

    c. How many times is the basic op executed?

    d. What is the efficiency class of this algorithm?

    e. Can you make any improvement?