拓扑排序是很重要的算法,使用boost库的topological_sort。
使用如下,输入数据:
{
"edge_list": [
[0, 1],
[2, 4],
[2, 5],
[0, 3],
[1, 4],
[4, 3]
]
}
输出拓扑排序数据如下:
{
"topo_order": [
2,
5,
0,
1,
4,
3
]
}
集成到可视化平台如下:
其中boost库demo如下:
#include <boost/config.hpp>
#include <iostream>
#include <list>
#include <algorithm>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>
#include <iterator>
#include <utility>
typedef std::pair< std::size_t, std::size_t > Pair;
/*
Sample output:
A topological ordering: 2 5 0 1 4 3
*/
int main(int, char*[])
{
// begin
using namespace boost;
/* Topological sort will need to color the graph. Here we use an
internal decorator, so we "property" the color to the graph.
*/
typedef adjacency_list< vecS, vecS, directedS,
property< vertex_color_t, default_color_type > >
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);
for (std::size_t j = 0; j < 6; ++j)
add_edge(edges[j].first, edges[j].second, G);
boost::property_map< Graph, vertex_index_t >::type id
= get(vertex_index, G);
typedef std::vector< Vertex > container;
container c;
topological_sort(G, std::back_inserter(c));
std::cout << "A topological ordering: ";
for (container::reverse_iterator ii = c.rbegin(); ii != c.rend(); ++ii)
std::cout << id[*ii] << " ";
std::cout << std::endl;
return 0;
}