asymptotic analysis.pdf

Upload: rajeev-pandey

Post on 14-Apr-2018

240 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Asymptotic Analysis.pdf

    1/23

    As m totic Anal sis

  • 7/30/2019 Asymptotic Analysis.pdf

    2/23

    How many resources will it take to solve a problem ofa given size?

    time space

    Expressed as a function of problem size (beyond

    some minimum size) how do requirements grow as size grows?

    Problem size

    number of elements to be handled size of thing to be operated on

  • 7/30/2019 Asymptotic Analysis.pdf

    3/23

    Constant

    f(n) = c

    ex: ettin arra element at known locationtrying on a shirt

    callin a friend for advice before an interview

    Linearf n = cn + ossible lower order terms

    ex: finding particular element in array (sequential

    search)trying on all your shirts

    callin all our n friends for dress advice

  • 7/30/2019 Asymptotic Analysis.pdf

    4/23

    Growth Functions (cont)

    Quadratic

    = 2

    ex: sorting all the elements in an array (using bubblesort tr in all our shirts n with all our ties(n)

    havin conference calls with each air of n

    friendsPolynomial

    f(n) = cnk [ + possible lower order terms]

    ex: looking for maximum substrings in arraytrying on all combinations of k separates (n of

    each)

    having conferences calls with each k-tuple of nfriends

  • 7/30/2019 Asymptotic Analysis.pdf

    5/23

    Growth Functions (cont)

    Exponential

    f(n) = cn [+ ossible lower order termsex: constructing all possible orders of array elements

    Logarithmicf n = lo n + ossible lower order terms

    ex: finding a particular array element (binary search)

    tr in on all combinationschoosing movie with n friends using phone tree

  • 7/30/2019 Asymptotic Analysis.pdf

    6/23

    What happens as problem size grows really, really

    large? (in the limit)

    constants dont matter lower order terms dont matter

  • 7/30/2019 Asymptotic Analysis.pdf

    7/23

    What particular input (of given size) gives

    worst/best/average complexity?

    Mileage example: how much petrol does it take to go20 kms ?

    Worst case: all uphill Best case: all downhill, just coast

    Average case: average terrain

  • 7/30/2019 Asymptotic Analysis.pdf

    8/23

    Growth of Various Functions

    Function Growth

    10000

    f(n)

    5n 0.1n^2+2n+40 2 n log n 0.00001 2^n

    8000

    9000

    6000

    7000

    4000

    5000

    2000

    3000

    0

    1000

    n

    10 210 410 610 810 1010

  • 7/30/2019 Asymptotic Analysis.pdf

    9/23

    Consider sequential search on an unsorted array of

    length n, what is time complexity?

    Best case:

    Worst case:

    Average case:

  • 7/30/2019 Asymptotic Analysis.pdf

    10/23

    Complexity Bounds

    Upper bound (big O):

    =

    T(n) cf(n) for some constants c, n0

    and n n0

    T(n) = (g(n)) if

    , 0 0Exact bound (theta):

    =

    T(n) = O(h(n)) and T(n) = (h(n))

    T(n) = o(p(n)) if

    n = p n an n p n

  • 7/30/2019 Asymptotic Analysis.pdf

    11/23

    1. If f n = O n and n = O h n then f n =

    O(h(n))

    . n = g n or any > , en n = g n

    3. If f1(n) = O(g1(n)) and f2(n) = O(g2(n)),

    then f1(n) + f2(n) = O(max (g1(n), g2(n)))

    . 1 n = g1 n an 2 n = g2 n ,

    then f1(n) * f2(n) = O(g1(n) * g2(n))

  • 7/30/2019 Asymptotic Analysis.pdf

    12/23

    Code:

    a = b;

    Complexity:

  • 7/30/2019 Asymptotic Analysis.pdf

    13/23

    Code:

    sum = 0;

    f or ( i =1; i

  • 7/30/2019 Asymptotic Analysis.pdf

    14/23

    Code:

    sum = 0;

    f or ( j =1; j

  • 7/30/2019 Asymptotic Analysis.pdf

    15/23

    Code:

    sum1 = 0;

    = =

    f or ( j =1; j

  • 7/30/2019 Asymptotic Analysis.pdf

    16/23

    Code:sum2 = 0;

    f or i =1 i

  • 7/30/2019 Asymptotic Analysis.pdf

    17/23

    Code:

    sum1 = 0;

    = = *=

    f or ( j =1; j

  • 7/30/2019 Asymptotic Analysis.pdf

    18/23

    Code:sum2 = 0;

    f or k=1 k

  • 7/30/2019 Asymptotic Analysis.pdf

    19/23

    1. Is upper bound the same as worst case?

    2. Does lower bound ha en with shortest in ut?

    3. What if there are multi le arameters?

    Ex: Rank order of p pixels in c colorsf or ( i = 0; i < c; i ++)

    count [ i ] = 0;

    f or ( i = 0; i < p; i ++)count [ val ue( i ) ] ++;

    sor t ( count )

  • 7/30/2019 Asymptotic Analysis.pdf

    20/23

    Does it matter?

    What determines s ace com lexit ?

    How can ou reduce it?

    What tradeoffs are involved?

  • 7/30/2019 Asymptotic Analysis.pdf

    21/23

    Theorem:

    O(cf(x) = O(f(x))

    Proof: T(x) = O(cf(x)) implies that there are constants c0

    and n0 such that T(x) c0(cf(x)) when x n0

    Therefore, T(x) c1(f(x)) when x n0 where c1 =c0c

    Therefore, T(x) = O(f(x))

  • 7/30/2019 Asymptotic Analysis.pdf

    22/23

    Theorem:

    Let T1(n) = O(f(n)) and T2(n) = O(g(n)).

    Then T1(n) + T2(n) = ???

  • 7/30/2019 Asymptotic Analysis.pdf

    23/23

    Theorem:

    Let T1(n) = O(f(n)) and T2(n) = O(g(n)).

    Then T1(n) * T2(n) = ???