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

""" 

Linear Extensions of Directed Acyclic Graphs. 

 

A linear extension of a directed acyclic graph is a total (linear) ordering on 

the vertices that is compatible with the graph in the following sense: 

if there is a path from x to y in the graph, the x appears before y in the 

linear extension. 

 

The algorithm implemented in this module is from "Generating Linear Extensions 

Fast" by Preusse and Ruskey, which can be found at 

http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.52.3057 . The algorithm 

generates the extensions in constant amortized time (CAT) -- a constant amount 

of time per extension generated, or linear in the number of extensions 

generated. 

 

EXAMPLES: 

 

Here we generate the 5 linear extensions of the following directed 

acyclic graph:: 

 

sage: from sage.graphs.linearextensions import LinearExtensions 

sage: D = DiGraph({ 0:[1,2], 1:[3], 2:[3,4] }) 

sage: D.is_directed_acyclic() 

True 

sage: LinearExtensions(D).list() 

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

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

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

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

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

 

Notice how all of the total orders are compatible with the ordering 

induced from the graph. 

 

We can also get at the linear extensions directly from the graph. From 

the graph, the linear extensions are known as topological sorts :: 

 

sage: D.topological_sort_generator() 

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

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

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

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

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

 

 

""" 

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

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

# 

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

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

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

from __future__ import print_function 

 

import sys 

from copy import copy 

from sage.combinat.combinat import CombinatorialClass 

 

 

class LinearExtensions(CombinatorialClass): 

def __init__(self, dag): 

r""" 

Creates an object representing the class of all linear extensions 

of the directed acyclic graph \code{dag}. 

 

Note that upon construction of this object some pre-computation is 

done. This is the "preprocessing routine" found in Figure 7 of 

"Generating Linear Extensions Fast" by Preusse and Ruskey. 

 

This is an in-place algorithm and the list self.le keeps track 

of the current linear extensions. The boolean variable self.is_plus 

keeps track of the "sign". 

 

EXAMPLES:: 

 

sage: from sage.graphs.linearextensions import LinearExtensions 

sage: D = DiGraph({ 0:[1,2], 1:[3], 2:[3,4] }) 

sage: l = LinearExtensions(D) 

sage: l == loads(dumps(l)) 

True 

 

""" 

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

#Precomputation# 

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

dag_copy = copy(dag) 

le = [] 

a = [] 

b = [] 

 

#The preprocessing routine found in Figure 7 of 

#"Generating Linear Extensions Fast" by 

#Pruesse and Ruskey 

while dag_copy.num_verts() != 0: 

#Find all the minimal elements of dag_copy 

minimal_elements = [] 

for node in dag_copy.vertices(): 

if len(dag_copy.incoming_edges(node)) == 0: 

minimal_elements.append(node) 

if len(minimal_elements) == 1: 

le.append(minimal_elements[0]) 

dag_copy.delete_vertex(minimal_elements[0]) 

else: 

ap = minimal_elements[0] 

bp = minimal_elements[1] 

a.append(ap) 

b.append(bp) 

le.append(ap) 

le.append(bp) 

dag_copy.delete_vertex(ap) 

dag_copy.delete_vertex(bp) 

self.max_pair = len(a) - 1 

 

self.le = le 

self.a = a 

self.b = b 

self.dag = dag 

self.mrb = 0 

self.mra = 0 

self.is_plus = True 

self.linear_extensions = None 

self._name = "Linear extensions of %s"%dag 

 

 

def switch(self, i): 

""" 

This implements the Switch procedure described on page 7 

of "Generating Linear Extensions Fast" by Pruesse and Ruskey. 

 

If i == -1, then the sign is changed. If i > 0, then self.a[i] 

and self.b[i] are transposed. 

 

Note that this meant to be called by the generate_linear_extensions 

method and is not meant to be used directly. 

 

EXAMPLES:: 

 

sage: from sage.graphs.linearextensions import LinearExtensions 

sage: D = DiGraph({ 0:[1,2], 1:[3], 2:[3,4] }) 

sage: l = LinearExtensions(D) 

sage: _ = l.list() 

sage: l.le = [0, 1, 2, 3, 4] 

sage: l.is_plus 

True 

sage: l.switch(-1) 

sage: l.is_plus 

False 

sage: l.a 

[1, 4] 

sage: l.b 

[2, 3] 

sage: l.switch(0) 

sage: l.le 

[0, 2, 1, 3, 4] 

sage: l.a 

[2, 4] 

sage: l.b 

[1, 3] 

 

 

""" 

if i == -1: 

self.is_plus = not self.is_plus 

if i >= 0: 

a_index = self.le.index(self.a[i]) 

b_index = self.le.index(self.b[i]) 

self.le[a_index] = self.b[i] 

self.le[b_index] = self.a[i] 

 

self.b[i], self.a[i] = self.a[i], self.b[i] 

 

if self.is_plus: 

self.linear_extensions.append(self.le[:]) 

 

 

def move(self, element, direction): 

""" 

This implements the Move procedure described on page 7 

of "Generating Linear Extensions Fast" by Pruesse and Ruskey. 

 

If direction is "left", then this transposes element with the 

element on its left. If the direction is "right", then this 

transposes element with the element on its right. 

 

Note that this is meant to be called by the generate_linear_extensions 

method and is not meant to be used directly. 

 

EXAMPLES:: 

 

sage: from sage.graphs.linearextensions import LinearExtensions 

sage: D = DiGraph({ 0:[1,2], 1:[3], 2:[3,4] }) 

sage: l = LinearExtensions(D) 

sage: _ = l.list() 

sage: l.le = [0, 1, 2, 3, 4] 

sage: l.move(1, "left") 

sage: l.le 

[1, 0, 2, 3, 4] 

sage: l.move(1, "right") 

sage: l.le 

[0, 1, 2, 3, 4] 

 

""" 

