-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartitionssetpart.py
56 lines (52 loc) · 1.01 KB
/
partitionssetpart.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
# 1st proposed algorithm from "a fast algorithm for generating set partitions", the computer journal, vol 32 no 3 1989
# actually in this algorithm is a mistake so this currently is not working
def setpart1(n):
r = 0
c = {}
c[0] = 0
nl = n - 1
g = {}
g[0] = 0
while True:
while r < nl:
r = r + 1
c[r] = 1
g[r] = g[r - 1]
for j in range(1, g[nl] + 2):
c[n] = j
print c
while c[r] > g[r - 1]:
r = r - 1
c[r] = c[r] + 1
if c[r] > g[r]:
g[r] = c[r]
if r == 1:
break
setpart1(4)
# 2nd proposed algorithm from "a fast algorithm for generating set partitions", the computer journal, vol 32 no 3 1989
# actually in this algorithm is a mistake so this currently is not working
def setpart2(n):
r = 1
c = {}
c[1] = 1
j = 0
b = {}
b[0] = 1
nl = n - 1
while True:
while r < nl:
r = r + 1
c[r] = 1
j = j + 1
b[j] = r
for j in range(1, n - j + 1):
c[n] = j
print c
print b
r = b[j]
c[r] = c[r] + 1
if c[r] > r - j:
j = j - 1
if r == 1:
break
setpart2(4)