Question

Please answer ALL parts completely-- answer with clear explanation will get a thumbs up!

Problem 2: Programming [2 points] Consider the following pseudo code where a function has been defined for a non negative integer x. Note that the function calls itself (within the while loop) making it a recursive function. def func (x) print x while i<x: i = i+1 func (x-1) (a) How many times does the program print if we call func (0) and func (1), respectively? (b) Suppose T(1) represents the number of times the function prints when called with an argument 1, T(2) represents the number of times the function prints when called with an argument 2, ..., T(n - 1) represents the number of times the function prints when called with an argument n -1, and T(n) represents the number of times the function prints when called with an argument n We can now make the following argument: func (n) will print once (it prints the number n) and will follow this up by calling func (n-1), n times. According to our notation, each such call prints T(n-1) times. Therefore T(n)=1+nT(n-1). Based on this information, does the program run in polynomial time? Provide an explanation for your answer.

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

(a) The code snippet is :

def func(x):

i = 0

print x

while i < x:

i = i + 1

func(x - 1)

If we call this function by 'func(0)', then the program will print only once as the value of 'x' in the function will be 0. So, x = 0 will be printed once and when the while loop condition will be checked, it will be false as 'i' is also 0, or 0 < 0 which is a false condition. Hence the function will print 'x' just once or 1 time.

If we call this function by 'func(1)', then the program will print twice as the value of 'x' in the function will be 1. So, x = 1 will be printed first and then the while loop condition will be checked, which is true as 'i' which is 0 is smaller than 'x', or 0 < 1 or the program will enter the while loop where the value of 'i' will be incremented by 1 to be i = 0 + 1 = 1. Now the function will be called again by passing x - 1 as the argument or 1 - 1 = 0. So, now i = 1 and value of 'x' on recursive call is 0, which will be printed. So again the while loop won't be executed as 'i' is not lesser than 'x', 1 < 0. Hence the function will print 'x' twice or 2 times.

(b) In this function, we can see that the recursive call is made inside a while loop. According to the notation of representing the number of times 'n' the function prints to be denoted as T(n), the relation obtained is T(n) = 1 + nT(n - 1). Assuming n > 1 for calculating the time complexity of the function. Also, T(0) = 1 since the function prints once when 0 is passed to it as argument. Now, we can say that T(0) = O(1) since the function has to just print once when x = 0. Then,

T(n) = 1 + n((1 + nT(n - 2)), since T(n - 1) = 1 + nT(n - 2)

Expnading this equation, we get a polynomial time running relation like

or, T(n) = (1 + n) + n2T(n - 2)

or, T(n) = (1 + n + n2) + n3T(n - 3)

or, T(n) = (1 + n + n2 + n3) + n4T(n - 4) ... and so on. This implies

T(n) = n * (n - 1) * (n - 2) * (n - 3) ... T(0) + n for n number of prints

or, T(n) = n! + n which means the polynomial time run complexity of this algorithm is O(n!)

or, T(n) = O(2n) as n! is almost equal to 2n in terms of polynomial runtime complexities.

Add a comment
Know the answer?
Add Answer to:
Please answer ALL parts completely-- answer with clear explanation will get a thumbs up! Problem 2:...
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
  • program in Java or C We read a number i and print all the bits in...

    program in Java or C We read a number i and print all the bits in the binary representation of i+1 strictly to the right of the first 1. One option to of doing that is with a recursive function that checks if its argument is strictly greater than 1 and, if so, calls itself on j right-shifted 1 bit and then prints j mod 2.

  • 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...

  • Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to...

    Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to implement a binary search algorithm to determine whether a given element is a member of a given sequence Each function will have two parameters, aseq, a sorted sequence, and target. isMemberR and isMemberI will return True if target is an element of the sequence, and False otherwise. Your implementations must implement the binary search algorithm described above. When function i sMemberR recursively invokes itself,...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...

    PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input. For example: Test Input Result print(yesOrNo()) hello blank...

  • Use MATLAB to solve this problem. Thank you. Upload the assignment to CANVAS, it should include...

    Use MATLAB to solve this problem. Thank you. Upload the assignment to CANVAS, it should include two things: the PDF and the zipped folder. Late assignments are not accepted. For the questions below, do not create the arrays or matrices by hand. Instead, use built-in MATLAB functions and shorthand notations. Otherwise, no points will be given. 1) Create a function called my product that computes the multiplication of the numbers in any vector using a while loop. The function should...

  • PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs...

    PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs to:             -define a recursive procedure/function and call it.             -use syscall operations to display integers and strings on the console window             -use syscall operations to read integers from the keyboard. Assignment Description: Implement a MIPS assembly language program that defines "main", and "function1" procedures. The function1 is recursive and should be defined as: function1(n) = (2*n)+9                      if n <= 5              =...

  • 1. What is the result of calling the append method on a list? 2. What must...

    1. What is the result of calling the append method on a list? 2. What must be defined prior to using a method like append? 3. Explain why two lines caused an IndexError. 4. What is the result of calling the remove method on a list? 5. Give one example of a list method that requires an argument and one that does not. 6. Describe the syntax similarities and differences between using a list method like append and Python built-in...

  • Explain what the code is doing line from line explanations please or summarize lines with an explanation def displayCart(): #displays the cart function """displayCart function - dis...

    Explain what the code is doing line from line explanations please or summarize lines with an explanation def displayCart(): #displays the cart function """displayCart function - displays the items in the cart ---------------------------------------------------------------------""" import os os.system('clear') print("\n\nCart Contents:") print("Your selected items:", cart) def catMenu(): #function that displays the category menu """catMenu function - displays the categories user picks from ---------------------------------------------------------------------""" import os os.system('clear') print(""" 1 - Books 2 - Electronics 3 - Clothing d - display cart contents x -...

  • 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...

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
ADVERTISEMENT