ATLAS Offline Software
graphAlgs.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 
5  """determine whether a digraph has a cycle"""
6 
7  def __init__(self, G):
8  self.onStack = [False for i in range(G.V)]
9  self.marked = [False for i in range(G.V)]
10  self.edgeTo = [-1 for i in range(G.V)]
11  self.cycle_ = []
12 
13  GV = G.V
14  for v in range(GV):
15  if not self.marked[v]:
16  self.dfs_(G, v)
17 
18  def has_cycle(self):
19  return len(self.cycle_) > 0
20 
21  def cycle(self):
22  return self.cycle_
23 
24  def dfs_(self, G, v):
25  self.onStack[v] = True
26  self.marked[v] = True
27 
28  for w in G.adj(v):
29  if self.cycle_: return
30  if not self.marked[w]:
31  self.edgeTo[w] = w
32  self.dfs_(G, w)
33  elif self.onStack[w]:
34  x = v
35  while x != w:
36  self.cycle_.append(x)
37  x = self.edgeTo(x)
38  self.cycle_.append(w)
39  self.cycle_.append(v)
40 
41  self.onStack[v] = False
42 
43 
44 
46  """Carry out a depth first traversal of a graph, noting the order
47  nodes are processed."""
48 
49  def __init__(self, G, roots = []):
50  self.marked = [False for v in range(G.V)]
51  self.pre_ = []
52  self.post_ = []
53  self.reversePost_ = []
54 
55  if not roots:
56  for v in range(G.V):
57  if not self.marked[v]:
58  self.dfs_(G, v)
59  else:
60  for v in roots:
61  if not self.marked[v]:
62  self.dfs_(G, v)
63 
64  self.reversePost_ = self.post_[:]
65  self.reversePost_.reverse()
66 
67  def pre(self): return self.pre_
68  def post(self): return self.post_
69  def reversePost(self): return self.reversePost_
70 
71  def dfs_(self, G, v):
72  self.pre_.append(v)
73  self.marked[v] = True
74 
75  for w in G.adj(v):
76  if not self.marked[w]:
77  self.dfs_(G, w)
78 
79 
80  self.post_.append(v)
81 
82 
84  """order the nodes of a digraph in topological (i.e. execution) order."""
85 
86  def __init__(self, G, roots=[]):
87  self.marked = [False for i in range(G.V)]
88  self.order_ = []
89  cycleFinder = DirectedCycle(G)
90  if not cycleFinder.has_cycle():
91  dfs = DepthFirstOrder(G, roots)
92 
93  # if all edges were reversed, this would be
94  # self.order = dfs.reversePost();
95  self.order_ = dfs.post()
96 
97  def order(self):
98  return self.order_
99 
100  def isDAG(self):
101  return len(self.order_) != 0
graphAlgs.DirectedCycle.cycle
def cycle(self)
Definition: graphAlgs.py:21
graphAlgs.Topological.marked
marked
Definition: graphAlgs.py:87
graphAlgs.Topological.order
def order(self)
Definition: graphAlgs.py:97
graphAlgs.DepthFirstOrder.pre
def pre(self)
Definition: graphAlgs.py:67
graphAlgs.Topological
Definition: graphAlgs.py:83
graphAlgs.Topological.order_
order_
Definition: graphAlgs.py:88
graphAlgs.DepthFirstOrder.dfs_
def dfs_(self, G, v)
Definition: graphAlgs.py:71
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
graphAlgs.DirectedCycle
Definition: graphAlgs.py:4
graphAlgs.DepthFirstOrder.post_
post_
Definition: graphAlgs.py:52
graphAlgs.DepthFirstOrder
Definition: graphAlgs.py:45
DeMoUpdate.reverse
reverse
Definition: DeMoUpdate.py:563
graphAlgs.DepthFirstOrder.pre_
pre_
Definition: graphAlgs.py:51
graphAlgs.Topological.__init__
def __init__(self, G, roots=[])
Definition: graphAlgs.py:86
graphAlgs.DepthFirstOrder.post
def post(self)
Definition: graphAlgs.py:68
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
graphAlgs.DirectedCycle.cycle_
cycle_
Definition: graphAlgs.py:11
graphAlgs.DepthFirstOrder.marked
marked
Definition: graphAlgs.py:50
graphAlgs.DirectedCycle.dfs_
def dfs_(self, G, v)
Definition: graphAlgs.py:24
graphAlgs.DepthFirstOrder.reversePost_
reversePost_
Definition: graphAlgs.py:53
graphAlgs.DirectedCycle.marked
marked
Definition: graphAlgs.py:9
graphAlgs.DepthFirstOrder.__init__
def __init__(self, G, roots=[])
Definition: graphAlgs.py:49
graphAlgs.DirectedCycle.__init__
def __init__(self, G)
Definition: graphAlgs.py:7
graphAlgs.DepthFirstOrder.reversePost
def reversePost(self)
Definition: graphAlgs.py:69
graphAlgs.Topological.isDAG
def isDAG(self)
Definition: graphAlgs.py:100
graphAlgs.DirectedCycle.has_cycle
def has_cycle(self)
Definition: graphAlgs.py:18
graphAlgs.DirectedCycle.edgeTo
edgeTo
Definition: graphAlgs.py:10
graphAlgs.DirectedCycle.onStack
onStack
Definition: graphAlgs.py:8