-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyckpath.py
213 lines (187 loc) · 5.5 KB
/
dyckpath.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
# -*- coding: utf-8 -*-
r"""
Code for working with Dyck paths.
A Dyck path is represented as a tuple, where each
entry gives the number of boxes above-and-to-the-right
of the baseline in a row.
"""
__all__ = [
'all_dyck_paths',
'ascii',
'boxes_under_path',
'graphical',
'is_dyck_path',
'is_primitive',
]
# ---------------------------------------------------------
import itertools as it
# ---------------------------------------------------------
def is_dyck_path(path):
r"""
Check whether `path` is a valid Dyck path.
The entries of `path` give the number of boxes
above the path in each row.
>>> is_dyck_path((0, 0, 0))
True
>>> is_dyck_path((1, 0, 0))
True
>>> is_dyck_path((0, 1, 0))
True
>>> is_dyck_path((1, 1, 0))
True
>>> is_dyck_path((2, 1, 0))
True
>>> is_dyck_path((1, 2, 0))
False
"""
return (isinstance(path, tuple) and
all(isinstance(entry, int) for entry in path) and
all(entry >= 0 for entry in path) and
all(path[i] < path[i+1]+2 for i in range(len(path)-1)) and
(len(path) == 0 or path[-1] == 0))
def is_primitive(path):
r"""
Check whether the Dyck path `path` is a primitive.
>>> is_primitive((0, 0, 0))
False
>>> is_primitive((1, 0, 0))
False
>>> is_primitive((0, 1, 0))
False
>>> is_primitive((1, 1, 0))
True
>>> is_primitive((2, 1, 0))
True
"""
return (len(path) == 0 or path.index(0) == len(path)-1)
def all_dyck_paths(n, primitive=False):
r"""
An iterator over all valid Dyck paths of length `n`.
If the optional keywork `primitive` is `True`, the iterator
only generates paths that don't touch the baseline partway
through the path.
>>> for path in sorted(all_dyck_paths(3)): print path
(0, 0, 0)
(0, 1, 0)
(1, 0, 0)
(1, 1, 0)
(2, 1, 0)
>>> for path in sorted(all_dyck_paths(3, primitive=True)): print path
(1, 1, 0)
(2, 1, 0)
>>> [len(list(all_dyck_paths(n))) for n in range(7)]
[1, 1, 2, 5, 14, 42, 132]
>>> [len(list(all_dyck_paths(n, primitive=True))) for n in range(7)]
[1, 1, 1, 2, 5, 14, 42]
"""
if n == 0:
yield ()
return
elif n == 1:
yield (0,)
return
elif n >= 2:
for tail in all_dyck_paths(n-1, primitive=primitive):
for head in range(primitive, tail[0]+2):
yield (head,) + tail
return
_cache = {}
def boxes_under_path(path):
r"""
Version mémoisée de _boxes_under_path.
"""
if path in _cache:
return _cache[path]
else:
result = _boxes_under_path(path)
_cache[path] = result
return result
def _boxes_under_path(path):
r"""
Return the set of boxes `(i, j)` below the path `path`.
>>> sorted(boxes_under_path((0, 0, 0)))
[]
>>> sorted(boxes_under_path((1, 0, 0)))
[(0, 1)]
>>> sorted(boxes_under_path((0, 1, 0)))
[(1, 2)]
>>> sorted(boxes_under_path((1, 1, 0)))
[(0, 1), (1, 2)]
>>> sorted(boxes_under_path((2, 1, 0)))
[(0, 1), (0, 2), (1, 2)]
>>> all(len(boxes_under_path(p)) == sum(p) for p in all_dyck_paths(5))
True
"""
return {(i, j) for i, k in enumerate(path) for j in range(i+1, i+1+k)}
# ---------------------------------------------------------
def ascii(path):
r"""
Return an ascii art representation of `path`.
>>> print ascii((0, 0, 0))
/\/\/\
>>> print ascii((1, 0, 0))
/\
/\/\/\
>>> print ascii((0, 1, 0))
/\
/\/\/\
>>> print ascii((1, 1, 0))
/\/\
/\/\/\
>>> print ascii((2, 1, 0))
/\
/\/\
/\/\/\
"""
assert is_dyck_path(path)
rows = []
for height in range(max(path)+1):
row = ' '*height
for entry in path:
row += '/\\' if entry >= height else ' '
rows.append(row.rstrip())
return '\n'.join(reversed(rows))
def graphical(path):
r"""
Return a sage graphics representation of `path`.
"""
from sage.all import Graphics, polygon
assert is_dyck_path(path)
result = Graphics()
# first a row of triangles
for i in range(len(path)):
result += polygon([(2*i, 0),
(2*i+1, 1),
(2*i+2, 0)],
color='black', fill=False, thickness=2)
# then all the boxes
for i, j in boxes_under_path(path):
result += polygon([(i+j, j-i),
(i+j+1, j-i+1),
(i+j+2, j-i),
(i+j+1, j-i-1)],
color='black', fill=False, thickness=2)
# reset some annoying options
result.axes(False)
result.set_aspect_ratio(1)
return result
# ---------------------------------------------------------
def test_generation(max_n=6):
r"""
Test that `all_dyck_paths` corresponds to `is_dyck_path`
and `is_primitive`.
>>> test_generation()
"""
for n in range(max_n+1):
iterated = sorted(all_dyck_paths(n))
filtered = filter(is_dyck_path, it.product(range(n), repeat=n))
assert iterated == filtered
primitive_iterated = sorted(all_dyck_paths(n, primitive=True))
primitive_filtered = filter(is_primitive, filtered)
assert primitive_iterated == primitive_filtered
# ---------------------------------------------------------
if __name__ == '__main__':
import doctest
doctest.testmod()
# ---------------------------------------------------------