49#include <lal/definitions.hpp>
50#include <lal/graphs/directed_graph.hpp>
51#include <lal/graphs/undirected_graph.hpp>
52#include <lal/internal/data_array.hpp>
77 bool is_directed = std::is_base_of_v<graphs::directed_graph, G>,
79 std::is_base_of_v<graphs::directed_graph, G> ||
80 std::is_base_of_v<graphs::undirected_graph, G>
86 typedef std::function<void (
const BFS<G>&,
node)> BFS_process_one;
87 typedef std::function<void (
const BFS<G>&,
node,
node,
bool)> BFS_process_two;
88 typedef std::function<bool (
const BFS<G>&,
node)> BFS_bool_one;
89 typedef std::function<bool (
const BFS<G>&,
node,
node)> BFS_bool_two;
93 BFS(
const G& g) : m_G(g), m_vis(m_G.get_num_nodes()) {
104 set_use_rev_edges(
false);
105 set_process_visited_neighbours(
false);
107 set_terminate_default();
108 set_process_current_default();
109 set_process_neighbour_default();
110 set_node_add_default();
113 void start_at(
node source) {
119 void start_at(
const std::vector<node>& sources) {
120 for (
const node& u : sources) {
130 void set_use_rev_edges(
bool use) { m_use_rev_edges = use; }
133 void set_terminate_default()
134 { m_term = [](
const BFS<G>&,
const node) ->
bool {
return false; }; }
135 void set_terminate(
const BFS_bool_one& f)
139 void set_process_current_default()
140 { m_proc_cur = [](
const BFS<G>&,
const node) ->
void { }; }
141 void set_process_current(
const BFS_process_one& f)
145 void set_process_neighbour_default()
146 { m_proc_neigh = [](
const BFS<G>&,
const node,
const node, bool) ->
void { }; }
147 void set_process_neighbour(
const BFS_process_two& f)
148 { m_proc_neigh = f; }
151 void set_node_add_default()
152 { m_add_node = [](
const BFS<G>&,
const node,
const node) ->
bool {
return true; }; }
153 void set_node_add(
const BFS_bool_two& f)
161 void set_process_visited_neighbours(
bool v) { m_proc_vis_neighs = v; }
164 void reset_visited() { m_vis.fill(0); }
166 void clear_structure() { std::queue<node> q; m_X.swap(q); }
169 void set_visited(
node u,
char vis) { m_vis[u] = vis; }
174 bool node_was_visited(
node u)
const {
return m_vis[u]; }
177 bool all_visited()
const {
178 return std::find(m_vis.begin(), m_vis.end(), 0) == m_vis.end();
182 const G& get_graph()
const {
return m_G; }
185 const data_array<char>& get_visited()
const {
return m_vis; }
191 void deal_with_neighbour(
node s,
node t,
bool ltr) {
193 if ((m_vis[t] and m_proc_vis_neighs) or not m_vis[t]) {
194 m_proc_neigh(*
this, s, t, ltr);
197 if (not m_vis[t] and m_add_node(*
this, s, t)) {
209 std::is_base_of_v<graphs::undirected_graph, GG>,
bool
212 void process_neighbours(
node s) {
213 for (
const node& t : m_G.get_neighbours(s)) {
217 deal_with_neighbour(s, t,
true);
225 std::is_base_of_v<graphs::directed_graph, GG>,
bool
228 void process_neighbours(
node s) {
229 for (
const node& t : m_G.get_out_neighbours(s)) {
233 deal_with_neighbour(s, t,
true);
236 if (m_use_rev_edges) {
237 for (
const node& t : m_G.get_in_neighbours(s)) {
241 deal_with_neighbour(s, t,
false);
246 node next_node()
const {
return m_X.front(); }
293 void do_traversal() {
294 while (not m_X.empty()) {
295 const node s = next_node();
299 m_proc_cur(*
this, s);
302 if (m_term(*
this, s)) {
break; }
304 process_neighbours(s);
312 std::queue<node> m_X;
314 data_array<char> m_vis;
316 bool m_proc_vis_neighs =
false;
318 bool m_use_rev_edges =
false;
343 BFS_process_one m_proc_cur;
359 BFS_process_two m_proc_neigh;
371 BFS_bool_two m_add_node;
Main namespace of the library.
Definition definitions.hpp:48
uint32_t node
Node type.
Definition definitions.hpp:51