Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

r""" 

Gray codes 

 

REFERENCES: 

 

.. [Knuth-TAOCP2A] \D. Knuth "The art of computer programming", fascicules 2A, 

"generating all n-tuples" 

 

.. [Knuth-TAOCP3A] \D. Knuth "The art of computer programming", fascicule 3A 

"generating all combinations" 

 

Functions 

--------- 

""" 

from __future__ import print_function 

from six.moves import range 

 

 

def product(m): 

r""" 

Iterator over the switch for the iteration of the product 

`[m_0] \times [m_1] \ldots \times [m_k]`. 

 

The iterator return at each step a pair ``(p,i)`` which corresponds to the 

modification to perform to get the next element. More precisely, one has to 

apply the increment ``i`` at the position ``p``. By construction, the 

increment is either ``+1`` or ``-1``. 

 

This is algorithm H in [Knuth-TAOCP2A]_: loopless reflected mixed-radix Gray 

generation. 

 

INPUT: 

 

- ``m`` -- a list or tuple of positive integers that correspond to the size 

of the sets in the product 

 

EXAMPLES:: 

 

sage: from sage.combinat.gray_codes import product 

sage: l = [0,0,0] 

sage: for p,i in product([3,3,3]): 

....: l[p] += i 

....: print(l) 

[1, 0, 0] 

[2, 0, 0] 

[2, 1, 0] 

[1, 1, 0] 

[0, 1, 0] 

[0, 2, 0] 

[1, 2, 0] 

[2, 2, 0] 

[2, 2, 1] 

[1, 2, 1] 

[0, 2, 1] 

[0, 1, 1] 

[1, 1, 1] 

[2, 1, 1] 

[2, 0, 1] 

[1, 0, 1] 

[0, 0, 1] 

[0, 0, 2] 

[1, 0, 2] 

[2, 0, 2] 

[2, 1, 2] 

[1, 1, 2] 

[0, 1, 2] 

[0, 2, 2] 

[1, 2, 2] 

[2, 2, 2] 

sage: l = [0,0] 

sage: for i,j in product([2,1]): 

....: l[i] += j 

....: print(l) 

[1, 0] 

 

TESTS:: 

 

sage: for t in [[2,2,2],[2,1,2],[3,2,1],[2,1,3]]: 

....: assert sum(1 for _ in product(t)) == prod(t)-1 

""" 

# n is the length of the element (we ignore sets of size 1) 

n = k = 0 

 

new_m = [] # will be the set of upper bounds m_i different from 1 

mm = [] # index of each set (we skip sets of cardinality 1) 

for i in m: 

i = int(i) 

if i <= 0: 

raise ValueError("accept only positive integers") 

if i > 1: 

new_m.append(i-1) 

mm.append(k) 

n += 1 

k += 1 

 

m = new_m 

f = list(range(n + 1)) # focus pointer 

o = [1] * n # switch +1 or -1 

a = [0] * n # current element of the product 

 

j = f[0] 

while j != n: 

f[0] = 0 

oo = o[j] 

a[j] += oo 

if a[j] == 0 or a[j] == m[j]: 

f[j] = f[j+1] 

f[j+1] = j+1 

o[j] = -oo 

 

yield (mm[j], oo) 

 

j = f[0] 

 

def combinations(n,t): 

