6 #include <marci_graph_traits.hh>
7 #include <marci_property_vector.hh>
11 template <typename graph_type>
13 typedef typename graph_traits<graph_type>::node_iterator node_iterator;
14 typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
15 typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
16 typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
20 node_property_vector<graph_type, bool> reached;
21 node_property_vector<graph_type, edge_iterator> pred;
22 node_property_vector<graph_type, int> dist;
23 std::queue<node_iterator> bfs_queue;
24 bfs(graph_type& _G, node_iterator _s) : G(_G), s(_s), reached(_G), pred(_G), dist(_G) {
26 for(each_node_iterator i=G.first_node(); i.is_valid(); ++i)
27 reached.put(i, false);
33 while (!bfs_queue.empty()) {
34 node_iterator v=bfs_queue.front();
35 out_edge_iterator e=G.first_out_edge(v);
37 for( ; e.is_valid(); ++e) {
38 node_iterator w=G.head(e);
39 std::cout << "scan node " << G.id(w) << " from node " << G.id(v) << std::endl;
40 if (!reached.get(w)) {
41 std::cout << G.id(w) << " is newly reached :-)" << std::endl;
43 dist.put(w, dist.get(v)+1);
47 std::cout << G.id(w) << " is already reached" << std::endl;
54 template <typename graph_type>
56 typedef typename graph_traits<graph_type>::node_iterator node_iterator;
57 typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
58 typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
59 typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
61 bfs_visitor(graph_type& _G) : G(_G) { }
62 void at_previously_reached(out_edge_iterator& e) {
63 //node_iterator v=G.tail(e);
64 node_iterator w=G.head(e);
65 std::cout << G.id(w) << " is already reached" << std::endl;
67 void at_newly_reached(out_edge_iterator& e) {
68 //node_iterator v=G.tail(e);
69 node_iterator w=G.head(e);
70 std::cout << G.id(w) << " is newly reached :-)" << std::endl;
74 template <typename graph_type, typename reached_type, typename visitor_type>
76 typedef typename graph_traits<graph_type>::node_iterator node_iterator;
77 typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
78 typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
79 typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
82 std::queue<out_edge_iterator>& bfs_queue;
83 reached_type& reached;
84 visitor_type& visitor;
86 while ( !bfs_queue.empty() && !bfs_queue.front().is_valid() ) { bfs_queue.pop(); }
87 if (bfs_queue.empty()) return;
88 out_edge_iterator e=bfs_queue.front();
89 //node_iterator v=G.tail(e);
90 node_iterator w=G.head(e);
91 if (!reached.get(w)) {
92 visitor.at_newly_reached(e);
93 bfs_queue.push(G.first_out_edge(w));
96 visitor.at_previously_reached(e);
99 bfs_iterator(graph_type& _G, std::queue<out_edge_iterator>& _bfs_queue, reached_type& _reached, visitor_type& _visitor) : G(_G), bfs_queue(_bfs_queue), reached(_reached), visitor(_visitor) {
100 //while ( !bfs_queue.empty() && !bfs_queue.front().is_valid() ) { bfs_queue.pop(); }
103 bfs_iterator<graph_type, reached_type, visitor_type>& operator++() {
104 //while ( !bfs_queue.empty() && !bfs_queue.front().is_valid() ) { bfs_queue.pop(); }
105 //if (bfs_queue.empty()) return *this;
106 if (!is_valid()) return *this;
107 ++(bfs_queue.front());
108 //while ( !bfs_queue.empty() && !bfs_queue.front().is_valid() ) { bfs_queue.pop(); }
113 // while ( !bfs_queue.empty() && !bfs_queue.front().is_valid() ) { bfs_queue.pop(); }
114 // if (bfs_queue.empty()) return;
115 // ++(bfs_queue.front());
116 // while ( !bfs_queue.empty() && !bfs_queue.front().is_valid() ) { bfs_queue.pop(); }
119 while ( !bfs_queue.empty() && !bfs_queue.front().is_valid() ) { bfs_queue.pop(); }
120 if (bfs_queue.empty()) return false; else return true;
123 // while ( !bfs_queue.empty() && !bfs_queue.front().is_valid() ) { bfs_queue.pop(); }
124 // if (bfs_queue.empty()) return true; else return false;
126 operator edge_iterator () { return bfs_queue.front(); }
130 template <typename graph_type, typename reached_type>
131 struct bfs_iterator1 {
132 typedef typename graph_traits<graph_type>::node_iterator node_iterator;
133 typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
134 typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
135 typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
138 std::queue<out_edge_iterator>& bfs_queue;
139 reached_type& reached;
141 bfs_iterator1(graph_type& _G, std::queue<out_edge_iterator>& _bfs_queue, reached_type& _reached) : G(_G), bfs_queue(_bfs_queue), reached(_reached) {
143 if (!bfs_queue.empty() && bfs_queue.front().is_valid()) {
144 out_edge_iterator e=bfs_queue.front();
145 node_iterator w=G.head(e);
146 if (!reached.get(w)) {
147 bfs_queue.push(G.first_out_edge(w));
148 reached.put(w, true);
155 bfs_iterator1<graph_type, reached_type>& operator++() {
156 if (!is_valid()) return *this;
157 ++(bfs_queue.front());
159 if (!bfs_queue.empty() && bfs_queue.front().is_valid()) {
160 out_edge_iterator e=bfs_queue.front();
161 node_iterator w=G.head(e);
162 if (!reached.get(w)) {
163 bfs_queue.push(G.first_out_edge(w));
164 reached.put(w, true);
173 while ( !bfs_queue.empty() && !bfs_queue.front().is_valid() ) { bfs_queue.pop(); }
174 if (bfs_queue.empty()) return false; else return true;
176 operator edge_iterator () { return bfs_queue.front(); }
177 bool is_newly_reached() { return newly_reached; }
183 #endif //MARCI_BFS_HH