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

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

r""" 

Ordered Set Partitions 

 

AUTHORS: 

 

- Mike Hansen 

 

- MuPAD-Combinat developers (for algorithms and design inspiration) 

 

- Travis Scrimshaw (2013-02-28): Removed ``CombinatorialClass`` and added 

entry point through :class:`OrderedSetPartition`. 

""" 

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

# Copyright (C) 2007 Mike Hansen <mhansen@gmail.com>, 

# 

# 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 import add_metaclass 

 

from sage.arith.all import factorial 

import sage.rings.integer 

from sage.sets.set import Set, Set_generic 

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets 

from sage.misc.inherit_comparison import InheritComparisonClasscallMetaclass 

from sage.misc.all import prod 

from sage.structure.parent import Parent 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.structure.list_clone import ClonableArray 

from sage.combinat.combinatorial_map import combinatorial_map 

from sage.combinat.combinat import stirling_number2 

from sage.combinat.composition import Composition, Compositions 

import sage.combinat.permutation as permutation 

from functools import reduce 

 

 

@add_metaclass(InheritComparisonClasscallMetaclass) 

class OrderedSetPartition(ClonableArray): 

""" 

An ordered partition of a set. 

 

An ordered set partition `p` of a set `s` is a list of pairwise 

disjoint nonempty subsets of `s` such that the union of these 

subsets is `s`. These subsets are called the parts of the partition. 

We represent an ordered set partition as a list of sets. By 

extension, an ordered set partition of a nonnegative integer `n` is 

the set partition of the integers from `1` to `n`. The number of 

ordered set partitions of `n` is called the `n`-th ordered Bell 

number. 

 

There is a natural integer composition associated with an ordered 

set partition, that is the sequence of sizes of all its parts in 

order. 

 

The number `T_n` of ordered set partitions of 

`\{ 1, 2, ..., n \}` is the so-called `n`-th *Fubini number* (also 

known as the `n`-th ordered Bell number; see 

:wikipedia:`Ordered Bell number`). Its exponential generating 

function is 

 

.. MATH:: 

 

\sum_n {T_n \over n!} x^n = {1 \over 2-e^x}. 

 

(See sequence A000670 in OEIS.) 

 

EXAMPLES: 

 

There are 13 ordered set partitions of `\{1,2,3\}`:: 

 

sage: OrderedSetPartitions(3).cardinality() 

13 

 

Here is the list of them:: 

 

sage: OrderedSetPartitions(3).list() 

[[{1}, {2}, {3}], 

[{1}, {3}, {2}], 

[{2}, {1}, {3}], 

[{3}, {1}, {2}], 

[{2}, {3}, {1}], 

[{3}, {2}, {1}], 

[{1}, {2, 3}], 

[{2}, {1, 3}], 

[{3}, {1, 2}], 

[{1, 2}, {3}], 

[{1, 3}, {2}], 

[{2, 3}, {1}], 

[{1, 2, 3}]] 

 

There are 12 ordered set partitions of `\{1,2,3,4\}` whose underlying 

composition is `[1,2,1]`:: 

 

sage: OrderedSetPartitions(4,[1,2,1]).list() 

[[{1}, {2, 3}, {4}], 

[{1}, {2, 4}, {3}], 

[{1}, {3, 4}, {2}], 

[{2}, {1, 3}, {4}], 

[{2}, {1, 4}, {3}], 

[{3}, {1, 2}, {4}], 

[{4}, {1, 2}, {3}], 

[{3}, {1, 4}, {2}], 

[{4}, {1, 3}, {2}], 

[{2}, {3, 4}, {1}], 

[{3}, {2, 4}, {1}], 

[{4}, {2, 3}, {1}]] 

 

Since :trac:`14140`, we can create an ordered set partition directly by 

:class:`OrderedSetPartition` which creates the parent object by taking the 

union of the partitions passed in. However it is recommended and 

(marginally) faster to create the parent first and then create the ordered 

set partition from that. :: 

 

sage: s = OrderedSetPartition([[1,3],[2,4]]); s 

[{1, 3}, {2, 4}] 

sage: s.parent() 

Ordered set partitions of {1, 2, 3, 4} 

 

REFERENCES: 

 

:wikipedia:`Ordered_partition_of_a_set` 

""" 

