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

from __future__ import print_function, division 

 

from itertools import combinations 

 

from sympy.core import Basic 

from sympy.combinatorics.graycode import GrayCode 

from sympy.core.compatibility import range 

 

 

class Subset(Basic): 

""" 

Represents a basic subset object. 

 

We generate subsets using essentially two techniques, 

binary enumeration and lexicographic enumeration. 

The Subset class takes two arguments, the first one 

describes the initial subset to consider and the second 

describes the superset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.next_binary().subset 

['b'] 

>>> a.prev_binary().subset 

['c'] 

""" 

 

_rank_binary = None 

_rank_lex = None 

_rank_graycode = None 

_subset = None 

_superset = None 

 

def __new__(cls, subset, superset): 

""" 

Default constructor. 

 

It takes the subset and its superset as its parameters. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.subset 

['c', 'd'] 

>>> a.superset 

['a', 'b', 'c', 'd'] 

>>> a.size 

2 

""" 

if len(subset) > len(superset): 

raise ValueError('Invalid arguments have been provided. The superset must be larger than the subset.') 

for elem in subset: 

if elem not in superset: 

raise ValueError('The superset provided is invalid as it does not contain the element %i' % elem) 

obj = Basic.__new__(cls) 

obj._subset = subset 

obj._superset = superset 

return obj 

 

def iterate_binary(self, k): 

""" 

This is a helper function. It iterates over the 

binary subsets by k steps. This variable can be 

both positive or negative. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.iterate_binary(-2).subset 

['d'] 

>>> a = Subset(['a', 'b', 'c'], ['a', 'b', 'c', 'd']) 

>>> a.iterate_binary(2).subset 

[] 

 

See Also 

======== 

next_binary, prev_binary 

""" 

bin_list = Subset.bitlist_from_subset(self.subset, self.superset) 

n = (int(''.join(bin_list), 2) + k) % 2**self.superset_size 

bits = bin(n)[2:].rjust(self.superset_size, '0') 

return Subset.subset_from_bitlist(self.superset, bits) 

 

def next_binary(self): 

""" 

Generates the next binary ordered subset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.next_binary().subset 

['b'] 

>>> a = Subset(['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.next_binary().subset 

[] 

 

See Also 

======== 

prev_binary, iterate_binary 

""" 

return self.iterate_binary(1) 

 

def prev_binary(self): 

""" 

Generates the previous binary ordered subset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset([], ['a', 'b', 'c', 'd']) 

>>> a.prev_binary().subset 

['a', 'b', 'c', 'd'] 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.prev_binary().subset 

['c'] 

 

See Also 

======== 

next_binary, iterate_binary 

""" 

return self.iterate_binary(-1) 

 

def next_lexicographic(self): 

""" 

Generates the next lexicographically ordered subset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.next_lexicographic().subset 

['d'] 

>>> a = Subset(['d'], ['a', 'b', 'c', 'd']) 

>>> a.next_lexicographic().subset 

[] 

 

See Also 

======== 

prev_lexicographic 

""" 

i = self.superset_size - 1 

indices = Subset.subset_indices(self.subset, self.superset) 

 

if i in indices: 

if i - 1 in indices: 

indices.remove(i - 1) 

else: 

indices.remove(i) 

i = i - 1 

while not i in indices and i >= 0: 

i = i - 1 

if i >= 0: 

indices.remove(i) 

indices.append(i+1) 

else: 

while i not in indices and i >= 0: 

i = i - 1 

indices.append(i + 1) 

 

ret_set = [] 

super_set = self.superset 

for i in indices: 

ret_set.append(super_set[i]) 

return Subset(ret_set, super_set) 

 

def prev_lexicographic(self): 

""" 

Generates the previous lexicographically ordered subset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset([], ['a', 'b', 'c', 'd']) 

>>> a.prev_lexicographic().subset 

['d'] 

>>> a = Subset(['c','d'], ['a', 'b', 'c', 'd']) 

>>> a.prev_lexicographic().subset 

['c'] 

 

See Also 

======== 

next_lexicographic 

""" 

i = self.superset_size - 1 

