Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem 5: Recursion (10 points) Here are two algorithms to compute 2 for any integer n >= 0. Algi (n) Alg2 (n) if n =
Problem 5: Recursion (10 points) Here are two algorithms to compute 2" for any integer n >= 0. Algi (n) Alg2 (n) if n = 0 return 1 else return 2 * Algi (n-1) if n == 0 return 1 if n = 1 return 2 else if n is even return Alg2 (n/2) * Alg2 (n/2) else return 2 * Alg2 ((n-1)/2) * Alg2 ((n-1)/2) a) Draw a diagram that illustrates the sequence of calls to both algorithms for n=8. b) How many calls are required for each algorithm above for n=8? (i.e. how many nodes in the call tree) c) Since each call takes O(1) in both algorithms above, based on your answer to b) which algorithm do you believe will be faster? d) Write a recurrence relation for each algorithm. Problem 5: Recursion (10 points) Here are two algorithms to compute 2" for any integer n >= 0. Algi (n) Alg2 (n) if n = 0 return 1 else return 2 * Algi (n-1) if n == 0 return 1 if n = 1 return 2 else if n is even return Alg2 (n/2) * Alg2 (n/2) else return 2 * Alg2 ((n-1)/2) * Alg2 ((n-1)/2) a) Draw a diagram that illustrates the sequence of calls to both algorithms for n=8. b) How many calls are required for each algorithm above for n=8? (i.e. how many nodes in the call tree) c) Since each call takes O(1) in both algorithms above, based on your answer to b) which algorithm do you believe will be faster? d) Write a recurrence relation for each algorithm
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started