topological_sort.hpp 2.6 KB

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