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