@staticmethod 

def __classcall_private__(cls, parts): 

""" 

Create a set partition from ``parts`` with the appropriate parent. 

 

EXAMPLES:: 

 

sage: s = OrderedSetPartition([[1,3],[2,4]]); s 

[{1, 3}, {2, 4}] 

sage: s.parent() 

Ordered set partitions of {1, 2, 3, 4} 

sage: t = OrderedSetPartition([[2,4],[1,3]]); t 

[{2, 4}, {1, 3}] 

sage: s != t 

True 

sage: OrderedSetPartition([]) 

[] 

""" 

P = OrderedSetPartitions( reduce(lambda x,y: x.union(y), map(Set, parts), Set([])) ) 

return P.element_class(P, parts) 

 

def __init__(self, parent, s): 

""" 

Initialize ``self``. 

 

EXAMPLES:: 

 

sage: OS = OrderedSetPartitions(4) 

sage: s = OS([[1, 3], [2, 4]]) 

sage: TestSuite(s).run() 

""" 

ClonableArray.__init__(self, parent, [Set(_) for _ in s]) 

 

def check(self): 

""" 

Check that we are a valid ordered set partition. 

 

EXAMPLES:: 

 

sage: OS = OrderedSetPartitions(4) 

sage: s = OS([[1, 3], [2, 4]]) 

sage: s.check() 

""" 

assert self in self.parent() 

 

@combinatorial_map(name='to composition') 

def to_composition(self): 

r""" 

Return the integer composition whose parts are the sizes of the sets 

in ``self``. 

 

EXAMPLES:: 

 

sage: S = OrderedSetPartitions(5) 

sage: x = S([[3,5,4], [1, 2]]) 

sage: x.to_composition() 

[3, 2] 

sage: y = S([[3,1], [2], [5,4]]) 

sage: y.to_composition() 

[2, 1, 2] 

""" 

return Composition([len(_) for _ in self]) 

 

class OrderedSetPartitions(UniqueRepresentation, Parent): 

""" 

Return the combinatorial class of ordered set partitions of ``s``. 

 

EXAMPLES:: 

 

sage: OS = OrderedSetPartitions([1,2,3,4]); OS 

Ordered set partitions of {1, 2, 3, 4} 

sage: OS.cardinality() 

75 

sage: OS.first() 

[{1}, {2}, {3}, {4}] 

sage: OS.last() 

[{1, 2, 3, 4}] 

sage: OS.random_element() 

[{3}, {1}, {2}, {4}] 

 

:: 

 

sage: OS = OrderedSetPartitions([1,2,3,4], [2,2]); OS 

Ordered set partitions of {1, 2, 3, 4} into parts of size [2, 2] 

sage: OS.cardinality() 

6 

sage: OS.first() 

[{1, 2}, {3, 4}] 

sage: OS.last() 

[{3, 4}, {1, 2}] 

sage: OS.list() 

[[{1, 2}, {3, 4}], 

[{1, 3}, {2, 4}], 

[{1, 4}, {2, 3}], 

[{2, 3}, {1, 4}], 

[{2, 4}, {1, 3}], 

[{3, 4}, {1, 2}]] 

 

:: 

 

sage: OS = OrderedSetPartitions("cat"); OS 

Ordered set partitions of {'a', 'c', 't'} 

sage: OS.list() 

[[{'a'}, {'c'}, {'t'}], 

[{'a'}, {'t'}, {'c'}], 

[{'c'}, {'a'}, {'t'}], 

[{'t'}, {'a'}, {'c'}], 

[{'c'}, {'t'}, {'a'}], 

[{'t'}, {'c'}, {'a'}], 

[{'a'}, {'c', 't'}], 

[{'c'}, {'a', 't'}], 

[{'t'}, {'a', 'c'}], 

[{'a', 'c'}, {'t'}], 

[{'a', 't'}, {'c'}], 

[{'c', 't'}, {'a'}], 

[{'a', 'c', 't'}]] 

""" 