indices = Subset.subset_indices(self.subset, self.superset) 

 

while i not in indices and i >= 0: 

i = i - 1 

 

if i - 1 in indices or i == 0: 

indices.remove(i) 

else: 

if i >= 0: 

indices.remove(i) 

indices.append(i - 1) 

indices.append(self.superset_size - 1) 

 

ret_set = [] 

super_set = self.superset 

for i in indices: 

ret_set.append(super_set[i]) 

return Subset(ret_set, super_set) 

 

def iterate_graycode(self, k): 

""" 

Helper function used for prev_gray and next_gray. 

It performs k step overs to get the respective Gray codes. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset([1, 2, 3], [1, 2, 3, 4]) 

>>> a.iterate_graycode(3).subset 

[1, 4] 

>>> a.iterate_graycode(-2).subset 

[1, 2, 4] 

 

See Also 

======== 

next_gray, prev_gray 

""" 

unranked_code = GrayCode.unrank(self.superset_size, 

(self.rank_gray + k) % self.cardinality) 

return Subset.subset_from_bitlist(self.superset, 

unranked_code) 

 

def next_gray(self): 

""" 

Generates the next Gray code ordered subset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset([1, 2, 3], [1, 2, 3, 4]) 

>>> a.next_gray().subset 

[1, 3] 

 

See Also 

======== 

iterate_graycode, prev_gray 

""" 

return self.iterate_graycode(1) 

 

def prev_gray(self): 

""" 

Generates the previous Gray code ordered subset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset([2, 3, 4], [1, 2, 3, 4, 5]) 

>>> a.prev_gray().subset 

[2, 3, 4, 5] 

 

See Also 

======== 

iterate_graycode, next_gray 

""" 

return self.iterate_graycode(-1) 

 

@property 

def rank_binary(self): 

""" 

Computes the binary ordered rank. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset([], ['a','b','c','d']) 

>>> a.rank_binary 

0 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.rank_binary 

3 

 

See Also 

======== 

iterate_binary, unrank_binary 

""" 

if self._rank_binary is None: 

self._rank_binary = int("".join( 

Subset.bitlist_from_subset(self.subset, 

self.superset)), 2) 

return self._rank_binary 

 

@property 

def rank_lexicographic(self): 

""" 

Computes the lexicographic ranking of the subset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.rank_lexicographic 

14 

>>> a = Subset([2, 4, 5], [1, 2, 3, 4, 5, 6]) 

>>> a.rank_lexicographic 

43 

""" 

if self._rank_lex is None: 

def _ranklex(self, subset_index, i, n): 

if subset_index == [] or i > n: 

return 0 

if i in subset_index: 

subset_index.remove(i) 

return 1 + _ranklex(self, subset_index, i + 1, n) 

return 2**(n - i - 1) + _ranklex(self, subset_index, i + 1, n) 

indices = Subset.subset_indices(self.subset, self.superset) 

self._rank_lex = _ranklex(self, indices, 0, self.superset_size) 

return self._rank_lex 

 

@property 

def rank_gray(self): 

""" 

Computes the Gray code ranking of the subset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset(['c','d'], ['a','b','c','d']) 

>>> a.rank_gray 

2 

>>> a = Subset([2, 4, 5], [1, 2, 3, 4, 5, 6]) 

>>> a.rank_gray 

27 

 

See Also 

======== 

iterate_graycode, unrank_gray 

""" 

if self._rank_graycode is None: 

bits = Subset.bitlist_from_subset(self.subset, self.superset) 

self._rank_graycode = GrayCode(len(bits), start=bits).rank 

return self._rank_graycode 

 

@property 

def subset(self): 

""" 

Gets the subset represented by the current instance. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.subset 

['c', 'd'] 

 

See Also 

======== 

superset, size, superset_size, cardinality 

""" 

return self._subset 

 

@property 

def size(self): 

""" 

Gets the size of the subset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.size 

2 

 

See Also 

======== 

subset, superset, superset_size, cardinality 

""" 

return len(self.subset) 

 

@property 

def superset(self): 

