topological_sort.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. {
  21. // Topological sort visitor
  22. //
  23. // This visitor merely writes the linear ordering into an
  24. // OutputIterator. The OutputIterator could be something like an
  25. // ostream_iterator, or it could be a back/front_insert_iterator.
  26. // Note that if it is a back_insert_iterator, the recorded order is
  27. // the reverse topological order. On the other hand, if it is a
  28. // front_insert_iterator, the recorded order is the topological
  29. // order.
  30. //
  31. template < typename OutputIterator >
  32. struct topo_sort_visitor : public dfs_visitor<>
  33. {
  34. topo_sort_visitor(OutputIterator _iter) : m_iter(_iter) {}
  35. template < typename Edge, typename Graph >
  36. void back_edge(const Edge&, Graph&)
  37. {
  38. BOOST_THROW_EXCEPTION(not_a_dag());
  39. }
  40. template < typename Vertex, typename Graph >
  41. void finish_vertex(const Vertex& u, Graph&)
  42. {
  43. *m_iter++ = u;
  44. }
  45. OutputIterator m_iter;
  46. };
  47. // Topological Sort
  48. //
  49. // The topological sort algorithm creates a linear ordering
  50. // of the vertices such that if edge (u,v) appears in the graph,
  51. // then u comes before v in the ordering. The graph must
  52. // be a directed acyclic graph (DAG). The implementation
  53. // consists mainly of a call to depth-first search.
  54. //
  55. template < typename VertexListGraph, typename OutputIterator, typename P,
  56. typename T, typename R >
  57. void topological_sort(VertexListGraph& g, OutputIterator result,
  58. const bgl_named_params< P, T, R >& params)
  59. {
  60. typedef topo_sort_visitor< OutputIterator > TopoVisitor;
  61. depth_first_search(g, params.visitor(TopoVisitor(result)));
  62. }
  63. template < typename VertexListGraph, typename OutputIterator >
  64. void topological_sort(VertexListGraph& g, OutputIterator result)
  65. {
  66. topological_sort(
  67. g, result, bgl_named_params< int, buffer_param_t >(0)); // bogus
  68. }
  69. } // namespace boost
  70. #endif /*BOOST_GRAPH_TOPOLOGICAL_SORT_H*/