A Dyck path of semilength \(n\) is a sequence of \(2n\) steps, each either an up step, \((1, 1)\), or a down step, \((1, -1)\), which starts at \((0, 0)\), ends at \((2n, 0)\) and never passes below the \(x\)-axis. Equivalently, it has \(n\) up steps and \(n\) down steps, and no initial segment of it has more down steps than up steps. Writing / for an up step and \ for a down step,
/\/\
/\/ \
is a Dyck path of semilength 4, whereas
/\/\/
/\/
and
/\ /\/\
\/
are not: the first ends two levels above its start, since it has five up steps and only three down steps, and the second, which has four of each, passes below the axis.
Written with ( for an up step and ) for a down step, a Dyck path becomes a Dyck word, a string of balanced brackets: the first path above is ()(()()). The height of the path after each step is the number of brackets still open, which is the counter j of the function check_parentheses in the post on parenthesis matching in Python, and the conditions on a Dyck path are those for the brackets to match: the counter never falls below zero, and it ends at zero. The paths take their name from the Dyck language of formal language theory, the set of Dyck words, which is named after the German mathematician Walther von Dyck (1856–1934), one of the founders of combinatorial group theory.
Catalan numbers
The number of Dyck paths of semilength \(n\) is the \(n\)th Catalan number,
Starting from \(C_0 = 1\), which counts the empty path, the Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430, … (sequence A000108 in the OEIS). They count a remarkable variety of other things: Stanley's book on them lists 214 [R. P. Stanley, Catalan Numbers, Cambridge University Press (2015), doi:10.1017/CBO9781139871495], among them the ways of bracketing a product of \(n + 1\) factors, the full binary trees with \(n + 1\) leaves (described below) and the triangulations of a convex polygon with \(n + 2\) sides (its divisions into triangles by diagonals that do not cross). The numbers are named after the Belgian mathematician Eugène Catalan (1814–1894), who counted the bracketings of a product in 1838. Euler had already counted the triangulations, in a letter to Goldbach in 1751, and the Mongolian astronomer Minggatu had met the numbers in power series for trigonometric functions in the 1730s; the name became standard only after John Riordan used it in 1968 [I. Pak, "History of Catalan numbers", arXiv:1408.5711, also an appendix to Stanley's book].
Dividing the paths by their number of peaks splits \(C_n\) into the Narayana numbers,
the numbers of Dyck paths of semilength \(n\) with \(k\) peaks: for \(n = 4\), these are 1, 6, 6 and 1, the rows of the figure above.
The Catalan numbers grow rapidly. By Stirling's approximation, \(\binom{2n}{n} \approx 4^n/\sqrt{\pi n}\) for large \(n\), and so
which overestimates \(C_{10}\) by 11 per cent and \(C_{100}\) by 1 per cent.
Reflection principle
The formula for \(C_n\) follows from the reflection principle. A path of \(2n\) steps with \(n\) up steps and \(n\) down steps is fixed by the choice of which of its steps go up, and so there are \(\binom{2n}{n}\) such paths. Those that are not Dyck paths, the bad paths, each reach the line \(y = -1\). The part of a bad path after its first visit to this line rises by one level overall, from \(y = -1\) to \(y = 0\); reflecting this part in the line, which exchanges its up and down steps, makes it fall by one level instead, and so gives a path from \((0, 0)\) to \((2n, -2)\), with \(n - 1\) up steps and \(n + 1\) down steps.
Conversely, a path from \((0, 0)\) to \((2n, -2)\) must reach the line \(y = -1\), and reflecting its part after its first visit to the line gives a bad path. Neither reflection changes the part of a path before its first visit, and so each undoes the other: the reflection is a bijection (a one-to-one correspondence) between the bad paths and the paths that end at \(y = -2\), of which there are \(\binom{2n}{n+1}\), the number of ways of choosing which \(n + 1\) of the steps go down. Hence
The reflection principle is usually attributed to Désiré André, who solved Bertrand's ballot problem in 1887, but André's solution used a different bijection [M. Renault, American Mathematical Monthly 115, 358–363 (2008), doi:10.1080/00029890.2008.11920537], and the reflection itself appears to be due to Dmitry Mirimanoff, in 1923, as Pak's history relates.
First-return decomposition
A non-empty Dyck path returns to the axis for the first time after some even number of steps, \(2i + 2\). Up to that point, it consists of an up step, a Dyck path \(A\) of semilength \(i\), raised by one level, and a down step; after it comes another Dyck path, \(B\), of semilength \(n - 1 - i\). In brackets, every non-empty Dyck word is \((A)B\) for exactly one pair of Dyck words \(A\) and \(B\), either of which may be empty, and every such pair gives a Dyck word. Summing over the position of the first return gives the recurrence
known as Segner's recurrence, after Johann Segner, who derived it for the triangulations of polygons in 1758.
The same decomposition gives the correspondence with full binary trees, in which every node has either two children or none (a leaf). The empty path corresponds to a single leaf, and the path \((A)B\) to a tree whose root has the trees of \(A\) and \(B\) as its left and right subtrees. Each pair of matching brackets becomes a node with children, and so a Dyck path of semilength \(n\) corresponds to a tree with \(n\) such nodes and \(n + 1\) leaves (a full binary tree always has one more leaf than it has nodes with children).
(()(()))(), made up of an up step and the down step of its first return to the axis (black) around a Dyck path \(A\) (blue), raised by one level, followed by a Dyck path \(B\) (red), and its full binary tree, whose root (black) has the trees of \(A\) and \(B\) as its subtrees. Circles are nodes with children, and squares are leaves.Program
The program dyck_paths.py, listed below, generates the Dyck paths of semilength \(n\) step by step. The generator dyck_paths extends a partial path by an up step if it has fewer than \(n\) of them and by a down step if it is above the axis, and yields the path once it has \(2n\) steps; since it tries the up step first, the paths come out in the lexicographic order of their Dyck words, with ( before ). The function draw_mountains draws a path in text, row by row from the top: an up step is drawn in the row of the level it starts from and a down step in the row of the level it ends at, so that each row holds the steps between two levels. Run with \(n = 3\),
python3 dyck_paths.py 3
the program prints the five Dyck paths of semilength 3, each above its Dyck word, and checks their number against the formula:
/\
/ \
/ \
((()))
/\/\
/ \
(()())
/\
/ \/\
(())()
/\
/\/ \
()(())
/\/\/\
()()()
Number of paths: 5; C(3) = 5
Since \(C_n\) grows as \(4^n/n^{3/2}\), the program is practical only for small \(n\): for \(n = 15\), it would print \(C_{15} = 9\,694\,845\) paths.
"""Generate the Dyck paths of semilength n, given on the command line, and
print each one drawn in text above its Dyck word, then the number of paths
and the Catalan number C(n)."""
import sys
from math import comb
def dyck_paths(n, path="", ups=0, height=0):
"""Generate the Dyck paths of semilength n as strings of / (an up step)
and \\ (a down step). The recursion continues a partial path, which has
ups up steps and ends at the given height."""
if len(path) == 2 * n:
yield path
return
if ups < n:
# An up step, if any are left.
yield from dyck_paths(n, path + "/", ups + 1, height + 1)
if height > 0:
# A down step, if the path is above the axis.
yield from dyck_paths(n, path + "\\", ups, height - 1)
def draw_mountains(path):
"""Return the path drawn in text, as lines of / and \\ from the top."""
rows, height = {}, 0
for k, step in enumerate(path):
# An up step is drawn in the row of the level it starts from, and a
# down step in the row of the level it ends at.
if step == "\\":
height -= 1
rows.setdefault(height, [" "] * len(path))[k] = step
if step == "/":
height += 1
return "\n".join("".join(rows[h]).rstrip()
for h in sorted(rows, reverse=True))
def dyck_word(path):
"""Return the path written as a string of balanced brackets."""
return path.replace("/", "(").replace("\\", ")")
if __name__ == "__main__":
n = int(sys.argv[1])
count = 0
for path in dyck_paths(n):
print(draw_mountains(path))
print(dyck_word(path), end="\n\n")
count += 1
print(f"Number of paths: {count}; C({n}) = {comb(2 * n, n) // (n + 1)}")