hw1-solns

7
HOMEWORK #1 SOLUTIONS [assume logarithmic base is 2 for all questions] Grade out of 100 Students required to do 6 problems. Each of 6 problems is worth 16 points for max of 96 points (so everyone gets 4 points simply for handing in assignment). Students can mark additional questions as "extra credit"; each extra credit question is worth 3 points. Perfect score with all extra credit is worth 109 points. --------------------------------------------------- QUESTION 1: Show whether the following statements are true or false. Clearly and succinctly describe why. 0.75 n n 1. 2 = O(2 ) 3n n 2. 2 = O(2 ) log n 3. 2 = O(log n) 4. log(n!) = Big-Theta(n log n) [exercise 1.4 Dasgupta] Hint: To show an upper bound, compare n! with n^n. To show a lower bound, com pare it with (n/2)^(n/2) SOLUTIONS: 0.75 1 n n 1. TRUE: because 2 < 2 3n 3 n n n 2. FALSE: 2 ==> (2 ) ==> 8 is not less than 2 log n 3. FALSE: 2 = n ====> O(n) grows faster than O(log n) 4. TRUE: n! <= n^n show upper bound: 1*2*3*...*n <= n*n*...*n ^^^^^^^^^^^ ^^^^^^^^^ n terms n terms show lower bound: (n/2)^(n/2) <= n!

Upload: siddiquentc

Post on 21-Jul-2016

147 views

Category:

Documents


0 download

DESCRIPTION

home work solution

TRANSCRIPT

Page 1: hw1-solns

HOMEWORK #1 SOLUTIONS[assume logarithmic base is 2 for all questions]

Grade out of 100

Students required to do 6 problems.Each of 6 problems is worth 16 points for max of 96 points(so everyone gets 4 points simply for handing in assignment).

Students can mark additional questions as "extra credit";each extra credit question is worth 3 points.

Perfect score with all extra credit is worth 109 points.

---------------------------------------------------QUESTION 1:Show whether the following statements are true or false. Clearly and succinctly describe why.

0.75 n n1. 2 = O(2 )

3n n2. 2 = O(2 )

log n3. 2 = O(log n)

4. log(n!) = Big-Theta(n log n) [exercise 1.4 Dasgupta]

Hint: To show an upper bound, compare n! with n^n. To show a lower bound, compare it with (n/2)^(n/2)

SOLUTIONS:

0.75 1 n n1. TRUE: because 2 < 2

3n 3 n n n2. FALSE: 2 ==> (2 ) ==> 8 is not less than 2

log n3. FALSE: 2 = n ====> O(n) grows faster than O(log n)

4. TRUE:

n! <= n^n

show upper bound: 1*2*3*...*n <= n*n*...*n ^^^^^^^^^^^ ^^^^^^^^^ n terms n terms

show lower bound: (n/2)^(n/2) <= n!

Page 2: hw1-solns

(n/2)*(n/2)*...*(n/2) <= 1*2*3*...*n ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^ (n/2) terms n terms

(n/2)*(n/2)*...*(n/2) <= 1*2*3*...*(n/2)*(1+n/2)*...*n ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ (n/2) terms (n/2) terms

so we know that

(n/2) n n (---) <= n! <= n 2

take log of all three....

(n/2) n n log (---) <= log(n!) <= log n 2

n n (---) log (---) <= log(n!) <= n log n 2 2

they're equivalent classes in terms of runtime performance---------------------------------------------------QUESTION 2:Do exercise 0.2 from Dasgupta.Show that, if c is a positive real number, then g(n) = 1 + c + c^2 + ... + c^n is:(a) Big-Theta(1) if c < 1(b) Big-Theta(n) if c = 1(c) Big-Theta(c^n) if c > 1

The moral: in big-theta terms, the sum of a geometric series is simply the first term if the series is strictly decreasing, the last term if the series is strictly increasing, or the number of terms if the series is unchanging.

NOTE: geometric series is a series with a multiplicative constant between successive terms

SOLUTION:

given the formula for the sum of a partial geometric series, for c != 1:

n+1 n+1 1 - c c - 1 g(n) = ---------- = ---------- 1 - c c - 1

(a) c < 1

n+1 1 > 1 - c > 1 - c

1

Page 3: hw1-solns

------- > g(n) > 1 1 - c

(b) c = 1

g(n) = 1 + 1 + 1 + ... + 1 = n + 1 = O(n)

(c) c > 1

n+1 n+1 n c > c - 1 > c

c n 1 n ------- c > g(n) > ------- c 1 - c 1 - c---------------------------------------------------QUESTION 3:Do exercise 1.39 from Dasgupta.Give a polynomial-time algorithm for computing

c ba mod p, given a, b, c, and prime p.

SOLUTION:

Okay if students simply do multiple loops to calculate b^c, then a^b^c, then a^b^c mod p

Better solution is to use Fermat's Little Theorem, which is:

p-1 a = 1 mod p

---------------------------------------------------QUESTION 4:Do exercise 2.1 from Dasgupta.Use the divide-and-conquer integer multiplication algorithm to multiply the two binary integers10011011 and 10111010

SOLUTION:

10011011 ==> 1001 and 1011

10111010 ==> 1011 and 1010

etc.

see Figure 2.1 algorithm

verify 10011011 is 155 and 10111010 is 186, so 155*186 = 28,830 or 0111000010011110

---------------------------------------------------QUESTION 5:Do exercise 2.2 from Dasgupta.Show that for any positive integers n and any base b, there must some power of b lying in therange [n, bn].

