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

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

""" 

Derangements 

 

AUTHORS: 

 

- Alasdair McAndrew (2010-05): Initial version 

- Travis Scrimshaw (2013-03-30): Put derangements into category framework 

""" 

 

#***************************************************************************** 

# Copyright (C) 2010 Alasdair McAndrew <amca01@gmail.com>, 

# 2013 Travis Scrimshaw <tscrim@ucdavis.edu> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from six.moves import range 

 

from sage.structure.parent import Parent 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets 

from sage.misc.all import prod 

from sage.misc.prandom import random, randint 

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing 

from sage.rings.all import ZZ, QQ 

from sage.rings.integer import Integer 

from sage.combinat.combinat import CombinatorialElement 

from sage.combinat.permutation import Permutation, Permutations 

 

 

class Derangement(CombinatorialElement): 

r""" 

A derangement. 

 

A derangement on a set `S` is a permutation `\sigma` such that `\sigma(x) 

\neq x` for all `x \in S`, i.e. `\sigma` is a permutation of `S` with no 

fixed points. 

 

EXAMPLES:: 

 

sage: D = Derangements(4) 

sage: elt = D([4,3,2,1]) 

sage: TestSuite(elt).run() 

""" 

def to_permutation(self): 

""" 

Return the permutation corresponding to ``self``. 

 

EXAMPLES:: 

 

sage: D = Derangements(4) 

sage: p = D([4,3,2,1]).to_permutation(); p 

[4, 3, 2, 1] 

sage: type(p) 

<class 'sage.combinat.permutation.StandardPermutations_all_with_category.element_class'> 

sage: D = Derangements([1, 3, 3, 4]) 

sage: D[0].to_permutation() 

Traceback (most recent call last): 

... 

ValueError: Can only convert to a permutation for derangements of [1, 2, ..., n] 

""" 

if self.parent()._set != tuple(range(1, len(self)+1)): 

raise ValueError("Can only convert to a permutation for derangements of [1, 2, ..., n]") 

return Permutation(list(self)) 

 

class Derangements(UniqueRepresentation, Parent): 

r""" 

The class of all derangements of a set or multiset. 

 

A derangement on a set `S` is a permutation `\sigma` such that `\sigma(x) 

\neq x` for all `x \in S`, i.e. `\sigma` is a permutation of `S` with no 

fixed points. 

 

For an integer, or a list or string with all elements 

distinct, the derangements are obtained by a standard result described 

in [DerUB]_. For a list or string with repeated elements, the derangements 

are formed by computing all permutations of the input and discarding all 

non-derangements. 

 

INPUT: 

 

- ``x`` -- Can be an integer which corresponds to derangements of 

`\{1, 2, 3, \ldots, x\}`, a list, or a string 

 

REFERENCES: 

 

.. [DerUB] http://www.u-bourgogne.fr/LE2I/jl.baril/derange.pdf 

 

- :wikipedia:`Derangement` 

 

EXAMPLES:: 

 

sage: D1 = Derangements([2,3,4,5]) 

sage: D1.list() 

[[3, 4, 5, 2], 

[5, 4, 2, 3], 

[3, 5, 2, 4], 

[4, 5, 3, 2], 

[4, 2, 5, 3], 

[5, 2, 3, 4], 

[5, 4, 3, 2], 

[4, 5, 2, 3], 

[3, 2, 5, 4]] 

sage: D1.cardinality() 

9 

sage: D1.random_element() # random 

[4, 2, 5, 3] 

sage: D2 = Derangements([1,2,3,1,2,3]) 

sage: D2.cardinality() 

10 

sage: D2.list() 

[[2, 1, 1, 3, 3, 2], 

[2, 1, 2, 3, 3, 1], 

[2, 3, 1, 2, 3, 1], 

[2, 3, 1, 3, 1, 2], 

[2, 3, 2, 3, 1, 1], 

[3, 1, 1, 2, 3, 2], 

[3, 1, 2, 2, 3, 1], 

[3, 1, 2, 3, 1, 2], 

[3, 3, 1, 2, 1, 2], 

[3, 3, 2, 2, 1, 1]] 

sage: D2.random_element() # random 

[2, 3, 1, 3, 1, 2] 

""" 

@staticmethod 

def __classcall_private__(cls, x): 

