minggu v-th

Upload: bayu-eko-punto-wibowo

Post on 02-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Minggu V-th

    1/22

    DAA

    Minggu V

  • 7/27/2019 Minggu V-th

    2/22

    Master method

    Used for many divide-and-conquer recurrences of the form T(n) = aT (n/b) + f (n) where a 1, b > 1, and f (n) > 0.

    Master TheoremLet a 1 and b > 1 be constants, let f (n) be a function, and

    let T (n) be defined on the nonnegative integers by therecurrence T(n) = aT(n/b) + f(n), where we interpret n/b tomean eithern/born/b. Then T (n) can be boundedasymptotically as follows.1. If f(n) = O(nlogb a-), for some constant> 0, then T(n) = (nlogb a )

    2. If f(n) = ((nlogb a ), then T(n) = (nlogb a lg n)

    3. If f(n) = (nlogb a+ ), for some constant> 0 and if af(n/b) cf(n),for some constant c < 1 and all sufficiently large n, then T (n) =(f (n)).

  • 7/27/2019 Minggu V-th

    3/22

    Based on the master theorem:

    Compare nlogb a vs. f (n):

    Case 1:f (n) = O(nlogba) for some constant > 0.

    (f (n) is polynomially smaller than nlogba.)

    Solution: T (n) = (nlogb a). (Intuitively: cost is dominated by leaves.)

    Case 2:f (n) = (nlogb a lgkn), where k 0. [This formulation of Case 2 is more general than in Master Theorem]

    (f (n) is within a polylog factor of nlogb a, but not smaller.)

    Solution: T (n) = (nlogb a lgk+1 n). (Intuitively: cost is n

    logb a lgk n at each level, and there are (lg n) levels.)

    Simple case: k = 0 f (n) = (nlogb a) T (n) = (nlogb a lg n). Case 3:f (n) = (nlog

    b

    a+) for some constant > 0 and f (n) satisfies the regularitycondition a f (n/b) c f (n) for some constant c < 1 and allsufficientlylarge n. (f (n) is polynomially greater than nlogb

    a.)

    Solution: T (n) = ( f (n)). (Intuitively: cost is dominated by root.)

  • 7/27/2019 Minggu V-th

    4/22

    What.s with the Case 3 regularity condition?

    Generally not a problem.

    It always holds wheneverf (n) = nkand f (n) = (nlogba+) for constant

    > 0. [Proving this makes a nice homework exercise. See below.] Soyou dont need to check it whenf (n) is a polynomial.

    [Heres a proof that the regularity condition holds whenf (n) = nkand f (n) = (nlogb

    a+) for constant > 0.

    Sincef (n) = (nlogba+) and f (n) = nk, we have that k > logb a. Using

    a base ofb and treating both sides as exponents, we have bk> blog

    ba

    = a, and so a/bk< 1. Since a, b, and k are constants, if we let c =

    a/bk

    , then c is a constantstrictly less than 1. We have that a f (n/b) =a(n/b)k= (a/bk)nk= c f (n), and so the regularity condition issatisfied.]

  • 7/27/2019 Minggu V-th

    5/22

    Examples:

    T (n) = 5T (n/2) + (n2) n

    log2 5 vs. n2

    Since log25 = 2 for some constant > 0, use Case 1T (n) = (nlg 5)

    T (n) = 27T (n/3) + (n3 lg n) nlog3 27= n3 vs. n3 lg n Use Case 2 with k = 1T (n) = (n3 lg2 n)

    T (n) = 5T (n/2) + (n3) nlog2

    5 vs. n3

    Now lg 5 + = 3 for some constant> 0

    Check regularity condition (dont really need to sincef (n) is a polynomial):

    a f (n/b) = 5(n/2)3 = 5n3/8 cn3 for c = 5/8 < 1

    Use Case 3T (n) = ((n3)

    T (n) = 27T (n/3) + (n3/ lg n) nlog3

    27= n3 vs. n3/ lg n = n3 lg1 n (n3 lgkn) for any k 0.

    Cannot use the master method.

  • 7/27/2019 Minggu V-th

    6/22

    Growth of Functions (overview)

    A way to describe behavior of functions in the limit.Were studying asymptoticefficiency.

    Describe growth of functions.

    Focus on whats important by abstracting away low-

    order terms and constant factors. How we indicate running times of algorithms.

    A way to compare sizes of functions:O

    =

    o

  • 7/27/2019 Minggu V-th

    7/22

    Asymptotic notation

    O-notation O(g(n)) = {f (n) : there exist positive constants c and n0 such that0 f

    (n) cg(n) for all n n0} .

  • 7/27/2019 Minggu V-th

    8/22

    Example: 2n2 = O(n2), with c = 1 and n0= 2.

    Examples of functions in O(n2): n2

    n2 + n

    n2 + 1000n 1000n2 + 1000n

    Also,

    n

    n/1000

    n1.99999

    n2/ lg lg lg n

  • 7/27/2019 Minggu V-th

    9/22

    -notation(g(n)) = { f (n) : there exist positive constants c and n0 such that0 cg(n) f (n)

    for all n n0} .

  • 7/27/2019 Minggu V-th

    10/22

    Example:

    n = (lg n), with c = 1 and n0 = 16.

    Examples of functions in (n2): n2

    n2 + n

    n2 n

    1000n2 + 1000n

    1000n2 1000n

    Also,

    n3

    n2.00001

    n2 lg lg lg n

    22n

  • 7/27/2019 Minggu V-th

    11/22

    -notation(g(n)) = { f (n) : there exist positive constants c1, c2, and n0 such that0

    c1g(n) f (n) c2g(n) for all n n0} .

    Example: n2/2 2n = (n2), with c1 = 1/4, c2 = 1/2, and n0= 8.

  • 7/27/2019 Minggu V-th

    12/22

    Theorem:

    f (n) = (g(n)) if and only if f = O(g(n)) and f = (g(n)) .

    Asymptotic notation in equations

    When on right-hand side: O(n2) stands for some anonymous function in

    the setO(n2).

    2n2+3n+1 = 2n2+ (n) means 2n2+3n+1 = 2n2+ f (n) for some f (n) (n).

    In particular,f(n) = 3n + 1.

    By the way, we interpret # of anonymous functions as = # of times the

    asymptotic notation appears:

  • 7/27/2019 Minggu V-th

    13/22

    When on left-hand side: No matter how the anonymous functions are

    chosen onthe left-hand side, there is a way to choose the anonymous

    functions on the right-hand side to make the equation valid.

    Interpret 2n2 + (n) = (n2) as meaning for all functions f (n) (n),

    there exists a functiong(n) (n2) such that 2n2 + f (n) = g(n).

    Can chain together:

    2n2

    + 3n + 1 = 2n2

    +

    (n) =

    (n2

    ) .Interpretation:

    First equation: There existsf (n) (n) such that 2n 2+3n+1 = 2n2+ f (n).

    Second equation: For allg(n) (n) (such as the f (n) used to make the

    firstequation hold), there exists h(n) (n2) such that 2n2+ g(n) = h(n).

  • 7/27/2019 Minggu V-th

    14/22

    o-notation

    o(g(n)) = { f (n) : for all constants c > 0, there

    exists a constant n0> 0 such that 0 f (n)

  • 7/27/2019 Minggu V-th

    15/22

    -notation

    (g(n)) = { f (n) : for all constants c > 0, there exists a

    constant n0> 0 such that 0 cg(n) < f (n) for all n n0} .

  • 7/27/2019 Minggu V-th

    16/22

    Comparisons of functions

    Relational properties:

  • 7/27/2019 Minggu V-th

    17/22

    Comparisons:

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

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

    No trichotomy. Although intuitively, we can liken O to ,

    to , etc., unlike real numbers, where a < b, a = b, or a >b, we might not be able to compare functions.

    Example: n1+sinn and n, since 1 + sin n oscillates between

    0 and 2.

  • 7/27/2019 Minggu V-th

    18/22

  • 7/27/2019 Minggu V-th

    19/22

    Logarithms

    Notations:

    lg n = log2 n (binary logarithm) ,

    ln n = loge n (natural logarithm) ,

    lgkn = (lg n)k(exponentiation) ,

    lg lg n = lg(lg n) (composition) .

    Logarithm functions apply only to the next term in the formula, so thatlg n + kmeans (lg n) + k, and not lg(n + k).

    In the expression logb a:

    If we hold b constant, then the expression is strictly increasing as aincreases.

    If we hold a constant, then the expression is strictly decreasing as bincreases

  • 7/27/2019 Minggu V-th

    20/22

  • 7/27/2019 Minggu V-th

    21/22

    Factorials

    n! = 1 2 3 n. Special case: 0! = 1.

    Can use Stirlings approximation, n! = (2n) (n/e)n (1 + (1/n)), to derive that

    lg(n!) = (n lg n).

  • 7/27/2019 Minggu V-th

    22/22

    Tugas:

    3.1-1; 3.1-2; 3.1-3; 3.1-4; 3.1-8;

    3.2-4; 3.3;