r""" 

Iterator through the switches of the revolving door algorithm. 

 

The revolving door algorithm is a way to generate all combinations of a set 

(i.e. the subset of given cardinality) in such way that two consecutive 

subsets differ by one element. At each step, the iterator output a pair 

``(i,j)`` where the item ``i`` has to be removed and ``j`` has to be added. 

 

The ground set is always `\{0, 1, ..., n-1\}`. Note that ``n`` can be 

infinity in that algorithm. 

 

See [Knuth-TAOCP3A]_. 

 

INPUT: 

 

- ``n`` -- (integer or ``Infinity``) -- size of the ground set 

 

- ``t`` -- (integer) -- size of the subsets 

 

EXAMPLES:: 

 

sage: from sage.combinat.gray_codes import combinations 

sage: b = [1, 1, 1, 0, 0] 

sage: for i,j in combinations(5,3): 

....: b[i] = 0; b[j] = 1 

....: print(b) 

[1, 0, 1, 1, 0] 

[0, 1, 1, 1, 0] 

[1, 1, 0, 1, 0] 

[1, 0, 0, 1, 1] 

[0, 1, 0, 1, 1] 

[0, 0, 1, 1, 1] 

[1, 0, 1, 0, 1] 

[0, 1, 1, 0, 1] 

[1, 1, 0, 0, 1] 

 

sage: s = set([0,1]) 

sage: for i,j in combinations(4,2): 

....: s.remove(i) 

....: s.add(j) 

....: print(sorted(s)) 

[1, 2] 

[0, 2] 

[2, 3] 

[1, 3] 

[0, 3] 

 

Note that ``n`` can be infinity:: 

 

sage: c = combinations(Infinity,4) 

sage: s = set([0,1,2,3]) 

sage: for _ in range(10): 

....: i,j = next(c) 

....: s.remove(i); s.add(j) 

....: print(sorted(s)) 

[0, 1, 3, 4] 

[1, 2, 3, 4] 

[0, 2, 3, 4] 

[0, 1, 2, 4] 

[0, 1, 4, 5] 

[1, 2, 4, 5] 

[0, 2, 4, 5] 

[2, 3, 4, 5] 

[1, 3, 4, 5] 

[0, 3, 4, 5] 

sage: for _ in range(1000): 

....: i,j = next(c) 

....: s.remove(i); s.add(j) 

sage: sorted(s) 

[0, 4, 13, 14] 

 

TESTS:: 

 

sage: def check_sets_from_iter(n,k): 

....: l = [] 

....: s = set(range(k)) 

....: l.append(frozenset(s)) 

....: for i,j in combinations(n,k): 

....: s.remove(i) 

....: s.add(j) 

....: assert len(s) == k 

....: l.append(frozenset(s)) 

....: assert len(set(l)) == binomial(n,k) 

sage: check_sets_from_iter(9,5) 

sage: check_sets_from_iter(8,5) 

sage: check_sets_from_iter(5,6) 

Traceback (most recent call last): 

... 

AssertionError: t(=6) must be >=0 and <=n(=5) 

 

""" 

from sage.rings.infinity import Infinity 

t = int(t) 

if n != Infinity: 

n = int(n) 

else: 

n = Infinity 

assert 0 <= t and t <= n, "t(={}) must be >=0 and <=n(={})".format(t,n) 

if t == 0 or t == n: 

return iter([]) 

if t % 2: 

return _revolving_door_odd(n,t) 

else: 

return _revolving_door_even(n,t) 

 

def _revolving_door_odd(n,t): 

r""" 

Revolving door switch for odd `t`. 

 

TESTS:: 

 

sage: from sage.combinat.gray_codes import _revolving_door_odd 

sage: sum(1 for _ in _revolving_door_odd(13,3)) == binomial(13,3) - 1 

True 

sage: sum(1 for _ in _revolving_door_odd(10,5)) == binomial(10,5) - 1 

True 

""" 

# note: the numerotation of the steps below follows Kunth TAOCP 

c = list(range(t)) + [n] # the combination (ordered list of numbers of length t+1) 

 

while True: 

# R3 : easy case 

if c[0] + 1 < c[1]: 

yield c[0], c[0]+1 

c[0] += 1 

continue 

 

j = 1 

while j < t: 

# R4 : try to decrease c[j] 

# at this point c[j] = c[j-1] + 1 

if c[j] > j: 

yield c[j], j-1 

c[j] = c[j-1] 

c[j-1] = j-1 

break 

j += 1 

 

# R5 : try to increase c[j] 

# at this point c[j-1] = j-1 

if c[j] + 1 < c[j+1]: 

yield c[j-1], c[j]+1 

c[j-1] = c[j] 

c[j] += 1 

break 

j += 1 

 

else: # j == t 

break 

 

def _revolving_door_even(n,t): 

r""" 

Revolving door algorithm for even `t`. 

 

TESTS:: 

 

sage: from sage.combinat.gray_codes import _revolving_door_even 

sage: sum(1 for _ in _revolving_door_even(13,4)) == binomial(13,4) - 1 

True 

sage: sum(1 for _ in _revolving_door_even(12,6)) == binomial(12,6) - 1 

True 

""" 

# note: the numerotation of the setps below follows Kunth TAOCP 

 

c = list(range(t)) + [n] # the combination (ordered list of numbers of length t+1) 

 

while True: 

# R3 : easy case 

if c[0] > 0: 

yield c[0], c[0]-1 

c[0] -= 1 

continue 

 

j = 1 

# R5 : try to increase c[j] 

# at this point c[j-1] = j-1 

if c[j] + 1 < c[j+1]: 

yield c[j-1], c[j]+1 

c[j-1] = c[j] 

c[j] += 1 

continue 

j += 1 

 

while j < t: 

# R4 : try to decrease c[j] 

# at this point c[j] = c[j-1] + 1 

if c[j] > j: 

yield c[j], j-1 

c[j] = c[j-1] 

c[j-1] = j-1 

break 

j += 1 

 

# R5 : try to increase c[j] 

# at this point c[j-1] = j-1 

if c[j] + 1 < c[j+1]: 

yield c[j-1], c[j] + 1 

c[j-1] = c[j] 

c[j] += 1 

break 

j += 1 

 

else: # j == t 

break