Question

Derive the runtime of dynamic Fibonacci algorithm shown below #### def fib( iN ): iP =...

Derive the runtime of dynamic Fibonacci algorithm shown below

####

def fib( iN ):

iP = iQ = 1

for i in range( iN - 1 ):

      iP, iQ = iP + iQ, iP

return iP

0 0
Add a comment Improve this question Transcribed image text
Answer #1

####

def fib( iN ):

iP = iQ = 1 ----------> Line 1

for i in range( iN - 1 ): ----------> Line 2

      iP, iQ = iP + iQ, iP ----------> Line 3

return iP ----------> Line 4

The complexity of Line 1: O(1) as it takes constant time.

The complexity of Line 2: O(iN) as the for loop is run iN-1 times from i = 0 to iN-2

The complexity of Line 3: O(1) as it takes constant time.

The complexity of Line 4: O(1) as it takes constant time.

Hence, the overall complexity of this code is:

O(iN) + O(1) = O(iN) (Answer)

Add a comment
Know the answer?
Add Answer to:
Derive the runtime of dynamic Fibonacci algorithm shown below #### def fib( iN ): iP =...
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
  • Write an algorithm to return the fibonacci number. Show the trace of you algorithm for fib(6)....

    Write an algorithm to return the fibonacci number. Show the trace of you algorithm for fib(6). when n <= 2 fib(n) = 1 when n > 2 fib(n) = fib(n-1) + fib(n-2)

  • The ­following Implementation of the Fibonacci function is a correct, but inefficient, def fibonacci(n): if n...

    The ­following Implementation of the Fibonacci function is a correct, but inefficient, def fibonacci(n): if n <= 2: return 1 else: return fib(n - 1) + fib(n - 2) In more details, the code shown runs very slowly for even relatively small values of n; it can take minutes or hours to compute even the 40th or 50th Fibonacci number. The code is inefficient because it makes too many recursive calls. It ends up recomputing each Fibonacci number many times....

  • Fibonacci function Fib(n) is given below. Fib(n)= Fib(n-1) + Fib(n-2) for n > 1 Fib(n)= 1...

    Fibonacci function Fib(n) is given below. Fib(n)= Fib(n-1) + Fib(n-2) for n > 1 Fib(n)= 1 for n=1 Fib(n)= 0 for n=0 Using following initialization unsigned int *Fib = new unsigned int[30]; and function prototypes void push(int n) unsigned int pop( ) insert first 30 Fibonacci numbers into dynamic array Fib and then pop them to print out first 30 numbers in descending order. The output of your program should be 514229, 317811, 196418, ......, 1, 1, 0

  • Programming Exercise 11.6 Х + | Instructions fib.py >_ Terminal + iit B 1 def fib(n):...

    Programming Exercise 11.6 Х + | Instructions fib.py >_ Terminal + iit B 1 def fib(n): 2 "*"Returns the nth Fibonacci number. " 3 if n < 3: lil 4 return 1 Modify the recursive Fibonacci function to employ the memoization technique discussed in this chapter. The function creates a dictionary and then defines a nested recursive helper function named memoizedFib You will need to create a dictionary to cache the sum of the fib function. The base case of...

  • Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to...

    Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to calculate the Fibonacci Function 1.) Use a recursive function called fib() to calculate the Fibonacci Function for the following values for the variable n. int n = 10; int n = 20; int n = 30; int n = 40; int n = 45; int n = 46; 2.) In addition to calculating and displaying the results, use a "timer" to see how long...

  • 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

  • Below you will find a recursive function that computes a Fibonacci sequence (Links to an external...

    Below you will find a recursive function that computes a Fibonacci sequence (Links to an external site.).   # Python program to display the Fibonacci sequence up to n-th term using recursive functions def recur_fibo(n): """Recursive function to print Fibonacci sequence""" if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) # Change this value for a different result nterms = 10 # uncomment to take input from the user #nterms = int(input("How many terms? ")) # check if the number...

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • MIPS - assembly language Task I (1 mark): Write a program to calculate the nth Fibonacci...

    MIPS - assembly language Task I (1 mark): Write a program to calculate the nth Fibonacci number based on an iterative approach where value n is read from the keyboard and stored in your main program that is calling fib.  The main program reads number ‘n’ -- restrict range of ‘n’ to 2-50, and display an error message if this condition is not satisfied.  The main program calls fib with parameter passing. Procedure fib should return the calculated...

  • CMPS 290 Programming Assignment Arrays and Recursion – Fibonacci Numbers In this programming assignment you will...

    CMPS 290 Programming Assignment Arrays and Recursion – Fibonacci Numbers In this programming assignment you will be working with arrays and multiple function calls, including a recursive function call. The goal of the assignment will be to, using a pre-set array of sequence numbers, calculate the Fibonacci sequence number for each value in the array (see the example at the bottom if this isn’t clear). Instructions and Requirements: • Create a program that assembles and runs to find the correct...

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