From Enumerating to Generating : A Linear Time Algorithm for Generating 2 D Lattice Paths with a Given Number of Turns

We propose a linear time algorithm, called G2DLP, for generating 2D lattice L(n1, n2) paths, equivalent to two-item } B , {A 2 1 n n multiset permutations, with a given number of turns. The usage of turn has three meanings: in the context of multiset permutations, it means that two consecutive elements of a permutation belong to two different items; in lattice path enumerations, it means that the path changes its direction, either from eastward to northward or from northward to eastward; in open shop scheduling, it means that we transfer a job from one type of machine to another. The strategy of G2DLP is divide-and-combine; the division is based on the enumeration results of a previous study and is achieved by aid of an integer partition algorithm and a multiset permutation algorithm; the combination is accomplished by a concatenation algorithm that constructs the paths we require. The advantage of G2DLP is twofold. First, it is optimal in the sense that it directly generates all feasible paths without visiting an infeasible one. Second, it can generate all paths in any specified order of turns, for example, a decreasing order or an increasing order. In practice, two applications, scheduling and cryptography, are discussed.


Introduction
It is common to find ourselves confronted with problems in which an exhaustive examination of all solutions is necessary or desirable [1].However, we do not want to use brute force to go through all OPEN ACCESS possible solutions.Therefore, there is a clear need to design an efficient algorithm that can eliminate certain cases from consideration.More than this, our goal is to design an optimal algorithm, whereby all infeasible solutions can be ignored.That is, it directly generates all feasible solutions without visiting any infeasible solutions.For example, we have proposed an algorithm for generating all permutations of {1, 2, … , n} with a given number of inversions, while never visiting any of the unqualified permutations [2].In most applications of permutation generation we are interested in minimizing the total running time, not the maximum time between successive visits [1].
In this study, by utilizing the enumeration results of a previous study [3] and the method of integer partition, we propose an optimal algorithm for generating 2D lattice L(n1, n2) paths according to a given number of turns.It identically generates two-item } B , {A 2 1 n n multiset permutations according to a given number of turns.Herein, 2D lattice L(n1, n2) denotes an integer rectangular lattice that has a horizontal x-axis and a vertical y-axis.A path is a route that starts at point (0, 0) then moves through a succession of steps, usually under the step set {<1, 0>, <0, 1>}, and finally ends at the target point (n1, n2).In other words, after leaving the starting point (0, 0), the path can apply unit steps only eastward or northward, but can change direction at any point (x, y) until it reaches the target point (n1, n2).Some studies deal with an under-diagonal path that only contains points (x, y) with y x  , for example [4].Some studies deal with a higher dimensional lattice, for example [5].For a comprehensive survey on the topic of lattice paths, please see [6].
The word "multiset" (often shortened to mset) which abbreviates the term "multiple-membership set", is now the commonly accepted name for replacing "bag", "bunch", "weighted set", "occurrence set", "heap", "sample", and "fireset" [7].A multiset is a set such that each item in the set has a multiplicity that specifies how many times the item repeats, and the cardinality of a multiset is the sum of the multiplicities of its all items.In short, a multiset is a set with repeated items.Obviously, a set is a special case of multiset in which each item occurs only once.Multisets are of interest in mathematics, physics, philosophy, logic, linguistics and computer science [7].Many studies have been devoted to multiset permutations [8][9][10][11][12][13][14][15].However, we know of no published algorithms for generating multiset permutations with a given number of turns.
There are various motivations behind the interest in the turn enumeration of lattice paths.Krattenthaler described three motivations from probability, statistics, and commutative algebra, respectively [16].He also showed the wide diversity of connections and applications in other domains like combinatorics, representation theory, and q-series.The motivation of our studies on the topic of lattice path was inspired by trying to solve a kind of open shop scheduling problem that is concerned with setup time among different types of machines [17].Here, setup time means the delay time that occurs when we arrange the processing route (or path) of a job from one type of machine to another.This kind of open shop scheduling problem is, intrinsically, a multiset permutation problem, while the latter is, essentially, a lattice path enumeration problem.
Therefore, in this study, turn has three meanings.In open shop scheduling problems, it means that we transfer a job from one type of machine to another type of machine; in multiset permutation problems, it refers to two consecutive elements of a permutation that belong to two different items (In fact, the number of turns of a multiset permutation is equal to the number of blocks of a multiset permutation minus one, if we call a block as a maximal sequence of consecutive elements that are belonging to same item.Note that, in a permutation of a set of n integers, a maximal sequence of consecutive integers that appear in consecutive positions is called a block.For example, the permutation π = (456723189) contains four blocks namely 4567, 23, 1, and 89.); in lattice path enumeration problems, it means that the path changes its direction either from eastward to northward (called an EN-turn) or from northward to eastward (called an NE-turn).Viewed in this light, the number of turns of paths in a 2D lattice L(n1, n2) can be regarded in the same way as in a permutation statistics, referring to turns of a two-item } n , {e 2 1 n n multiset permutation.Here, notation "e" denotes eastward, and notation "n" denotes northward.Since we have studied the enumeration of a 2D lattice path with a given number of turns, it is natural to a step further: from enumerating to generating.
The remainder of this paper is organized as follows.In Section 2, the proposed algorithm is described.In Section 3, analyses of algorithms are discussed.In Section 4, some experimental results are presented.In Section 5, two applications, scheduling and cryptography, are discussed.Finally, conclusions are summarized and a concomitant problem is proposed for future research.

Algorithms
In this Section, we propose an optimal algorithm for generating 2D lattice L(n1, n2) paths according to a given number of turns.For completeness, the following results are introduced without proof [3].Note that, throughout the paper, all variables are positive integers.Definition 1.Let t denote the number of turns of a path that has a EN-turns and b NE-turns, that is, when first step is eastward, or denote the number of paths with a given number of turns t, then: and In the identity (1), the term of stands for the number of paths that their first step is eastward, and the term of Based on Theorem 1, we propose the Algorithm G2DLP where two Algorithms H and L, borrowed from others with some amendments and additions to meet the purpose of this study, are embedded respectively in Algorithms Partition and Permute.(The Algorithm G2DLP is put in Appendix) Algorithm Partition is based on an Algorithm H that generates all partitions of an integer [18] (p.38).Since Algorithm H treats, for example partitioning integer 11 into 4 parts, a partition of 8111 as same as another partition of 1811, we need to call Algorithm Permute to permute the partitions produced by Algorithm H. On the other hand, Algorithm Permute is based on an Algorithm L that generates all permutations of a multiset in lexicographic order [1] (pp. 39-40).Since Algorithm L assumes that the initial elements are in a non-decreasing order but Algorithm H produces partitions of an integer in a colex order (That is, lexicographic order of the reflected sequence.),we have to reverse the elements of a partition, provided by Algorithm H, in accordance with Algorithm L.
The strategy of G2DLP is divide-and-combine.According to a given number of turns t, we first delicately divide n1 and n2, the x-axis and vertical y-axis of a 2D lattice L(n1, n2), to several small parts respectively, totally 1  t parts.The division is based on the identities (1) and ( 2) and achieved by aid of an integer partition algorithm (Partition) and a multiset permutation algorithm (Permute).Then combine these small parts, by Algorithm Concatenate, to a path that is we require.
Note that, in 2D lattice L(n1, n2), from the starting point (0, 0), if the first step is eastward then totally there are 1 1  n points the path can choose that will not arrive directly to the target point (n1, n2); if the first step is northward then totally there are 1 2  n points the path can choose that will not arrive directly to the target point (n1, n2).It is also worth noting that the upper indexes and k in the lower index of Theorem 1 but we use k and k+1 in Algorithm G2DLP?The reasoning is that when we choose We will present the experimental results, in Section 4, on some cases of 2D lattice L(n1, n2) in Table 1.For a 2D lattice L (3,4), if we set 2  t , then after two callings of Algorithm Partition and those concomitant callings of Algorithm Permute, the contents of arrays perm1 and perm2 are {(1, 2), (2, 1)} and {(4)} respectively.Now, when calling Concatenate (parts, "e", "n"), we alternatively access these two array through three pickups.The first parameter of algorithm Concatenate, parts, means we have decomposed a path to parts parts.Here, since t = 2 we have parts = 3.Therefore, we need three pickups to construct a path.First, we pick up the first element of (1, 2), here is 1, then pick up the first element of (4) and finally pick up the second element of (1, 2), here is 2, thus we have one "e", four "n", and two "e", that is "ennnnee."Next, we pick up the first element of (2, 1), here is 2, then pick up the first element of (4) and finally pick up the second element of (2, 1), here is 1, thus we have two "e", four "n", and one "e", that is "eennnne."The two paths, numbered 3 and 4, can be found in Table 2, which is presented in Section 4.

Analyses
Obviously, the problem of generating 2D lattice L(n1, n2) paths according to a given number of turns is more challenging than a simple multiset permutation problem.This is because, for a simple multiset permutation algorithm, to generate permutations with a given number of turns, it requires both a time to generate all permutations and a time to perform additional comparisons and calculations to pick out those permutations with a given number of turns.Furthermore, if we want generate all paths in a specified order of turns, for example a decreasing or an increasing, we must spend additional time to sort them according to the number of turns they have.Not surprisingly, we can easily prove the following theorem.; and in case of , we have: , then after multiplying the term by Second, with regard to the Algorithm Permute, the running time is reasonably efficient [1] (p. 40).When the partition produced by Algorithm H is simply a set containing distinct elements, for example 5321 as a partition of partitioning integer 11 into 4 parts, the means of the number of comparisons by step L2 and step L3 are about 1.718 and 1.359, respectively [1] (pp. 101-102); and the average number of interchanges in step L4 is about 0.543 [1] (p. 102).On the other hand, when the partition produced by Algorithm H is a multiset, for example 7211 as a partition of partitioning integer 11 into 4 parts, the means of the number of comparisons by step L2 and step L3 are a little more complicated; even so, it is reasonably efficient.For details, see [1] (p. 102).
Finally, as to the Algorithm Concatenate, for generating a path, it only performs n_1 + n_2 concatenations (either "e" or "n").These concatenations are accomplished by parts pickups that access the two arrays, perms1 and perms2, alternatively.Moreover, since Algorithm G2DLP generates all feasible paths without visiting any infeasible ones, we claim that Algorithm G2DLP is a linear time, or CAT algorithm.An algorithm runs in constant amortized time (CAT) if the amount of computation, after a small amount of preprocessing, is proportional to the number of objects that are generated [19].What they call CAT has been called linear by Reingold, Nievergelt, and Deo [20], and in several papers [21].The experimental results, in next Section, Tables 3 and 4 and Figure 1 will support this assertion, Table 5 and Figure 2 will support the Theorem 2.

Experimental Results
In the following, we present path distributions with respect to their number of turns on some cases of two-item } n , {e To confirm the claim that the Algorithm G2DLP is a linear time algorithm, we perform a series of experiments and present the results in Table 3 and Figure 1.The running time (h: m: s) is for generating only but without printing out the paths, by using an Acer ® notebook with an Intel® Core™ i5-4200U CPU @ 1.6 GHz and a VBA program executed under the Microsoft Excel environment.Based on the experimental results in Table 3 and Figure 1, we confirm that Algorithm G2DLP is a linear time algorithm.In order to highlight the superiority of Algorithm G2DLP, we perform a series of experiments and present the running times for generating 2D lattice L (15,15) paths, according to the number of turns, in the following Table 4.Note that we skip the two trivial paths that with number of turns 1.From the results of Table 4, it is easy to know that, on average, the running time of Algorithm G2DLP for generating a path is almost a constant (mean: seconds).This result confirms again that Algorithm G2DLP is a linear time algorithm.Table 4. Running Time of G2DLP for 2D lattice L (15,15).

Number of Paths
Running Time (h:m:s)

Number of Paths
Running Time (h:m:s) 2 28  0:00:00  0:00:00 * Please refer to the running time listed at the bottom row of Table 5.
To confirm the assertion of Theorem 2, we perform a series of experiments, by using a simple multiset permutation algorithm Permute + .Here, we only include the L2, L3, L4 parts of Algorithm Permute, and substitute L1 part with a loop of statements to perform comparisons and calculations to pick out those permutations according to a given number of turns.The running times for generating 2D lattice L(n1, n2) paths, with 2 1 n n  according to a given number of turns are presented in the following Table 5 and Figure 2. Based on the experimental results in Table 5 and Figure 2, we have supported the assertion of Theorem 2.

Applications
Lattice paths have been applied in several areas, for example to solve interesting and nontrivial problems in the theory of queues [22].In addition, scholars engaged in cryptography have long been aware of the importance of lattices and lattice reduction in cryptography, both for cryptographic construction and cryptographic analysis [23].Kaparthi [5] points out that, it has been shown, by [24], that lattice path enumeration problems are equivalent to the partial orders enumeration problems that have applications in the area of computer science in deciding the efficiency of sorting algorithms [25], and in social choice theory in studying various kinds of discrete ranking structures, and preference orders among decision makers [26].Here, we propose two potential applications as follows.

Scheduling
As mentioned in Introduction, the motivation of this study was inspired by trying to solve a kind of open shop scheduling problem that is concerned with setup time among different types of machines [17].Here, setup time means delay time that occurs when we arrange the processing route (or path) of a job from one type of machine to another type of machine.In scheduling problems, if the paths of jobs are fixed beforehand, and are the same for all jobs, it is called a flow-shop [27]; on the other hand, if the paths of jobs are not given in advance, but chosen by the scheduler, it is called an open-shop [28].What we are trying to solve is an open shop scheduling problem that comprised a set of J jobs, {1, 2, …, J}, and n machines with two types of A and B. That is, every job must pass through all n machines for each machine once.
Assume that n1 and n2 be the number of machines of type A and B respectively, and that π π π π j j j jn   be a path of job j where π jk , for all }, , , can be a symbol of "A" or "B" that stands for the k th operation of job j is processed by a machine of type A or B, respectively.To be a valid path, obviously, in a path π j of a job j there must have n1 A's, and n2 B's.For example, if different feasible schedules can be chosen by the scheduler.How to choose a schedule depends on what objective the scheduler aims at.
One widely used objective is to minimize the make span, the time needed to complete all jobs.Traditionally, it is common to assume that, when its previous operation has been completed by a machine, a job is immediately available to be processed by another machine.However, in practice, there is often a significant time delay between the completion of an operation and the start of the next operation of the same job.Furthermore, in some cases, the processing times might be negligible compare to the delay times.Therefore, they have a small influence on the make span [29].Several real world applications can be found in [30].The open shop scheduling problem with time delays was first introduced in [31], and shown to be NP-hard even for two machines and identical time delays.
Let d stand for the delay times needed to transfer a job from a machine of type A (or B) to a machine of type B (or A), and Dj stand for the total delay times needed for a job j to complete its path.Here, we assume that transfer a job between two machines of the same type does not cause any delay time.Without  Our objective is to minimize the total delay times of all jobs, that is, to minimize .D is minimized.Note that in the multiset permutation, we call there is a turn if two consecutive elements are belonging to two different items.That is, a turn occurs if we transfer a job from a machine of type A (or B) to a machine of type B (or A).This means that the delay time of a job that goes through a path with t turns is t.
Being a scheduler, a reasonable strategy for us is to choose, as soon as possible, those feasible and available paths that possess minimum turns.To do that, we are confronted with two issues.The first one is to answer the question of what is the number of permutations that possess a given number of turns, the second one is to design an algorithm that can generate the paths in a non-decreasingly order of turns.The first question has been resolved in [3], and now we have answered the second question.Therefore, the result of this study can provide for a scheduler as a good start to plan his scheduling.

