williamr@2: // williamr@2: //======================================================================= williamr@2: // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. williamr@2: // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek williamr@2: // williamr@2: // Distributed under the Boost Software License, Version 1.0. (See williamr@2: // accompanying file LICENSE_1_0.txt or copy at williamr@2: // http://www.boost.org/LICENSE_1_0.txt) williamr@2: //======================================================================= williamr@2: // williamr@2: #ifndef BOOST_GRAPH_TOPOLOGICAL_SORT_HPP williamr@2: #define BOOST_GRAPH_TOPOLOGICAL_SORT_HPP williamr@2: williamr@2: #include williamr@2: #include williamr@2: #include williamr@2: #include williamr@2: #include williamr@2: williamr@2: namespace boost { williamr@2: williamr@2: williamr@2: // Topological sort visitor williamr@2: // williamr@2: // This visitor merely writes the linear ordering into an williamr@2: // OutputIterator. The OutputIterator could be something like an williamr@2: // ostream_iterator, or it could be a back/front_insert_iterator. williamr@2: // Note that if it is a back_insert_iterator, the recorded order is williamr@2: // the reverse topological order. On the other hand, if it is a williamr@2: // front_insert_iterator, the recorded order is the topological williamr@2: // order. williamr@2: // williamr@2: template williamr@2: struct topo_sort_visitor : public dfs_visitor<> williamr@2: { williamr@2: topo_sort_visitor(OutputIterator _iter) williamr@2: : m_iter(_iter) { } williamr@2: williamr@2: template williamr@2: void back_edge(const Edge& u, Graph&) { throw not_a_dag(); } williamr@2: williamr@2: template williamr@2: void finish_vertex(const Vertex& u, Graph&) { *m_iter++ = u; } williamr@2: williamr@2: OutputIterator m_iter; williamr@2: }; williamr@2: williamr@2: williamr@2: // Topological Sort williamr@2: // williamr@2: // The topological sort algorithm creates a linear ordering williamr@2: // of the vertices such that if edge (u,v) appears in the graph, williamr@2: // then u comes before v in the ordering. The graph must williamr@2: // be a directed acyclic graph (DAG). The implementation williamr@2: // consists mainly of a call to depth-first search. williamr@2: // williamr@2: williamr@2: template williamr@2: void topological_sort(VertexListGraph& g, OutputIterator result, williamr@2: const bgl_named_params& params) williamr@2: { williamr@2: typedef topo_sort_visitor TopoVisitor; williamr@2: depth_first_search(g, params.visitor(TopoVisitor(result))); williamr@2: } williamr@2: williamr@2: template williamr@2: void topological_sort(VertexListGraph& g, OutputIterator result) williamr@2: { williamr@2: topological_sort(g, result, williamr@2: bgl_named_params(0)); // bogus williamr@2: } williamr@2: williamr@2: } // namespace boost williamr@2: williamr@2: #endif /*BOOST_GRAPH_TOPOLOGICAL_SORT_H*/