Generating Functions Week 2 - SM
Generating Functions Week 2 - SM
WEEK - 2
Ordinary Generating Functions
Often, our goal in the analysis of algorithms is to
derive specific expressions for the values of terms in
a sequence of quantities a0 ,a1 ,a2 , … 0 ,1 ,2 ,… that
measure some performance parameter. In this
chapter we see the benefits of working with a single
mathematical object that represents the whole
sequence.
Definition. Given a sequence a0,a1,a2,…,ak,…
0,1,2,…,,…, the function
#include <iostream>
using namespace std;
// A recursive function to find nth catalan number
unsigned long int catalan(unsigned int n)
{
// Base case
if (n <= 1)
return 1;
// catalan(n) is sum of
// catalan(i)*catalan(n-i-1)
unsigned long int res = 0;
for (int i = 0; i < n; i++)
res += catalan(i) * catalan(n - i - 1);
return res;
}
int main()
{
for (int i = 0; i < 10; i++)
cout << catalan(i) << " ";
return 0;
}
Program for nth Catalan Number using Dynamic Programming:
#include <iostream>
using namespace std;
// Driver code
int main()
{
for (int i = 0; i < 10; i++)
cout << catalanDP(i) << " ";
return 0;
}
Time Complexity: O(n2)
Auxiliary Space: O(n)
Exponential Generating Functions
1. a (Generating functions can be used to solve recurrence relations by transforming them into algebraic
equations. They can also be used to find closed-form expressions for sequences, allowing us to compute
specific terms without having to iterate through the entire sequence. Additionally, generating functions can
be used to analyze the time complexity of algorithms by examining the coefficients of the power series
representation.)
2. Integration is not a common operation performed on generating functions. Differentiation, addition, and
multiplication are commonly used operations to manipulate generating functions and derive useful
3. G(x) = 1 + x + x^2 + x^3 + ... is an example of a generating function. It represents the sequence of
coefficients where each term corresponds to the power of x in the series. F(n), A(n), and S(n) are not
5. d.
6. c
Explaination Of Aanswer
8. B) Differentiation
9. B) Generating functions are mathematical tools that can be used to represent and
12. b) OGFs provide a convenient and concise way to represent and manipulate