-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinatoria.rb
More file actions
288 lines (237 loc) · 5.9 KB
/
combinatoria.rb
File metadata and controls
288 lines (237 loc) · 5.9 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
require "rational"
module Combinatoria
Id = "$kNotwork: combinatoria.rb,v 1.16 2003/04/20 20:09:56 gotoken Exp $"
REVISION = Id[/\d+(\.\d+)+/]
module Stream # ruby-list:13696
def next
callcc{|@cc|
@cont.call if @cont
@iter.call {|obj|
callcc{|@cont| @cc.call(obj) }
}
@cc.call(nil)
}
end
def rewind()
@cont = @cc = nil
end
end
class Combination
include Enumerable
include Stream
def initialize(seq,k)
@seq, @k = seq, k
@size = Combinatoria::C(seq.size, @k)
@iter, @cc, @cont = method(:each), nil, nil
end
attr_reader :size
def each()
# A tricky efficient algorithm which is developed by M. Beeler,
# R. W. Gosper and R. Schroeppel (Samuel P. Harbison and
# Guy L. Steele Jr. "C: A Reference Manual.", Prentice-Hall,
# second edition, p176 (1987))
n = @seq.size
if n.zero? or @k.zero?
yield(@seq.class.new())
return
else
x = (1<<@k)-1
comb = @seq.class.new
(0..n-1).each{|i| comb << @seq[i] unless x[i].zero?}
while (x & ~((1<<n)-1)).zero?
yield(comb)
smallest = x & -x
ripple = x + smallest
new_smallest = ripple & -ripple
ones = ((new_smallest / smallest) >> 1) - 1
x = ripple | ones
comb = @seq.class.new
(0..n-1).each{|i| comb << @seq[i] unless x[i].zero?}
end
end
end
end
class Permutation
include Enumerable
include Stream
def initialize(seq, k = nil)
@seq, @k = seq, (k||seq.size)
@size = Combinatoria::P(seq.size, @k)
@iter, @cc, @cont = method(:each), nil, nil
end
def each(&block)
if @k.zero?
yield(@seq.class.new())
elsif @k == @seq.size
permutate(@seq,0,&block)
else
for comb in Combinatoria::Combination.new(@seq,@k)
permutate(comb,0,&block)
end
end
end
private
def permutate(a,k,&block)
n = a.size-1
if n == k
yield(a)
else
for i in k..n
b = a.dup
b[k],b[i] = b[i],b[k]
permutate(b,k+1,&block)
end
end
end
end
class Subset
include Enumerable
include Stream
def initialize(seq, k)
@seq, @k = seq, (k || seq.size)
@size = 2**seq.size
@comb = Combinatoria::Combination.new(@seq,@i)
@iter, @cc, @cont = method(:each), nil, nil
end
attr_reader :size
def each()
for i in 0..@k
Combinatoria::Combination.new(@seq,i).each{|c| yield(c)}
end
end
end
module Iterator
def combination(k)
Combinatoria::Combination.new(self,k)
end
def each_combination(k, &block)
Combinatoria::Combination.new(self,k).each(&block)
end
def combinations(k)
Combinatoria::Combination.new(self,k).inject([]){|i,j| i<<j}
end
def permutation(k = size)
Combinatoria::Permutation.new(self,k)
end
def each_permutation(k = size, &block)
Combinatoria::Permutation.new(self,k).each(&block)
end
def permutations(k = size)
Combinatoria::Permutation.new(self,k).inject([]){|i,j| i<<j}
end
def subset(k = size)
Combinatoria::Subset.new(self,k)
end
def each_subset(k = size, &block)
Combinatoria::Subset.new(self,k).each(&block)
end
def subsets(k = size)
Combinatoria::Subset.new(self,k).inject([]){|i,j| i<<j}
end
end
module_function
def sum(range, zero=0)
if block_given?
range.inject(zero){|s,x| s+yield(x)}
else
range.inject(zero){|s,x| x}
end
end
def product(range, one=1)
if block_given?
range.inject(one){|s,x| s*yield(x)}
else
range.inject(one){|s,x| s*x}
end
end
def C(n,k)
r = k < n/2 ? k : n-k
(0..n) === k ? (1..k).inject(1){|i,j| i*(n-j+1)/j} : 0
end
def P(n,k)
(0..n) === k ? (1..k).inject(1){|i,j| i*(n-j+1)} : 0
end
def H(n,k)
Combinatoria::C(n+k-1,k)
end
Bernoulli = {0=>Rational(1,1)}
def Bernoulli(k)
Combinatoria::Bernoulli[k] ||=
-(0..k-1).inject(0){|s,i|s+C(k+1,k+1-i)*Bernoulli(i)}/C(k+1,1)
end
Stirling1 = {}
def Stirling1(n,m)
Combinatoria::Stirling1[[n,m]] ||=
if n < m or n < 1 or m < 1
0
elsif n == m
1
else
Stirling1(n-1,m-1) - (n-1)*Stirling1(n-1,m)
end
end
Stirling2 = {}
def Stirling2(n,m)
Combinatoria::Stirling2[[n,m]] ||=
if n < m or n < 1 or m < 1
0
elsif m == 1 or m == n
1
else
Stirling2(n-1,m-1) + m*Stirling2(n-1,m)
end
end
Partition = {}
def Partition(n, m = nil)
if m
Combinatoria::Partition[[n,m]] ||=
if n < m or n < 1 or m < 1
0
elsif m == 1 or m == n
1
else
Partition(n-m,m) + Partition(n-1,m-1)
end
else
Combinatoria::Partition[n] ||= (1..n).inject(0){|s,i| s+Partition(n,i)}
end
end
def Bell(n)
Combinatoria::Partition(n)
end
end
class Array
include Combinatoria::Iterator
end
class String
include Combinatoria::Iterator
end
module Enumerable
def combination(k)
Combinatoria::Combination.new(to_a,k)
end
def each_combination(k, &block)
Combinatoria::Combination.new(to_a,k).each(&block)
end
def combinations(k)
Combinatoria::Combination.new(to_a,k).inject([]){|i,j| i<<j}
end
def permutation(k = size)
Combinatoria::Permutation.new(to_a,k)
end
def each_permutation(k = size, &block)
Combinatoria::Permutation.new(to_a,k).each(&block)
end
def permutations(k = size)
Combinatoria::Permutation.new(to_a,k).inject([]){|i,j| i<<j}
end
def subset(k = size)
Combinatoria::Subset.new(to_a,k)
end
def each_subset(k = size, &block)
Combinatoria::Subset.new(to_a,k).each(&block)
end
def subsets(k = size)
Combinatoria::Subset.new(to_a,k).inject([]){|i,j| i<<j}
end
end