Question

In Python State g(n)'s runtime complexity: def f(n): if n <= 1:` return 1 return 1...

In Python

State g(n)'s runtime complexity:

def f(n):
   if n <= 1:`
      return 1
   return 1 + f(n/2)

def g(n):
   i = 1
   while i < n:
       f(i)
       i *= 2
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Recursive function for f is
T(n) = T(n/2) + 1
     = T(n/4) + 1 + 1
     = T(n/8) + 1 + 1 + 1
     ......
     ......
     ......
     = T(n/n) + 1 + .... + 1 + 1 + 1 [log(n) +1 terms]
     = T(1) + 1 + .... + 1 + 1 + 1 [log(n) +1 terms]
     = 10 + 1 + .... + 1 + 1 + 1 [log(n) +1 terms]
     = clog(n)
     (Where c is a constant)
     = O(logn)
So, Time complexity of function f() = O(logn)

========================================

Time complexity of g(n):
Number of values for i are log(n)
So, The while loop in g() is running for log(n) times.
Each time in the while loop it is making call to function f()

So, Total time complexity of g(n) = O(log(n) * log(n))

Add a comment
Know the answer?
Add Answer to:
In Python State g(n)'s runtime complexity: def f(n): if n <= 1:` return 1 return 1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given the following two functions: def f(n): if n <= 0: return 0 return 1 +...

    Given the following two functions: def f(n): if n <= 0: return 0 return 1 + f(n - 1) def g(n): summ = 0 for i in range(0, n, 1): summ += 1 return summ (2 pts) State the runtime complexity of both f() and g() (2 pts) State the memory (space) complexity for both f() and g() (6 pts) Write another function called h(n) that does the same thing, but is significantly faster.

  • IN PYTHON Functions f(), g(), and h() are defined as follows: def f(x):     return 2*g(x)...

    IN PYTHON Functions f(), g(), and h() are defined as follows: def f(x):     return 2*g(x) def g(x):     return h(x)**2 def h(x):     return x//2 Consider the execution of function call f(2). Show the state of the program stack just prior to executing statement return x//2.

  • 1. What is the Big-Oh runtime complexity of the following algorithm? A. for (i = 5;...

    1. What is the Big-Oh runtime complexity of the following algorithm? A. for (i = 5; i <= 2 * n; i++)     cout << 2 * n + i – 1; << enld; 2. State the preconditions &/or postcondition for each of the following. A. ) if (x >= 0) y = x + y;     else y = y - x; B.) /* precondition: m <= n */    s = 0;    for (i = m; i <= n;...

  • PYTHON: Im stuck here, big O notation and runtime. What is it and Why are they...

    PYTHON: Im stuck here, big O notation and runtime. What is it and Why are they those? Please look at the pic, need help as Im confused. Thank You! def method3(n): for i in range(n): for j in range(100): for k in range(n): print(i+j+k) What is the runtime (tightest/closest bound in terms of O) for the above python function (method 3)? Please briefly explain. Enter your answer here def method4(n): for i in range(n): for j in range(n, o, -2):...

  • Question 18 CLO3 Analyze the following code and answer the questions that follow def F(n): If n <= 1: return n else: return F(n-1)+F(n-2) for i in range (n) print (F(i)) Result: 0 1 1 2 3 5 8...

    Question 18 CLO3 Analyze the following code and answer the questions that follow def F(n): If n <= 1: return n else: return F(n-1)+F(n-2) for i in range (n) print (F(i)) Result: 0 1 1 2 3 5 8 13 a. Write number of operations as a function when the code is execute b If n 7, what is the total number of operations? c. What is the complexity of the algorithm behind the code? (2 Marks) (2 Marks) (1...

  • Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MA...

    Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MAX) Marks (20,24,26,19,5,31,24,32,32,45 print (MaxVal (Marks) a. Express the number of operations in terms of a function f(n), where n is the input size. (1 Mark) b. What is the total number of operations in the for loop of the algorithm...

  • What is the time-complexity of the algorithm abc? Procedure abc(n: integer) s := 0 i :=1...

    What is the time-complexity of the algorithm abc? Procedure abc(n: integer) s := 0 i :=1 while i ≤ n s := s+1 i := 2*i return s consider the following algorithm: Procedure foo(n: integer) m := 1 for i := 1 to n for j :=1 to i2m:=m*1 return m c.) Find a formula that describes the number of operations the algorithm foo takes for every input n? d.)Express the running time complexity of foo using big-O/big-

  • PYTHON this implementation is really hard, Im stuck especially with the requirements they give. PLEASE HELP!...

    PYTHON this implementation is really hard, Im stuck especially with the requirements they give. PLEASE HELP! THANK YOU! RECURSIVE! Give a recursive python implementation of the following function: def check_Decreasing_Order(lissy): Given lissy, a python list of integers, the above function will return True if lissy is sorted in decreasing order, otherwise it will return false. For example, print(check_Decreasing Order([100,50,8, -2])) True print(check_Decreasing_Order([108,50,8,2,35])) False Implementation Requirements: 1. Your implementation must be recursive. 2. If you need more parameters, you may define...

  • PYTHON please help! im stuck on this homework question, THANK YOU! please follow the rules! Give...

    PYTHON please help! im stuck on this homework question, THANK YOU! please follow the rules! Give a recursive python implementation of the following function: def check_Decreasing_Order(lissy): Given lissy, a python list of integers, the above function will return True if lissy is sorted in decreasing order, otherwise it will return false. For example, print(check_Decreasing Order([100,50,8, -2])) True print(check_Decreasing_Order([108,50,8,2,35])) False Implementation Requirements: 1. Your implementation must be recursive. 2. If you need more parameters, you may define new functions or helper...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT