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

r""" 

Paths in Directed Acyclic Graphs 

""" 

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

# 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 __future__ import absolute_import 

 

from .combinat import CombinatorialClass 

import sage.graphs.digraph as digraph 

 

 

def GraphPaths(g, source=None, target=None): 

""" 

Returns the combinatorial class of paths in the directed acyclic 

graph g. 

 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

 

If source and target are not given, then the returned class 

contains all paths (including trivial paths containing only one 

vertex). 

 

:: 

 

sage: p = GraphPaths(G); p 

Paths in Multi-digraph on 5 vertices 

sage: p.cardinality() 

37 

sage: p.random_element() 

[1, 2, 3, 4, 5] 

 

If the source is specified, then the returned class contains all of 

the paths starting at the vertex source (including the trivial 

path). 

 

:: 

 

sage: p = GraphPaths(G, source=3); p 

Paths in Multi-digraph on 5 vertices starting at 3 

sage: p.list() 

[[3], [3, 4], [3, 4, 5], [3, 4, 5]] 

 

If the target is specified, then the returned class contains all of 

the paths ending at the vertex target (including the trivial 

path). 

 

:: 

 

sage: p = GraphPaths(G, target=3); p 

Paths in Multi-digraph on 5 vertices ending at 3 

sage: p.cardinality() 

5 

sage: p.list() 

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

 

If both the target and source are specified, then the returned 

class contains all of the paths from source to target. 

 

:: 

 

sage: p = GraphPaths(G, source=1, target=3); p 

Paths in Multi-digraph on 5 vertices starting at 1 and ending at 3 

sage: p.cardinality() 

3 

sage: p.list() 

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

 

Note that G must be a directed acyclic graph. 

 

:: 

 

sage: G = DiGraph({1:[2,2,3,5], 2:[3,4], 3:[4], 4:[2,5,7], 5:[6]}, multiedges=True) 

sage: GraphPaths(G) 

Traceback (most recent call last): 

... 

TypeError: g must be a directed acyclic graph 

""" 

if not isinstance(g, digraph.DiGraph): 

raise TypeError("g must be a DiGraph") 

elif not g.is_directed_acyclic(): 

raise TypeError("g must be a directed acyclic graph") 

 

if source is None and target is None: 

return GraphPaths_all(g) 

elif source is not None and target is None: 

if source not in g: 

raise ValueError("source must be in g") 

return GraphPaths_s(g, source) 

elif source is None and target is not None: 

if target not in g: 

raise ValueError("target must be in g") 

return GraphPaths_t(g, target) 

else: 

if source not in g: 

raise ValueError("source must be in g") 

if target not in g: 

raise ValueError("target must be in g") 

return GraphPaths_st(g, source, target) 

 

class GraphPaths_common: 

def outgoing_edges(self, v): 

""" 

Returns a list of v's outgoing edges. 

 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G) 

sage: p.outgoing_edges(2) 

[(2, 3, None), (2, 4, None)] 

""" 

return [i for i in self.graph.outgoing_edge_iterator(v)] 

 

def incoming_edges(self, v): 

""" 

Returns a list of v's incoming edges. 

 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G) 

sage: p.incoming_edges(2) 

[(1, 2, None), (1, 2, None)] 

""" 

return [i for i in self.graph.incoming_edge_iterator(v)] 

 

def outgoing_paths(self, v): 

""" 

Returns a list of the paths that start at v. 

 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: gp = GraphPaths(G) 

sage: gp.outgoing_paths(3) 

[[3], [3, 4], [3, 4, 5], [3, 4, 5]] 

sage: gp.outgoing_paths(2) 

[[2], 

[2, 3], 

[2, 3, 4], 

[2, 3, 4, 5], 

[2, 3, 4, 5], 

[2, 4], 

[2, 4, 5], 

[2, 4, 5]] 

""" 

source_paths = [ [v] ] 

for e in self.outgoing_edges(v): 

target = e[1] 

target_paths = self.outgoing_paths(target) 

target_paths = [ [v]+path for path in target_paths] 

 

source_paths += target_paths 

 

return source_paths 

 

 

def incoming_paths(self, v): 

""" 

Returns a list of paths that end at v. 

 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: gp = GraphPaths(G) 

sage: gp.incoming_paths(2) 

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

""" 

target_paths = [ [v] ] 

for e in self.incoming_edges(v): 

source = e[0] 

source_paths = self.incoming_paths(source) 

source_paths = [ path + [v] for path in source_paths ] 

