02b RunningTime (Optional)

download 02b RunningTime (Optional)

of 9

Transcript of 02b RunningTime (Optional)

  • 8/13/2019 02b RunningTime (Optional)

    1/9

  • 8/13/2019 02b RunningTime (Optional)

    2/9

    Page 2

    Mathematical References

    Sequences Finite sequence where n = 1, 2, 3, 4, 5

    Infinite sequence generates thesequence

    Observe that the general rule for the nth termi.e. 2n is inserted as an element in the middle.

    nan 2

    10;8;6;4;2 54321 aaaaa

    nan 2

    ,2,,10;8;6;4;2 54321 naaaaa

  • 8/13/2019 02b RunningTime (Optional)

    3/9

    Page 3

    Logarithms

    xb

    a x

    x

    x y x

    y xog y x

    y x xy

    x

    b

    ba

    b y

    b

    bbb

    bbb

    a

    log

    loglog

    log

    loglog

    log1log

    loglog)(log

    k k n 22 1

    k nk log1

    Theorem : If k and n are positiveintegers satisfying

    Then

    Theorem:

  • 8/13/2019 02b RunningTime (Optional)

    4/9

    Page 4

    Mathematical Induction

    Using mathematical induction we can provethat a collection of statements are true. The statements are usually indexed by an integer

    n. For example, the assertion that

    Is true for all n, is an assertion about all n. i.e. aninfinitely many statements.

    2)1(

    1

    nni

    n

    i

  • 8/13/2019 02b RunningTime (Optional)

    5/9

    Page 5

    Mathematical Induction

    In order to prove that a sequence ofstatements s(1), s(2) , s(3) . are trueusing mathematical statement we mustfollow two steps. Basic step : prove that statement s(1) is true Inductive step : Assume that the nth statement

    s(n) is true and use this assumption to provethat statement s(n+1) is true.

  • 8/13/2019 02b RunningTime (Optional)

    6/9

    Page 6

    Example Proof by induction

    Prove that

    Basic step Show that the statement is true

    for n =1. This would be the first of the infinitelymany statements. Plug in n=1 into the question and see if it is true.

    2)1(

    1

    nnin

    i

    12 )11(111

    1ii Yes, it is true!

  • 8/13/2019 02b RunningTime (Optional)

    7/9

    Page 7

    Example proof by induction

    Induction step: Suppose the equation is true for n, thismeans that we assume that

    Is true for n. And we will use it to prove that the nextconsecutive statement i.e. (n+1)st statement is true aswell. State with the (n+1)st statement

    2

    )1(

    1

    nni

    n

    i

    )1(1

    1

    1

    niin

    i

    n

    i

    2)2)(1(

    )1(2

    )1( nnn

    nn

    Start withthe sum 2

    )2)(1(1

    1

    nn

    i

    n

    i

    Need toshow

  • 8/13/2019 02b RunningTime (Optional)

    8/9

    Page 8

    Analysis of Algorithms Analysis of algorithms is the process of estimating the

    running time and space (memory) utilization of analgorithm as a function of the input size.

    These estimates are also referred to as time and space

    complexity of an algorithm. The estimated time complexity is independent of themachine the algorithm will run on.

    This allows us to compare time complexity ofdifferent algorithms.

  • 8/13/2019 02b RunningTime (Optional)

    9/9

    Page 9

    Best case, worst case and average case timecomplexity

    Among all inputs of size n the one that takes the leastamount of time to run produces the best case timecomplexity.

    Similarly the input of size n that takes the longest to runwill produce the worst case time complexity for analgorithm. We will mostly be interested in worst caserunning time of algorithms.

    Average case time complexity is obtained by finding thetime complexity of all input classes of size n and findingtheir average. Often very complicated to find