1 //=======================================================================
2 // Copyright 2002 Indiana University.
3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
9
10 #include <boost/graph/graph_as_tree.hpp>
11 #include <boost/graph/adjacency_list.hpp>
12 #include <boost/cstdlib.hpp>
13 #include <iostream>
14
15 class tree_printer
16 {
17 public:
preorder(Node,Tree &)18 template < typename Node, typename Tree > void preorder(Node, Tree&)
19 {
20 std::cout << "(";
21 }
inorder(Node n,Tree & t)22 template < typename Node, typename Tree > void inorder(Node n, Tree& t)
23 {
24 std::cout << get(boost::vertex_name, t)[n];
25 }
postorder(Node,Tree &)26 template < typename Node, typename Tree > void postorder(Node, Tree&)
27 {
28 std::cout << ")";
29 }
30 };
31
main()32 int main()
33 {
34 using namespace boost;
35 typedef adjacency_list< vecS, vecS, directedS,
36 property< vertex_name_t, std::string > >
37 graph_t;
38 typedef graph_traits< graph_t >::vertex_descriptor vertex_t;
39
40 graph_t g;
41
42 vertex_t a = add_vertex(g), b = add_vertex(g), c = add_vertex(g);
43
44 add_edge(a, b, g);
45 add_edge(a, c, g);
46
47 typedef property_map< graph_t, vertex_name_t >::type vertex_name_map_t;
48 vertex_name_map_t name = get(vertex_name, g);
49 name[a] = "A";
50 name[b] = "B";
51 name[c] = "C";
52
53 typedef iterator_property_map< std::vector< vertex_t >::iterator,
54 property_map< graph_t, vertex_index_t >::type >
55 parent_map_t;
56 std::vector< vertex_t > parent(num_vertices(g));
57 typedef graph_as_tree< graph_t, parent_map_t > tree_t;
58 tree_t t(
59 g, a, make_iterator_property_map(parent.begin(), get(vertex_index, g)));
60
61 tree_printer vis;
62 traverse_tree(a, t, vis);
63
64 return exit_success;
65 }
66