no from comples

download no from comples

of 21

Transcript of no from comples

  • 7/29/2019 no from comples

    1/21

    ESC101NFundamentals of Computing

    Arnab Bhattacharya

    [email protected]

    Indian Institute of Technology, Kanpurhttp://www.iitk.ac.in/esc101/

    1st

    semester, 2010-11

    Tue, Wed, Fri 0800-0900 at L7

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 1 / 6

    http://localhost/var/www/apps/conversion/tmp/scratch_5/[email protected]://www.iitk.ac.in/esc101/http://www.iitk.ac.in/esc101/http://localhost/var/www/apps/conversion/tmp/scratch_5/[email protected]://find/
  • 7/29/2019 no from comples

    2/21

    Growth of functions

    Efficiency of a program (or function) is measured by its timecomplexity

    Time complexity is measured for large inputs

    Input size is in the limit

    Provides a way to study asymptotic complexity of functions

    Generally, algorithms that are better for large inputs are better in

    most cases except very small sized inputs

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 2 / 6

    http://find/
  • 7/29/2019 no from comples

    3/21

    Asymptotic upper bound: O-notation

    O(g(n)) = {f(n) : c, n0 > 0 such that n n0, 0 f(n) cg(n)}

    g(n) is an asymptotic upper bound of f(n)

    n0n

    Time f(n)

    cg(n)

    f(n) = O(g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 3 / 6

    http://find/
  • 7/29/2019 no from comples

    4/21

    Asymptotic upper bound: O-notation

    O(g(n)) = {f(n) : c, n0 > 0 such that n n0, 0 f(n) cg(n)}

    g(n) is an asymptotic upper bound of f(n)

    f(n) = 1/2n2 3n = O(n2)

    n0n

    Time f(n)

    cg(n)

    f(n) = O(g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 3 / 6

    http://find/
  • 7/29/2019 no from comples

    5/21

    Asymptotic upper bound: O-notation

    O(g(n)) = {f(n) : c, n0 > 0 such that n n0, 0 f(n) cg(n)}

    g(n) is an asymptotic upper bound of f(n)

    f(n) = 1/2n2 3n = O(n2)

    f(n) = 3n = O(n2)

    n0n

    Time f(n)

    cg(n)

    f(n) = O(g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 3 / 6

    http://find/http://goback/
  • 7/29/2019 no from comples

    6/21

    Asymptotic upper bound: O-notation

    O(g(n)) = {f(n) : c, n0 > 0 such that n n0, 0 f(n) cg(n)}

    g(n) is an asymptotic upper bound of f(n)

    f(n) = 1/2n2 3n = O(n2)

    f(n) = 3n = O(n2)

    f(n) = 6n3 = O(n2)

    n0n

    Time f(n)

    cg(n)

    f(n) = O(g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 3 / 6

    http://find/http://goback/
  • 7/29/2019 no from comples

    7/21

    Asymptotic upper bound: O-notation

    O(g(n)) = {f(n) : c, n0 > 0 such that n n0, 0 f(n) cg(n)}

    g(n) is an asymptotic upper bound of f(n)

    f(n) = 1/2n2 3n = O(n2)

    f(n) = 3n = O(n2)

    f(n) = 6n3 = O(n2)

    o(g(n)): for any positive constant c> 0, 0 f(n) < cg(n)

    f(n) is asymptotically smaller than g(n) if f(n) = o(g(n))

    n0n

    Time f(n)

    cg(n)

    f(n) = O(g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 3 / 6

    http://find/
  • 7/29/2019 no from comples

    8/21

    Asymptotic lower bound: -notation

    (g(n)) = {f(n) : c, n0 > 0 such that n n0, 0 cg(n) f(n)}

    g(n) is an asymptotic lower bound of f(n)

    n0n

    Time

    f(n)

    cg(n)

    f(n) = (g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 4 / 6

    http://find/
  • 7/29/2019 no from comples

    9/21

    Asymptotic lower bound: -notation

    (g(n)) = {f(n) : c, n0 > 0 such that n n0, 0 cg(n) f(n)}

    g(n) is an asymptotic lower bound of f(n)

    f(n) = 1/2n2 3n = (n2)

    n0n

    Time

    f(n)

    cg(n)

    f(n) = (g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 4 / 6

    http://find/
  • 7/29/2019 no from comples

    10/21

    Asymptotic lower bound: -notation

    (g(n)) = {f(n) : c, n0 > 0 such that n n0, 0 cg(n) f(n)}

    g(n) is an asymptotic lower bound of f(n)

    f(n) = 1/2n2 3n = (n2)

    f(n) = 6n3 = (n2)

    n0n

    Time

    f(n)

    cg(n)

    f(n) = (g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 4 / 6

    http://find/
  • 7/29/2019 no from comples

    11/21

    Asymptotic lower bound: -notation

    (g(n)) = {f(n) : c, n0 > 0 such that n n0, 0 cg(n) f(n)}

    g(n) is an asymptotic lower bound of f(n)

    f(n) = 1/2n2 3n = (n2)

    f(n) = 6n3 = (n2)

    f(n) = 3n = (n2)

    n0n

    Time

    f(n)

    cg(n)

    f(n) = (g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 4 / 6

    http://find/
  • 7/29/2019 no from comples

    12/21

    Asymptotic lower bound: -notation

    (g(n)) = {f(n) : c, n0 > 0 such that n n0, 0 cg(n) f(n)}

    g(n) is an asymptotic lower bound of f(n)

    f(n) = 1/2n2 3n = (n2)

    f(n) = 6n3 = (n2)

    f(n) = 3n = (n2)

    (g(n)): for any positive constant c> 0, 0 cg(n) < f(n)

    f(n) is asymptotically larger than g(n) if f(n) = (g(n))

    n0n

    Time

    f(n)

    cg(n)

    f(n) = (g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 4 / 6

    http://find/http://goback/
  • 7/29/2019 no from comples

    13/21

    Asymptotic tight bound: -notation

    (g(n)) = {f(n) : c1, c2, n0 > 0 such thatn n0, 0 c1g(n) f(n) c2g(n)}

    g(n) asymptotically bounds f(n) from both above and below

    c2g(n)

    c1g(n)

    n0

    f(n)

    n

    Time

    f(n) = (g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 5 / 6

    http://find/
  • 7/29/2019 no from comples

    14/21

    Asymptotic tight bound: -notation

    (g(n)) = {f(n) : c1, c2, n0 > 0 such thatn n0, 0 c1g(n) f(n) c2g(n)}

    g(n) asymptotically bounds f(n) from both above and belowf(n) = 1/2n2 3n = (n2)

    Choose c1 = 1/14, c2 = 1/2, n0 = 7

    Other choices may also exist

    c2g(n)

    c1g(n)

    n0

    f(n)

    n

    Time

    f(n) = (g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 5 / 6

    http://find/
  • 7/29/2019 no from comples

    15/21

    Asymptotic tight bound: -notation

    (g(n)) = {f(n) : c1, c2, n0 > 0 such thatn n0, 0 c1g(n) f(n) c2g(n)}

    g(n) asymptotically bounds f(n) from both above and belowf(n) = 1/2n2 3n = (n2)

    Choose c1 = 1/14, c2 = 1/2, n0 = 7

    Other choices may also exist

    f(n) = 6n3 = (n2)

    Requires n c2/6 which is not true for all large n since c2 is constant

    c2g(n)

    c1g(n)

    n0

    f(n)

    n

    Time

    f(n) = (g(n))

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 5 / 6

    http://find/
  • 7/29/2019 no from comples

    16/21

    Examples

    Searching an array of size nfor ( i = 0 ; i < n ; i + + )

    if ( a[ i] == key )return i ;

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 6 / 6

    E l

    http://find/
  • 7/29/2019 no from comples

    17/21

    Examples

    Searching an array of size nfor ( i = 0 ; i < n ; i + + )

    if ( a[ i] == key )return i ;

    Average time is n/2 = O(n)

    Also (n) and (n)

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 6 / 6

    E l

    http://find/
  • 7/29/2019 no from comples

    18/21

    Examples

    Searching an array of size nfor ( i = 0 ; i < n ; i + + )

    if ( a[ i] == key )return i ;

    Average time is n/2 = O(n)

    Also (n) and (n)

    Transposing a matrix of size n n

    for ( i = 0 ; i < n ; i + + )for ( j = 0; j < n ; j ++)

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

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 6 / 6

    E l

    http://find/
  • 7/29/2019 no from comples

    19/21

    Examples

    Searching an array of size nfor ( i = 0 ; i < n ; i + + )

    if ( a[ i] == key )return i ;

    Average time is n/2 = O(n)

    Also (n) and (n)

    Transposing a matrix of size n n

    for ( i = 0 ; i < n ; i + + )for ( j = 0; j < n ; j ++)

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

    O(n2)

    Also (n2) and (n2)

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 6 / 6

    E l

    http://find/
  • 7/29/2019 no from comples

    20/21

    Examples

    Searching an array of size nfor ( i = 0 ; i < n ; i + + )

    if ( a[ i] == key )

    return i ;

    Average time is n/2 = O(n)

    Also (n) and (n)

    Transposing a matrix of size n n

    for ( i = 0 ; i < n ; i + + )for ( j = 0; j < n ; j ++)

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

    O(n2)

    Also (n2) and (n2)

    Accessing an element of an array of size na [i ] = valu e ;

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 6 / 6

    E l

    http://find/
  • 7/29/2019 no from comples

    21/21

    Examples

    Searching an array of size nfor ( i = 0 ; i < n ; i + + )

    if ( a[ i] == key )

    return i ;

    Average time is n/2 = O(n)

    Also (n) and (n)

    Transposing a matrix of size n n

    for ( i = 0 ; i < n ; i + + )for ( j = 0; j < n ; j ++)

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

    O(n2)

    Also (n2) and (n2)

    Accessing an element of an array of size na [i ] = valu e ;

    Constant time, denoted as O(1) since it only requires addresscalculation and does not depend on the size of the array

    Also (1) and (1)

    Arnab Bhattacharya ([email protected]) ESC101N 2010 I 6 / 6

    http://find/