Last active
September 16, 2015 15:02
-
-
Save Zia-/e21775d75824b6a5474e to your computer and use it in GitHub Desktop.
Modified form of "example/bfs-example.cpp" code (https://github.com/boostorg/graph/blob/master/example/bfs-example.cpp) showing BFS with visitors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/graph/adjacency_list.hpp> | |
#include <boost/graph/breadth_first_search.hpp> | |
#include <boost/pending/indirect_cmp.hpp> | |
#include <boost/range/irange.hpp> | |
#include <iostream> | |
template < typename TimeMap > class bfs_time_visitor:public boost::default_bfs_visitor { | |
typedef typename boost::property_traits < TimeMap >::value_type T; | |
public: | |
bfs_time_visitor(TimeMap tmap, T & t):m_timemap(tmap), m_time(t) { } | |
template < typename Vertex, typename Graph > | |
void discover_vertex(Vertex u, const Graph & g) const | |
{ | |
put(m_timemap, u, m_time++); | |
} | |
TimeMap m_timemap; | |
T & m_time; | |
}; | |
int main() { | |
// Select the graph type we wish to use | |
typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS > UndirectedGraph; | |
// Set up the vertex IDs and names | |
enum { r, s, t, u, v, w, x, y, N }; | |
const char* name = "rstuvwxy"; | |
// Specify the edges in the graph | |
typedef std::pair < int, int > E; | |
E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t), | |
E(w, x), E(x, t), E(t, u), E(x, y), E(u, y) | |
}; | |
// Number of edges in the graph. We need this to populate the graph using loop. sizeof(edge_array) = 80 and sizeof(E) = 8 | |
const int n_edges = sizeof(edge_array) / sizeof(E); | |
// Populating the graph using for loop | |
UndirectedGraph undigraph(N); | |
for (std::size_t j = 0; j < n_edges; ++j) | |
boost::add_edge(edge_array[j].first, edge_array[j].second, undigraph); | |
// Typedefs | |
typedef boost::graph_traits < UndirectedGraph >::vertices_size_type Size; | |
// Imp Point: distance_map, predecessor_map, vertex_index_map, all type of maps is defined like " get(____, g) " | |
// a vector to hold the discover time property for each vertex. Not a clear code the following 4 lines | |
// iterator_property_map is a default of color_map, so basically we are making a color map here. | |
std::vector < Size > discover_time(num_vertices(undigraph)); | |
typedef boost::iterator_property_map< std::vector<Size>::iterator, | |
boost::property_map<UndirectedGraph, boost::vertex_index_t>::const_type> discovertime_propertymap_type; | |
discovertime_propertymap_type discovertime_propertymap(discover_time.begin(), get(boost::vertex_index, undigraph)); | |
Size time = 0; | |
// Creating a visitor obj | |
bfs_time_visitor < discovertime_propertymap_type > vis(discovertime_propertymap, time); | |
// Calling the bfs function | |
boost::breadth_first_search(undigraph, vertex(s, undigraph), visitor(vis)); | |
// Use std::sort to order the vertices by their discover time | |
std::vector<boost::graph_traits<UndirectedGraph>::vertices_size_type > discover_order(N); | |
boost::integer_range < int >range(0, N); | |
std::copy(range.begin(), range.end(), discover_order.begin()); | |
std::sort(discover_order.begin(), discover_order.end(), | |
boost::indirect_cmp < discovertime_propertymap_type, std::less < Size > >(discovertime_propertymap)); | |
std::cout << "order of discovery: "; | |
for (int i = 0; i < N; ++i) | |
std::cout << name[discover_order[i]] << " "; | |
std::cout << std::endl; | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment