最近需要使用连通域算法,于是就使用了boost库的算法。主要是用connected_components函数。
测试用法:
输入数据:
{
"edge_list": [
["A", "B"],
["B", "E"],
["E", "A"],
["C", "F"],
["J", "K"]
]
}
运行连通域算法:
{
"num": 3,
"components": [
["A","B", "E" ],
["C", "F" ],
[ "J", "K" ]
]
}
集成到到可视化平台运行如下:
demo代码:
#include <boost/config.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
using namespace std;
int main(int, char*[])
{
using namespace boost;
{
typedef adjacency_list< vecS, vecS, undirectedS > Graph;
Graph G;
add_edge(0, 1, G);
add_edge(1, 4, G);
add_edge(4, 0, G);
add_edge(2, 5, G);
std::vector< int > component(num_vertices(G));
int num = connected_components(G, &component[0]);
std::vector< int >::size_type i;
cout << "Total number of components: " << num << endl;
for (i = 0; i != component.size(); ++i)
cout << "Vertex " << i << " is in component " << component[i]
<< endl;
cout << endl;
}
return 0;
}