-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkSubset.py
68 lines (50 loc) · 1.01 KB
/
kSubset.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
'''
k-SUBSET
--------
A list of items taken from
[1,2,3,...,n] that are distinct
Ranking includes ONLY length k subsets
(number of 1s in list is k)
'''
from comFuncs import combination
def colexRank(T, k):
'''ranks colex ordering
Algorithm 2.9 in book'''
r = 0
for i in range(1,k+1):
r += combination(T[i-1]-1, k+1-i)
return r
def colexUnrank(r, k, n):
'''unranks colex ordering
Algorithm 2.10 in book'''
T = [0]*k
x = n
for i in range(1,k+1):
c = combination(x, k+1-i)
while c > r:
x -= 1
c = combination(x, k+1-i)
T[i-1] = x+1
r -= combination(x, k+1-i)
return T
def colexSuccessor(T, k, n):
'''Finds next item in colex ordering
Exercise 2.6 in book'''
T = T[::-1]
inOrder = True
offender = -1
for i in range(1, k):
if T[i-1] + 1 != T[i]:
inOrder = False
offender = i-1
break
if inOrder:
if T[k-1] == n:
return "undefined"
T[k-1] += 1
dec = T[0] - 1
for i in range(0,k-1):
T[i] -= dec
else:
T[offender] += 1
return T[::-1]