index = self.le.index(element) 

if direction == "right": 

self.le[index] = self.le[index+1] 

self.le[index+1] = element 

elif direction == "left": 

self.le[index] = self.le[index-1] 

self.le[index-1] = element 

else: 

print("Bad direction!") 

sys.exit() 

if self.is_plus: 

self.linear_extensions.append(self.le[:]) 

 

 

def right(self, i, letter): 

""" 

If letter =="b", then this returns True if and only if 

self.b[i] is incomparable with the elements to its right 

in self.le. If letter == "a", then it returns True if 

and only if self.a[i] is incomparable with the element to its 

right in self.le and the element to the right is not 

self.b[i] 

 

This is the Right function described on page 8 of 

"Generating Linear Extensions Fast" by Pruesse and Ruskey. 

 

Note that this is meant to be called by the generate_linear_extensions 

method and is not meant to be used directly. 

 

EXAMPLES:: 

 

sage: from sage.graphs.linearextensions import LinearExtensions 

sage: D = DiGraph({ 0:[1,2], 1:[3], 2:[3,4] }) 

sage: l = LinearExtensions(D) 

sage: _ = l.list() 

sage: l.le 

[0, 1, 2, 4, 3] 

sage: l.a 

[1, 4] 

sage: l.b 

[2, 3] 

sage: l.right(0, "a") 

False 

sage: l.right(1, "a") 

False 

sage: l.right(0, "b") 

False 

sage: l.right(1, "b") 

False 

 

""" 

if letter == "a": 

x = self.a[i] 

yindex = self.le.index(x) + 1 

if yindex >= len(self.le): 

return False 

y = self.le[ yindex ] 

if self.incomparable(x,y) and y != self.b[i]: 

return True 

return False 

elif letter == "b": 

x = self.b[i] 

yindex = self.le.index(x) + 1 

if yindex >= len(self.le): 

return False 

y = self.le[ yindex ] 

if self.incomparable(x,y): 

return True 

return False 

else: 

raise ValueError("Bad letter!") 

 

def generate_linear_extensions(self, i): 

""" 

This a Python version of the GenLE routine found in Figure 8 

of "Generating Linear Extensions Fast" by Pruesse and Ruskey. 

 

Note that this is meant to be called by the list 

method and is not meant to be used directly. 

 

EXAMPLES:: 

 

sage: from sage.graphs.linearextensions import LinearExtensions 

sage: D = DiGraph({ 0:[1,2], 1:[3], 2:[3,4] }) 

sage: l = LinearExtensions(D) 

sage: l.linear_extensions = [] 

sage: l.linear_extensions.append(l.le[:]) 

sage: l.generate_linear_extensions(l.max_pair) 

sage: l.linear_extensions 

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

 

""" 

if i >= 0: 

self.generate_linear_extensions(i-1) 

mrb = 0 

typical = False 

while self.right(i, "b"): 

mrb += 1 

self.move(self.b[i], "right") 

self.generate_linear_extensions(i-1) 

mra = 0 

if self.right(i, "a"): 

typical = True 

cont = True 

while cont: 

mra += 1 

self.move(self.a[i], "right") 

self.generate_linear_extensions(i-1) 

cont = self.right(i, "a") 

if typical: 

self.switch(i-1) 

self.generate_linear_extensions(i-1) 

if mrb % 2 == 1: 

mla = mra -1 

else: 

mla = mra + 1 

for x in range(mla): 

self.move(self.a[i], "left") 

self.generate_linear_extensions(i-1) 

 

if typical and (mrb % 2 == 1): 

self.move(self.a[i], "left") 

else: 

self.switch(i-1) 

self.generate_linear_extensions(i-1) 

for x in range(mrb): 

self.move(self.b[i], "left") 

self.generate_linear_extensions(i-1) 

 

def list(self): 

""" 

Returns a list of the linear extensions of the directed acyclic graph. 

 

Note that once they are computed, the linear extensions are 

cached in this object. 

 

EXAMPLES:: 

 

sage: from sage.graphs.linearextensions import LinearExtensions 

sage: D = DiGraph({ 0:[1,2], 1:[3], 2:[3,4] }) 

sage: LinearExtensions(D).list() 

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

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

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

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

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

""" 

if self.linear_extensions is not None: 

return self.linear_extensions[:] 

 

self.linear_extensions = [] 

self.linear_extensions.append(self.le[:]) 

self.generate_linear_extensions(self.max_pair) 

self.switch(self.max_pair) 

self.generate_linear_extensions(self.max_pair) 

self.linear_extensions.sort() 

return self.linear_extensions[:] 

 

 

def incomparable(self, x, y): 

""" 

Returns True if vertices x and y are incomparable in the directed 

acyclic graph when thought of as a poset. 

 

EXAMPLES:: 

 

sage: from sage.graphs.linearextensions import LinearExtensions 

sage: D = DiGraph({ 0:[1,2], 1:[3], 2:[3,4] }) 

sage: l = LinearExtensions(D) 

sage: l.incomparable(0,1) 

False 

sage: l.incomparable(1,2) 

True 

""" 

if (not self.dag.shortest_path(x, y)) and (not self.dag.shortest_path(y, x)): 

return True 

return False