""" 

Normalize ``x`` to ensure a unique representation. 

 

EXAMPLES:: 

 

sage: D = Derangements(4) 

sage: D2 = Derangements([1, 2, 3, 4]) 

sage: D3 = Derangements((1, 2, 3, 4)) 

sage: D is D2 

True 

sage: D is D3 

True 

""" 

if x in ZZ: 

x = list(range(1, x + 1)) 

return super(Derangements, cls).__classcall__(cls, tuple(x)) 

 

def __init__(self, x): 

""" 

Initalize ``self``. 

 

EXAMPLES:: 

 

sage: D = Derangements(4) 

sage: TestSuite(D).run() 

sage: D = Derangements('abcd') 

sage: TestSuite(D).run() 

sage: D = Derangements([2, 2, 1, 1]) 

sage: TestSuite(D).run() 

""" 

Parent.__init__(self, category=FiniteEnumeratedSets()) 

self._set = x 

self.__multi = len(set(x)) < len(x) 

 

def _repr_(self): 

""" 

Return a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: Derangements(4) 

Derangements of the set [1, 2, 3, 4] 

sage: Derangements('abcd') 

Derangements of the set ['a', 'b', 'c', 'd'] 

sage: Derangements([2,2,1,1]) 

Derangements of the multiset [2, 2, 1, 1] 

""" 

if self.__multi: 

return "Derangements of the multiset %s"%list(self._set) 

return "Derangements of the set %s"%list(self._set) 

 

def _element_constructor_(self, der): 

""" 

Construct an element of ``self`` from ``der``. 

 

EXAMPLES:: 

 

sage: D = Derangements(4) 

sage: elt = D([3,1,4,2]); elt 

[3, 1, 4, 2] 

sage: elt.parent() is D 

True 

""" 

if isinstance(der, Derangement): 

if der.parent() is self: 

return der 

raise ValueError("Cannot convert %s to an element of %s"%(der, self)) 

return self.element_class(self, der) 

 

Element = Derangement 

 

def __iter__(self): 

""" 

Iterate through ``self``. 

 

EXAMPLES:: 

 

sage: D = Derangements(4) 

sage: D.list() # indirect doctest 

[[2, 3, 4, 1], 

[4, 3, 1, 2], 

[2, 4, 1, 3], 

[3, 4, 2, 1], 

[3, 1, 4, 2], 

[4, 1, 2, 3], 

[4, 3, 2, 1], 

[3, 4, 1, 2], 

[2, 1, 4, 3]] 

sage: D = Derangements([1,44,918,67]) 

sage: D.list() 

[[44, 918, 67, 1], 

[67, 918, 1, 44], 

[44, 67, 1, 918], 

[918, 67, 44, 1], 

[918, 1, 67, 44], 

[67, 1, 44, 918], 

[67, 918, 44, 1], 

[918, 67, 1, 44], 

[44, 1, 67, 918]] 

sage: D = Derangements(['A','AT','CAT','CATS']) 

sage: D.list() 

[['AT', 'CAT', 'CATS', 'A'], 

['CATS', 'CAT', 'A', 'AT'], 

['AT', 'CATS', 'A', 'CAT'], 

['CAT', 'CATS', 'AT', 'A'], 

['CAT', 'A', 'CATS', 'AT'], 

['CATS', 'A', 'AT', 'CAT'], 

['CATS', 'CAT', 'AT', 'A'], 

['CAT', 'CATS', 'A', 'AT'], 

['AT', 'A', 'CATS', 'CAT']] 

sage: D = Derangements('CART') 

sage: D.list() 

[['A', 'R', 'T', 'C'], 

['T', 'R', 'C', 'A'], 

['A', 'T', 'C', 'R'], 

['R', 'T', 'A', 'C'], 

['R', 'C', 'T', 'A'], 

['T', 'C', 'A', 'R'], 

['T', 'R', 'A', 'C'], 

['R', 'T', 'C', 'A'], 

['A', 'C', 'T', 'R']] 

sage: D = Derangements([1,1,2,2,3,3]) 

sage: D.list() 

[[2, 2, 3, 3, 1, 1], 

[2, 3, 1, 3, 1, 2], 

[2, 3, 1, 3, 2, 1], 

[2, 3, 3, 1, 1, 2], 

[2, 3, 3, 1, 2, 1], 

[3, 2, 1, 3, 1, 2], 

[3, 2, 1, 3, 2, 1], 

[3, 2, 3, 1, 1, 2], 

[3, 2, 3, 1, 2, 1], 

[3, 3, 1, 1, 2, 2]] 

sage: D = Derangements('SATTAS') 

sage: D.list() 

[['A', 'S', 'S', 'A', 'T', 'T'], 

['A', 'S', 'A', 'S', 'T', 'T'], 

['A', 'T', 'S', 'S', 'T', 'A'], 

['A', 'T', 'S', 'A', 'S', 'T'], 

['A', 'T', 'A', 'S', 'S', 'T'], 

['T', 'S', 'S', 'A', 'T', 'A'], 

['T', 'S', 'A', 'S', 'T', 'A'], 

['T', 'S', 'A', 'A', 'S', 'T'], 

['T', 'T', 'S', 'A', 'S', 'A'], 

['T', 'T', 'A', 'S', 'S', 'A']] 

sage: D = Derangements([1,1,2,2,2]) 

sage: D.list() 

[] 

""" 