Cryptography
The proposed algorithm can be used in cryptography.For example, we can combine the G2DLP with a substitution cipher system to become a product cipher system [32], [33]  , as a secret key to choose a permutation of {A, B, …, Z, ",", ".", ""} as a substitution Σ.This can be accomplished by an unranking algorithm that we proposed in a previous study [34].For example, if we select 30 2291 1316475575 .7 κ   E as a secret key, then by using the unranking algorithm, we can obtain a substitution Σ as the following Table 6.Table 6.A substitution Σ of 29 characters.By using the substitution Σ, we can encrypt a plaintext Ψ of l characters into a cipher text Ω1 of l numbers that each one is an integer between 1 and 29.Then, by using the G2DLP, we can choose two integers, for example  The characteristic of the proposed cryptosystem is threefold.First, to be a polyalphabetic cryptosystem [33] (p.13), we can run the unranking algorithm several times, each time use the same secret key κ and the latest generated substitution Σ that is initialized as {A, B, …, Z, ",", ".", ""} at first.Therefore, as being a polyalphabetic cryptosystem, it can exempt from the cryptanalysis that is based on the statistical properties of usage frequencies of English alphabet [33]  Ω2, then we can encrypt the character "P" into one of 20612592 different strings of 30 bits, and encrypt the character "M" into one of 18036018 different strings of 30 bits.This is because, see Table 6, the letter "P" represents the number 16, and the character "M" represents the number 13; and in a 2D lattice L (15, 15), see Table 4, there are totally 20612592 different paths possess 16 turns, and 18036018 different paths possess 13 turns.If someone tries to break the cryptosystem but he does not know the decryption scheme, this will make his attack very difficult.Third, it is easy to implement the proposed cryptosystem to become a non-synchronous stream cipher [33] (p.24).That is, we can use the unranking algorithm as a key stream generator that depends not only on the secret key κ but also on the plaintext itself.This strategy can greatly increase the complexity of breaking the proposed cryptosystem.Based on the characteristics mentioned above, we can say that the proposed cryptosystem can be an applicable and safe system.

