Chris@16
|
1 //
|
Chris@16
|
2 //=======================================================================
|
Chris@16
|
3 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
|
Chris@16
|
4 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
|
Chris@16
|
5 //
|
Chris@16
|
6 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
7 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //=======================================================================
|
Chris@16
|
10 //
|
Chris@16
|
11 #ifndef BOOST_GRAPH_TOPOLOGICAL_SORT_HPP
|
Chris@16
|
12 #define BOOST_GRAPH_TOPOLOGICAL_SORT_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/config.hpp>
|
Chris@16
|
15 #include <boost/property_map/property_map.hpp>
|
Chris@16
|
16 #include <boost/graph/depth_first_search.hpp>
|
Chris@16
|
17 #include <boost/graph/visitors.hpp>
|
Chris@16
|
18 #include <boost/graph/exception.hpp>
|
Chris@16
|
19 #include <boost/throw_exception.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 namespace boost {
|
Chris@16
|
22
|
Chris@16
|
23
|
Chris@16
|
24 // Topological sort visitor
|
Chris@16
|
25 //
|
Chris@16
|
26 // This visitor merely writes the linear ordering into an
|
Chris@16
|
27 // OutputIterator. The OutputIterator could be something like an
|
Chris@16
|
28 // ostream_iterator, or it could be a back/front_insert_iterator.
|
Chris@16
|
29 // Note that if it is a back_insert_iterator, the recorded order is
|
Chris@16
|
30 // the reverse topological order. On the other hand, if it is a
|
Chris@16
|
31 // front_insert_iterator, the recorded order is the topological
|
Chris@16
|
32 // order.
|
Chris@16
|
33 //
|
Chris@16
|
34 template <typename OutputIterator>
|
Chris@16
|
35 struct topo_sort_visitor : public dfs_visitor<>
|
Chris@16
|
36 {
|
Chris@16
|
37 topo_sort_visitor(OutputIterator _iter)
|
Chris@16
|
38 : m_iter(_iter) { }
|
Chris@16
|
39
|
Chris@16
|
40 template <typename Edge, typename Graph>
|
Chris@16
|
41 void back_edge(const Edge&, Graph&) { BOOST_THROW_EXCEPTION(not_a_dag()); }
|
Chris@16
|
42
|
Chris@16
|
43 template <typename Vertex, typename Graph>
|
Chris@16
|
44 void finish_vertex(const Vertex& u, Graph&) { *m_iter++ = u; }
|
Chris@16
|
45
|
Chris@16
|
46 OutputIterator m_iter;
|
Chris@16
|
47 };
|
Chris@16
|
48
|
Chris@16
|
49
|
Chris@16
|
50 // Topological Sort
|
Chris@16
|
51 //
|
Chris@16
|
52 // The topological sort algorithm creates a linear ordering
|
Chris@16
|
53 // of the vertices such that if edge (u,v) appears in the graph,
|
Chris@16
|
54 // then u comes before v in the ordering. The graph must
|
Chris@16
|
55 // be a directed acyclic graph (DAG). The implementation
|
Chris@16
|
56 // consists mainly of a call to depth-first search.
|
Chris@16
|
57 //
|
Chris@16
|
58
|
Chris@16
|
59 template <typename VertexListGraph, typename OutputIterator,
|
Chris@16
|
60 typename P, typename T, typename R>
|
Chris@16
|
61 void topological_sort(VertexListGraph& g, OutputIterator result,
|
Chris@16
|
62 const bgl_named_params<P, T, R>& params)
|
Chris@16
|
63 {
|
Chris@16
|
64 typedef topo_sort_visitor<OutputIterator> TopoVisitor;
|
Chris@16
|
65 depth_first_search(g, params.visitor(TopoVisitor(result)));
|
Chris@16
|
66 }
|
Chris@16
|
67
|
Chris@16
|
68 template <typename VertexListGraph, typename OutputIterator>
|
Chris@16
|
69 void topological_sort(VertexListGraph& g, OutputIterator result)
|
Chris@16
|
70 {
|
Chris@16
|
71 topological_sort(g, result,
|
Chris@16
|
72 bgl_named_params<int, buffer_param_t>(0)); // bogus
|
Chris@16
|
73 }
|
Chris@16
|
74
|
Chris@16
|
75 } // namespace boost
|
Chris@16
|
76
|
Chris@16
|
77 #endif /*BOOST_GRAPH_TOPOLOGICAL_SORT_H*/
|