graph-thingie.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2008 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. // An example of using read_graphviz to load a GraphViz Dot textual
  6. // graph into a BGL adjacency_list graph that has custom properties.
  7. // Author: Ronald Garcia
  8. #include <boost/graph/graphviz.hpp>
  9. #include <boost/graph/adjacency_list.hpp>
  10. #include <boost/foreach.hpp>
  11. #include <string>
  12. #include <sstream>
  13. using namespace boost;
  14. using namespace std;
  15. //
  16. // Create a custom graph properties
  17. // (see the documentation for adjacency list)
  18. struct graph_identifier_t { typedef graph_property_tag kind; };
  19. struct vertex_label_t { typedef vertex_property_tag kind; };
  20. int main() {
  21. // Vertex properties
  22. typedef property < vertex_name_t, string,
  23. property < vertex_label_t, string,
  24. property < vertex_root_t, int > > > vertex_p;
  25. // Edge properties
  26. typedef property < edge_name_t, string > edge_p;
  27. // Graph properties
  28. typedef property < graph_name_t, string,
  29. property < graph_identifier_t, string > > graph_p;
  30. // adjacency_list-based type
  31. typedef adjacency_list < vecS, vecS, directedS,
  32. vertex_p, edge_p, graph_p > graph_t;
  33. // Construct an empty graph and prepare the dynamic_property_maps.
  34. graph_t graph(0);
  35. dynamic_properties dp;
  36. property_map<graph_t, vertex_name_t>::type vname =
  37. get(vertex_name, graph);
  38. dp.property("node_id",vname);
  39. property_map<graph_t, vertex_label_t>::type vlabel =
  40. get(vertex_label_t(), graph);
  41. dp.property("label",vlabel);
  42. property_map<graph_t, vertex_root_t>::type root =
  43. get(vertex_root, graph);
  44. dp.property("root",root);
  45. property_map<graph_t, edge_name_t>::type elabel =
  46. get(edge_name, graph);
  47. dp.property("label",elabel);
  48. // Use ref_property_map to turn a graph property into a property map
  49. ref_property_map<graph_t*,string>
  50. gname(get_property(graph,graph_name));
  51. dp.property("name",gname);
  52. // Use ref_property_map to turn a graph property into a property map
  53. ref_property_map<graph_t*,string>
  54. gid(get_property(graph,graph_identifier_t()));
  55. dp.property("identifier",gid);
  56. // Sample graph as an istream;
  57. const char* dot =
  58. "digraph \
  59. { \
  60. graph [name=\"GRAPH\", identifier=\"CX2A1Z\"] \
  61. \
  62. a [label=\"NODE_A\", root=\"1\"] \
  63. b [label=\"NODE_B\", root=\"0\"] \
  64. \
  65. a -> b [label=\"EDGE_1\"] \
  66. b -> c [label=\"EDGE_2\"] \
  67. }";
  68. istringstream gvgraph(dot);
  69. bool status = read_graphviz(gvgraph,graph,dp,"node_id");
  70. if (!status) {
  71. cerr << "read_graphviz() failed." << endl;
  72. return -1;
  73. }
  74. cout << "graph " << get("name",dp,&graph) <<
  75. " (" << get("identifier",dp,&graph) << ")\n\n";
  76. BOOST_FOREACH( graph_t::vertex_descriptor v, vertices(graph) ) {
  77. cout << "vertex " << get("node_id",dp,v) <<
  78. " (" << get("label",dp,v) << ")\n";
  79. }
  80. return 0;
  81. }