• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //=======================================================================
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
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 //  Sample output
11 //  DFS parenthesis:
12 //  (0(2(3(4(11)4)3)2)0)
13 
14 #include <boost/config.hpp>
15 #include <assert.h>
16 #include <iostream>
17 
18 #include <vector>
19 #include <algorithm>
20 #include <utility>
21 
22 #include "boost/graph/visitors.hpp"
23 #include "boost/graph/adjacency_list.hpp"
24 #include "boost/graph/breadth_first_search.hpp"
25 #include "boost/graph/depth_first_search.hpp"
26 
27 using namespace boost;
28 using namespace std;
29 
30 struct open_paren : public base_visitor< open_paren >
31 {
32     typedef on_discover_vertex event_filter;
operator ()open_paren33     template < class Vertex, class Graph > void operator()(Vertex v, Graph&)
34     {
35         std::cout << "(" << v;
36     }
37 };
38 struct close_paren : public base_visitor< close_paren >
39 {
40     typedef on_finish_vertex event_filter;
operator ()close_paren41     template < class Vertex, class Graph > void operator()(Vertex v, Graph&)
42     {
43         std::cout << v << ")";
44     }
45 };
46 
main(int,char * [])47 int main(int, char*[])
48 {
49 
50     using namespace boost;
51 
52     typedef adjacency_list<> Graph;
53     typedef std::pair< int, int > E;
54     E edge_array[] = { E(0, 2), E(1, 1), E(1, 3), E(2, 1), E(2, 3), E(3, 1),
55         E(3, 4), E(4, 0), E(4, 1) };
56 #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
57     Graph G(5);
58     for (std::size_t j = 0; j < sizeof(edge_array) / sizeof(E); ++j)
59         add_edge(edge_array[j].first, edge_array[j].second, G);
60 #else
61     Graph G(edge_array, edge_array + sizeof(edge_array) / sizeof(E), 5);
62 #endif
63 
64     std::cout << "DFS parenthesis:" << std::endl;
65     depth_first_search(G,
66         visitor(make_dfs_visitor(std::make_pair(open_paren(), close_paren()))));
67     std::cout << std::endl;
68     return 0;
69 }
70