This section provides a detailed description of the main stages in the development of combinatorial generation algorithms for directed lattice paths. First, we consider simple directed lattice paths, and then generalize the obtained results to directed lattice paths.
3.1. Simple Directed Lattice Paths
Theorem 1. The number of all simple directed lattice paths that begin at , end at and consist of steps in the set can be calculated using the following recurrence:where and for . Proof. Let us consider the end point of a directed lattice path. Based on the given set of possible steps , the end point can be reached from the following k points:
- 1.
From to by performing the step ;
- 2.
From to by performing the step ;
- ⋮
- k.
From to by performing the step .
Figure 1 demonstrates a set of possible steps
and all options for reaching the point
using one step from
S.
There is only one way to reach the point
from the point
: to achieve this, it is necessary to perform step
. The number of all simple directed lattice paths that begin at
, end at
and consist of steps in the set
is equal to
. To calculate the number of all simple directed lattice paths that begin at
and end at
, it is necessary to sum the number of all simple directed lattice paths from
to each point
,
. Therefore, we obtain the desired recurrence (
1).
Next, we define the initial conditions for the obtained recurrence (
1):
- 1.
The value of shows the number of all simple directed lattice paths that begin at and end at . There is only one such lattice path: the empty lattice path that does not contain any performed steps. Therefore, we obtain the first initial condition ;
- 2.
Since the simple directed lattice path begins at and is true for each step , it is impossible to reach the point , where . Therefore, we obtain the second initial condition for .
Figure 2 demonstrates a set of possible steps
and all possible and impossible steps for a simple directed lattice path that begins at
and ends at
.
Hence, the obtained recurrence (
1) with the defined initial conditions can be applied for calculating the number of all simple directed lattice paths that begin at
, end at
and consist of steps in the set
. □
The initial conditions for
can be slightly improved by adding the following condition that reduces the number of recursive calls when calculating
:
In addition, we can use the following initial condition, which also reduces the number of recursive calls when calculating
:
Figure 3 demonstrates the set of possible steps
and all recursive calls when applying (
1) to calculate
without (
Figure 3a) and with (
Figure 3b) the use of (
2) and (
3).
The obtained recurrence (
1) with the use of the initial conditions (
2) and (
3) satisfies the requirements of the method for developing combinatorial generation algorithms based on AND/OR trees.
Figure 4 shows the corresponding AND/OR tree structure for
. In this tree, the root, labeled
, is an OR node with
k child nodes labeled
i (
). This structure matches the summation in (
1). Each node labeled
i has only one child node, labeled
, that corresponds to the summand in the summation in (
1). The subtree of the node labeled
, which is shown as a node with a triangle, is constructed in a similar way for the new values of parameters
n and
m; i.e., a recursive call is needed. Such recursive calls stop when some initial condition for
are satisfied:
If we obtain a node where , then it is a leaf node;
If we obtain a node where , then it is necessary to remove this node and its parent node labeled i.
According to the applied method [
34], the total number of variants of the AND/OR tree structure for
is equal to
. The constructed tree structure does not contain AND nodes, since there are no multiplications in the recurrence (
1). Therefore, a variant of such an AND/OR tree is a path from the root to a leaf node. A bijection between the considered set of all simple directed lattice paths and the set of all variants of the constructed AND/OR tree structure is defined by the following rules:
Selecting a child of the node labeled (each child is labeled with a specific value of the parameter i) corresponds to selecting one of the possible steps when reaching the point , i.e., . The subtree of the selected child node determines the remaining part of the simple directed lattice path from to . It is also important to note that it is necessary to fix some order on the set of possible steps S, because the AND/OR tree is an ordered structure;
A leaf node labeled , where , means reaching the point from by performing n steps of the form , i.e., ;
A leaf node labeled , where , means reaching the point from by performing n steps of the form , i.e., ;
A leaf node labeled means reaching the starting point ; i.e., there are no additional steps required.
Figure 5 shows the corresponding AND/OR tree structure for
with the set of possible steps
. This structure is constructed based on (
1) with the use of (
2) and (
3). The total number of its variants is equal to
, which corresponds to the following simple directed lattice paths:
,
,
,
,
,
.
Since the constructed tree structure does not contain AND nodes and the choice between children of OR nodes is associated with the choice of feasible solutions when constructing lattice paths. This tree structure is similar to the state space tree obtained by applying the backtracking method [
30]. Traversing this tree allows for exhaustive generation of the considered simple directed lattice paths. However, in contrast to the backtracking method, the applied method [
34] makes it possible to obtain other classes of combinatorial generation algorithms for the set of simple directed lattice paths.
According to the applied method for developing combinatorial generation algorithms based on AND/OR trees, we obtain algorithms for ranking (Algorithm 1) and unranking (Algorithm 2) simple directed lattice paths that begin at , end at and consist of steps in the set . In these combinatorial generation algorithms, denotes an empty lattice path and denotes the concatenation of lattice paths; i.e., returns the lattice path .
Algorithm 1: An algorithm for ranking a simple directed lattice path that begins at , ends at and consists of steps in the ordered set . |
|
Algorithm 2: An algorithm for unranking a simple directed lattice path that begins at , ends at and consists of steps in the ordered set . |
|
Algorithms 1 and 2 have at most
n recursive calls, where each recursive call requires calculations of
maximum
k times. Hence, Algorithms 1 and 2 have a time complexity of
, where
is a function that describes the increase in the time of calculating value
depending on the values of parameters
n and
m, as well as the set of possible steps
S. If we use recurrence (
1) for calculating
, which has time complexity
, we obtain time complexity
for ranking and unranking algorithms. The time complexity of the developed combinatorial generation algorithms can be improved by using formulas for calculating
that are more efficient in terms of computational complexity. Such formulas can be found by considering simple directed lattice paths with a specific set of possible steps.
Table 1 presents an example of applying Algorithm 1 for ranking and Algorithm 2 for unranking simple directed lattice paths that begin at
, end at
and consist of steps in the ordered set
.
Thus, using the developed algorithms, it is possible to rank any simple directed lattice paths, as well as generate a specific lattice path according to a given rank. Moreover, some lattice paths can be transformed to the form of simple directed lattice paths. For example, we can consider north-East lattice paths [
37]. A north–east lattice path is a lattice path that begins at
, ends at
and consists of steps in the set
. The step
is called the north step and the step
is called the east step. The number of all north–east lattice paths can be calculated using the following explicit formula:
North–east lattice paths are not directed lattice paths. If we change the basis and consider another coordinate system (red
-axis and
-axis in
Figure 6), then the north–east lattice paths are transformed into simple directed lattice paths. The obtained simple directed lattice paths begin at
, end at
, and consist of steps in the set
in the new coordinate system. The connection between the new and old coordinate systems is stated in the following formulas:
As a result, we obtain the set of possible steps that satisfy the requirements of simple directed lattice paths.
The number of all simple directed lattice paths that begin at
, end at
and consist of steps in the set
is equal to
. Hence,
Applying Algorithms 1 and 2 for the considered simple directed lattice paths, we obtain algorithms for ranking and unranking north–east lattice paths. For example, to rank a north–east lattice path
,
that begins at
and ends at
, it is necessary to change the basis by using (
5), obtain the corresponding simple directed lattice path
,
, and execute
According to Theorem 1, for the case
, we obtain
If we apply (
7) when executing Algorithm 3 or Algorithm 4, we obtain a time complexity of
. A significant improvement will be obtained if we use explicit formula (
4) in (
6):
Algorithm 3: An algorithm for ranking a simple directed lattice path that begins at , ends at and consists of steps in the ordered set . |
|
Algorithm 4: An algorithm for unranking a simple directed lattice path that begins at , ends at and consists of steps in the ordered set . |
|
Then, we obtain a polynomial time complexity for Algorithms 3 and 4. The results of computational experiments using software implementations for Algorithms 3 and 4 confirm the theoretical time complexity.
3.2. Simple Directed Lattice Paths with Restrictions
Various restrictions can be applied to the generated lattice paths. There may be restrictions of the following type: fixing the end point of the lattice path, prohibiting going beyond certain boundaries, calculating some statistics of the lattice path, etc. For example, the following four types of directed lattice paths are distinguished in [
17]:
- 1.
Walk: a directed lattice path that ends at , where m is not specified;
- 2.
Bridge: a directed lattice path that ends at ;
- 3.
Meander: a directed lattice path that ends at , where m is not specified and never falls below the x-axis;
- 4.
Excursion: a directed lattice path that ends at and never falls below the x-axis.
Next, we consider these types of lattice paths for simple directed lattice paths. Simple walks and bridges are the special cases of the simple directed lattice paths from Theorem 1. Therefore, we can calculate the number of all simple walks or bridges through .
Corollary 1. The number of all simple walks that begin at , end at , where m is not specified, and consist of steps in the set can be calculated using the following formula: Proof. Simple walks differ from the simple directed lattice paths from Theorem 1 in that
m is not specified. Therefore, we need to consider all possible values of
m and sum up the number of simple directed lattice paths for each of them. The minimum possible value of
m will be obtained if all
n steps of the form
are performed in a simple directed lattice path, where
. The maximum possible value of
m will be obtained if all
n steps of the form
are performed in a simple directed lattice path, where
. Combining all the conditions, we obtain the desired formula (
9). □
Corollary 2. The number of all simple bridges that begin at , end at and consist of steps in the set can be calculated using the following formula: To calculate the number of simple meanders and excursions, it is necessary to prevent them from falling below the
x-axis. Therefore, we add the appropriate initial condition to recurrence (
1) and obtain the following theorem:
Theorem 2. The number of all simple directed lattice paths that begin at , end at , consist of steps in the set and never fall below the x-axis can be calculated using the following recurrence:where , for and for . Proof. In contrast to Theorem 1, for the considered simple directed lattice paths, it is impossible to reach the point , where . Therefore, we obtain an initial condition that for . □
Since the considered simple directed lattice paths never fall below the
x-axis, only the following additional initial conditions can be added to
:
According to the applied method for developing combinatorial generation algorithms based on AND/OR trees, we obtain algorithms for ranking (Algorithm 5) and unranking (Algorithm 6) simple directed lattice paths that begin at , end at , consist of steps in the set and never fall below the x-axis.
Algorithm 5: An algorithm for ranking a simple directed lattice path that begins at , ends at , consists of steps in the ordered set and never fall below the x-axis. |
|
Algorithm 6: An algorithm for unranking a simple directed lattice path that begins at , ends at , consists of steps in the ordered set and never fall below the x-axis. |
|
Simple meanders and excursions are the special cases of the simple directed lattice paths from Theorem 2. Therefore, we can calculate the number of all simple meanders and excursions through .
Corollary 3. The number of all simple meanders that begin at , end at , where m is not specified, consist of steps in the set and never fall below the x-axis can be calculated using the following formula: Proof. The proof is similar to the proof of Corollary 1. □
Corollary 4. The number of all simple excursions that begin at , end at , consist of steps in the set and never fall below the x-axis can be calculated using the following formula: As an example, we consider the applications of Algorithms 5 and 6 for Dyck paths [
7]. A Dyck
n-path is a lattice path that begins at
, ends at
, consists of steps in the set
and never rises above the diagonal
. The number of all Dyck
n-paths is equal to the Catalan numbers
(the sequence
in the OEIS [
38]).
Dyck paths are not directed lattice paths. If we change the basis and consider another coordinate system (red
-axis and
-axis in
Figure 7), then the Dyck paths are transformed into simple directed lattice paths. The obtained simple directed lattice paths begin at
, end at
, consist of steps in the set
and never fall below the
-axis in the new coordinate system.
The number of all simple directed lattice paths that begin at
, end at
, consist of steps in the set
and never fall below the
-axis is equal to
. Hence,
. Applying Algorithms 5 and 6 for the considered simple directed lattice paths, we obtain algorithms for ranking and unranking Dyck paths. For example, to rank a Dyck path
,
, that begins at
and ends at
, it is necessary to change the basis, obtain the corresponding simple directed lattice path
,
, and execute
3.3. Directed Lattice Paths
All obtained results on recurrent formulas and combinatorial generation algorithms for simple directed lattice paths can be generalized to the case of directed lattice paths. Similar results in obtaining recurrences for directed lattice paths were obtained in [
39] (Example 8).
Theorem 3. The number of all directed lattice paths that begin at , end at and consist of steps in the set can be calculated using the following recurrence: Proof. The proof is similar to the proof of Theorem 1. □
Theorem 4. The number of all directed lattice paths that begin at , end at , consist of steps in the set and never fall below the x-axis can be calculated using the following recurrence: Proof. The proof is similar to the proof of Theorem 2. □
The obtained recurrences (
10) and (
11) also satisfy the requirements of the method for developing combinatorial generation algorithms based on AND/OR trees. Thus, this makes it possible to obtain combinatorial generation algorithms for directed lattice paths. Algorithms 7 and 8 present the obtained algorithms for ranking and unranking directed lattice paths from Theorem 3. Moreover, if a lattice path can be transformed into a directed lattice path, then combinatorial generation algorithms can also be obtained for it. For example, next, we consider the transformation of several well-known lattice paths into directed lattice paths.
Algorithm 7: An algorithm for ranking a directed lattice path that begins at , ends at and consists of steps in the ordered set . |
|
Algorithm 8: An algorithm for unranking a directed lattice path that begins at , ends at and consists of steps in the ordered set . |
|
A Delannoy path is a lattice path that begins at
, ends at
and consists of steps in the set
. The number of all Delannoy paths is equal to the Delannoy numbers
(sequence
in the OEIS [
38]). Delannoy paths are not directed lattice paths. If we change the basis and consider another coordinate system (red
-axis and
-axis in
Figure 8), then the Delannoy paths are transformed into directed lattice paths. The obtained directed lattice paths begin at
, end at
and consist of steps in the set
in the new coordinate system.
The number of all directed lattice paths that begin at , end at and consist of steps in the set is equal to . Hence, . Applying ranking and unranking algorithms for the considered directed lattice paths, we obtain appropriate algorithms for Delannoy paths.
A Schroder path is a lattice path that begins at
, ends at
, consists of steps in the set
and never rises above the diagonal
. The number of all Schroder paths is equal to the Schroder numbers
(sequence
in the OEIS [
38]). Schroder paths are not directed lattice paths. If we change the basis and consider another coordinate system (red
-axis and
-axis in
Figure 9), then the Schroder paths are transformed into directed lattice paths. The obtained directed lattice paths begin at
, end at
, consist of steps in the set
and never fall below the
-axis in the new coordinate system.
The number of all directed lattice paths that begin at , end at , consist of steps in the set and never fall below the x-axis is equal to . Hence, . Applying ranking and unranking algorithms for the considered directed lattice paths, we obtain appropriate algorithms for Schroder paths.
A Motzkin path is a lattice path that begins at
, ends at
, consists of steps in the set
and never rises above the diagonal
. The number of all Motzkin paths is equal to the Motzkin numbers (sequence
in the OEIS [
38]). Motzkin paths are not directed lattice paths. If we change the basis and consider another coordinate system (red
-axis and
-axis in
Figure 10), then the Motzkin paths are transformed into directed lattice paths. The obtained directed lattice paths begin at
, end at
, consist of steps in the set
and never fall below the
-axis in the new coordinate system. Applying ranking and unranking algorithms for the considered directed lattice paths, we obtain appropriate algorithms for Motzkin paths.