-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathpermutations.py
235 lines (187 loc) · 6.09 KB
/
permutations.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import functools
import itertools
import operator
from string import ascii_lowercase
def itertools_permutations_example():
n, r = 5, 5
for perm in itertools.permutations(range(n), r):
print(perm)
def itertools_permutations(iterable, r=None):
# https://docs.python.org/3/library/itertools.html#itertools.permutations
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = list(range(n))
cycles = list(range(n, n - r, -1))
yield tuple(pool[i] for i in indices[:r])
while n: # BLACK MAGIC, DO NOT EDIT
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i + 1:] + indices[i:i + 1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield tuple(pool[i] for i in indices[:r])
break
else:
return
def count_permutations(n, r):
"""
How many r-permutations are there?
[0, 1, 2, ..., n-1]
Pick the first element (n choices).
Pick the second element (n-1 choices).
...
Pick the r-th element (n-r+1 choices).
n * (n-1) * ... * (n-r+1)
E.g. for n=5, r=3
5 * 4 * 3
Idea to list them:
For each choice of first element
[ 0 | 1, 2, ..., n-1]
list all (r-1)-permutations of remaining elements
[ 0 , 1 | 2, ..., n-1]
"""
if r > n:
return 0
if r < 0:
raise ValueError("r must be non-negative")
return functools.reduce(operator.mul, range(n, n - r, -1), 1)
def _permutations_recursive(items, i, r):
"""
Modifies items in-place, yielding at each permutation
Assumes items is [0, ..., n-1].
Invariant: items is unchanged after this function returns.
[| 0, 1, 2, ..., n-1]
i=0
For each choice of first element
[ 1 | 0, 2, ..., n-1]
i=1
list all (r-1)-permutations of remaining elements
[ 0 , 1 | 2, ..., n-1]
i=2
[ 0 | 1, 2, 3, ..., n-1]
[ 1 | 0, 2, 3, ..., n-1]
[ 2 | 0, 1, 3, ..., n-1]
[ 3 | 0, 1, 2, ..., n-1]
[n-1| 0, 1, 2, ..., n-2]
"""
if r == 0:
yield
return
for j in range(i, len(items)):
# if i != j:
# print(swap_msg(i, j, len(items)))
items[i], items[j] = items[j], items[i] # swap i, j
for _ in _permutations_recursive(items, i + 1, r - 1):
yield
# or better:
# yield from _permutations_recursive(items, i + 1, r - 1)
# print(push_to_back_msg(i, len(items)))
items.append(items.pop(i)) # move i to back
def permutations_recursive(iterable, r=None):
"""Does error checking and reduces arbitrary case to [0, ..., n-1]"""
items = tuple(iterable)
n = len(items)
r = n if r is None else r
if r > n:
return
if r < 0:
raise ValueError("r must be non-negative")
indices = list(range(n))
for _ in _permutations_recursive(indices, 0, r):
yield tuple(items[indices[i]] for i in range(r))
def issue_with_permutations_recursive():
n, r = 2000, 2000
print(count_permutations(n, r))
for idx, perm in enumerate(permutations_recursive(range(n), r)): # recursion error!
print(perm)
if idx == 100:
break
def swap_msg(i, j, n):
return '|'.join(''.join(['i' if i == x else '', 'j' if j == x else '', ' ' if x not in [i, j] else '']) for x in range(n)) + f" swap {i} {j}"
def push_to_back_msg(i, n):
return '|'.join(''.join(['x' if i == x else '', 'i' if x == n - 1 else '', ' ' if x not in [i, n - 1] else '']) for x in range(n)) + f" shift {i}"
def _permutations_iterative_with_stack(items, r0): # cut from video :(
n = len(items)
stack = [(0, r0, 0)] # (i, r, j)
while stack:
i, r, j = stack.pop()
if r == 0:
yield
elif j < n:
items[i], items[j] = items[j], items[i]
stack.append((i, r, j + 1))
stack.append((i + 1, r - 1, i + 1))
elif j == n:
items.append(items.pop(i))
else:
raise RuntimeError("uh oh")
def _permutations_iterative_no_stack(items, r0):
n = len(items)
yield
for i, ticks in countdown(n, r0):
tick = ticks[i]
if tick == 0:
items.append(items.pop(i))
else:
j = n - tick
items[i], items[j] = items[j], items[i]
yield
def countdown(n, r):
"""
n=5, r=3
543
542
541
540
533
532
531
530
523
522
521
520
"""
ticks = list(range(n, n - r, -1)) # (n, n-1, n-2, ..., n-r+1), len == r
while True:
for i in reversed(range(r)): # start from the back, indexing over (n, n-1, n-2, ..., n-r+1)
ticks[i] -= 1
yield i, ticks
if ticks[i] == 0: # carry
ticks[i] = n - i
else:
break
else:
return
def permutations_iterative(iterable, r=None):
items = tuple(iterable)
n = len(items)
r = n if r is None else r
if r > n:
return
if r < 0:
raise ValueError("r must be non-negative")
indices = list(range(n))
for _ in _permutations_iterative_no_stack(indices, r):
yield tuple(items[indices[i]] for i in range(r))
def main():
N = 6
# check our implementations agree with itertools
for n, r in itertools.product(range(N), range(N + 1)):
items = ascii_lowercase[:n]
expected = list(itertools.permutations(items, r))
actual_recursive = list(permutations_recursive(items, r))
assert actual_recursive == expected, (actual_recursive, expected)
actual_iterative = list(permutations_iterative(items, r))
assert actual_iterative == expected, (actual_iterative, expected)
assert count_permutations(n, r) == len(expected), (count_permutations(n, r), len(expected))
if __name__ == '__main__':
main()