classes.soe.ucsc.edu cmps102 fall01 solutions 2

Upload: ashok-chakravarthy

Post on 05-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Classes.soe.Ucsc.edu Cmps102 Fall01 Solutions 2

    1/4

    CS 102: INTRO. TO THE ANALYSIS OF ALGORITHMS

    ASSIGNMENT 2

    SOLUTIONS

    Problem 1.n

    i=1 i2 = n(n+1)(2n+1)6 for n 1.

    Solution. The proof will be by induction on n. The base case is n = 1. Theleft-hand side of the equation becomes

    1i=1 i

    2 = 1 and the right-hand side is1(1+1)(2+1)

    6 =23

    6 = 1. Thus the base case holds.Now assume that the above equation hold for n = k 1 where k N is some

    constant such that k 1 1. Thus in,k

    i=1

    i2 =

    k1i=1

    i2

    + k2

    we can replace the second summaion and get,

    ki=1

    i2 =(k 1)(k 1 + 1)[2(k 1) + 1]

    6+ k2 =

    k(k 1)(2k 1)

    6+

    6k2

    6

    =k[(k 1)(2k 1) + 6k]

    6=

    k(2k2 3k + 1 + 6k)

    6

    =k(2k2 + 3k + 1)

    6=

    k(k + 1)(2k + 1)

    6.

    Thus the equation holds for some k given that it holds k1. By induction it followsthat the equation holds for all n N.

    Problem 2.n

    i=0 xi = x

    n+11

    x1for x = 1 and n 0.

    Solution. The proof is by induction on n. The base case is n = 0. We prove it thus,

    0i=0

    xi = x0 = 1 =x1 1

    x 1.

    Now, assume that the equation is true for some k N and n = k 1 0.Consider,

    ki=0

    xi

    =k1

    i=0xi

    + xk

    =

    xk 1

    x 1 + xk

    Where the second equality comes from applying the induction hypothesis. Contin-uing,

    ki=0

    xi =xk 1

    x 1+

    (x 1)xk

    x 1=

    xk 1 + xk+1 xk

    x 1=

    xk+1 1

    x 1.

    Which is the desired result for n = k. By induction, the equation is true for alln N.

    1

  • 8/2/2019 Classes.soe.Ucsc.edu Cmps102 Fall01 Solutions 2

    2/4

    CS 102: INTRO. TO THE ANALYSIS OF ALGORITHMS ASSIGNMENT 2 SOLUTIONS 2

    Problem 3. Consider the following statement: the sum of cubes of the first npositive integers is equal to the square of the sum of these integers. Restate this as

    a formal mathematical theorem using -notation. Prove your theorem.Solution. Theorem 1. For n N and n 1,

    ni=1

    i3 =

    ni=1

    i

    2.

    Proof. The proof is by induction on n. The base case is when n = 1,

    1i=1

    i3 = 1 =

    1

    i=1

    i

    2.

    Now assume that the theorem holds for n = k 1 for some k N, k 1 1.Thus we get that,

    ki=1

    i3 =

    k1i=1

    i3

    + k3 =

    k1i=1

    i

    2+ k3

    Where the last equation comes from applying the induction hypothesis. Now we

    use the fact thatk1

    i=1 i = k(k 1)/2 to show that,

    ki=1

    i3 =

    k(k 1)

    2

    2+ k3 =

    k2(k2 2k + 1)

    4+

    4k3

    4

    =k2(k2 + 2k + 1)

    4

    =k2(k + 1)2

    4

    = k(k + 1)

    2

    2

    .

    Which is the equation with n = k. Therefore, by induction, the theorem holds.

    Problem 4. n5 n is divisible by 5 for every positive integer n.

    Solution. The proof will follow by induction on the n. We prove the base case,n = 1, follows because n5 n = 15 1 = 0 is divisible by 5.

    Now we assume that k5k is divisible by 5 for some positive integer k. Consider,

    (k + 1)5 (k + 1) = k5 + 5k4 + 10k3 + 10k2 + 5k + 1 k 1

    = (k5 k) + 5(k4 + 2k3 + 2k2 + k)

    The first term is divisible by 5 because of the induction hypothesis and the secondterm is divisible by 5 because is contains a factor of 5. Thus the sum is divisibleby 5. This proves the induction hypothesis and completes the proof.

    Problem 5. Find (and prove) an exact closed form solution to f(n) mapping thenatural numbers to the reals defined by

    f(n) =

    n if n = 0 or n = 1,

    5f(n 1) + 6f(n 2) otherwise.

  • 8/2/2019 Classes.soe.Ucsc.edu Cmps102 Fall01 Solutions 2

    3/4

    CS 102: INTRO. TO THE ANALYSIS OF ALGORITHMS ASSIGNMENT 2 SOLUTIONS 3

    Solution. Claim: f(n) = 17

    6n 17

    (1)n.The proof is by induction on n.

    Base: (note that there are two base cases since the recurrence uses two previousvalues f(n 1) and f(n 2)).

    n=0 : f(0) = 60

    7

    (1)0

    7= 1

    7 1

    7= 0, and we have f(0) = 0 by the recursive

    definition of f.

    n=1 : f(1) = 61

    7 (

    1)1

    7= 6

    7+ 1

    7= 1, and we have f(1) = 1 by the recursive

    definition of f.

    Inductive Step: Assume n > 1. Assume f(j) = 17 6j 1

    7 (1)j , for all j such

    that 0 j < n. Let IH(j) be the statement f(j) = 6j

    7 (

    1)j

    7, so we are assuming

    IH(j) for 0 j < n. We need to show that f(n) = 6n/7 (1)n/7.Since n > 1, we can use the recursive definition of f to get that

    f(n) = 5f(n 1) + 6f(n 2).

    We can use IH(n 1) and IH(n 2) to rewrite f(n 1) and f(n 2). So from theinductive hypothesis we get:

    f(n) = 5

    6n1

    7

    (1)n1

    7

    + 6

    6n2

    7

    (1)n2

    7

    =5 6n1

    7+

    6 6n2

    7

    5 (1)n1

    7

    6 (1)n2

    7

    =6n2

    7[5(6) + 6]

    (1)n2

    7[5(1) + 6]

    =6n2

    7(36)

    (1)n2

    7(1)

    = 6n2

    7 (62) (1)

    n2

    7 (1)2

    =1

    7 6n

    1

    7(1)n

    which is what we needed to show.

    Problem 6. Define the following recurrence

    F(n) =

    1 if n = 06 if n = 16F(n 1) + 9F(n 2) for all n 2

    Show that F(n) n3n

    for all n 0.Solution. Proof by induction on n. Note that there will need to be two base casesfor this induction proof. One (n = 0) is not sufficient because then our inductivestep would have to cover 1, . . . , n. This presents a problem because our recurrenceis not defined for n = 1. In general when there are two recursive references to afunction like F, namely F(n 1 and F(n 2), two base cases are required for aproof by induction.

    Base Step: n = 0: By definition F(0) = 1 (0)30 = 0. n = 1: again we haveF(1) = 6 (1)31 = 3.

  • 8/2/2019 Classes.soe.Ucsc.edu Cmps102 Fall01 Solutions 2

    4/4

    CS 102: INTRO. TO THE ANALYSIS OF ALGORITHMS ASSIGNMENT 2 SOLUTIONS 4

    Inductive Step: Let n 2. Assume F(j) j3j for all 2 j < n. This isthe inductive hypothesis. Our goal is to show that this assumption implies that

    F(n) n3

    n

    . Consider the recursive definition of F:F(n) = 6F(n 1) + 9F(n 2)

    6(n 1)3n1 + 9(n 2)3n2

    by the inductive hypothesis. Note the inequality, and that here we have applied theinductive hypothesis twice once for F(n 1) and once for F(n 2). Continuingon...

    F(n) 3n2(6(n 1)3 + 9(n 2))

    = 3n2(18n 18 + 9n 18)

    = 3n2(27n 36)

    = 3n2(3 32n 4 32)

    = n3n(3n 4)

    Recall that we are deaing with n 2. In this case we have that 3n 4 n. Ofcourse, this can also be proved by induction.

    n3n

    as desired. By the principle of mathematical induction we are done.