""" 

Gets the superset of the subset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.superset 

['a', 'b', 'c', 'd'] 

 

See Also 

======== 

subset, size, superset_size, cardinality 

""" 

return self._superset 

 

@property 

def superset_size(self): 

""" 

Returns the size of the superset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.superset_size 

4 

 

See Also 

======== 

subset, superset, size, cardinality 

""" 

return len(self.superset) 

 

@property 

def cardinality(self): 

""" 

Returns the number of all possible subsets. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

>>> a.cardinality 

16 

 

See Also 

======== 

subset, superset, size, superset_size 

""" 

return 2**(self.superset_size) 

 

@classmethod 

def subset_from_bitlist(self, super_set, bitlist): 

""" 

Gets the subset defined by the bitlist. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> Subset.subset_from_bitlist(['a', 'b', 'c', 'd'], '0011').subset 

['c', 'd'] 

 

See Also 

======== 

bitlist_from_subset 

""" 

if len(super_set) != len(bitlist): 

raise ValueError("The sizes of the lists are not equal") 

ret_set = [] 

for i in range(len(bitlist)): 

if bitlist[i] == '1': 

ret_set.append(super_set[i]) 

return Subset(ret_set, super_set) 

 

@classmethod 

def bitlist_from_subset(self, subset, superset): 

""" 

Gets the bitlist corresponding to a subset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> Subset.bitlist_from_subset(['c', 'd'], ['a', 'b', 'c', 'd']) 

'0011' 

 

See Also 

======== 

subset_from_bitlist 

""" 

bitlist = ['0'] * len(superset) 

if type(subset) is Subset: 

subset = subset.args[0] 

for i in Subset.subset_indices(subset, superset): 

bitlist[i] = '1' 

return ''.join(bitlist) 

 

@classmethod 

def unrank_binary(self, rank, superset): 

""" 

Gets the binary ordered subset of the specified rank. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> Subset.unrank_binary(4, ['a', 'b', 'c', 'd']).subset 

['b'] 

 

See Also 

======== 

iterate_binary, rank_binary 

""" 

bits = bin(rank)[2:].rjust(len(superset), '0') 

return Subset.subset_from_bitlist(superset, bits) 

 

@classmethod 

def unrank_gray(self, rank, superset): 

""" 

Gets the Gray code ordered subset of the specified rank. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import Subset 

>>> Subset.unrank_gray(4, ['a', 'b', 'c']).subset 

['a', 'b'] 

>>> Subset.unrank_gray(0, ['a', 'b', 'c']).subset 

[] 

 

See Also 

======== 

iterate_graycode, rank_gray 

""" 

graycode_bitlist = GrayCode.unrank(len(superset), rank) 

return Subset.subset_from_bitlist(superset, graycode_bitlist) 

 

@classmethod 

def subset_indices(self, subset, superset): 

"""Return indices of subset in superset in a list; the list is empty 

if all elements of subset are not in superset. 

 

Examples 

======== 

 

>>> from sympy.combinatorics import Subset 

>>> superset = [1, 3, 2, 5, 4] 

>>> Subset.subset_indices([3, 2, 1], superset) 

[1, 2, 0] 

>>> Subset.subset_indices([1, 6], superset) 

[] 

>>> Subset.subset_indices([], superset) 

[] 

 

""" 

a, b = superset, subset 

sb = set(b) 

d = {} 

for i, ai in enumerate(a): 

if ai in sb: 

d[ai] = i 

sb.remove(ai) 

if not sb: 

break 

else: 

return list() 

return [d[bi] for bi in b] 

 

 

def ksubsets(superset, k): 

""" 

Finds the subsets of size k in lexicographic order. 

 

This uses the itertools generator. 

 

Examples 

======== 

 

>>> from sympy.combinatorics.subsets import ksubsets 

>>> list(ksubsets([1, 2, 3], 2)) 

[(1, 2), (1, 3), (2, 3)] 

>>> list(ksubsets([1, 2, 3, 4, 5], 2)) 

[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), \ 

(2, 5), (3, 4), (3, 5), (4, 5)] 

 

See Also 

======== 

class:Subset 

""" 

return combinations(superset, k)