News & Events

Boost Graph Visualisation using dot and Graphviz

Debugging graphs can sometimes be annoying a useful way is to visualise it. No problem Boost has not only a graph solution but a way to visualise it too.

It is really simple first the header,

#include <boost/graph/graphviz.hpp>

Define the name of each node, easily done by an array

const std::string names [] = {"A", "B", "C", "D", "E" } ;

or via a vector

std::vector<std::string> names;
      for (int i = 0 ; i < _nodes.size() ; i++){
          std::stringstream ss;
          names.push_back(ss.str());
      }

Then save it out

std::ofstream dmp;
      dmp.open("dmp.dot");
      boost::write_graphviz(dmp,g, boost::make_label_writer(&names[0]));

And then the result:

 

Graph_Dot_Undirected Graph_Dot_Directed

Left undirected graph, right directed hence the arrow heads.

Stuart James