Conclusions
Basically, the problem of generating 2D lattice L(n1, n2) paths, equivalent to two-item multiset } B , {A We propose an efficient Algorithm G2DLP that significantly reduces this time complexity to a linear order.
The principle behind Algorithm G2DLP is divide-and-combine.According to a given number of turns t, we first delicately divide n_1 and n_2, the horizontal x-axis and vertical y-axis of a 2D lattice L(n1, n2), to several small parts respectively, totally 1  t parts.The division is based on the enumeration results of a previous study and is achieved by aid of an integer partition algorithm (Partition) and a multiset permutation algorithm (Permute).Then combine these small parts, by a concatenate algorithm (Concatenate), to the paths we require.The experimental results confirm that Algorithm G2DLP is a linear time, or CAT (constant amortized time), algorithm.That is, its amount of computation, after a small amount of preprocessing, is proportional to the number of paths it generates.
The advantage of G2DLP is twofold.First, it is optimal in the sense that it directly generates all feasible paths without visiting any infeasible path.Second, it can generate all paths in any specified order of turns, for example, a decreasing order or an increasing order.In practice, the result of this study can provide a good starting point for a scheduler attempting to deal with an open shop scheduling problem, and can be used as an encryption scheme of a cryptosystem.Finally, a concomitant problem, how to design an algorithm that can generate 3D lattice L(n1, n2, n3) paths according to a given number of turns, is proposed for future research.

Theorem 2 .
The time complexity of a simple multiset permutation algorithm for generating twopermutations according to a given number of turns is )

