• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2008 Trustees of Indiana University
2 
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 // An example of using read_graphviz to load a GraphViz Dot textual
8 // graph into a BGL adjacency_list graph that has custom properties.
9 
10 // Author: Ronald Garcia
11 
12 #include <boost/graph/graphviz.hpp>
13 #include <boost/graph/adjacency_list.hpp>
14 #include <boost/foreach.hpp>
15 #include <string>
16 #include <sstream>
17 
18 using namespace boost;
19 using namespace std;
20 
21 //
22 // Create a custom graph properties
23 //  (see the documentation for adjacency list)
24 struct graph_identifier_t
25 {
26     typedef graph_property_tag kind;
27 };
28 struct vertex_label_t
29 {
30     typedef vertex_property_tag kind;
31 };
32 
main()33 int main()
34 {
35 
36     // Vertex properties
37     typedef property< vertex_name_t, string,
38         property< vertex_label_t, string, property< vertex_root_t, int > > >
39         vertex_p;
40     // Edge properties
41     typedef property< edge_name_t, string > edge_p;
42     // Graph properties
43     typedef property< graph_name_t, string,
44         property< graph_identifier_t, string > >
45         graph_p;
46     // adjacency_list-based type
47     typedef adjacency_list< vecS, vecS, directedS, vertex_p, edge_p, graph_p >
48         graph_t;
49 
50     // Construct an empty graph and prepare the dynamic_property_maps.
51     graph_t graph(0);
52     dynamic_properties dp;
53 
54     property_map< graph_t, vertex_name_t >::type vname
55         = get(vertex_name, graph);
56     dp.property("node_id", vname);
57 
58     property_map< graph_t, vertex_label_t >::type vlabel
59         = get(vertex_label_t(), graph);
60     dp.property("label", vlabel);
61 
62     property_map< graph_t, vertex_root_t >::type root = get(vertex_root, graph);
63     dp.property("root", root);
64 
65     property_map< graph_t, edge_name_t >::type elabel = get(edge_name, graph);
66     dp.property("label", elabel);
67 
68     // Use ref_property_map to turn a graph property into a property map
69     ref_property_map< graph_t*, string > gname(get_property(graph, graph_name));
70     dp.property("name", gname);
71 
72     // Use ref_property_map to turn a graph property into a property map
73     ref_property_map< graph_t*, string > gid(
74         get_property(graph, graph_identifier_t()));
75     dp.property("identifier", gid);
76     // Sample graph as an istream;
77 
78     const char* dot = "digraph \
79 { \
80   graph [name=\"GRAPH\", identifier=\"CX2A1Z\"] \
81     \
82     a [label=\"NODE_A\", root=\"1\"] \
83     b [label=\"NODE_B\", root=\"0\"] \
84  \
85  a -> b [label=\"EDGE_1\"] \
86  b -> c [label=\"EDGE_2\"] \
87 }";
88 
89     istringstream gvgraph(dot);
90 
91     bool status = read_graphviz(gvgraph, graph, dp, "node_id");
92     if (!status)
93     {
94         cerr << "read_graphviz() failed." << endl;
95         return -1;
96     }
97 
98     cout << "graph " << get("name", dp, &graph) << " ("
99          << get("identifier", dp, &graph) << ")\n\n";
100 
101     BOOST_FOREACH (graph_t::vertex_descriptor v, vertices(graph))
102     {
103         cout << "vertex " << get("node_id", dp, v) << " ("
104              << get("label", dp, v) << ")\n";
105     }
106 
107     return 0;
108 }
109