if self.__multi: 

for p in Permutations(self._set): 

if not self._fixed_point(p): 

yield self.element_class(self, list(p)) 

else: 

for d in self._iter_der(len(self._set)): 

yield self.element_class(self, [self._set[i-1] for i in d]) 

 

def _iter_der(self, n): 

r""" 

Iterate through all derangements of the list `[1, 2, 3, \ldots, n]` 

using the method given in [DerUB]_. 

 

EXAMPLES:: 

 

sage: D = Derangements(4) 

sage: list(D._iter_der(4)) 

[[2, 3, 4, 1], 

[4, 3, 1, 2], 

[2, 4, 1, 3], 

[3, 4, 2, 1], 

[3, 1, 4, 2], 

[4, 1, 2, 3], 

[4, 3, 2, 1], 

[3, 4, 1, 2], 

[2, 1, 4, 3]] 

""" 

if n <= 1: 

return 

elif n == 2: 

yield [2,1] 

elif n == 3: 

yield [2,3,1] 

yield [3,1,2] 

elif n >= 4: 

for d in self._iter_der(n-1): 

for i in range(1, n): 

s = d[:] 

ii = d.index(i) 

s[ii] = n 

yield s + [i] 

for d in self._iter_der(n-2): 

for i in range(1, n): 

s = d[:] 

s = [x >= i and x+1 or x for x in s] 

s.insert(i-1, n) 

yield s + [i] 

 

def _fixed_point(self, a): 

""" 

Return ``True`` if ``a`` has a point in common with ``self._set``. 

 

EXAMPLES:: 

 

sage: D = Derangements(5) 

sage: D._fixed_point([3,1,2,5,4]) 

False 

sage: D._fixed_point([5,4,3,2,1]) 

True 

""" 

return any(x == y for (x, y) in zip(a, self._set)) 

 

def _count_der(self, n): 

""" 

Count the number of derangements of `n` using the recursion 

`D_2 = 1, D_3 = 2, D_n = (n-1) (D_{n-1} + D_{n-2})`. 

 

EXAMPLES:: 

 

sage: D = Derangements(5) 

sage: D._count_der(2) 

1 

sage: D._count_der(3) 

2 

sage: D._count_der(5) 

44 

""" 

if n <= 1: 

return Integer(0) 

if n == 2: 

return Integer(1) 

if n == 3: 

return Integer(2) 

# n >= 4 

last = Integer(2) 

second_last = Integer(1) 

for i in range(4, n+1): 

current = (i-1) * (last + second_last) 

second_last = last 

last = current 

return last 

 

def cardinality(self): 

