topological_sort
Creates a linear ordering of the vertices such that if edge (u,v) appears in the graph, then v comes before u in the ordering.
Complexity: O(V + E)
Defined in: <boost/graph/topological_sort.hpp>
Example
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
struct VertexProps { std::string name; };
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, VertexProps>;
int main() {
Graph g{5};
g[0].name = "A"; g[1].name = "B"; g[2].name = "C";
g[3].name = "D"; g[4].name = "E";
boost::add_edge(0, 2, g); // A -> C
boost::add_edge(1, 2, g); // B -> C
boost::add_edge(2, 3, g); // C -> D
boost::add_edge(2, 4, g); // C -> E
std::vector<Graph::vertex_descriptor> order;
boost::topological_sort(g, std::back_inserter(order));
std::cout << "Topological order: ";
for (auto v : order) { std::cout << g[v].name << " "; }
std::cout << std::endl;
}
Topological order: D E C A B
(1) Named parameter version
template <typename VertexListGraph, typename OutputIterator,
typename P, typename T, typename R>
void topological_sort(
VertexListGraph& g, OutputIterator result,
const bgl_named_params<P, T, R>& params = all defaults)
| Direction | Parameter | Description |
|---|---|---|
IN |
|
A directed acyclic graph (DAG). The graph type must be a model of
Vertex List Graph and
Incidence Graph.
If the graph is not a DAG then a
|
OUT |
|
The vertex descriptors of the graph will be output to the |
UTIL/OUT |
|
This is used by the algorithm to keep track of its progress through the
graph. The type |
IN |
|
This maps each vertex to an integer in the range
|
(2) Positional version
template <typename VertexListGraph, typename OutputIterator>
void topological_sort(VertexListGraph& g, OutputIterator result);
Equivalent to (1) with every named parameter left at its default. Uses get(vertex_index, g) for the index map and an internally allocated default_color_type color map; requires the graph to have an interior vertex_index_t property.
| Direction | Parameter | Description |
|---|---|---|
IN |
|
Same requirements as (1). Must be a DAG. |
OUT |
|
Same as (1). Output is in reverse topological order. |
Description
The topological sort
algorithm creates a linear ordering of the vertices such that if edge (u,v) appears in the graph, then v comes before u in the ordering. The graph must be a directed acyclic graph (DAG). The implementation consists mainly of a call to depth_first_search().
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[6] = { Pair(0,1), Pair(2,4),
Pair(2,5),
Pair(0,3), Pair(1,4),
Pair(4,3) };
Graph G(6, edges, edges + 6);
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