Appendix G
C++ and Maple
TM
Programs
Sections G.1 through G.4 contain C++ programs to count the number of
compositions and words, respectively, that avoid either a single pattern en-
tered by the user or all subsequence patterns τ of a fixed length. Section
G.5 contains a C++ and a Maple program. The C++ program TOU
AUTO
computes the states and the transfer matrix of the automaton for avoidance of
subsequence patterns in words. The Maple program TOU
FORMULA com-
putes the first few values and an explicit formula for the sequence {AW
τ
[k]
(n)}
from the transfer matrix obtained as output of the C++ program. These pro-
grams, as well as Java versions of the programs in Sections G.1 and G.3 can be
downloaded from the author’s website http://math.haifa.ac.il/toufik/
manbook.html. The Java versions are provided for readers that do not have
access to a C++ compiler, but they are slower than the C++ versions and
are not suitable for massive computation.
G.1 Program to compute AC
τ
N
(n) for given τ
This program computes the number of compositions of n for n =1, 2,...,20
that avoid a pattern entered by the user. If the pattern is a subsequence
pattern of length three, four, five, or six then the functions vpatterns3(),
vpatterns4(), vpatterns5(),andvpatterns6() are to be used. For a sub-
word pattern of length dp the relevant function is swpatterns(dp), while for
generalized patterns of length four we use gpatterns112(), gpatterns121(),
and gpatterns22(dp). Note that the routine gpatterns22 is more general as
it enumerates the compositions that avoid the generalized pattern (dp − 2, 2)
for dp ≥ 4. The routine gpatterns22(dp) illustrates how the user might
modify the other routines for generalized patterns of length four to deal with
generalized patterns of length greater than four. The program also contains
two routines that check whether a pattern occurs: testK for subsequence
patterns and testW for the other two types of patterns.
The routine builtcomps recursively creates all compositions of n and in-
creases the count if the composition avoids the pattern p. The user can adjust
the type of patterns for which the program checks by making the relevant
399
© 2010 by Taylor and Francis Group, LLC