@staticmethod 

def __classcall_private__(cls, s, c=None): 

""" 

Choose the correct parent based upon input. 

 

EXAMPLES:: 

 

sage: OrderedSetPartitions(4) 

Ordered set partitions of {1, 2, 3, 4} 

sage: OrderedSetPartitions(4, [1, 2, 1]) 

Ordered set partitions of {1, 2, 3, 4} into parts of size [1, 2, 1] 

""" 

if isinstance(s, (int, sage.rings.integer.Integer)): 

if s < 0: 

raise ValueError("s must be non-negative") 

s = frozenset(range(1, s+1)) 

else: 

s = frozenset(s) 

 

if c is None: 

return OrderedSetPartitions_s(s) 

 

if isinstance(c, (int, sage.rings.integer.Integer)): 

return OrderedSetPartitions_sn(s, c) 

if c not in Compositions(len(s)): 

raise ValueError("c must be a composition of %s"%len(s)) 

return OrderedSetPartitions_scomp(s, Composition(c)) 

 

def __init__(self, s): 

""" 

Initialize ``self``. 

 

EXAMPLES:: 

 

sage: OS = OrderedSetPartitions(4) 

sage: TestSuite(OS).run() 

""" 

self._set = s 

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

 

def _element_constructor_(self, s): 

""" 

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

 

EXAMPLES:: 

 

sage: OS = OrderedSetPartitions(4) 

sage: OS([[1,3],[2,4]]) 

[{1, 3}, {2, 4}] 

""" 

if isinstance(s, OrderedSetPartition): 

if s.parent() == self: 

return s 

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

return self.element_class(self, list(s)) 

 

Element = OrderedSetPartition 

 

def __contains__(self, x): 

""" 

TESTS:: 

 

sage: OS = OrderedSetPartitions([1,2,3,4]) 

sage: all(sp in OS for sp in OS) 

True 

""" 

#x must be a list 

if not isinstance(x, (OrderedSetPartition, list, tuple)): 

return False 

 

#The total number of elements in the list 

#should be the same as the number is self._set 

if sum(map(len, x)) != len(self._set): 

return False 

 

#Check to make sure each element of the list 

#is a set 

u = Set([]) 

for s in x: 

if not isinstance(s, (set, frozenset, Set_generic)): 

return False 

u = u.union(s) 

 

#Make sure that the union of all the 

#sets is the original set 

if u != Set(self._set): 

return False 

 

return True 

 

class OrderedSetPartitions_s(OrderedSetPartitions): 

""" 

Class of ordered partitions of a set `S`. 

""" 

def _repr_(self): 

""" 

TESTS:: 

 

sage: OrderedSetPartitions([1,2,3,4]) 

Ordered set partitions of {1, 2, 3, 4} 

""" 

return "Ordered set partitions of %s"%Set(self._set) 

 

def cardinality(self): 

""" 

EXAMPLES:: 

 

sage: OrderedSetPartitions(0).cardinality() 

1 

sage: OrderedSetPartitions(1).cardinality() 

1 

sage: OrderedSetPartitions(2).cardinality() 

3 

sage: OrderedSetPartitions(3).cardinality() 

13 

sage: OrderedSetPartitions([1,2,3]).cardinality() 

13 

sage: OrderedSetPartitions(4).cardinality() 

75 

sage: OrderedSetPartitions(5).cardinality() 

541 

""" 

return sum([factorial(k)*stirling_number2(len(self._set),k) for k in range(len(self._set)+1)]) 

 

def __iter__(self): 

""" 

EXAMPLES:: 

 

sage: [ p for p in OrderedSetPartitions([1,2,3]) ] 

[[{1}, {2}, {3}], 

[{1}, {3}, {2}], 

[{2}, {1}, {3}], 

[{3}, {1}, {2}], 

[{2}, {3}, {1}], 

[{3}, {2}, {1}], 

[{1}, {2, 3}], 

[{2}, {1, 3}], 

[{3}, {1, 2}], 

[{1, 2}, {3}], 

[{1, 3}, {2}], 

[{2, 3}, {1}], 

[{1, 2, 3}]] 

""" 

