Closed form expression for the number of ordered partitions of a list

70 Views Asked by At

Suppose I have a list $L = [e_1, e_2, \dots, e_n]$ and integer $k \geq 2$. I want to compute the number of ways to partition $L$ into $k$ sublists while maintaining the order of the elements. For example, one such partition would be $L_1 = [e_1]$ and $L_2 = [e_2,\dots,e_n]$ for $k = 2$. I wrote the following Python code to do this for me:

def num_partitions(l, k):
    if k == 1:
        return 1
    else:
        total = 0
        for i in range(1, len(l)):
            total += num_partitions(l[i:], k - 1)
        return total

Is there a closed form expression for this function?

1

There are 1 best solutions below

2
On BEST ANSWER

This yields to the Stars and Bars technique. We insert separators into $k-1$ of the $n-1$ gaps between list elements. There are $\binom{n-1}{k-1}$ ways to do this.