blib/lib/Math/DyckWords.pm

filter: Statementstmt 98.3% 121 / 123 Branchbran 92.5% 50 / 54 Conditioncond 60.7% 31 / 51 MC/DCmcdc 42.3% 11 / 26 Subroutinesub 100% 16 / 16 Podpod 88.8% 8 / 9 totaltotal 84.9% 237 / 279 CC 46 SCAR 25.1
CC
46
Cov
89%
monotonic_path_count
32.6
dyck_words_by_lex
14.1
dyck_words_by_position
6.0
CRAP 12.3
↑ Summary
1 #######################################################################
2 # $Id: DyckWords.pm,v 1.2 2010/04/14 03:41:06 mmertel Exp $
3  
4 =head1 NAME
5  
6 Math::DyckWords - Perl module for generating Dyck words. Dyck words
7 are named after the mathematician Walther von Dyck.
8  
9 =head1 SYNOPSIS
10  
11 use Math::DyckWords qw( dyck_words_by_lex
12 dyck_words_by_position
13 dyck_words_by_swap
14 ranking
15 unranking
16 catalan_number );
17  
18 @words = dyck_words_by_lex( 4 );
19 @words = dyck_words_by_position( 4 );
20 @words = dyck_words_by_swap( 4 );
21 $rank = ranking( '01010101' );
22 $word = unranking( 3, 2 );
23  
24 =head1 DESCRIPTION
25  
26 Dyck words are even numbered string of X's and Y's, or 0's and 1's,
27 or any other binary alphabet for that matter, such that no initial
28 segment has more Y's or 1's. The following are the Dyck words of
29 length 2n where n = 3:
30  
31 000111
32 010011
33 010101
34 001101
35 001011
36  
37 Another common use of Dyck words is in dealing with the balanced
38 parenthesis problem. Substituting the left and right parentheses
39 for the 0's an 1's listed above we have:
40  
41 ((()))
42 ()(())
43 ()()()
44 (())()
45 (()())
46  
47 There is also a relationship between Dyck words and Catalan numbers.
48 Catalan numbers have many applications in combinatorics and consists
49 of a sequence of ever increasing integers following the formula:
50  
51 (2n)!/(n!(n+1)!)
52  
53 The first few numbers in the Catalan sequence are:
54  
55 1, 1, 2, 5, 14, 132, 429, 1430, 4862, 16796, 58786, 208012
56  
57 The relationship between Dyck words and the Catalan sequence can
58 be easily seen as the nth Catalan number is equal to the number of
59 permutations, or unique Dyck words of length 2n. For example,
60 the 3rd Catalan number, using a zero index, is 5. This is the same
61 number of Dyck words of length 2n where n = 3.
62  
63 The algorithms in this module are based on those presented in the
64 scholarly paper "Generating and ranking of Dyck words" by Zoltan Kasa
65 available on-line at http://arxiv4.library.cornell.edu/pdf/1002.2625,
66 and the provide three different Dyck word generators - lexigraphical,
67 positional, and one that generates Dyck words by swapping characters.
68  
69 =head1 EXPORT
70  
71 None by default.
72  
73 =cut
74  
75 package Math::DyckWords;
76  
77 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 1 our $VERSION = '0.03';
87 1 our @ISA = qw( Exporter );
88 1 our @EXPORT_OK = qw( dyck_words_by_lex
89 dyck_words_by_position
90 dyck_words_by_swap
91 ranking
92 unranking
93 catalan_number );
94  
95 =head1 FUNCTIONS
96  
97 =over
98  
99 =item dyck_words_by_lex( $n )
100  
101 This algorithm returns a list of all Dyck words of length 2n in ascending
102 lexicographic order, i.e. 000111, 001011, 001101, 010011, 010101
103  
104 =back
105  
106 =cut
107  
108 1 my @words;
109  
110 195 ▶sub dyck_words_by_lex {
Subroutine dyck_words_by_lex: called CC=14 SCAR=26.5
Pod documented
111 195 my ( $n, $X, $i, $n0, $n1 ) = @_;
112  
113 # initialization - the first time called, the only argument
114 # is the length 2n of the words
115 195 ▶ if( not defined $X ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $X) 1 194 195
116 1 ( $X, $i, $n0, $n1 ) = ( '0', 1, 1, 0 );
117 1 @words = ();
118 }
119  
120 # Case 1: We can continue by adding 0 and 1.
121 195 ▶ if( $n0 < $n && $n1 < $n && $n0 > $n1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n0 < $n and $n1 < $n and $n0 > $n1) 41 154 195
Condition83%5/6
$n0 < $n and $n1 < $n and $n0 > $n1
!ll&&!rl&&r
1322241
$n0 < $n and $n1 < $n
!ll&&!rl&&r
132063
MC/DC66%2/3
$n0 < $n and $n1 < $n and $n0 > $n1
$n0 < $n $n1 < $n $n0 > $n1
Truth table75%3/4
A and B and C
A= $n0 < $nB= $n1 < $nC= $n0 > $n1
ABCresult
0XX0
10X0
1100
1111
122 41 dyck_words_by_lex( $n, $X . '0', $i++, $n0 + 1, $n1 );
123 41 dyck_words_by_lex( $n, $X . '1', $i++, $n0, $n1 + 1 );
124 }
125  
126 # Case 2: We can continue by adding 0 only.
127 195 ▶ if( ( $n0 < $n && $n1 < $n && $n0 == $n1 ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($n0 < $n and $n1 < $n and $n0 == $n1 or $n0 < $n and $n1 == $n) 22 173 195
Condition75%9/12
$n0 < $n and $n1 < $n and $n0 == $n1 or $n0 < $n and $n1 == $n
l!l&&r!l&&!r
220173
$n0 < $n and $n1 < $n and $n0 == $n1
!ll&&!rl&&r
1324122
$n0 < $n and $n1 < $n
!ll&&!rl&&r
132063
$n0 < $n and $n1 == $n
!ll&&!rl&&r
132410
MC/DC40%2/5
$n0 < $n and $n1 < $n and $n0 == $n1 or $n0 < $n and $n1 == $n
$n0 < $n $n1 < $n $n0 == $n1 $n0 < $n $n1 == $n
Truth table30%3/10
A and B and C or D and E
A= $n0 < $nB= $n1 < $nC= $n0 == $n1D= $n0 < $nE= $n1 == $n
ABCDEresult
111XX1
0XX111
10X111
110111
0XX0X0
0XX100
10X0X0
10X100
1100X0
110100
128 ( $n0 < $n && $n1 == $n ) )
129 {
130 22 dyck_words_by_lex( $n, $X . '0', $i++, $n0 + 1, $n1 );
131 }
132  
133 # Case 3: We can continue by adding 1 only.
134 195 ▶ if( $n0 == $n && $n1 < $n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n0 == $n and $n1 < $n) 90 105 195
Condition100%3/3
$n0 == $n and $n1 < $n
!ll&&!rl&&r
634290
MC/DC100%2/2
$n0 == $n and $n1 < $n
$n0 == $n $n1 < $n
Truth table100%3/3
A and B
A= $n0 == $nB= $n1 < $n
ABresult
0X0
100
111
135 90 dyck_words_by_lex( $n, $X . '1', $i++, $n0, $n1 + 1 );
136 }
137  
138 # Case 5: A Dyck word is obtained.
139 195 ▶ if( $n0 == $n && $n1 == $n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n0 == $n and $n1 == $n) 42 153 195
Condition100%3/3
$n0 == $n and $n1 == $n
!ll&&!rl&&r
639042
MC/DC100%2/2
$n0 == $n and $n1 == $n
$n0 == $n $n1 == $n
Truth table100%3/3
A and B
A= $n0 == $nB= $n1 == $n
ABresult
0X0
100
111
140 42 push @words, $X;
141 }
142  
143 # All Dyck words have been obtained
144 195 return @words;
145 }
146  
147 =over
148  
149 =item dyck_words_by_position( $n )
150  
151 This algorithm returns a list of all Dyck words of length 2n in descending
152 lexicographic order, i.e. 010101, 010011, 001101, 001011, 000111.
153  
154 =back
155  
156 =cut
157  
158 1 ▶sub dyck_words_by_position {
Subroutine dyck_words_by_position: called CC=6 SCAR=17.9
Pod documented
159 1 my $n = shift;
160  
161 # reset the return list
162 1 @words = ();
163  
164 # generate the maximum Dyck word of length n - which has 1s in all
165 # even numbered positions, i.e. 2468 = 01010101
166 1 my @b = map { $_ * 2 } ( 1 .. $n );
167  
168 # set a flag
169 1 my $found = 1;
170  
171 1 ▶ while( $found ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 42 1 43
172 # save the Dyck word to the return list
173 42 push @words, translate_positions( @b );
174  
175 # reset flag
176 42 $found = 0;
177  
178 # reverse iterate through the length of the word
179 # setting the appropriate bits to 1's or 0's
180 42 ▶ for( my $i = $n - 1; $i >= 1; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 1) 89 1 90
181 89 ▶ if( $b[ $i - 1 ] < $n + $i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b[$i - 1] < $n + $i) 41 48 89
182 41 $b[ $i - 1 ] += 1;
183  
184 41 ▶ for( my $j = $i + 1; $j <= $n - 1; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $n - 1) 44 41 85
185 44 ▶ $b[ $j - 1 ] = $b[ $j - 2 ] + 1 > $j * 2
Branch: 1 branch
BranchTrueFalseTotal
$b[$j - 2] + 1 > $j * 2 ? : 26 18 44
186 ? $b[ $j - 2 ] + 1
187 : $j * 2;
188 }
189 41 $found = 1;
190 41 last;
191 }
192 }
193 }
194 1 return @words;
195 }
196  
197 =over
198  
199 =item translate_positions( @p )
200  
201 This function translates an array of integer values indicating
202 the position of 1's in the resultant Dyck word, and is called by
203 the dyck_words_by_position function.
204  
205 =back
206  
207 =cut
208  
209 43 ▶sub translate_positions( @ ) {
Subroutine translate_positions: called CC=3 SCAR=11.0
Pod documented
210 43 my $n = scalar @_;
211  
212 # convert the list of positions to a hash for easier lookup
213 43 my %position;
214 43 @position{ @_ } = @_;
215  
216 43 my $word;
217 43 ▶ for( my $i = 0; $i < $n * 2; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n * 2) 430 43 473
218 430 ▶ $word .= exists $position{ $i + 1 } ? '1' : '0';
Branch: 1 branch
BranchTrueFalseTotal
exists $position{$i + 1} ? : 215 215 430
219 }
220 43 return $word;
221 }
222  
223 =over
224  
225 =item dyck_words_by_position( $n )
226  
227 This algorithm returns a list of all Dyck words of length 2n in no
228 particular order, i.e. 010101, 001101, 001011, 000111, 010011. This
229 is done by changing the first occurrence of '10' to '01'.
230  
231 =back
232  
233 =cut
234  
235 42 ▶sub dyck_words_by_swap {
Subroutine dyck_words_by_swap: called CC=4 SCAR=13.9
Pod undocumented
236 42 my ( $n, $X, $k ) = @_;
237  
238 42 ▶ if( not defined $X ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $X) 1 41 42
239 1 $X = join '', ( '01' x $n );
240 1 $k = 0;
241 1 @words = ( $X );
242 }
243  
244 42 my $i = $k;
245  
246 42 ▶ while( $i < $n * 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n * 2) 83 0 83
247 83 my $j = index( $X, '10', $i );
248 83 ▶ if( $j > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j > 0) { } 41 42 83
249 41 my @Y = split //, $X;
250 # swap
251 41 ( $Y[ $j ], $Y[ $j + 1 ] ) =
252 ( $Y[ $j + 1 ], $Y[ $j ] );
253 41 my $Y = join '', @Y;
254 41 push @words, $Y;
255 41 dyck_words_by_swap( $n, $Y, $j - 1 );
256 41 $i = $j + 2;
257 }
258 else {
259 42 return @words;
260 }
261 }
262 }
263  
264 =over
265  
266 =item monotonic_path_count( $n, $i, $j )
267  
268 Ranking Dyck words means to determine the position of a Dyck
269 word in a given ordered sequence of all Dyck words.
270 For ranking these words we will use the following function,
271 where f(n,i,j) represents the number of paths between (0,0)
272 and (i,j) not crossing the diagonal x = y of the grid.
273  
274 =back
275  
276 =cut
277  
278 141 ▶sub monotonic_path_count {
Subroutine monotonic_path_count: called CC=13 SCAR=34.8
Pod documented
279 141 my ( $n, $i, $j ) = @_;
280  
281 141 ▶ if( $n >= $i and $i >= 0 and $j == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $i and $i >= 0 and $j == 0) 58 83 141
Condition50%3/6
$n >= $i and $i >= 0 and $j == 0
!ll&&!rl&&r
08358
$n >= $i and $i >= 0
!ll&&!rl&&r
00141
MC/DC33%1/3
$n >= $i and $i >= 0 and $j == 0
$n >= $i $i >= 0 $j == 0
Truth table50%2/4
A and B and C
A= $n >= $iB= $i >= 0C= $j == 0
ABCresult
0XX0
10X0
1100
1111
282 58 return 1;
283 }
284 83 ▶ if( $n >= $i and $i > $j and $j >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $i and $i > $j and $j >= 1) 47 36 83
Condition66%4/6
$n >= $i and $i > $j and $j >= 1
!ll&&!rl&&r
36047
$n >= $i and $i > $j
!ll&&!rl&&r
03647
MC/DC33%1/3
$n >= $i and $i > $j and $j >= 1
$n >= $i $i > $j $j >= 1
Truth table50%2/4
A and B and C
A= $n >= $iB= $i > $jC= $j >= 1
ABCresult
0XX0
10X0
1100
1111
285 47 return monotonic_path_count( $n, $i - 1, $j ) +
286 monotonic_path_count( $n, $i, $j - 1 );
287 }
288 36 ▶ if( $n >= $i and $i >= 1 and $j == $i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $i and $i >= 1 and $j == $i) 36 0 36
Condition33%2/6
$n >= $i and $i >= 1 and $j == $i
!ll&&!rl&&r
0036
$n >= $i and $i >= 1
!ll&&!rl&&r
0036
MC/DC0%0/3
$n >= $i and $i >= 1 and $j == $i
$n >= $i $i >= 1 $j == $i
Truth table25%1/4
A and B and C
A= $n >= $iB= $i >= 1C= $j == $i
ABCresult
0XX0
10X0
1100
1111
289 36 return monotonic_path_count( $n, $i, $i - 1 );
290 }
291 0 ▶ if( $n >= $j and $j > $i and $i >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $j and $j > $i and $i >= 0) 0 0 0
Condition0%0/6
$n >= $j and $j > $i and $i >= 0
!ll&&!rl&&r
000
$n >= $j and $j > $i
!ll&&!rl&&r
000
MC/DC0%0/3
$n >= $j and $j > $i and $i >= 0
$n >= $j $j > $i $i >= 0
Truth table0%0/4
A and B and C
A= $n >= $jB= $j > $iC= $i >= 0
ABCresult
0XX0
10X0
1100
1111
292 0 return 0;
293 }
294 }
295  
296 =over
297  
298 =item positions( $w )
299  
300 This function converts a Dyck word string of 1's and 0's into a list
301 of positions where the 1's are located, i.e. 2468 => 01010101
302  
303 =back
304  
305 =cut
306  
307 1 ▶sub positions( $ ) {
Subroutine positions: called CC=3 SCAR=11.0
Pod documented
308 1 my $w = shift;
309  
310 1 my ( $i, @p ) = ( 1, () );
311 1 foreach my $p ( split //, $w ) {
312 10 ▶ if( $p == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p == 1) 5 5 10
313 5 push @p, $i;
314 }
315 10 $i++;
316 }
317 1 return @p;
318 }
319  
320 =over
321  
322 =item ranking( $w )
323  
324 This function returns the rank of an individual Dyck word $w in the
325 list of all Dyck words of the same length.
326  
327 =back
328  
329 =cut
330  
331 1 ▶sub ranking( $ ) {
Subroutine ranking: called CC=5 SCAR=16.1
Pod documented
332 1 my @b = positions( shift );
333  
334 1 my @c = ( 2 );
335 1 my $n = scalar @b;
336 1 ▶ for( my $j = 2; $j <= $n; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $n) 4 1 5
337 4 ▶ $c[ $j - 1 ] = $b[ $j - 2 ] + 1 > $j * 2
Branch: 1 branch
BranchTrueFalseTotal
$b[$j - 2] + 1 > $j * 2 ? : 1 3 4
338 ? $b[ $j - 2 ] + 1
339 : $j * 2;
340 }
341 1 my $nr = 1;
342 1 ▶ for( my $i = 1; $i <= $n - 1; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n - 1) 4 1 5
343 4 ▶ for( my $j = $c[ $i - 1]; $j <= $b[ $i - 1] - 1; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $b[$i - 1] - 1) 3 4 7
344 3 $nr = $nr + monotonic_path_count( $n, $n - $i, $n + $i - $j );
345 }
346 }
347 1 return $nr;
348 }
349  
350 =over
351  
352 =item unranking( $n, $r )
353  
354 This function returns the rank $r Dyck word of length $n.
355  
356 =back
357  
358 =cut
359  
360 1 ▶sub unranking( $$ ) {
Subroutine unranking: called CC=5 SCAR=16.1
Pod documented
361 1 my ( $n, $nr ) = @_;
362  
363 # initialize the dyck word to all '0'
364 1 my @b = ( '0' x ( $n * 2 ) );
365  
366 1 $nr--;
367  
368 1 ▶ for( my $i = 1; $i <= $n; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 5 1 6
369 5 ▶ $b[ $i ] = $b[ $i - 1 ] + 1 > $i * 2
Branch: 1 branch
BranchTrueFalseTotal
$b[$i - 1] + 1 > $i * 2 ? : 1 4 5
370 ? $b[ $i - 1 ] + 1
371 : $i * 2;
372  
373 5 my $j = $n + $i - $b[ $i ];
374 5 my $np = monotonic_path_count( $n, $n - $i, $j );
375  
376 5 ▶ while( $nr >= $np && ( $b[ $i ] < $n + $i ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nr >= $np and $b[$i] < $n + $i) 3 5 8
Condition66%2/3
$nr >= $np and $b[$i] < $n + $i
!ll&&!rl&&r
503
MC/DC50%1/2
$nr >= $np and $b[$i] < $n + $i
$nr >= $np $b[$i] < $n + $i
Truth table66%2/3
A and B
A= $nr >= $npB= $b[$i] < $n + $i
ABresult
0X0
100
111
377 3 $nr = $nr - $np;
378 3 $b[ $i ] = $b[ $i ] + 1;
379 3 $j = $j - 1;
380 3 $np = monotonic_path_count( $n, $n - $i, $j );
381 }
382 }
383 # discard the zeroth element of the list of positions
384 1 shift @b;
385  
386 1 return translate_positions( @b );
387 }
388  
389 =over
390  
391 =item catalan_number( $n )
392  
393 Using the formula - (2n)!/(n!(n+1)!) - this function returns the
394 corresponding number $n from the Catalan sequence.
395  
396 =back
397  
398 =cut
399  
400 1 ▶sub catalan_number( $ ) {
Subroutine catalan_number: called CC=1 SCAR=0.0
Pod documented
401 1 my $x = shift;
402  
403 1 my $X = Math::BigInt->new( $x );
404 1 my $Y = $X->copy;
405 1 my $Z = $X->copy;
406  
407 1 return $X->bmul( 2 )->bfac->bdiv(
408 $Y->bfac->bmul( $Z->badd( 1 )->bfac )
409 );
410 }
411  
412 1 1;
↑ Summary