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