DiaDes  0.1
DIAgnosis of Discrete-Event System
BFS.hh
Go to the documentation of this file.
1 #ifndef __DIADES__GRAPH__BFS_HH_
2 #define __DIADES__GRAPH__BFS_HH_
3 #include<iostream>
4 #include<queue>
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 BFS : 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::queue<Node> _queue;
43 
44 
45 
46  public:
59  BFS(Graph & graph, const Container & nodes, const Opposite & opposite, Solution & s, PathCut & cut, Accumulator & results):
60  Search(graph,nodes,opposite,s,cut,results),_queue(){}
61 
65  virtual ~BFS(){}
66 
71  virtual void initialize()
72  {
73  Search::initialize();
74  while(!_queue.empty())
75  {
76  _queue.pop();
77  }
78  for(const typename Search::Node & n : Search::_nodes)
79  {
80  _queue.push(n);
81  Search::_m.mark(n);
82  }
83  }
84 
91  bool finished() const { return _queue.empty(); }
92 
97  void next()
98  {
99  if(!finished())
100  {
101 
102  Node n = _queue.front();
103  //cout << "VISITED NODE: " << n << endl;
104  _queue.pop();
105 
106  if(Search::_s.isSolution(n))
107  {
108  Search::_results.push_back(n);
109  Search::_updated = true;
110  //cout << "SOLUTION" << endl;
111  }
112  else
113  {
114  IncidentEdges edges(Search::_graph,n,Search::_cut);
115  //cout << "NEXT NODES (QUEUED): " << endl;
116  for(typename IncidentEdges::Iterator edgeIt = edges.begin();
117  edgeIt != edges.end();
118  ++edgeIt)
119  {
120  const Node & nextNode = Search::_opposite.next(*edgeIt,n);
121  if(!Search::_m.isMarked(nextNode))
122  {
123  Search::_m.mark(nextNode);
124  _queue.push(nextNode);
125  //cout << '\t' << nextNode << endl;
126  }
127  }
128  }
129  }
130  }
131 
132  };
133 
134  };
135 };
136 
137 #endif
138 
IncidentEdges::Node Node
Definition: GraphSearch.hh:63
Search::Opposite Opposite
Definition: BFS.hh:35
GraphSearch< TIncidentEdges, TOpposite, TContainer, TAccumulator, TMark, TSolution, TPathCut > Search
Definition: BFS.hh:31
std::queue< Node > _queue
Definition: BFS.hh:42
TIncidentEdges IncidentEdges
Definition: GraphSearch.hh:58
void next()
Definition: BFS.hh:97
Search::Solution Solution
Definition: BFS.hh:36
IncidentEdges::Graph Graph
Definition: GraphSearch.hh:62
BFS(Graph &graph, const Container &nodes, const Opposite &opposite, Solution &s, PathCut &cut, Accumulator &results)
Definition: BFS.hh:59
Namespace of the Diades project.
Search::Node Node
Definition: BFS.hh:33
virtual void initialize()
Definition: BFS.hh:71
bool finished() const
Definition: BFS.hh:91
Search::PathCut PathCut
Definition: BFS.hh:37
Search::Accumulator Accumulator
Definition: BFS.hh:38
Search::Container Container
Definition: BFS.hh:34
Search::IncidentEdges IncidentEdges
Definition: BFS.hh:39
virtual ~BFS()
Definition: BFS.hh:65
Search::Graph Graph
Definition: BFS.hh:32