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

""" 

Baxter permutations 

""" 

from six.moves import range 

 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.structure.parent import Parent 

from sage.sets.disjoint_union_enumerated_sets import DisjointUnionEnumeratedSets 

from sage.combinat.permutation import Permutations 

 

from sage.rings.integer_ring import ZZ 

 

 

class BaxterPermutations(UniqueRepresentation, Parent): 

r""" 

The combinatorial class of Baxter permutations. 

 

A Baxter permutation is a permutation avoiding the generalized 

permutation patterns `2-41-3` and `3-14-2`. In other words, a 

permutation `\sigma` is a Baxter permutation if for any subword `u 

:= u_1u_2u_3u_4` of `\sigma` such that the letters `u_2` and `u_3` 

are adjacent in `\sigma`, the standardized version of `u` is 

neither `2413` nor `3142`. 

 

See [Gir12]_ for a study of Baxter permutations. 

 

INPUT: 

 

- ``n`` -- (default: ``None``) a nonnegative integer, the size of 

the permutations. 

 

OUTPUT: 

 

Return the combinatorial class of the Baxter permutations of size ``n`` 

if ``n`` is not ``None``. Otherwise, return the combinatorial class 

of all Baxter permutations. 

 

EXAMPLES:: 

 

sage: BaxterPermutations(5) 

Baxter permutations of size 5 

sage: BaxterPermutations() 

Baxter permutations 

 

REFERENCES: 

 

.. [Gir12] Samuele Giraudo, 

*Algebraic and combinatorial structures on pairs of twin binary trees*, 

:arxiv:`1204.4776v1`. 

""" 

@staticmethod 

def __classcall_private__(classe, n=None): 

""" 

EXAMPLES:: 

 

sage: BaxterPermutations(5) 

Baxter permutations of size 5 

sage: BaxterPermutations() 

Baxter permutations 

""" 

if n is None: 

return BaxterPermutations_all() 

return BaxterPermutations_size(n) 

 

 

class BaxterPermutations_size(BaxterPermutations): 

r""" 

The enumerated set of Baxter permutations of a given size. 

 

See :class:`BaxterPermutations` for the definition of Baxter 

permutations. 

 

EXAMPLES:: 

 

sage: from sage.combinat.baxter_permutations import BaxterPermutations_size 

sage: BaxterPermutations_size(5) 

Baxter permutations of size 5 

""" 

def __init__(self, n): 

""" 

EXAMPLES:: 

 

sage: from sage.combinat.baxter_permutations import BaxterPermutations_size 

sage: BaxterPermutations_size(5) 

Baxter permutations of size 5 

""" 

self.element_class = Permutations(n).element_class 

self._n = ZZ(n) 

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets 

super(BaxterPermutations, self).__init__(category=FiniteEnumeratedSets()) 

 

def _repr_(self): 

""" 

Return a string representation of ``self`` 

 

EXAMPLES:: 

 

sage: from sage.combinat.baxter_permutations import BaxterPermutations_size 

sage: BaxterPermutations_size(5) 

Baxter permutations of size 5 

""" 

return "Baxter permutations of size %s" % self._n 

 

def __contains__(self, x): 

r""" 

Return ``True`` if and only if ``x`` is a Baxter permutation of 

size ``self._n``. 

 

INPUT: 

 

- ``x`` -- a permutation. 

 

EXAMPLES:: 

 

sage: Permutation([2, 1, 4, 3]) in BaxterPermutations(4) 

True 

sage: Permutation([2, 1, 4, 3]) in BaxterPermutations(5) 

False 

sage: Permutation([3, 1, 4, 2]) in BaxterPermutations(4) 

False 

sage: [len([p for p in Permutations(n) if p in BaxterPermutations(n)]) for n in range(7)] 

[1, 1, 2, 6, 22, 92, 422] 

sage: sorted([p for p in Permutations(6) if p in BaxterPermutations(6)]) == sorted(BaxterPermutations(6).list()) 

True 

""" 

if not x in Permutations(self._n): 

return False 

for i in range(1, len(x) - 1): 

a = x[i] 

b = x[i + 1] 

if a < b: # Hunting pattern 3-14-2. 

max_l = 0 

for x_j in x[:i]: 

if x_j > a and x_j < b and x_j > max_l: 

max_l = x_j 

min_r = len(x) + 1 

for x_j in x[i+2:]: 

if x_j > a and x_j < b and x_j < min_r: 

min_r = x_j 

if max_l > min_r: 

return False 

else: # Hunting pattern 2-41-3. 

min_l = len(x) + 1 

for x_j in x[:i]: 

if x_j < a and x_j > b and x_j < min_l: 

min_l = x_j 

max_r = 0 

for x_j in x[i+2:]: 

if x_j < a and x_j > b and x_j > max_r: 

max_r = x_j 

if min_l < max_r: 

return False 

return True 

 

def __iter__(self): 

r""" 

Efficient generation of Baxter permutations. 

 

OUTPUT: 

 

An iterator over the Baxter permutations of size ``self._n``. 

 

EXAMPLES:: 

 

sage: BaxterPermutations(4).list() 

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

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

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

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

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

sage: [len(BaxterPermutations(n)) for n in range(9)] 

[1, 1, 2, 6, 22, 92, 422, 2074, 10754] 

 

TESTS:: 

 

sage: all(a in BaxterPermutations(n) for n in range(7) 

....: for a in BaxterPermutations(n)) 

True 

 

ALGORITHM: 

 

The algorithm using generating trees described in [BBF08]_ is used. 

The idea is that all Baxter permutations of size `n + 1` can be 

obtained by inserting the letter `n + 1` either just before a left 

to right maximum or just after a right to left maximum of a Baxter 

permutation of size `n`. 

 

REFERENCES: 

 

.. [BBF08] \N. Bonichon, M. Bousquet-Melou, E. Fusy. 

Baxter permutations and plane bipolar orientations. 

Seminaire Lotharingien de combinatoire 61A, article B61Ah, 2008. 

""" 

