Ho problemi a compilare il BFS di un grafico molto semplice. Qualsiasi cosa io faccia mi sono vari messaggi del compilatore circa le chiamate di metodo senza eguali (ho provato boost::visitor
ed estendendo boost::default_bfs_visitor
ecc)Come attraversare il grafico in uso boost BFS
#include <stdint.h>
#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
int main() {
typedef boost::adjacency_list<boost::vecS, boost::hash_setS, boost::undirectedS, uint32_t, uint32_t, boost::no_property> graph_t;
graph_t graph(4);
graph_t::vertex_descriptor a = boost::vertex(0, graph);
graph_t::vertex_descriptor b = boost::vertex(1, graph);
graph_t::vertex_descriptor c = boost::vertex(2, graph);
graph_t::vertex_descriptor d = boost::vertex(3, graph);
graph[a] = 0;
graph[b] = 1;
graph[c] = 2;
graph[d] = 3;
std::pair<graph_t::edge_descriptor, bool> result = boost::add_edge(a, b, 0, graph);
result = boost::add_edge(a, c, 1, graph);
result = boost::add_edge(c, b, 2, graph);
class {
public:
void initialize_vertex(const graph_t::vertex_descriptor &s, graph_t &g) {
std::cout << "Initialize: " << g[s] << std::endl;
}
void discover_vertex(const graph_t::vertex_descriptor &s, graph_t &g) {
std::cout << "Discover: " << g[s] << std::endl;
}
void examine_vertex(const graph_t::vertex_descriptor &s, graph_t &g) {
std::cout << "Examine vertex: " << g[s] << std::endl;
}
void examine_edge(const graph_t::edge_descriptor &e, graph_t &g) {
std::cout << "Examine edge: " << g[e] << std::endl;
}
void tree_edge(const graph_t::edge_descriptor &e, graph_t &g) {
std::cout << "Tree edge: " << g[e] << std::endl;
}
void non_tree_edge(const graph_t::edge_descriptor &e, graph_t &g) {
std::cout << "Non-Tree edge: " << g[e] << std::endl;
}
void gray_target(const graph_t::edge_descriptor &e, graph_t &g) {
std::cout << "Gray target: " << g[e] << std::endl;
}
void black_target(const graph_t::edge_descriptor &e, graph_t &g) {
std::cout << "Black target: " << g[e] << std::endl;
}
void finish_vertex(const graph_t::vertex_descriptor &s, graph_t &g) {
std::cout << "Finish vertex: " << g[s] << std::endl;
}
} bfs_visitor;
boost::breadth_first_search(graph, a, bfs_visitor);
return 0;
}
Come visitare il grafico utilizzando bfs_visitor
?
PS. Ho visto e compilato "How to create a C++ Boost undirected graph and traverse it in depth first search (DFS) order?" ma non mi ha aiutato.
Non riuscivo a farlo funzionare con la classe nidificata in un metodo (ho ancora ricevuto strani messaggi del compilatore sull'errore di istanziazione del modello). Sai quale potrebbe essere il problema? –
@MaciejPiechotka Come spiegato [qui] (http://stackoverflow.com/questions/5751977/local-type-as-template-arguments-in-c) prima di C++ 11 non è possibile utilizzare i tipi locali come parametri del modello. 'boost :: visitor' restituisce una struttura che ha il tipo del suo argomento come uno dei parametri. L'uso di '-std = C++ 11' fa sì che funzioni con g ++ 4.8.0 (anche se non riesce ancora con clang 3.2). –