ATLAS Offline Software
Digraph.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 class Digraph:
4  """Digraph is an implementation of digraphs which supports
5  adding edges and reversal"""
6 
7  def __init__(self, V=0, fn=''):
8  self.adjTable_ = {}
9  self.E = 0
10  if fn:
11  self.fromFile(fn)
12  else:
13  self.V = V
14  for i in range(V):
15  self.adjTable_[i] = []
16 
17  def fromFile(self, fn):
18  state = 'START'
19  with open(fn, 'r') as fh:
20  for ll in fh:
21  l = ll.strip()
22  if not l or l.startswith('#'): continue
23 
24  if state == 'START':
25  self.V = int(l.strip())
26  for i in range(self.V):
27  self.adjTable_[i] = list()
28  state = 'NE'
29  continue
30  if state == 'NE':
31  # self.E = int(l.strip())
32  state = 'EDGES'
33  continue
34 
35  if state == 'EDGES':
36  toks = l.strip().split()
37  assert len(toks) == 2
38  v = int(toks[0])
39  w = int(toks[1])
40  self.addEdge(v, w)
41  continue
42 
43  def addEdge(self, v, w):
44  assert v < self.V
45  assert w < self.V
46  self.adjTable_[v].append(w)
47  self.E += 1
48 
49  def adj(self, v):
50  assert v < self.V
51  return self.adjTable_[v]
52 
53  def reverse(self):
54  R = Digraph(self.V)
55  for v in range(self.V):
56  for w in self.adj(v):
57  R.addEdge(w, v)
58  return R
59 
60  def __str__(self):
61  lines = ['Digraph. V: '+ str(self.V) + ' E: ' + str(self.E)]
62  for v in range(self.V):
63  for w in self.adj(v):
64  lines.append('%d -> %d' % (v, w))
65 
66  return '\n'.join(lines)
Digraph.Digraph.reverse
def reverse(self)
Definition: Digraph.py:53
Digraph.Digraph
Definition: Digraph.py:3
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
Digraph.Digraph.__str__
def __str__(self)
Definition: Digraph.py:60
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
Digraph.Digraph.adj
def adj(self, v)
Definition: Digraph.py:49
Digraph.Digraph.E
E
Definition: Digraph.py:9
Digraph.Digraph.adjTable_
adjTable_
Definition: Digraph.py:8
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
Digraph.Digraph.__init__
def __init__(self, V=0, fn='')
Definition: Digraph.py:7
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
Digraph.Digraph.addEdge
def addEdge(self, v, w)
Definition: Digraph.py:43
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
Trk::open
@ open
Definition: BinningType.h:40
Digraph.Digraph.fromFile
def fromFile(self, fn)
Definition: Digraph.py:17
Digraph.Digraph.V
V
Definition: Digraph.py:13
str
Definition: BTagTrackIpAccessor.cxx:11
Trk::split
@ split
Definition: LayerMaterialProperties.h:38