for x in Compositions(len(self._set)): 

for z in OrderedSetPartitions(self._set, x): 

yield self.element_class(self, z) 

 

class OrderedSetPartitions_sn(OrderedSetPartitions): 

def __init__(self, s, n): 

""" 

TESTS:: 

 

sage: OS = OrderedSetPartitions([1,2,3,4], 2) 

sage: OS == loads(dumps(OS)) 

True 

""" 

OrderedSetPartitions.__init__(self, s) 

self.n = n 

 

def __contains__(self, x): 

""" 

TESTS:: 

 

sage: OS = OrderedSetPartitions([1,2,3,4], 2) 

sage: all(sp in OS for sp in OS) 

True 

sage: OS.cardinality() 

14 

sage: len(filter(lambda x: x in OS, OrderedSetPartitions([1,2,3,4]))) 

14 

""" 

return OrderedSetPartitions.__contains__(self, x) and len(x) == self.n 

 

def __repr__(self): 

""" 

TESTS:: 

 

sage: OrderedSetPartitions([1,2,3,4], 2) 

Ordered set partitions of {1, 2, 3, 4} into 2 parts 

""" 

return "Ordered set partitions of %s into %s parts"%(Set(self._set),self.n) 

 

def cardinality(self): 

""" 

Return the cardinality of ``self``. 

 

The number of ordered partitions of a set of size `n` into `k` 

parts is equal to `k! S(n,k)` where `S(n,k)` denotes the Stirling 

number of the second kind. 

 

EXAMPLES:: 

 

sage: OrderedSetPartitions(4,2).cardinality() 

14 

sage: OrderedSetPartitions(4,1).cardinality() 

1 

""" 

return factorial(self.n)*stirling_number2(len(self._set), self.n) 

 

def __iter__(self): 

""" 

EXAMPLES:: 

 

sage: [ p for p in OrderedSetPartitions([1,2,3,4], 2) ] 

[[{1, 2, 3}, {4}], 

[{1, 2, 4}, {3}], 

[{1, 3, 4}, {2}], 

[{2, 3, 4}, {1}], 

[{1, 2}, {3, 4}], 

[{1, 3}, {2, 4}], 

[{1, 4}, {2, 3}], 

[{2, 3}, {1, 4}], 

[{2, 4}, {1, 3}], 

[{3, 4}, {1, 2}], 

[{1}, {2, 3, 4}], 

[{2}, {1, 3, 4}], 

[{3}, {1, 2, 4}], 

[{4}, {1, 2, 3}]] 

""" 

for x in Compositions(len(self._set),length=self.n): 

for z in OrderedSetPartitions_scomp(self._set,x): 

yield self.element_class(self, z) 

 

class OrderedSetPartitions_scomp(OrderedSetPartitions): 

def __init__(self, s, comp): 

""" 

TESTS:: 

 

sage: OS = OrderedSetPartitions([1,2,3,4], [2,1,1]) 

sage: OS == loads(dumps(OS)) 

True 

""" 

OrderedSetPartitions.__init__(self, s) 

self.c = Composition(comp) 

 

def __repr__(self): 

""" 

TESTS:: 

 

sage: OrderedSetPartitions([1,2,3,4], [2,1,1]) 

Ordered set partitions of {1, 2, 3, 4} into parts of size [2, 1, 1] 

""" 

return "Ordered set partitions of %s into parts of size %s"%(Set(self._set), self.c) 

 

def __contains__(self, x): 

""" 

TESTS:: 

 

sage: OS = OrderedSetPartitions([1,2,3,4], [2,1,1]) 

sage: all(sp in OS for sp in OS) 

True 

sage: OS.cardinality() 

12 

sage: len(filter(lambda x: x in OS, OrderedSetPartitions([1,2,3,4]))) 

12 

""" 

return OrderedSetPartitions.__contains__(self, x) and [len(_) for _ in x] == self.c 

 

def cardinality(self): 

