VOOZH about

URL: https://www.geeksforgeeks.org/dsa/eulerian-number/

⇱ Eulerian Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Eulerian Number

Last Updated : 4 Dec, 2023

In combinatorics, the Eulerian Number A(n, m), is the number of permutations of the numbers 1 to n in which exactly m elements are greater than previous element.

For example, there are 4 permutations of the number 1 to 3 in which exactly 1 element is greater than the previous elements. 

👁 Eulerian

Examples:

Input : n = 3, m = 1
Output : 4
Please see above diagram (There
are 4 permutations where 1 no. is
greater.

Input : n = 4, m = 1
Output : 11

Eulerian Numbers are the coefficients of the Eulerian polynomials described below. 

👁 Image

The Eulerian polynomials are defined by the exponential generating function 

👁 Image

The Eulerian polynomials can be computed by the recurrence 

👁 Image

👁 Image

An explicit formula for A(n, m) is 

👁 Image

We can calculate A(n, m) by recurrence relation: 

👁 Image

Example: 

Suppose, n = 3 and m = 1. 
Therefore, 
A(3, 1) 
= (3 - 1) * A(2, 0) + (1 + 1) * A(2, 1) 
= 2 * A(2, 0) + 2 * A(2, 1) 
= 2 * 1 + 2 * ( (2 - 1) * A(1, 0) + (1 + 1) * A(1, 1)) 
= 2 + 2 * (1 * 1 + 2 * ((1 - 1) * A(0, 0) + (1 + 1) * A(0, 1)) 
= 2 + 2 * (1 + 2 * (0 * 1 + 2 * 0) 
= 2 + 2 * (1 + 2 * 0) 
= 2 + 2 * 1 
= 2 + 2 
= 4 
We can verify this with example shown above.

Below is the implementation of finding A(n, m):  


Output
4

Time Complexity: O(2n)
Auxiliary Space: O(log(n)), Due, to recursive call stack

Below is the implementation of finding A(n, m) using Dynamic Programming: 


Output
4

Time Complexity: O(n*m)
Auxiliar Space: O(n*m)

Efficient approach: Space optimization

In the previous approach, the current value dp[i][j] is only depend upon the current and previous row values of DP. So to optimize the space complexity we use a single 1D array to store the computations.

Implementation steps:

  • Create a 1D vector dp of size m+1.
  • Set a base case by initializing the values of DP .
  • Now iterate over subproblems by the help of nested loop and get the current value from previous computations.
  • Now Create a variable prev to store previous computations and a temp variable to update prev at every iteration.
  • After every iteration assign the value of temp to prev for further iteration.
  • At last return and print the final answer stored in dp[m].

Implementation:


Output
4

Time Complexity: O(n*m)
Auxiliary Space: O(m)

Comment