D
Since a path of a job can be viewed as a permutation of a two-item } paths of a job can be obtained by an algorithm that can generate all feasible permutations of this multiset.The question is how to choose J permutations, from the !
pair of secret keys to encrypt the cipher text Ω1 into a new cipher text Ω2 of strings of ) ( 2 1 n n  bits.On the other hand, we can use a decoder to compute the number of turns of each string to obtain the cipher text Ω1 of l numbers.Finally, by using the substitution Σ -1 , we can transfer the cipher text Ω1 into the plaintext Ψ of l characters.The cryptosystem mentioned above is presented as following Figure 3.
to a given number of turns is more challenging than a simple multiset permutation problem.The time complexity of a simple multiset permutation algorithm for generating two

Table 1 .
(3,4) then, all paths of the 2D lattice L(3,4)generated by G2DLP are presented, according to the number of turns, in Table2.

Table 1 .
The path distributions with respect to their number of turns.

Table 3 .
Running Time of G2DLP vs.Total Paths.

n1 + n2 Number of Turns Number of Paths Total Paths Running Time (h:m:s)
please refer to the data in Table4where the number 23557248 is the maximum.
**Note that, although there are some differences in the running time for generating the paths with a different number of turns, but the difference is very small.For example, in the case of 30  n even for generating the paths with a given number of turns 2, only 28 paths, its running time is 0:17:54.Therefore, for each case of n, we only run Permute + for generating the paths with a given number of turns 1 n that will have the maximum number of paths; for example, in the case of 30  n different feasible paths for a job.If we assume that there are no two jobs with the same path, there are totally (p. 27).Second, the proposed cryptosystem can encrypt a character into one of different numbers of strings.For example, if n as a pair of secret keys to encrypt the cipher text Ω1 into a new cipher text