Eulerian Numbers

Note

The Eulerian number is meant below. Distinguish it from the Euler number and Euler's number (mathematical constants associated with Euler, such as γ\gamma or e\mathrm{e}).

In combinatorics, Eulerian numbersare the number of permutations of 11 through nn with exactly mm elements greater than their predecessors (that is, with mm ascents). They are defined as follows:

A(n,m)=⟨nm−1⟩A(n, m) = \left\langle \begin{matrix} n\\ m - 1 \end{matrix} \right\rangle

For example, among the numbers 11 through 33, there are 44 permutations in which exactly one element is greater than its predecessor:

PermutationAdjacent elements satisfying the conditionCount
1 2 31, 2 & 2, 32
1 3 21, 31
2 1 31, 31
2 3 12, 31
3 1 21, 21
3 2 10

Thus, by the definition of A(n,m)A(n, m), when nn equals 33 and mm equals 11, the Eulerian number is 44, meaning that there are 44 permutations with 11 element greater than its predecessor.

For small values of nn and mm, Eulerian numbers can be obtained directly:

A(n,m)A(n, m)Permutations satisfying the conditionCount
A(1,0)A(1, 0)(1)(1)1
A(2,0)A(2, 0)(2,1)(2, 1)1
A(2,1)A(2, 1)(1,2)(1, 2)1
A(3,0)A(3, 0)(3,2,1)(3, 2, 1)1
A(3,1)A(3, 1)(1,3,2),(2,1,3),(2,3,1),(3,1,2)(1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2)4
A(3,2)A(3, 2)(1,2,3)(1, 2, 3)1

Formula

Eulerian numbers can be computed recursively or by dynamic programming.

First, when m≥nm \ge n or n=0n = 0, no permutation satisfies the condition, so the Eulerian number is 00.

Second, when m=0m = 0, only the descending permutation satisfies the condition, so the Eulerian number is 11.

Finally, starting from a permutation of n−1n-1, insert nn to obtain a permutation of nn. Since inserting nn can increase the Eulerian number by at most 11, A(n,m)A(n, m) can transition only from A(n−1,m−1)A(n-1, m-1) and A(n−1,m)A(n-1, m).

Consider inserting nn: when pi−1<pip_{i-1} < p_{i}, if nn is inserted before pip_{i}, that is, nn is inserted into an ascent, the Eulerian number is unchanged; inserting nn at the beginning also leaves it unchanged; otherwise, if nn is inserted elsewhere, the Eulerian number increases by 11.

For a transition from A(n−1,m−1)A(n-1, m-1) to A(n,m)A(n, m), the Eulerian number must increase by 11. Thus, nn cannot be inserted inside an ascent or at the beginning, leaving n−(m−1)−1=n−mn - (m-1) - 1 = n-m choices.

For a transition from A(n−1,m)A(n-1, m) to A(n,m)A(n, m), the Eulerian number must remain unchanged. Thus, nn can only be inserted inside an ascent or at the beginning, giving m+1m+1 choices.

Combining the cases gives

A(n,m)={0,m>n or n=0,1,m=0,(n−m)⋅A(n−1,m−1)+(m+1)⋅A(n−1,m),otherwise.A(n, m) = \begin{cases} 0, & m > n \text{ or } n = 0, \\ 1, & m = 0, \\ (n-m) \cdot A(n-1, m-1) + (m+1) \cdot A(n-1, m), & \text{otherwise}. \end{cases}

Implementation

C++

int eulerianNumber(int n, int m) {
  if (m >= n || n == 0) return 0;
  if (m == 0) return 1;
  return (((n - m) * eulerianNumber(n - 1, m - 1)) +
          ((m + 1) * eulerianNumber(n - 1, m)));
}

Python

def eulerianNumber(n, m):
    if m >= n or n == 0:
        return 0
    if m == 0:
        return 1
    return ((n - m) * eulerianNumber(n - 1, m - 1)) + (
        (m + 1) * eulerianNumber(n - 1, m)
    )

Exercises

This content comes from OI Wiki: Eulerian Numbers. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.