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 | #include <boost/config.hpp> |
---|
10 | #include <assert.h> |
---|
11 | #include <iostream> |
---|
12 | |
---|
13 | #include <vector> |
---|
14 | #include <algorithm> |
---|
15 | #include <utility> |
---|
16 | |
---|
17 | |
---|
18 | #include <boost/graph/adjacency_list.hpp> |
---|
19 | #include <boost/graph/depth_first_search.hpp> |
---|
20 | #include <boost/graph/visitors.hpp> |
---|
21 | |
---|
22 | /* |
---|
23 | This calculates the discover finishing time. |
---|
24 | |
---|
25 | Sample Output |
---|
26 | |
---|
27 | Tree edge: 0 --> 2 |
---|
28 | Tree edge: 2 --> 1 |
---|
29 | Back edge: 1 --> 1 |
---|
30 | Tree edge: 1 --> 3 |
---|
31 | Back edge: 3 --> 1 |
---|
32 | Tree edge: 3 --> 4 |
---|
33 | Back edge: 4 --> 0 |
---|
34 | Back edge: 4 --> 1 |
---|
35 | Forward or cross edge: 2 --> 3 |
---|
36 | 1 10 |
---|
37 | 3 8 |
---|
38 | 2 9 |
---|
39 | 4 7 |
---|
40 | 5 6 |
---|
41 | |
---|
42 | */ |
---|
43 | |
---|
44 | using namespace boost; |
---|
45 | using namespace std; |
---|
46 | |
---|
47 | |
---|
48 | template <class VisitorList> |
---|
49 | struct edge_categorizer : public dfs_visitor<VisitorList> { |
---|
50 | typedef dfs_visitor<VisitorList> Base; |
---|
51 | |
---|
52 | edge_categorizer(const VisitorList& v = null_visitor()) : Base(v) { } |
---|
53 | |
---|
54 | template <class Edge, class Graph> |
---|
55 | void tree_edge(Edge e, Graph& G) { |
---|
56 | cout << "Tree edge: " << source(e, G) << |
---|
57 | " --> " << target(e, G) << endl; |
---|
58 | Base::tree_edge(e, G); |
---|
59 | } |
---|
60 | template <class Edge, class Graph> |
---|
61 | void back_edge(Edge e, Graph& G) { |
---|
62 | cout << "Back edge: " << source(e, G) |
---|
63 | << " --> " << target(e, G) << endl; |
---|
64 | Base::back_edge(e, G); |
---|
65 | } |
---|
66 | template <class Edge, class Graph> |
---|
67 | void forward_or_cross_edge(Edge e, Graph& G) { |
---|
68 | cout << "Forward or cross edge: " << source(e, G) |
---|
69 | << " --> " << target(e, G) << endl; |
---|
70 | Base::forward_or_cross_edge(e, G); |
---|
71 | } |
---|
72 | }; |
---|
73 | template <class VisitorList> |
---|
74 | edge_categorizer<VisitorList> |
---|
75 | categorize_edges(const VisitorList& v) { |
---|
76 | return edge_categorizer<VisitorList>(v); |
---|
77 | } |
---|
78 | |
---|
79 | int |
---|
80 | main(int , char* []) |
---|
81 | { |
---|
82 | |
---|
83 | using namespace boost; |
---|
84 | |
---|
85 | typedef adjacency_list<> Graph; |
---|
86 | |
---|
87 | Graph G(5); |
---|
88 | add_edge(0, 2, G); |
---|
89 | add_edge(1, 1, G); |
---|
90 | add_edge(1, 3, G); |
---|
91 | add_edge(2, 1, G); |
---|
92 | add_edge(2, 3, G); |
---|
93 | add_edge(3, 1, G); |
---|
94 | add_edge(3, 4, G); |
---|
95 | add_edge(4, 0, G); |
---|
96 | add_edge(4, 1, G); |
---|
97 | |
---|
98 | typedef graph_traits<Graph>::vertex_descriptor Vertex; |
---|
99 | typedef graph_traits<Graph>::vertices_size_type size_type; |
---|
100 | |
---|
101 | std::vector<size_type> d(num_vertices(G)); |
---|
102 | std::vector<size_type> f(num_vertices(G)); |
---|
103 | int t = 0; |
---|
104 | depth_first_search(G, visitor(categorize_edges( |
---|
105 | make_pair(stamp_times(&d[0], t, on_discover_vertex()), |
---|
106 | stamp_times(&f[0], t, on_finish_vertex()))))); |
---|
107 | |
---|
108 | std::vector<size_type>::iterator i, j; |
---|
109 | for (i = d.begin(), j = f.begin(); i != d.end(); ++i, ++j) |
---|
110 | cout << *i << " " << *j << endl; |
---|
111 | |
---|
112 | return 0; |
---|
113 | } |
---|
114 | |
---|