if self._n == 0: 

yield Permutations(0)([]) 

elif self._n == 1: 

yield Permutations(1)([1]) 

else: 

for b in BaxterPermutations(self._n - 1): 

# Left to right maxima. 

for i in [self._n - 2 - i for i in b.reverse().saliances()]: 

yield Permutations(self._n)(b[:i] + [self._n] + b[i:]) 

# Right to left maxima. 

for i in b.saliances(): 

yield Permutations(self._n)(b[:i + 1] + [self._n] + b[i + 1:]) 

 

def _an_element_(self): 

""" 

Return an element of ``self``. 

 

EXAMPLES:: 

 

sage: BaxterPermutations(4)._an_element_() 

[4, 3, 2, 1] 

""" 

return self.first() 

 

def cardinality(self): 

r""" 

Return the number of Baxter permutations of size ``self._n``. 

 

For any positive integer `n`, the number of Baxter 

permutations of size `n` equals 

 

.. MATH:: 

 

\sum_{k=1}^n \dfrac 

{\binom{n+1}{k-1} \binom{n+1}{k} \binom{n+1}{k+1}} 

{\binom{n+1}{1} \binom{n+1}{2}} . 

 

This is :oeis:`A001181`. 

 

EXAMPLES:: 

 

sage: [BaxterPermutations(n).cardinality() for n in range(13)] 

[1, 1, 2, 6, 22, 92, 422, 2074, 10754, 58202, 326240, 1882960, 11140560] 

 

sage: BaxterPermutations(3r).cardinality() 

6 

sage: parent(_) 

Integer Ring 

""" 

if self._n == 0: 

return 1 

from sage.arith.all import binomial 

return sum((binomial(self._n + 1, k) * 

binomial(self._n + 1, k + 1) * 

binomial(self._n + 1, k + 2)) // 

((self._n + 1) * binomial(self._n + 1, 2)) 

for k in range(self._n)) 

 

 

class BaxterPermutations_all(DisjointUnionEnumeratedSets, BaxterPermutations): 

r""" 

The enumerated set of all Baxter permutations. 

 

See :class:`BaxterPermutations` for the definition of Baxter 

permutations. 

 

EXAMPLES:: 

 

sage: from sage.combinat.baxter_permutations import BaxterPermutations_all 

sage: BaxterPermutations_all() 

Baxter permutations 

""" 

def __init__(self, n=None): 

r""" 

EXAMPLES:: 

 

sage: from sage.combinat.baxter_permutations import BaxterPermutations_all 

sage: BaxterPermutations_all() 

Baxter permutations 

""" 

self.element_class = Permutations().element_class 

from sage.categories.examples.infinite_enumerated_sets import NonNegativeIntegers 

from sage.sets.family import Family 

DisjointUnionEnumeratedSets.__init__(self, 

Family(NonNegativeIntegers(), 

BaxterPermutations_size), 

facade=False, keepkey=False) 

 

def _repr_(self): 

r""" 

Return a string representation of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.combinat.baxter_permutations import BaxterPermutations_all 

sage: BaxterPermutations_all() 

Baxter permutations 

""" 

return "Baxter permutations" 

 

def __contains__(self, x): 

r""" 

Return ``True`` if and only if ``x`` is a Baxter permutation. 

 

INPUT: 

 

- ``x`` -- any object. 

 

EXAMPLES:: 

 

sage: Permutation([4, 2, 1, 7, 3, 8, 5, 6]) in BaxterPermutations() 

False 

sage: Permutation([4, 3, 6, 9, 7, 5, 1, 2, 8]) in BaxterPermutations() 

True 

""" 

if not x in Permutations(): 

return False 

return x in BaxterPermutations(len(x)) 

 

def to_pair_of_twin_binary_trees(self, p): 

r""" 

Apply a bijection between Baxter permutations of size ``self._n`` 

and the set of pairs of twin binary trees with ``self._n`` nodes. 

 

INPUT: 

 

- ``p`` -- a Baxter permutation. 

 

OUTPUT: 

 

The pair of twin binary trees `(T_L, T_R)` where `T_L` 

(resp. `T_R`) is obtained by inserting the letters of ``p`` from 

left to right (resp. right to left) following the binary search 

tree insertion algorithm. This is called the *Baxter P-symbol* 

in [Gir12]_ Definition 4.1. 

 

.. NOTE:: 

 

This method only works when ``p`` is a permutation. For words 

with repeated letters, it would return two "right binary 

search trees" (in the terminology of [Gir12]_), which conflicts 

with the definition in [Gir12]_. 

 

EXAMPLES:: 

 

sage: BaxterPermutations().to_pair_of_twin_binary_trees(Permutation([])) 

(., .) 

sage: BaxterPermutations().to_pair_of_twin_binary_trees(Permutation([1, 2, 3])) 

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

sage: BaxterPermutations().to_pair_of_twin_binary_trees(Permutation([3, 4, 1, 2])) 

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

""" 

from sage.combinat.binary_tree import LabelledBinaryTree 

left = LabelledBinaryTree(None) 

right = LabelledBinaryTree(None) 

for a in p: 

left = left.binary_search_insert(a) 

for a in reversed(p): 

right = right.binary_search_insert(a) 

return (left, right)