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
  • Write a function called printStars. The function receives a parameter containing an integer value. If the...

    Write a function called printStars. The function receives a parameter containing an integer value. If the parameter is positive, the funciton prints (to standard output) the given number of asterisks. Otherwise the function does nothing. The function does not return a value. Thus, if printStars(8) is called, ******** (8 asterisks) will be printed. The function must not use a loop of any kind (for, while, do-while) to accomplish its job. Instead, it should examine its parameter, returning if the parameters...

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

  • The problem is this Design a function that accepts a list as an argument and returns...

    The problem is this Design a function that accepts a list as an argument and returns the largest value in the list. The function should use recursion to find the largest item. My coding so far is this def main(): # Prints the largest value in list user_input = input('Enter a list of numbers seperated by a space: ') user_list = user_input.split() print('Largest number is ', max_Number(user_list), '.') # Recursion function that finds the maximum number in a sequence 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,...

  • I need help for the order of growth for functions in Python 3. Q1: What is...

    I need help for the order of growth for functions in Python 3. Q1: What is the order of growth for the following functions? Kinds of Growth Here are some common orders of growth, ranked from no growth to fastest growth: 1. Θ(1) — constant time takes the same amount of time regardless of input size 2. Θ(log n) — logarithmic time 3. Θ(n) — linear time 4. Θ(n log n) — linearithmic time 5. Θ(n2 ) 6. Θ(n3 ),...

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

  • # Simple python Question. ============================ Usinginput(),ascertain whether or not a user wants to convert fahrenheit to...

    # Simple python Question. ============================ Usinginput(),ascertain whether or not a user wants to convert fahrenheit to celsius, or celcius to farenheit. Then have your python script do so, and output the answer. Create a ​function​ called question_2 to calculate the sum of 2 given numbers, if the values are equal, then return thrice of their sum. Create a ​function ​called question_3​ ​that accepts 3 arguments. The function will compare the three arguments, and return the highest value. Create a ​function​...

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

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