Page 4: hw1-solns

SOLUTION:

Start with n = 1. Statement is true: there is some power of b lying in range [1,b].

Suppose that given any number b, there is always a power of b in [q,bq].If so, given b and q, we know that there is a power of b, say b^x, in interval [q,bq].

Next, consider interval [q+1,b(q+1)]. If b^x > q, then b^x is in [q+1,b(q+1)] and we're done.Otherwise, b^x = q, and b(b^x) = bq is in [q+1,b(q+1)], as 1 < bq < b(q+1).

Thus the claim follows on induction.

Also could show via contradiction.

---------------------------------------------------QUESTION 6:Do exercise 2.4 from Dasgupta.Suppose you are choosing between the following three algorithms:

Algorithm A solves problems by dividing them into five subproblems of half the size, recursivelysolving each subproblem, and then combining the solutions in linear time.

Algorithm B solves problems of size n by recursively solving two subproblems of size n - 1and then combining the solutions in constant time.

Algorithm C solves problems of size n by dividing them into nine subproblems of size n/3,recursively solving each subproblem, and then combining the solutions in O(n^2) time.

What are the running times of each of these algorithms (in big-O notation), and which would youchoose?

SOLUTION:

A T(n) = 5 * T( n/2 ) + O(n)

a = 5; b = 2; d = 1 (from general form)

d < log a =====> 1 < log 5 b 2

log 5 2 2.322 so O( n ) = O( n )

B T(n) = 2 * T( n - 1 ) + O(1)

T(1) = T(2) = 2*T(1) T(3) = 2*T(2) = 2*2*T(1) T(4) = 2*T(3) = 2*2*2*T(1) etc.

Page 5: hw1-solns

n O( 2 )

2C T(n) = 9 * T( n/3 ) + O( n )

a = 9; b = 3; d = 2 (from general form)

d = log a =====> 2 = log 9 b 3

2 so O( n log n )

ALGORITHM C is the best of the three....---------------------------------------------------QUESTION 7:Do exercises 2.5(a), 2.5(b), 2.5(c), 2.5(d) from Dasgupta.Solve the following recurrence relations and give a big-theta bound for each of them.(a) T(n) = 2T(n/3) + 1(b) T(n) = 5T(n/4) + n(c) T(n) = 7T(n/7) + n(d) T(n) = 9T(n/3) + n^2

SOLUTIONS:(a) T(n) = 2 * T( n/3 ) + 1

a = 2; b = 3; d = 0

d < log a ====> 0 < log 2 b 3

log 2 3 so O( n )

(b) T(n) = 5 * T( n/4 ) + n

a = 5; b = 4; d = 1

d < log a ====> 1 < log 5 b 4

log 5 4 so O( n )

(c) T(n) = 7 * T( n/7 ) + n

a = 7; b = 7; d = 1

d = log a ====> 1 = log 7 b 7

so O( n log n )

2(d) T(n) = 9 * T( n/3 ) + n

Page 6: hw1-solns

a = 9; b = 3; d = 2

d = log a ====> 2 = log 9 b 3

2 so O( n log n )---------------------------------------------------QUESTION 8:Do exercise 2.12 from Dasgupta.How many lines, as a function of n (in big-theta() form), does the following program print? Write a recurrence and solve it. You may assume n is a power of 2.

function f(n) if n > 1: print_line(��still going��) f(n/2) f(n/2)

SOLUTION: kT(n) = 2 * T( n/2 ) + 1 with n = 2

T(1) = 0T(2) = 2 * T(1) + 1 = 2 * 0 + 1 = 1T(4) = 2 * T(2) + 1 = 2 * 1 + 1 = 3T(8) = 2 * T(4) + 1 = 2 * 3 + 1 = 7T(16) = 2 * T(8) + 1 = 2 * 7 + 1 = 15T(32) = 2 * T(16) + 1 = 2 * 15 + 1 = 31etc.

pattern is 1, 3, 7, 15, 31 or (n-1)

kwe know n = 2 , so:

k k-1T(2 ) = 2 * T(2 ) + 1

k-1 = 2 * ( 2 - 1 ) + 1

k = 2 - 1

= n - 1

= big-theta( n )---------------------------------------------------QUESTION 9:Do exercises 2.13(a), 2.13(b) from Dasgupta.A binary tree is full if all of its vertices have either zero or two children. Let Bn denote thenumber of full binary trees with n vertices.(a) By drawing out all full binary trees with 3, 5, or 7 vertices, determine the exact values ofB3, B5, and B7. Why have we left out even numbers of vertices, like B4?(b) For general n, derive a recurrence relation for Bn.

Page 7: hw1-solns

SOLUTIONS:(a) B3 = 1; B5 = 2; B7 = 5

Any full binary tree must have an odd number of vertices, so B4, B6, etc. make no sense.

i.e. B = 0 for all k 2k

(b) Decompose the tree into two sub-trees rooted at the two children of the root. The total number of full binary trees is the summation of the products of numbers of full binary trees from the two children:

n-2 --- \ Bn = > B B / i n-1-i --- i=1

(hint: there are i nodes in the left subtree, and therefore n-1-i nodes in the right subtree)

e.g. n is 7:

B = B B + B B + B B + B B + B B 7 1 5 2 4 3 3 4 2 5 1

= 2 + 0 + 1 + 0 + 2

B = B B + B B + B B + B B + B B + B B + B B 9 1 7 2 6 3 5 4 4 5 3 6 2 7 1

= 5 + 0 + 2 + 0 + 2 + 0 + 5

= 14