target_paths += source_paths 

return target_paths 

 

def paths_from_source_to_target(self, source, target): 

""" 

Returns a list of paths from source to target. 

 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: gp = GraphPaths(G) 

sage: gp.paths_from_source_to_target(2,4) 

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

""" 

source_paths = self.outgoing_paths(source) 

paths = [] 

for path in source_paths: 

if path[-1] == target: 

paths.append(path) 

return paths 

 

def paths(self): 

""" 

Returns a list of all the paths of self. 

 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: gp = GraphPaths(G) 

sage: len(gp.paths()) 

37 

""" 

paths = [] 

for source in self.graph.vertices(): 

paths += self.outgoing_paths(source) 

return paths 

 

class GraphPaths_all(CombinatorialClass, GraphPaths_common): 

""" 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G) 

sage: p.cardinality() 

37 

""" 

def __init__(self, g): 

""" 

TESTS:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G) 

sage: p == loads(dumps(p)) 

True 

""" 

self.graph = g 

 

def __repr__(self): 

""" 

TESTS:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G) 

sage: repr(p) 

'Paths in Multi-digraph on 5 vertices' 

""" 

return "Paths in %s"%repr(self.graph) 

 

def list(self): 

""" 

Returns a list of the paths of self. 

 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: len(GraphPaths(G).list()) 

37 

""" 

return self.paths() 

 

class GraphPaths_t(CombinatorialClass, GraphPaths_common): 

def __init__(self, g, target): 

""" 

TESTS:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G, target=4) 

sage: p == loads(dumps(p)) 

True 

""" 

self.graph = g 

self.target = target 

 

def __repr__(self): 

""" 

TESTS:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G, target=4) 

sage: repr(p) 

'Paths in Multi-digraph on 5 vertices ending at 4' 

""" 

return "Paths in %s ending at %s"%(repr(self.graph), self.target) 

 

def list(self): 

""" 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G, target=4) 

sage: p.list() 

[[4], 

[2, 4], 

[1, 2, 4], 

[1, 2, 4], 

[3, 4], 

[1, 3, 4], 

[2, 3, 4], 

[1, 2, 3, 4], 

[1, 2, 3, 4]] 

""" 

return self.incoming_paths(self.target) 

 

class GraphPaths_s(CombinatorialClass, GraphPaths_common): 

def __init__(self, g, source): 

""" 

TESTS:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G, 4) 

sage: p == loads(dumps(p)) 

True 

""" 

self.graph = g 

self.source = source 

 

def __repr__(self): 

""" 

TESTS:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G, 4) 

sage: repr(p) 

'Paths in Multi-digraph on 5 vertices starting at 4' 

""" 

return "Paths in %s starting at %s"%(repr(self.graph), self.source) 

 

def list(self): 

""" 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G, 4) 

sage: p.list() 

[[4], [4, 5], [4, 5]] 

""" 

return self.outgoing_paths(self.source) 

 

class GraphPaths_st(CombinatorialClass, GraphPaths_common): 

""" 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: GraphPaths(G,1,2).cardinality() 

2 

sage: GraphPaths(G,1,3).cardinality() 

3 

sage: GraphPaths(G,1,4).cardinality() 

5 

sage: GraphPaths(G,1,5).cardinality() 

10 

sage: GraphPaths(G,2,3).cardinality() 

1 

sage: GraphPaths(G,2,4).cardinality() 

2 

sage: GraphPaths(G,2,5).cardinality() 

4 

sage: GraphPaths(G,3,4).cardinality() 

1 

sage: GraphPaths(G,3,5).cardinality() 

2 

sage: GraphPaths(G,4,5).cardinality() 

2 

""" 

def __init__(self, g, source, target): 

""" 

TESTS:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G,1,2) 

sage: p == loads(dumps(p)) 

True 

""" 

self.graph = g 

self.source = source 

self.target = target 

 

def __repr__(self): 

""" 

TESTS:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G,1,2) 

sage: repr(p) 

'Paths in Multi-digraph on 5 vertices starting at 1 and ending at 2' 

""" 

return "Paths in %s starting at %s and ending at %s"%(repr(self.graph), self.source, self.target) 

 

def list(self): 

""" 

EXAMPLES:: 

 

sage: G = DiGraph({1:[2,2,3], 2:[3,4], 3:[4], 4:[5,5]}, multiedges=True) 

sage: p = GraphPaths(G,1,2) 

sage: p.list() 

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

""" 

return self.paths_from_source_to_target(self.source, self.target)