Boost/BGL/IO/WriteGraph

From ProgrammingExamples
< Boost‎ | BGL
Jump to: navigation, search

WriteGraph.cpp

// http://www.boost.org/doc/libs/1_38_0/libs/graph/doc/write-graphviz.html
 
#include <iostream>
#include <string>
#include <fstream>
 
//#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/undirected_graph.hpp>
#include <boost/graph/graphviz.hpp>
 
typedef boost::undirected_graph<boost::no_property> Graph;
 
int main(int argc, char*argv[])
{
  if(argc < 2)
  {
    std::cerr << "Required: filename.dot" << std::endl;
    return -1;
  }
  std::string filename = argv[1];
  std::ofstream fout(filename.c_str());
 
  Graph g;
  Graph::vertex_descriptor v0 = g.add_vertex();
  Graph::vertex_descriptor v1 = g.add_vertex();
  boost::add_edge(v0,v1,g);
 
  boost::write_graphviz(fout,g);
 
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(WriteGraph)
 
set(Boost_USE_MULTITHREADED ON)
FIND_PACKAGE(Boost 1.38 COMPONENTS program_options required)
 
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS})
 
ADD_EXECUTABLE(WriteGraph WriteGraph.cpp)
target_link_libraries(WriteGraph boost_graph)

output.dot

The file that is produced looks like:

graph G {
0;
1;
0--1 ;
}

You can then convert the graph to an image with:

neato -Tpng eroded_0.dot > eroded_0.png