Answered step by step
Verified Expert Solution
Link Copied!

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 =

image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions