How to create a C++ Boost undirected graph and traverse it in depth first search (DFS) order?
// Boost DFS example on an undirected graph.
// Create a sample graph, traverse its nodes
// in DFS order and print out their values.
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <iostream>
using namespace std;
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS> MyGraph;
typedef boost::graph_traits<MyGraph>::vertex_descriptor MyVertex;
class MyVisitor : public boost::default_dfs_visitor
{
public:
void discover_vertex(MyVertex v, const MyGraph& g) const
{
cerr << v << endl;
return;
}
};
int main()
{
MyGraph g;
boost::add_edge(0, 1, g);
boost::add_edge(0, 2, g);
boost::add_edge(1, 2, g);
boost::add_edge(1, 3, g);
MyVisitor vis;
boost::depth_first_search(g, boost::visitor(vis));
return 0;
}
How to traverse graph in boost use BFS
You can see here a list of the overloads of breadth_first_search
. If you don't want to specify every one of the parameters you need to use the named-parameter version. It would look like this:
breadth_first_search(graph, a, boost::visitor(bfs_visitor));
This would work as is if you had used vecS
as your VertexList storage in your graph definition or if you had constructed and initialized an internal vertex_index property map. Since you are using hash_setS
you need to change the invocation to:
breath_first_search(graph, a, boost::visitor(bfs_visitor).vertex_index_map(my_index_map));
You are already using an index map in your uint32_t bundled property. You can use get(boost::vertex_bundle, graph)
to access it.
There was also a problem with your visitor. You should derive it from boost::default_bfs_visitor
and the graph_t
parameter of your member functions needs to be const qualified.
Full code:
#include <stdint.h>
#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
typedef boost::adjacency_list<boost::vecS, boost::hash_setS, boost::undirectedS, uint32_t, uint32_t, boost::no_property> graph_t;
struct my_visitor : boost::default_bfs_visitor{
void initialize_vertex(const graph_t::vertex_descriptor &s, const graph_t &g) const {
std::cout << "Initialize: " << g[s] << std::endl;
}
void discover_vertex(const graph_t::vertex_descriptor &s, const graph_t &g) const {
std::cout << "Discover: " << g[s] << std::endl;
}
void examine_vertex(const graph_t::vertex_descriptor &s, const graph_t &g) const {
std::cout << "Examine vertex: " << g[s] << std::endl;
}
void examine_edge(const graph_t::edge_descriptor &e, const graph_t &g) const {
std::cout << "Examine edge: " << g[e] << std::endl;
}
void tree_edge(const graph_t::edge_descriptor &e, const graph_t &g) const {
std::cout << "Tree edge: " << g[e] << std::endl;
}
void non_tree_edge(const graph_t::edge_descriptor &e, const graph_t &g) const {
std::cout << "Non-Tree edge: " << g[e] << std::endl;
}
void gray_target(const graph_t::edge_descriptor &e, const graph_t &g) const {
std::cout << "Gray target: " << g[e] << std::endl;
}
void black_target(const graph_t::edge_descriptor &e, const graph_t &g) const {
std::cout << "Black target: " << g[e] << std::endl;
}
void finish_vertex(const graph_t::vertex_descriptor &s, const graph_t &g) const {
std::cout << "Finish vertex: " << g[s] << std::endl;
}
};
int main() {
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);
my_visitor vis;
breadth_first_search(graph, a, boost::visitor(vis).vertex_index_map(get(boost::vertex_bundle,graph)));
return 0;
}
boost minimum spanning tree, how to do depth first?
Update:
sehe gives an updated and more efficient solution here:
https://stackoverflow.com/a/49429372/85371
Here is a solution to the problem and good example of Kruskal and writing a custom DFS visitors. It should run as is. Example output in shown in the code below as to be self contained. As I mentioned in the comment the output of the MST algorithm is a set of edges. This shows you how to construct a new graph using that data.
Example take from http://en.wikipedia.org/wiki/Kruskals_algorithm.
Any suggestions for improvement would be appreciated. Thanks.
/**
Kruskal example from http://en.wikipedia.org/wiki/Kruskal's_algorithm
MST followed by DFS
Written by Paul W. Bible
*/
#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
using namespace std;
using namespace boost;
typedef adjacency_list < vecS, vecS, undirectedS,
property< vertex_index_t, size_t> ,
property< edge_index_t, size_t, property<edge_weight_t,double> > > Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef graph_traits<Graph>::edge_descriptor Edge;
typedef boost::property_map< Graph, boost::vertex_index_t>::type VertexIndexMap;
typedef boost::property_map< Graph, boost::edge_weight_t>::type WeightMap;
//DFS visitor, got help from http://stackoverflow.com/questions/14126/how-to-create-a-c-boost-undirected-graph-and-traverse-it-in-depth-first-search
// and http://www.boost.org/doc/libs/1_55_0/libs/graph/example/dfs-example.cpp
struct MyVis:default_dfs_visitor{
//Default dfs is templeted to work with any Edge or Graph class
// you will need to pass external graph info to the class
MyVis(vector<string> vNames):vertNames(vNames){}
template < typename Edge, typename Graph >
void tree_edge(Edge e, const Graph& g) const {
//This works since all graph verts will have an index
VertexIndexMap vMap = get(boost::vertex_index,g);
//print output message, source and target get the edge vertices
cout << "Edge " << vertNames.at(vMap[source(e,g)]) << " " << vertNames.at(vMap[target(e,g)]) << endl;
//cout << vertNames.size() << endl;
}
private:
vector<string> vertNames;
};
int main(int argc, char* argv[]){
Graph G;
vector<Vertex> verts;
vector<Edge> edges;
/* Vertices
0 A
1 B
2 C
3 D
4 E
5 F
6 G
*/
//add 7 vertices
for(size_t i = 0; i < 7; ++i){
Vertex v = add_vertex(G);
verts.push_back(v);
}
//vertex to index map, typdef above
VertexIndexMap vertexIndexMap = get(boost::vertex_index, G);
vector<string> vertexNames(num_vertices(G));
// Create the external property map, this map wraps the storage vector vertexNames
boost::iterator_property_map< std::vector< string >::iterator, VertexIndexMap >
vertexNameMap(vertexNames.begin(), vertexIndexMap);
//set names
vertexNames.at(0) = "A";
vertexNames.at(1) = "B";
vertexNames.at(2) = "C";
vertexNames.at(3) = "D";
vertexNames.at(4) = "E";
vertexNames.at(5) = "F";
vertexNames.at(6) = "G";
//get internal weight map
WeightMap weightMap = get(edge_weight,G);
//Edge 1 A -> B, weight 7
pair<Edge,bool> myPair = add_edge(verts.at(0),verts.at(1),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 7.0;
//Edge 2 A -> D, weight 5
myPair = add_edge(verts.at(0),verts.at(3),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 5.0;
//Edge 3 B -> C, weight 8
myPair = add_edge(verts.at(1),verts.at(2),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 8.0;
//Edge 4 B -> D, weight 9
myPair = add_edge(verts.at(1),verts.at(3),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 9.0;
//Edge 5 B -> E, weight 7
myPair = add_edge(verts.at(1),verts.at(4),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 7.0;
//Edge 6 C -> E, weight 5
myPair = add_edge(verts.at(2),verts.at(4),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 5.0;
//Edge 7 D -> E, weight 15
myPair = add_edge(verts.at(3),verts.at(4),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 15.0;
//Edge 8 D -> F, weight 6
myPair = add_edge(verts.at(3),verts.at(5),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 6.0;
//Edge 9 E -> F, weight 8
myPair = add_edge(verts.at(4),verts.at(5),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 8.0;
//Edge 10 E -> G, weight 9
myPair = add_edge(verts.at(4),verts.at(6),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 9.0;
//Edge 11 F -> G, weight 11
myPair = add_edge(verts.at(5),verts.at(6),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 11.0;
//output
cout << "vertices " << num_vertices(G) << endl;
cout << "edges " << num_edges(G) << endl;
//create a stoage vector for MST edges
vector<Edge> spanning_tree_edges;
kruskal_minimum_spanning_tree(G, std::back_inserter(spanning_tree_edges));
cout << "num MST edges " << spanning_tree_edges.size() << endl;
//create a graph for the MST
Graph MST;
//get a weight map for the MST, may be used later
WeightMap mstWeightMap = get(edge_weight,MST);
//create a list of original names for the MST graph.
vector<string> mstNames(num_vertices(G)); //the MST must span all verts in G
//Index map for verts in MST, all graphs use an indepenent index system.
VertexIndexMap mstIndexMap = get(boost::vertex_index, MST);
cout << "MST Edges" << endl;
for(size_t i = 0; i < spanning_tree_edges.size(); ++i){
//get the edge
Edge e = spanning_tree_edges.at(i);
//get the vertices
Vertex v1 = source(e,G);
Vertex v2 = target(e,G);
// output edge information
cout << "edge weight " << weightMap[e] << " v1 " << vertexNameMap[v1] << " v2 " << vertexNameMap[v2] << endl;
//insert the edge to the MST graph
// Both graphs will share the vertices in verts list.
myPair = add_edge(v1,v2,MST);
//set the correct weights
// may be needed at some point
Edge mstE = myPair.first;
mstWeightMap[mstE] = weightMap[e];
//get the vertex index in the MST and set the name to that of original graph
// mstNames will be used by the visitor
mstNames.at(mstIndexMap[v1]) = vertexNameMap[v1];
mstNames.at(mstIndexMap[v2]) = vertexNameMap[v2];
}
//Create your custom visitor and pass names to the visitor
MyVis vis(mstNames);
cout << "DFS on MST: start node E" << endl;
//call dfs, see visitor implimentation above.
boost::depth_first_search(MST, visitor(vis).root_vertex(verts.at(4)));
cout << "DFS on MST: start node B" << endl;
//call dfs, see visitor implimentation above.
boost::depth_first_search(MST, visitor(vis).root_vertex(verts.at(1)));
/* output
vertices 7
edges 11
num MST edges 6
MST Edges
edge weight 5 v1 A v2 D
edge weight 5 v1 C v2 E
edge weight 6 v1 D v2 F
edge weight 7 v1 B v2 E
edge weight 7 v1 A v2 B
edge weight 9 v1 E v2 G
DFS on MST: start node E
Edge E C
Edge E B
Edge B A
Edge A D
Edge D F
Edge E G
DFS on MST: start node B
Edge B E
Edge E C
Edge E G
Edge B A
Edge A D
Edge D F
*/
//hold for output
cin.get();
}
Cycles in an Undirected Graph
I think that depth first search solves it. If an unexplored edge leads to a node visited before, then the graph contains a cycle. This condition also makes it O(n), since you can explore maximum n edges without setting it to true or being left with no unexplored edges.
Related Topics
C++ Ifstream Error Using String as Opening File Path
Clean Eclipse Index, It Is Out of Sync with Code
What Should I Know About Structured Exceptions (Seh) in C++
Some Clarification Needed About Synchronous Versus Asynchronous Asio Operations
Debug Assertion Failed! Expression: _Acrt_First_Block == Header
Difference Between Logical and Physical Const-Ness
Making a C++ Class a Monitor (In the Concurrent Sense)
Template Metaprogramming - Difference Between Using Enum Hack and Static Const
How to Enable_Shared_From_This of Both Parent and Derived
Vc++ Fatal Error Lnk1168: Cannot Open Filename.Exe for Writing
List of C++ Name Resolution (And Overloading) Rules
Sharing Precompiled Headers Between Projects in Visual Studio
Broken C++ Std Libraries on MACos High Sierra 10.13
What Is the Branch in the Destructor Reported by Gcov
Error: Class Has Not Been Declared Despite Header Inclusion, and the Code Compiling Fine Elsewhere