r""" 

Counts the number of derangements of a positive integer, a 

list, or a string. The list or string may contain repeated 

elements. If an integer `n` is given, the value returned 

is the number of derangements of `[1, 2, 3, \ldots, n]`. 

 

For an integer, or a list or string with all elements 

distinct, the value is obtained by the standard result 

`D_2 = 1, D_3 = 2, D_n = (n-1) (D_{n-1} + D_{n-2})`. 

 

For a list or string with repeated elements, the number of 

derangements is computed by Macmahon's theorem. If the numbers 

of repeated elements are `a_1, a_2, \ldots, a_k` then the number 

of derangements is given by the coefficient of `x_1 x_2 \cdots 

x_k` in the expansion of `\prod_{i=0}^k (S - s_i)^{a_i}` where 

`S = x_1 + x_2 + \cdots + x_k`. 

 

EXAMPLES:: 

 

sage: D = Derangements(5) 

sage: D.cardinality() 

44 

sage: D = Derangements([1,44,918,67,254]) 

sage: D.cardinality() 

44 

sage: D = Derangements(['A','AT','CAT','CATS','CARTS']) 

sage: D.cardinality() 

44 

sage: D = Derangements('UNCOPYRIGHTABLE') 

sage: D.cardinality() 

481066515734 

sage: D = Derangements([1,1,2,2,3,3]) 

sage: D.cardinality() 

10 

sage: D = Derangements('SATTAS') 

sage: D.cardinality() 

10 

sage: D = Derangements([1,1,2,2,2]) 

sage: D.cardinality() 

0 

""" 

if self.__multi: 

sL = set(self._set) 

A = [self._set.count(i) for i in sL] 

R = PolynomialRing(QQ, 'x', len(A)) 

S = sum(i for i in R.gens()) 

e = prod((S-x)**y for (x, y) in zip(R.gens(), A)) 

return Integer(e.coefficient(dict([(x, y) for (x, y) in zip(R.gens(), A)]))) 

return self._count_der(len(self._set)) 

 

def _rand_der(self): 

""" 

Produces a random derangement of `[1, 2, \ldots, n]`. 

 

This is an 

implementation of the algorithm described by Martinez et. al. in 

[Martinez08]_. 

 

EXAMPLES:: 

 

sage: D = Derangements(4) 

sage: D._rand_der() 

[2, 3, 4, 1] 

""" 

n = len(self._set) 

A = list(range(1, n + 1)) 

mark = [x<0 for x in A] 

i,u = n,n 

while u >= 2: 

if not(mark[i-1]): 

while True: 

j = randint(1,i-1) 

if not(mark[j-1]): 

A[i-1], A[j-1] = A[j-1], A[i-1] 

break 

p = random() 

if p < (u-1) * self._count_der(u-2) // self._count_der(u): 

mark[j-1] = True 

u -= 1 

u -= 1 

i -= 1 

return A 

 

def random_element(self): 

r""" 

Produces all derangements of a positive integer, a list, or 

a string. The list or string may contain repeated elements. 

If an integer `n` is given, then a random 

derangements of `[1, 2, 3, \ldots, n]` is returned 

 

For an integer, or a list or string with all elements 

distinct, the value is obtained by an algorithm described in 

[Martinez08]_. For a list or string with repeated elements the 

derangement is formed by choosing an element at random from the list of 

all possible derangements. 

 

OUTPUT: 

 

A single list or string containing a derangement, or an 

empty list if there are no derangements. 

 

REFERENCES: 

 

.. [Martinez08] 

http://www.siam.org/proceedings/analco/2008/anl08_022martinezc.pdf 

 

EXAMPLES:: 

 

sage: D = Derangements(4) 

sage: D.random_element() # random 

[2, 3, 4, 1] 

sage: D = Derangements(['A','AT','CAT','CATS','CARTS','CARETS']) 

sage: D.random_element() # random 

['AT', 'CARTS', 'A', 'CAT', 'CARETS', 'CATS'] 

sage: D = Derangements('UNCOPYRIGHTABLE') 

sage: D.random_element() # random 

['C', 'U', 'I', 'H', 'O', 'G', 'N', 'B', 'E', 'L', 'A', 'R', 'P', 'Y', 'T'] 

sage: D = Derangements([1,1,1,1,2,2,2,2,3,3,3,3]) 

sage: D.random_element() # random 

[3, 2, 2, 3, 1, 3, 1, 3, 2, 1, 1, 2] 

sage: D = Derangements('ESSENCES') 

sage: D.random_element() # random 

['N', 'E', 'E', 'C', 'S', 'S', 'S', 'E'] 

sage: D = Derangements([1,1,2,2,2]) 

sage: D.random_element() 

[] 

""" 

if self.__multi: 

L = list(self) 

if len(L) == 0: 

return self.element_class(self, []) 

i = randint(0, len(L)) 

return L[i] 

temp = self._rand_der() 

return self.element_class(self, [self._set[i-1] for i in temp])