Skip to content

Instantly share code, notes, and snippets.

@fmoessbauer
Created November 24, 2017 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmoessbauer/163b9928ae9170cfe2651173f416314b to your computer and use it in GitHub Desktop.
Save fmoessbauer/163b9928ae9170cfe2651173f416314b to your computer and use it in GitHub Desktop.
Export a boost graph to dimacs format.
#ifndef BGL_WRITE_DIMACS_INC
#define BGL_WRITE_DIMACS_INC
/**
* export boost graph to dimacs graph format
*/
template<typename graph_t>
void write_dimacs(const graph_t & graph, std::ostream & out){
auto num_edges = boost::num_edges(graph);
auto num_vertices = boost::num_vertices(graph);
boost::graph_traits<graph_t>::edge_iterator ei, ei_end;
out << "p edge " << num_vertices << " " << num_edges << std::endl;
for (tie(ei, ei_end) = boost::edges(graph); ei != ei_end; ++ei){
auto source = boost::source(*ei, graph);
auto target = boost::target(*ei, graph);
auto sidx = boost::get(boost::vertex_index, graph, source);
auto tidx = boost::get(boost::vertex_index, graph, target);
out << "e " << sidx+1 << " " << tidx+1 << std::endl;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment