CS 173 [A]: Discrete Structures, Fall 01 Homework 8 Solutions This homework contains 4 problems worth a total of 35 points. It is due on Wednesday, November 14th, at 5pm. 1 Induction Proofs With Inequalities 10 points) Claim: For all positive integers n, 4 n < n)!. Prove the above claim by induction on n. Base Case: When n =, 4 n = 4 = 16 and n)! = 4! = 4, so the inequality holds for this value of n. Induction: Suppose that 4 k < k)! holds for all k =, 3,..., n. We need to show that 4 n+1 < n + )!. First, observe that by the inductive hypothesis, 4 n < n)!, so 4 n+1 = 4 4 n < 4 n)! = n)!. Since n, it follows that n + ) > and n + 1) >, so 4 n)! = n)! < n)!n + 1)n + ) = n + )!. Putting these equations together gives that 4 n+1 < n + )!, which is what we needed to show. Running Times of Tree Algorithms 8 points) Suppose that the structure of a node in a 3 ary tree that stores positive) integer values is defined by the following pseudocode: TreeNode { integer value; TreeNode left_child; TreeNode middle_child; TreeNode right_child; Let s use NIL as the symbol for a TreeNode that s not present. Consider next the following pseudocode for the function computet) that takes a TreeNode structure T as input. integer computetreenode tn) { 1
iftn is NIL) return 0; else return tn.value + computetn.left_child) + computetn.middle_child) + computetn.right_child); Part a): Suppose that T represents the root node of a 3 ary tree. In words, what does the function computet) do? The function computes the sum of all elements stored in the tree. Part b): Suppose that T represents the root node of a full, complete 3 ary tree of height h. What is the running time of computet) as a function of h? Express your answer using Big O notation. The function compute runs in constant time each time it is called. The function is called once on every node in the tree and once for each NIL node not present at height h + 1 in the tree. This means that compute is called h+1 3 i = 3h+ 1 times. So the running time of computet) is O3 h ). 3 Pseudocode Running Time 9 points) Suppose that a call to the function f1n) takes On) time and that a call to fn) takes O n ) time. Let the functions function1n), functionn), and function3n) be defined by the following pseudocode where n is a non negative integer. function1integer n) { for i = 0, 1,..., n: f1i);
functioninteger n) { for i = 0, 1,..., n: fi); function3integer n) { function1n); functionn); Part a): Suppose that n is a non negative integer. What is the running time of a call to function1n)? Express your answer as a simplified function of n using Big O notation. The total running time of the call to function1n) is ) ) nn + 1) Oi) = O i = O = On ). Part b): Suppose that n is a non negative integer. What is the running time of a call to functionn)? Express your answer as a simplified function of n using Big O notation. The total running time of the call to functionn) is ) O i ) = O i = O n+1 1 ) = O n ). An alternate solution can be obtained by taking the sum O n ) = On n ). 3
Part c): Suppose that n is a non negative integer. Using your answers for parts a) and b), find the running time of a call to function3n)? Express your answer as a simplified function of n using Big O notation. Hint: Which of the running times from parts a) and b) dominates the running time of the function? The total running time of the call to function3n) is the sum of the running times of function1n) and functionn). In this case, On ) + O n ) = O n ) or On ) + On n ) = On n ), depending on the form of the solution from part b). 4 Recursive Definition of Running Time 10 points) Consider the following pseudocode for the function functiongn) that takes a non negative integer n as input. integer functionginteger n) { ifn <= 0) return 0; integer count = 0; for i = 1,,..., n: count = count + 1; return count + functiongn-1) + functiongn-1) + functiongn-1); Part a): What is the return value of functiongn) for n = 0, 1,, 3, 4? functiong0) = 0 functiong1) = 1 functiong) = 5 functiong3) = 18 functiong4) = 58 4
Part b): Give a recursive definition, gn), for the running time of functiongn) where n is a non negative integer. Be sure to include a base case or cases. The running time is given by g0) = c 0 and for n 1 where c 0, c 1, c are constants. Part c): gn) = 3gn 1) + c 1 n + c Give a closed form formula for the recursive definition of gn) from part b) as a function of n. Obtain the closed form using unrolling or a similar method. Only state the formula for gn) it is not necessary to prove that the formula is correct by induction). The solution to the recurrence from part b) can be solved exactly using Mathematica or Wolfram Alpha as gn) = 1 4 4c0 3 n + c 1 n + 3 n+1 3 ) + c 3 n 1) ). This gives that the running time is exponential as O3 n ). 5