Chapter 5 Application of Catalan Numbers

Here we discover some application of catalan numbers in real problem solving. For example, some knowledge content about combinatorial and discrete mathematics.

5.1 Paths on a triangle

Definition 5.1
Let TN2 denote the infinite “triangle” T={(k,n),0kn} (see the figure below).

For (k,n)T we denote by Pk,n the number of paths such that: * the path starts at (0,0) ends at (n,k) and entirely lies inside T * the paths only takes unit steps in the North and East directions.

For example this figure shows that P2,3=5:

Escalier

Figure 5.1: Escalier

We write a function Paths(K,N) which returns a table (or a matrix) of all the values of Pk,n for kK,nN.

# implement of the function Path
# we use the recursive method by implement the auxiliary function Path
def Path(k, n):
    res = [['']]
    up_count = [[0]]
    right_count = [[0]]
    if n == 0:
        return []
    if k == 0:
        return ['up ' * n]
    for i in range(1, n + k + 1):
        res.append([])
        up_count.append([])
        right_count.append([])
        for j in range(len(res[i-1])):
            curr_path = res[i-1][j]
            if up_count[i-1][j] < n:
                res[i].append(curr_path + 'up ')
                up_count[i].append(up_count[i-1][j] + 1)
                right_count[i].append(right_count[i-1][j])
            if right_count[i-1][j] < k:
                if right_count[i-1][j] < up_count[i-1][j]:
                    res[i].append(curr_path + 'right ')
                    up_count[i].append(up_count[i-1][j])
                    right_count[i].append(right_count[i-1][j] + 1)
    return res[-1]


def Paths(K, N):
    result = np.zeros((K + 1, N + 1))
    for k in range(K + 1):
        for n in range(N + 1):
            result[k, n] = len(Path(k, n))

    return result
    
print("--------- Another Test Sample ----------")
print(Paths(5, 6))
print("--------- Test Sample for Calatan number ----------")
lis = []
matrix = Paths(5, 6)
for i in range(6):
    lis.append(matrix[i][i + 1])
print(lis)

--------- Another Test Sample ----------
[[  0.   1.   1.   1.   1.   1.   1.]
 [  0.   1.   2.   3.   4.   5.   6.]
 [  0.   0.   2.   5.   9.  14.  20.]
 [  0.   0.   0.   5.  14.  28.  48.]
 [  0.   0.   0.   0.  14.  42.  90.]
 [  0.   0.   0.   0.   0.  42. 132.]]
--------- Test Sample for Calatan number ----------
[1.0, 2.0, 5.0, 14.0, 42.0, 132.0]

We denote the sequence uk=Pk,k+1. From the matrix we can find that uk is same with the sequence of the Calatan Number, which means Pk,k+1=ck=j=0k1=ckcn1k. Now lets prove it.

First we try to discuss in a more simple model. For example, I try to compute P3,4=14. For each points (k,k+1) where 1k3, we consider those points as step points, which means for example, we consider (2,3) as an intermediate point, we have for all the possible paths to (3,4) go through (2,3), is the multiple of P2,3P2,1=c0c2. For each step point, the paths go through the step points we can consider the process of two parts, from (0,0) to the step points and from step points to (k,k+1).

Then we can expand this model into more general cases. we try to compute Pk,k+1, the for all step points (m,m+1) where 0mk1. For all the possible paths to Pk,k+1, we know it go through at least one step point, since all the paths start from (0,0) and has to go through (0,1) cause we have to go up at the beginning. Therefore we can consider all the paths into to step. First from (0,0) to the step point, which we have Pm,m+1 and from the step point to (k,k+1), which is equivalent to from (0,1) to (km1,km), we have Pkm1,km possibilities. Then number of paths to (k,k+1) is the multiplication of Pm,m+1 and Pkm1,km. So we have the sum for all step points that Pk,k+1=m=0k1Pkm1,kmPm,m+1 Since P0,1=1 and P1,2=2. And ck=j=0k1=ckcn1k by the recursive formula of the Catalan number. Therefore we prove that Pk,k+1=ck=j=0k1=ckcn1k Also we have Pk,k is also Catalan number, which is easy to explain since Pk,k=Pk,k+1, since from (k,k) to (k,k+1) there only on way which is go on step on direction East, so we have Pk,k=Pk,k+1=ck=j=0k1=ckcn1k So the paths to diagonal positions are also Catalan numbers.


