Question

urgent

L. Consider the following pseudocode for finding binomial coefficients: Binom(n, r) Input: integers n and r Output: n choose

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

Answer 1.

Time Complexity of give program : O(2n)

Since , this is a recurrence relation

T(n,r) = T(n-1,r) + T (n-1,r-1) + O(1)

Now , at each step n doubles . Thus giving total time Complexity of O (2n)

Answer 2 . For finding binomial coefficients efficiently , we use dynamuc programming , as there are many repeating subproblems . For this we have 2 choices :

Choice 1: Simple Dynamic Programming

Time Complexity : O(n*r)

Space Complexity : O(n*r)

C++ Code :

#include<bits/stdc++.h>

// c++ code for finding binomial

using namespace std;

int minimum(int x, int y) // function to find min

{

return (x< y) ? x : y;  

}

//minimum function

int binomial(int n, int r)

{

int A[n+1] [r+1];  

for ( int x = 0; x <= n; x++)

{

for ( int y = 0; y <= minimum(x, r); y++)

{

if (y == 0 || y == x)

A[x] [y] = 1;

else

A[x][y] = A[x-1] [y-1]+ A[x-1][y];}

}

return A[n][r];

}

// main function

int main()

{

int n,r;

cin>>n>>r;

cout << binomial(n, r);

}

Time Complexity : O(n*r)

Space Complexity : O(n*r)

Choice 2: Space Optimised Dynamic Programming

Time Complexity : O(n*r)

Space Complexity : O(r)

C CODE :

#include<bits/stdc++.h>
// Code Space optimised
using namespace std;

int binomial(int n, int r)
{ int A[r+1];
//initalize array to 0
memset(A, 0, sizeof(A));

A[0] = 1;  
int x=0;
int y=0;
for ( x = 1; x <= n;x++)
{
for (y = min(x, r);y>0;y--)

A[y] = A[y] + A[y-1];

}

return A[y];

}

int main()

// Main function driver code

{

int n;

int r;

cin>>n>>r;

printf ("%d ", binomial(n,r) );

return 0;

}

Time Complexity : O(n*r)

Space Complexity : O(r)

Add a comment
Know the answer?
Add Answer to:
urgent L. Consider the following pseudocode for finding binomial coefficients: Binom(n, r) Input: integers n and r Output: n choose r if r-0 or r-n thern return 1 end else return Binom(n-1, r-1) B...
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
  • URGENT Question 3 25 pts ArrayMystery: Input: n: a positive integer Pseudocode: Let output be an...

    URGENT Question 3 25 pts ArrayMystery: Input: n: a positive integer Pseudocode: Let output be an empty array For i = 1 to n j = 1 While ij <= n Addj to the end of output j - j + 1 Return output Answer the following questions about the ArrayMystery algorithm above. a) How many times will the inner while loop iterate? You should express your answer in terms of i and n, using Big-Oh notation. Briefly justify your...

  • Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a...

    Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a and b are integers while (a>0) B- a/2 A a-2 end while return b; i. What is the time complexity of the IterativeFunction pseudocode shown above? ii. What is the space complexity of the IterativeFunction pseudocode shown above? 2. What is the time complexity of the following algorithm (Note that n(n+1) 2,2 n(n+1)(2n+1) 2 and ): Provide both T(n) and order, e(f(n)). int A=0;...

  • Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array...

    Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...

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