# `topological_sort`

Graphs: | directed acyclic
Properties: | color
Complexity: | time: O(V + E)
Where Defined: | `boost/graph/topological_sort.hpp`

    (1)
    template <class VertexListGraph, class OutputIterator>
    void topological_sort(VertexListGraph& G, OutputIterator iter)

    (2)
    template <class VertexListGraph, class OutputIterator, class Visitor>
    void topological_sort(VertexListGraph& G, OutputIterator iter,
                          Visitor visit)

    (3)
    template <class VertexListGraph, class OutputIterator,
              class ColorMap, class DFSVisitor>
    void topological_sort(VertexListGraph& G, OutputIterator iter,
                          ColorMap color, DFSVisitor visit)

The topological sort algorithm creates a linear ordering of the vertices such that if edge (u,v) appears in the graph, then u comes before v in the ordering. The graph must be a directed acyclic graph (DAG). The implementation consists mainly of a call to `depth_first_search()`.

### Requirements on Types

- The graph type must be a model of VertexListGraph.
- Vertex descriptors of the graph will be output to the `OutputIterator`, which must model OutputIterator.
- The type `Visitor` must be a model of Visitor.
- The `ColorMap` must be a ReadWritePropertyMap and vertex descriptors from the graph must be usable as the map's key type.
- The `DFSVisitor` type must be a model of DFSVisitor.

### Example

Calculate a topological ordering of the vertices.

      typedef adjacency_list< vecS, vecS, directedS, color_property<> > Graph;
      typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
      Pair edges[7] = { Pair(0,1), Pair(2,4),
                        Pair(2,5),
                        Pair(0,3), Pair(1,4),
                        Pair(4,3), Pair(5,5) };
      Graph G(6, edges, edges + 7);

      typedef std::vector< Vertex > container;
      container c;
      topological_sort(G, std::back_inserter(c));

      cout << "A topological ordering: ";
      for ( container::reverse_iterator ii=c.rbegin(); ii!=c.rend(); ++ii)
        cout << index(*ii) << " ";
      cout << endl;

The output is:

      A topological ordering: 2 5 0 1 4 3

Copyright © 2000 | Jeremy Siek, Univ.of Notre Dame (jsiek@lsc.nd.edu)