DiaDes  0.1
DIAgnosis of Discrete-Event System
DFS.hh
Go to the documentation of this file.
1 #ifndef __DIADES__GRAPH__DFS_HH_
2 #define __DIADES__GRAPH__DFS_HH_
3 #include<iostream>
4 #include<stack>
6 
7 namespace Diades
8 {
9  namespace Graph
10  {
11 
19  template<typename TIncidentEdges,
20  typename TOpposite,
21  typename TContainer,
22  typename TAccumulator,
23  typename TMark,
24  typename TSolution,
25  typename TPathCut>
26  class DFS : public GraphSearch<TIncidentEdges,TOpposite,TContainer,TAccumulator,
27  TMark,TSolution,TPathCut>
28  {
29  public:
30  typedef GraphSearch<TIncidentEdges,TOpposite,TContainer,TAccumulator,
31  TMark,TSolution,TPathCut> Search;
32  typedef typename Search::Graph Graph;
33  typedef typename Search::Node Node;
34  typedef typename Search::Container Container;
35  typedef typename Search::Opposite Opposite;
36  typedef typename Search::Solution Solution;
37  typedef typename Search::PathCut PathCut;
38  typedef typename Search::Accumulator Accumulator;
40 
41  private:
42  std::stack<Node> _stack;
43 
44 
45 
46 
47  public:
48 
61  DFS(Graph & graph, const Container & nodes, const Opposite & opposite, Solution & s, PathCut & cut, Accumulator & results):
62  Search(graph,nodes,opposite,s,cut,results),_stack(){}
63 
67  virtual ~DFS(){}
68 
73  virtual void initialize()
74  {
75  Search::initialize();
76  while(!_stack.empty())
77  {
78  _stack.pop();
79  }
80  for(const Node & n : Search::_nodes)
81  {
82  _stack.push(n);
83  Search::_m.mark(n);
84  }
85  }
86 
93  bool finished() const { return _stack.empty(); }
94 
95 
100  void next()
101  {
102  if(!finished())
103  {
104 
105  Node n = _stack.top();
106  //std::cout << "VISITED NODE: " << n << std::endl;
107  _stack.pop();
108  if(Search::_s.isSolution(n))
109  {
110  Search::_results.push_back(n);
111  Search::_updated = true;
112  //std::cout << "SOLUTION" << std::endl;
113  }
114  else
115  {
116  IncidentEdges edges(Search::_graph,n,Search::_cut);
117  //std::cout << "NEXT NODES (STACKED): " << std::endl;
118  for(typename IncidentEdges::Iterator edgeIt = edges.begin();
119  edgeIt != edges.end();
120  ++edgeIt)
121  {
122  const Node & nextNode = Search::_opposite.next(*edgeIt,n);
123 
124  if(!Search::_m.isMarked(nextNode))
125  {
126  Search::_m.mark(nextNode);
127  //std::cout << '\t' << nextNode << std::endl;
128  _stack.push(nextNode);
129  }
130  }
131  }
132  }
133  }
134  };
135 
136  };
137 };
138 
139 #endif
140 
Search::Opposite Opposite
Definition: DFS.hh:35
IncidentEdges::Node Node
Definition: GraphSearch.hh:63
GraphSearch< TIncidentEdges, TOpposite, TContainer, TAccumulator, TMark, TSolution, TPathCut > Search
Definition: DFS.hh:31
virtual ~DFS()
Definition: DFS.hh:67
TIncidentEdges IncidentEdges
Definition: GraphSearch.hh:58
Search::IncidentEdges IncidentEdges
Definition: DFS.hh:39
Search::Container Container
Definition: DFS.hh:34
Search::PathCut PathCut
Definition: DFS.hh:37
Search::Node Node
Definition: DFS.hh:33
Search::Accumulator Accumulator
Definition: DFS.hh:38
IncidentEdges::Graph Graph
Definition: GraphSearch.hh:62
std::stack< Node > _stack
Definition: DFS.hh:42
Search::Solution Solution
Definition: DFS.hh:36
Namespace of the Diades project.
boost::adjacency_list Graph
Definition: ScaleFree.cc:9
void next()
Definition: DFS.hh:100
virtual void initialize()
Definition: DFS.hh:73
DFS(Graph &graph, const Container &nodes, const Opposite &opposite, Solution &s, PathCut &cut, Accumulator &results)
Definition: DFS.hh:61
bool finished() const
Definition: DFS.hh:93
Search::Graph Graph
Definition: DFS.hh:32