We can also use other method to prove Path(K,K) is Catalan Numbers.

The method is from (Wiki Page) of Catalan number. We count the number of paths which start and end on the diagonal of a n×n grid. All such paths have n right and n up steps. Since we have 2n steps in total, therefore all the possibility are binomial number (2nn).

Graph of the algorithms

Figure 5.2: Graph of the algorithms

However, we are restrict by the triangle. As the graph shown above, the path below the diagonal line is not valid. Therefore, we use make the invalid portion of the path flipped like the graph shown. Since we have 2n steps in total and now we have n+1 up then we got n1 right steps. Due to this reason, we reach at (n1,n+1) in the end. Because every monotonic path in the (n1)×(n+1) grid meets the higher diagonal, and because the reflection process is reversible, the reflection is therefore a bijection between bad paths in the original grid. Therefore, for the bad paths we got (n1+n+1n1)=(2nn1)=(2nn+1) And the valid paths of the Catalan paths are Cn=(2nn)(2nn+1)=1n+1(2nn)

5.2 Well-formed parentheses expressions

Definition 5.2
It can be shown that cn counts the number of expressions containing n pairs of parentheses which are correctly matched. For the first values we obtain n=1:()n=2:(())()()n=3:((()))(())()()(())()()()(()())

Write a recursive function Parentheses(n) which returns the list of all well-formed parentheses expressions with n pairs of parentheses and check for different values that the list has length cn

As we mentioned above, the Well-formed parentheses expressions Parentheses(k) is exactly the same in the Paths on triangle problem from (0,0) to (k,k), Paths(k,k).

We can consider ( as a step in the North direction and ) as a step in the East direction. For every well-formed parentheses expression, we start from (, and the condition for an well-formed parentheses is at any point, the number of ( is larger than ). For example, ()((())), we can see that at any index of this expression, the number of ( before the index is larger than ). These conditions correspond to the condition in the Paths on triangle model that the first step we go up, and the steps in the North direction is always larger than the steps in the East direction otherwise we are out of the triangle board. From (0,0) to (k,k), we has the same number of steps in the North and East direction which is the same that we have same amount of ( and ). Therefore we prove that Parentheses(k) is counted by Catalan numbers.


Or we explain in this way. We have p(0)=1 as there is only one way to arrange no parentheses and p(1)=1.

For n2, we can consider the well-formed parentheses expression as two part, (part1)part2 where part 1 and part 2 are splited by a pair of racket. Then we assume part 1 has k paris of parentheses and part 2 has n1k paris of parentheses. Since k[0,n1]. Therefore we have the expression pn=k=0n1pkpn1k This agrees with the formula of the catalan number.

5.3 Binary trees

A binary tree is a tree in which every internal node (in grey in above pictures) has exactly two children. Leaves (in green) have no children. The size of a binary tree is its number of internal nodes. There is one binary tree of size 1, and two binary trees of size 2, five binary trees of size 3:
Binary Tree

Figure 5.3: Binary Tree

Let tn be the number of binary trees of size n, by convention we put t0=1 (this corresponds to a leaf without any internal node).

We can prove that tn is the n-th Catalan number.

For the Catalan numbers, we have the definition that c0=1c1=1cn=k=0n1ckcn1k=c0cn1+c1cn2++cn1c0( for n2).

Which can also be rewritten as: c0=1c1=1cn+1=k=0nckcnk

For t1=1 we have zero parent so there only one case. Then we consider more general cases for n2. We imagine a tree t with n+1 nodes has one root with two subtrees as children t1 and t2. Since the root of t is a parent node, t1 and t2 must have n parent nodes together (i.e the sum of nodes for t1 and t2 is n). A subtree t1 or t2 can be empty. Therefore the ways of organize t1 which k nodes is TkTnk. Then we have i=0nTiTni Since i[0,n]. So we finish the prove that the n-th Catalan number.