这个问题与以下内容有关:Using boost connected components with cartesian points
我在示例中做了一些更改以使用笛卡尔点 . 这是我目前的代码:
using namespace boost;
typedef adjacency_list <vecS, vecS, undirectedS, cv::Point> Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef graph_traits<Graph>::vertices_size_type VertexIndex;
int main()
{
const int VERTEX_COUNT = 10;
Graph graph(VERTEX_COUNT);
std::vector<VertexIndex> rank(num_vertices(graph));
std::vector<Vertex> parent(num_vertices(graph));
typedef VertexIndex* Rank;
typedef Vertex* Parent;
disjoint_sets<Rank,Parent> ds(&rank[0], &parent[0]);
initialize_incremental_components(graph,ds);
incremental_components(graph,ds);
graph_traits<Graph>::edge_descriptor edge;
bool flag;
std::vector<Vertex> verticesVector;
//vector of points which creates graph
std::vector<cv::Point> points;
points.push_back(cv::Point(0,0));
points.push_back(cv::Point(1,1));
points.push_back(cv::Point(2,2));
points.push_back(cv::Point(3,1));
points.push_back(cv::Point(4,2));
points.push_back(cv::Point(0,2));
points.push_back(cv::Point(2,3));
points.push_back(cv::Point(3,3));
int i = 0;
for(std::vector<cv::Point>::iterator it = points.begin();
it !=points.end();it++, i++)
{
verticesVector.push_back(add_vertex(graph));
graph[i].x = (*it).x;
graph[i].y = (*it).y;
}
typedef component_index<VertexIndex> Components;
Components components(parent.begin(), parent.end());
BOOST_FOREACH(VertexIndex current_index, components)
{
std::cout<<"component "<<current_index<<" contains: ";
BOOST_FOREACH(VertexIndex child_index, components[current_index])
{
std::cout<<child_index<<" "<<" x = "<<graph[current_index].x<<";"<<graph[current_index].y<<"||";
}
std::cout<<std::endl;
}
boost::tie(edge,flag) = add_edge(verticesVector[0],verticesVector[1],graph);
ds.union_set(verticesVector[0],verticesVector[1]);
boost::tie(edge,flag) = add_edge(verticesVector[1],verticesVector[2],graph);
ds.union_set(verticesVector[1],verticesVector[2]);
boost::tie(edge,flag) = add_edge(verticesVector[2],verticesVector[3],graph);
ds.union_set(verticesVector[2],verticesVector[3]);
boost::tie(edge,flag) = add_edge(verticesVector[3],verticesVector[4],graph);
ds.union_set(verticesVector[3],verticesVector[4]);
boost::tie(edge,flag) = add_edge(verticesVector[1],verticesVector[5],graph);
ds.union_set(verticesVector[1],verticesVector[5]);
boost::tie(edge,flag) = add_edge(verticesVector[5],verticesVector[6],graph);
ds.union_set(verticesVector[5],verticesVector[6]);
boost::tie(edge,flag) = add_edge(verticesVector[6],verticesVector[7],graph);
ds.union_set(verticesVector[6],verticesVector[7]);
Components components2(parent.begin(), parent.end());
/*BOOST_FOREACH(Vertex current_vertex, vertices(graph))
{
std::cout<< "representative["<<current_vertex<<"] = "
<< ds.find_set(current_vertex)<<std::endl;
}
std::cout<<std::endl;*/
for(std::vector<Vertex>::iterator it = verticesVector.begin(); it != verticesVector.end(); it++)
{
std::cout<<"representative = "<<ds.find_set((*it))<<std::endl;
}
BOOST_FOREACH(VertexIndex current_index, components2)
{
std::cout<<"component "<<current_index<<" contains: ";
BOOST_FOREACH(VertexIndex child_index, components[current_index])
{
std::cout<<child_index<<" "<<" x,y = "<<graph[current_index].x<<";"<<graph[current_index].y<<"||";
}
std::cout<<std::endl;
}
//write_graphviz(std::cout, graph);
getchar();
return 0;
}
我在尝试union_set()时遇到异常:访问违规读取位置......我无法弄清楚原因 . 我已经阅读了有关boost lib的所有主题,试图在docs中搜索 .
1 回答
感谢本教程,我已经弄清楚了:Algorithm for selecting all edges and vertices connected to one vertex
问题是:
我先用一些顶点初始化了矢量(从示例复制粘贴并修改不是最好的想法)
正如教程所说"Notice that graph[ a VertexID ] gives a Vertex, but graph[ an EdgeID ] gives an Edge "
在添加一些顶点后应初始化不相交集
工作代码:
我还在学习boost Graph所以请纠正我的错误,制作更优雅的解决方案 . 我的结论也可能不完全正确 .