read_graphviz.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2006 Trustees of Indiana University
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // A simple example of using read_graphviz to load a GraphViz Dot textual
  6. // graph into a BGL adjacency_list graph
  7. // Author: Ronald Garcia
  8. #include <boost/graph/graphviz.hpp>
  9. #include <boost/graph/adjacency_list.hpp>
  10. #include <string>
  11. #include <sstream>
  12. using namespace boost;
  13. using namespace std;
  14. int main() {
  15. // Vertex properties
  16. typedef property < vertex_name_t, std::string,
  17. property < vertex_color_t, float > > vertex_p;
  18. // Edge properties
  19. typedef property < edge_weight_t, double > edge_p;
  20. // Graph properties
  21. typedef property < graph_name_t, std::string > graph_p;
  22. // adjacency_list-based type
  23. typedef adjacency_list < vecS, vecS, directedS,
  24. vertex_p, edge_p, graph_p > graph_t;
  25. // Construct an empty graph and prepare the dynamic_property_maps.
  26. graph_t graph(0);
  27. dynamic_properties dp;
  28. property_map<graph_t, vertex_name_t>::type name =
  29. get(vertex_name, graph);
  30. dp.property("node_id",name);
  31. property_map<graph_t, vertex_color_t>::type mass =
  32. get(vertex_color, graph);
  33. dp.property("mass",mass);
  34. property_map<graph_t, edge_weight_t>::type weight =
  35. get(edge_weight, graph);
  36. dp.property("weight",weight);
  37. // Use ref_property_map to turn a graph property into a property map
  38. boost::ref_property_map<graph_t*,std::string>
  39. gname(get_property(graph,graph_name));
  40. dp.property("name",gname);
  41. // Sample graph as an std::istream;
  42. std::istringstream
  43. gvgraph("digraph { graph [name=\"graphname\"] a c e [mass = 6.66] }");
  44. bool status = read_graphviz(gvgraph,graph,dp,"node_id");
  45. return status ? EXIT_SUCCESS : EXIT_FAILURE;
  46. }