cp's OEIS Frontend

This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.

A161169 Triangle read by rows: T(n,k) = number of permutations of {1..n} with at most k inversions.

Original entry on oeis.org

1, 1, 1, 2, 1, 3, 5, 6, 1, 4, 9, 15, 20, 23, 24, 1, 5, 14, 29, 49, 71, 91, 106, 115, 119, 120, 1, 6, 20, 49, 98, 169, 259, 360, 461, 551, 622, 671, 700, 714, 719, 720, 1, 7, 27, 76, 174, 343, 602, 961, 1416, 1947, 2520, 3093, 3624, 4079, 4438, 4697, 4866, 4964, 5013
Offset: 0

Views

Author

Shreevatsa R, Jun 04 2009

Keywords

Comments

T(n,k) is also the number of permutations with Kendall tau distance ("bubble-sort distance") to the identity permutation being at most k. This is the number of swaps performed by the bubble-sort algorithm to sort the sequence.
The above only gives T(n,k) for k<=n(n-1)/2, but T(n,k)=n! for all k>=n(n-1)/2.

Examples

			Triangle begins:
  1;
  1;
  1, 2;
  1, 3,  5,  6;
  1, 4,  9, 15, 20,  23,  24;
  1, 5, 14, 29, 49,  71,  91, 106, 115, 119, 120;
  1, 6, 20, 49, 98, 169, 259, 360, 461, 551, 622, 671, 700, 714, 719, 720;
  ...
T(3,2)=5 because there are 5 permutations of {1,2,3} with at most 2 inversions: (1,2,3) with 0 inversions, (1,3,2), (2,1,3) with 1 inversion each, (2,3,1), (3,1,2) with 2 inversions each.
T(n,0)=1 because there is exactly 1 permutation (the identity permutation) with no inversions,
T(n,k) = n! for all k >= n(n-1)/2 because all permutations have at most n(n-1)/2 inversions.
		

Crossrefs

Partial sums of A008302.

Programs

  • Python
    ct = {(0,0): 1}
    def c(n,k):
        if k<0: return 0
        k = min(k, n*(n-1)/2)
        if (n, k) in ct: return ct[(n, k)]
        ct[(n, k)] = c(n, k-1) + c(n-1, k) - c(n-1, k-n)
        return ct[(n, k)]

Formula

T(n,k) = Sum_{i s.t. n-i<=k} T(n-1, k-(n-i));
T(n,k) = Sum_{i=max(1,n-k)..n} T(n-1, k-n+i);
T(n,k) = Sum_{j=max(k-n+1,0)..n} T(n-1, j).
T(n,k) = T(n,k-1) + T(n-1,k) - T(n-1,k-n), taking T(n,k)=0 for k<0.
Also, T(n,k) = n! - T(n, n(n-1)/2-k-1).
For k<=n, T(n,k) = A008302(n+1,k).
G.f.: (1/(1-x))*Product_{j=1..n} (1-x^j)/(1-x) = Sum_{k>=0} T(n,k) x^k. - Alejandro H. Morales, Apr 04 2024