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 denote the infinite “triangle”
(see the figure below).
For we denote by the number of paths such that: * the path starts at ends at and entirely lies inside * the paths only takes unit steps in the North and East directions.
For example this figure shows that :

Figure 5.1: Escalier
We write a function Paths(K,N)
which returns a table (or a matrix) of all the values of for .
# implement of the function Path
# we use the recursive method by implement the auxiliary function Path
def Path(k, n):
= [['']]
res = [[0]]
up_count = [[0]]
right_count 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])):
= res[i-1][j]
curr_path if up_count[i-1][j] < n:
+ 'up ')
res[i].append(curr_path -1][j] + 1)
up_count[i].append(up_count[i-1][j])
right_count[i].append(right_count[iif right_count[i-1][j] < k:
if right_count[i-1][j] < up_count[i-1][j]:
+ 'right ')
res[i].append(curr_path -1][j])
up_count[i].append(up_count[i-1][j] + 1)
right_count[i].append(right_count[ireturn res[-1]
def Paths(K, N):
= np.zeros((K + 1, N + 1))
result for k in range(K + 1):
for n in range(N + 1):
= len(Path(k, n))
result[k, n]
return result
print("--------- Another Test Sample ----------")
print(Paths(5, 6))
print("--------- Test Sample for Calatan number ----------")
= []
lis = Paths(5, 6)
matrix for i in range(6):
+ 1])
lis.append(matrix[i][i 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 . From the matrix we can find that is same with the sequence of the Calatan Number, which means . Now lets prove it.
First we try to discuss in a more simple model. For example, I try to compute . For each points where , we consider those points as step points, which means for example, we consider as an intermediate point, we have for all the possible paths to go through , is the multiple of . For each step point, the paths go through the step points we can consider the process of two parts, from to the step points and from step points to .
Then we can expand this model into more general cases. we try to compute , the for all step points where . For all the possible paths to , we know it go through at least one step point, since all the paths start from and has to go through cause we have to go up at the beginning. Therefore we can consider all the paths into to step. First from to the step point, which we have and from the step point to , which is equivalent to from to , we have possibilities. Then number of paths to is the multiplication of and . So we have the sum for all step points that Since and . And by the recursive formula of the Catalan number. Therefore we prove that Also we have is also Catalan number, which is easy to explain since , since from to there only on way which is go on step on direction East, so we have 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 grid. All such paths have n right and n up steps. Since we have steps in total, therefore all the possibility are binomial number .

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 steps in total and now we have up then we got right steps. Due to this reason, we reach at in the end. Because every monotonic path in the 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 And the valid paths of the Catalan paths are
5.2 Well-formed parentheses expressions
Definition 5.2
It can be shown that counts the number of expressions containing pairs of parentheses which are correctly matched. For the first values we obtain
Write a recursive function Parentheses(n)
which returns the list of all well-formed parentheses expressions with pairs of parentheses and check for different values that the list has length
As we mentioned above, the Well-formed parentheses expressions Parentheses(k)
is exactly the same in the Paths on triangle problem from to , 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 to , 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 as there is only one way to arrange no parentheses and .
For , 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 paris of parentheses and part 2 has paris of parentheses. Since . Therefore we have the expression
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 , and two binary trees of size , five binary trees of size :
Figure 5.3: Binary Tree
Let be the number of binary trees of size , by convention we put (this corresponds to a leaf without any internal node).
We can prove that is the -th Catalan number.
For the Catalan numbers, we have the definition that
Which can also be rewritten as:
For we have zero parent so there only one case. Then we consider more general cases for . We imagine a tree with nodes has one root with two subtrees as children and . Since the root of is a parent node, and must have parent nodes together (i.e the sum of nodes for and is n). A subtree or can be empty. Therefore the ways of organize which k nodes is . Then we have Since . So we finish the prove that the -th Catalan number.