Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help in answering these face questions with all provided information 4.4. Further Examples of Recursion 173 def power(x, n): Compute the value

I need help in answering these face questions with all provided information

image text in transcribedimage text in transcribed
4.4. Further Examples of Recursion 173 def power(x, n): "" Compute the value x**n for integer n. if n == return 1 else: partial = power(x, n ff 2) # rely on truncated division result = partial * partial if n % 2 == 1: # if n odd. include extra factor ofx result =l= x return result QKDOOMJONLn-lhbJNIt ,_L Code Fragment 4.12: Computing the power function using repeated squaring. To illustrate the execution of our improved algorithm, Figure 4.12 provides a recursion trace of the computation power(2. 13). return 64 a: 64 a: 2 = 8192 Figure 4.12: Recursion trace for an execution of power(2, 13). To analyze the running time of the revised algorithm, we observe that the expo- nent in each recursive call of function power(x,n) is at most half of the preceding exponent. As we saw with the analysis of binary search, the number of times that we can divide n in half before getting to one or less is 0(logn). Therefore, our new formulation of the power function results in 0(log n) recursive calls. Each individ- ual activation of the function uses 0(1) operations (excluding the recursive calls), and so the total number of operations for computing power(x,n) is 0(logn). This is a signicant improvement over the original 0(n)-time algorithm. The improved version also provides signicant saving in reducing the memory usage. The rst version has a recursive depth of 0(a), and therefore 0(n) activation records are simultaneous stored in memory. Because the recursive depth of the improved version is O(log :1), its memory usages is 0(log n) as well. Assuming it is possible to sort n numbers in 0(nlognl time, show that it is possible to solve the three-way set disjointness problem in 0(nlognl time. Bob built a Web site and gave the URL only to his n friends, which he numbered from 1 g. He told friend number 5; that he/ she can visit the Web site at most g' times. Now Bob has a counter, C, keeping track of the total number of visits to the site (but not the identities of who visits). What is the minimum value for C such that Bob can know that one of his friends has visited his/her maximum allowed number of times? Describe a recursive algorithm for finding the maximum element in a g- guence, S, of n elements. What is your running time and space usage? Draw the recursion trace for the computation of gowerl 2,18) , using the repeated squaring algorithm, as implemented in Code Fragment 4.12. Write a short recursive Python function that nds the minimum and max- imum values in a sequence without using any loops

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions