Lecture-3-CS210-2012 (1).pptx

download Lecture-3-CS210-2012 (1).pptx

of 20

Transcript of Lecture-3-CS210-2012 (1).pptx

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    1/20

    Data Structures and Algorithms

    (CS210/ESO207/ESO211)

    Lecture 3 Time complexity

    Big O notation

    Solving a problem: Maximum sum subarray

    1

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    2/20

    Time complexity of an algorithm

    Definition:

    The time complexity of an algorithm is the worst casenumber of

    instructions executed as a functionof the input size(or a

    parameter defining the input size).

    Example: the time complexity of searching for a 0 in a matrix M[, ] is at

    most 2+ cfor some constant c.

    2

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    3/20

    Example:Time complexity of matrix multiplication

    Matrix-mult(C[n,n],D[n,n])

    { fori= 0to n-1

    { forj=0to n-1

    { M[i,j]0;

    fork=0to n-1

    { M[i,j]M[i,j] + C[i,k]*D[k,j];

    }

    }

    }Return M

    }

    3

    Time complexity = + +

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    4/20

    Comparing efficiencyof two algorithms

    Let A and B be two algorithms to solve a given problem.

    Algorithm A has time complexity : 2 2 + 125

    Algorithm B has time complexity : 5 2+ 67 + 400

    Question:Which algorithm is more efficient ?

    Obviously A is more efficient than B

    4

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    5/20

    Comparing efficiencyof two algorithms

    Let A and B be two algorithms to solve a given problem.

    Algorithm A has time complexity : 2 + 125

    Algorithm B has time complexity : 50 + 125

    Question:Which one would you prefer based on the efficiencycriteria ?

    Answer : A is more efficient thanB for < 50

    B is more efficient than A for > 50

    5

    Time complexity is

    reallyan issue onlywhen the input is of

    large size

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    6/20

    Rule1

    We must compare the time complexities of two

    algorithms for asymptotically large valueof input size

    only

    6

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    7/20

    Comparing efficiencyof two algorithms

    Algorithm A with time complexity 50 + 125

    is certainly more efficient than

    Algorithm B has time complexity : 2 + 125

    7

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    8/20

    Exercise 1:

    A judgment question for you !

    Algorithm A for a given problem has time complexity f()= 5 2 + +1250

    Researchers have designed two new algorithms B andC

    Algorithm Bhas time complexity g() = 2 + 10

    Algorithm Chas time complexity h() = 10 . + 20 + 2000

    8

    Which of B and C is an

    improvement over Ain the

    true sense ?

    lim

    g()

    f()= 1/5 lim

    h()

    f()= ?0

    C is an improvement over A

    in the true sense.

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    9/20

    Another Rule

    An algorithm Xis superior to another algorithm Yif

    the ratioof time complexity of Xand time complexity

    of Yapproaches 0for asymptotically large input size.

    9

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    10/20

    Some insights from the Exercise 1

    Algorithm A for a given problem has time complexity f()= 5 2 + +1250

    Researchers have designed two new algorithms B andC

    Algorithm Bhas time complexity g() = 2 + 10

    Algorithm Chas time complexity h() = 10 . + 20 + 2000

    Insight 1:multiplicative or additive Constantsdo notplay any role.

    Insight 2:the highest order term govern the time complexity asymptotically.

    10

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    11/20

    ORDERNOTATIONS :

    A NEATAND PRECISEDESCRIPTION OFTIME COMPLEXITY

    11

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    12/20

    Order notation

    Definition: Let f(n)and g(n)be any two increasing functions of n. f(n)issaid to be of the order of g(n) if there exist constants cand such that

    f(n) cg(n) for all n>

    12

    f(n)

    c g(n)

    If f(n)is of the order of g(n),

    we write f(n)= O(g(n))

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    13/20

    Order notation :

    Examples

    20 2= O(2)

    100 = O(2)

    2 = O(2.)

    2000= O(1)

    Simple observations:

    If f(n)= O(g(n))and g(n)= O(h(n)), then

    f(n)= O(h(n))

    If f(n)= O(h(n))and g(n)= O(h(n)), then f(n)+g(n)=

    13

    O(h(n))?

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    14/20

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    15/20

    Max-sum subarray problem:

    Designing efficient algorithm

    Given an array Astoring nnumbers, find its subarraythe sum of

    whose elements is maximum?

    15

    3 -5 3 8 2 -4 9 -6 3 -2 -8 3 -5 1 7 -9A

    47

    -218

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    16/20

    Max-sum subarray problem:

    A trivial algorithm

    A_trivial_algo(A)

    {maxA[0];

    Fori=0to n-1

    Forj=ito n-1

    { tempcompute_sum(A,i,j);

    ifmax< temp thenmaxtemp;

    }

    return max;

    }

    compute_sum(A, i,j)

    { sumA[i];

    Fork=i+1toj sumsum+A[k];

    return sum;

    }16

    Homework: Prove that its

    time complexity is O(3)

    We shall now design an O() time

    algorithm for this problem. You are

    advised to make some initial attempts

    (few minutes at least). So do not jump

    to the next slide.

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    17/20

    Max-sum subarray problem:

    Designing an efficient algorithm

    Facts from the world of algorithms:

    1. There is no formulafor designing efficient algorithms. Almost every

    second problem demands a freshapproach.

    2. Designing an efficient algorithm or data structure requires1. A deep insightinto the existing efficient algorithms for various problems and

    the ability to use that insight at right place.

    2. Ability to make key observationabout the problem and its solution.

    3. Ability to ask right kind of questionswhen some promising idea fails.

    4. A positive attitude and a lot of perseverance.

    17

    We shall demonstrate the above facts

    during this course many times.

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    18/20

    Max-sum subarray problem:

    Towards designing an O(n) time algorithm

    LetS(i): the sum of the largest-sum subarray ending at A[i].

    Note that A[i] surely contributes to S(i) (empty subarrays are not considered).

    18

    3 -5 3 8 2 -4 8 -6 3 -2 -8 3 -5 1 7 -9A

    i=5

    -4

    6

    -2

    9

    47

    S(i)=9for i= 5

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    19/20

    Max-sum subarray problem:

    Towards designing an O(n) time algorithm

    Observations:

    In order to solve our problem, it suffices to compute S(i) for all possible 0

    i n-1.

    Since our objective is to achieve O(n) timecomplexitybound, we need a

    way to compute S(i) in O(1) time only. How can we achieve this(ambitious) goal ?

    Lesson you should have learnt from the iterative algo for Fibonacci

    number

    Solve a problem incrementally.

    How to use the above insight from Fibonacci number ?

    19

    Question: what is the relation

    between S(i) and S(i-1) ?

  • 8/11/2019 Lecture-3-CS210-2012 (1).pptx

    20/20

    Max-sum subarray problem:

    Towards designing an O(n) time algorithm

    IfS(i-1) 0 then

    S(i)=A[i]

    IfS(i-1) > 0 then

    S(i) = S(i-1) + A[i]

    Homework:

    Prove the above mentioned relation between S(i) and S(i-1).

    Design an O(n) time algorithm for Max-sum subarray usingthe above formulation.

    20