r""" 

Return the cardinality of ``self``. 

 

The number of ordered set partitions of a set of length `k` with 

composition shape `\mu` is equal to 

 

.. MATH:: 

 

\frac{k!}{\prod_{\mu_i \neq 0} \mu_i!}. 

 

EXAMPLES:: 

 

sage: OrderedSetPartitions(5,[2,3]).cardinality() 

10 

sage: OrderedSetPartitions(0, []).cardinality() 

1 

sage: OrderedSetPartitions(0, [0]).cardinality() 

1 

sage: OrderedSetPartitions(0, [0,0]).cardinality() 

1 

sage: OrderedSetPartitions(5, [2,0,3]).cardinality() 

10 

""" 

return factorial(len(self._set))/prod([factorial(i) for i in self.c]) 

 

def __iter__(self): 

""" 

TESTS:: 

 

sage: [ p for p in OrderedSetPartitions([1,2,3,4], [2,1,1]) ] 

[[{1, 2}, {3}, {4}], 

[{1, 2}, {4}, {3}], 

[{1, 3}, {2}, {4}], 

[{1, 4}, {2}, {3}], 

[{1, 3}, {4}, {2}], 

[{1, 4}, {3}, {2}], 

[{2, 3}, {1}, {4}], 

[{2, 4}, {1}, {3}], 

[{3, 4}, {1}, {2}], 

[{2, 3}, {4}, {1}], 

[{2, 4}, {3}, {1}], 

[{3, 4}, {2}, {1}]] 

 

sage: len(OrderedSetPartitions([1,2,3,4], [1,1,1,1])) 

24 

 

sage: [ x for x in OrderedSetPartitions([1,4,7], [3]) ] 

[[{1, 4, 7}]] 

 

sage: [ x for x in OrderedSetPartitions([1,4,7], [1,2]) ] 

[[{1}, {4, 7}], [{4}, {1, 7}], [{7}, {1, 4}]] 

 

sage: [ p for p in OrderedSetPartitions([], []) ] 

[[]] 

 

sage: [ p for p in OrderedSetPartitions([1], [1]) ] 

[[{1}]] 

 

Let us check that it works for large size (:trac:`16646`):: 

 

sage: OrderedSetPartitions(42).first() 

[{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}] 

""" 

comp = self.c 

lset = [x for x in self._set] 

l = len(self.c) 

dcomp = [-1] + comp.descents(final_descent=True) 

 

p = [] 

for j in range(l): 

p += [j + 1] * comp[j] 

 

for x in permutation.Permutations(p): 

res = permutation.to_standard(x).inverse() 

res = [lset[x - 1] for x in res] 

yield self.element_class(self, [Set(res[dcomp[i]+1:dcomp[i+1]+1]) 

for i in range(l)]) 

 

########################################################## 

# Deprecations 

 

 

class SplitNK(OrderedSetPartitions_scomp): 

def __setstate__(self, state): 

r""" 

For unpickling old ``SplitNK`` objects. 

 

TESTS:: 

 

sage: loads(b"x\x9ck`J.NLO\xd5K\xce\xcfM\xca\xccK,\xd1+.\xc8\xc9," 

....: b"\x89\xcf\xcb\xe6\n\x061\xfc\xbcA\xccBF\xcd\xc6B\xa6\xda" 

....: b"Bf\x8dP\xa6\xf8\xbcB\x16\x88\x96\xa2\xcc\xbc\xf4b\xbd\xcc" 

....: b"\xbc\x92\xd4\xf4\xd4\"\xae\xdc\xc4\xec\xd4x\x18\xa7\x905" 

....: b"\x94\xd1\xb45\xa8\x90\r\xa8>\xbb\x90=\x03\xc85\x02r9J\x93" 

....: b"\xf4\x00\xb4\xc6%f") 

Ordered set partitions of {0, 1, 2, 3, 4} into parts of size [2, 3] 

""" 

self.__class__ = OrderedSetPartitions_scomp 

n = state['_n'] 

k = state['_k'] 

OrderedSetPartitions_scomp.__init__(self, range(state['_n']), (k,n-k)) 

 

from sage.structure.sage_object import register_unpickle_override 

register_unpickle_override("sage.combinat.split_nk", "SplitNK_nk", SplitNK)