ATLAS Offline Software
Loading...
Searching...
No Matches
Trigger
TrigT1
Global
GlobalSimulation
python
Digraph.py
Go to the documentation of this file.
1
# Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2
3
# Data structure adapted from Sedgewick and Wayne, Algorithms v4 p569
4
5
class
Digraph
:
6
"""Digraph is an implementation of digraphs which supports
7
adding edges and reversal"""
8
9
def
__init__
(self, V=0, fn=''):
10
self.
adjTable_
= {}
11
self.
E
= 0
12
if
fn:
13
self.
fromFile
(fn)
14
else
:
15
self.
V
= V
16
for
i
in
range(V):
17
self.
adjTable_
[i] = []
18
19
def
fromFile
(self, fn):
20
state =
'START'
21
with
open(fn,
'r'
)
as
fh:
22
for
ll
in
fh:
23
l = ll.strip()
24
if
not
l
or
l.startswith(
'#'
):
continue
25
26
if
state ==
'START'
:
27
self.
V
= int(l.strip())
28
for
i
in
range(self.
V
):
29
self.
adjTable_
[i] = list()
30
state =
'NE'
31
continue
32
if
state ==
'NE'
:
33
# self.E = int(l.strip())
34
state =
'EDGES'
35
continue
36
37
if
state ==
'EDGES'
:
38
toks = l.strip().
split
()
39
assert
len(toks) == 2
40
v = int(toks[0])
41
w = int(toks[1])
42
self.
addEdge
(v, w)
43
continue
44
45
def
addEdge
(self, v, w):
46
assert
v < self.
V
47
assert
w < self.
V
48
self.
adjTable_
[v].append(w)
49
self.
E
+= 1
50
51
def
adj
(self, v):
52
assert
v < self.
V
53
return
self.
adjTable_
[v]
54
55
def
reverse
(self):
56
R =
Digraph
(self.
V
)
57
for
v
in
range(self.
V
):
58
for
w
in
self.
adj
(v):
59
R.addEdge(w, v)
60
return
R
61
62
def
__str__
(self):
63
lines = [
'Digraph. V: '
+ str(self.
V
) +
' E: '
+ str(self.
E
)]
64
for
v
in
range(self.
V
):
65
for
w
in
self.
adj
(v):
66
lines.append(
'%d -> %d'
% (v, w))
67
68
return
'\n'
.join(lines)
Digraph.Digraph
Definition
Digraph.py:5
Digraph.Digraph.reverse
reverse(self)
Definition
Digraph.py:55
Digraph.Digraph.addEdge
addEdge(self, v, w)
Definition
Digraph.py:45
Digraph.Digraph.__init__
__init__(self, V=0, fn='')
Definition
Digraph.py:9
Digraph.Digraph.V
V
Definition
Digraph.py:15
Digraph.Digraph.adj
adj(self, v)
Definition
Digraph.py:51
Digraph.Digraph.E
int E
Definition
Digraph.py:11
Digraph.Digraph.__str__
__str__(self)
Definition
Digraph.py:62
Digraph.Digraph.adjTable_
dict adjTable_
Definition
Digraph.py:10
Digraph.Digraph.fromFile
fromFile(self, fn)
Definition
Digraph.py:19
split
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition
hcg.cxx:177
Generated on
for ATLAS Offline Software by
1.14.0