简体   繁体   English

使用C ++ Boost库从图形中删除顶点及其所有邻居

[英]removing vertex and all his neighbours from an graph with c++ boost library

I want to remove a vertex w with his neighbours from an graph G. 我想从图G中删除与他的邻居的顶点w。

My Code: 我的代码:

// remove all neighbours
MyGraph::adjacency_iterator n_iter, n_end;
for (tr1::tie(n_iter, n_end) = boost::adjacent_vertices (*w, G1); n_iter != n_end; ++n_iter)
{
    boost::remove_vertex(*n_iter, G1);
}

MyGraph::vertex_iterator vertex_iter, vertex_end;
Vertex vertex_w = G[*w];

// remove vertex himself
for (tr1::tie(vertex_iter, vertex_end) = boost::vertices(G1);vertex_iter != vertex_end; ++vertex_iter)
{
    Vertex vertex = G1[*vertex_iter];
    if (vertex.p_index == vertex_w.p_index)
    {
        boost::remove_vertex(*vertex_iter, G1);
        break;
    }
}

I tried to iterate through the adjacent vertexes and delete them. 我试图遍历相邻的顶点并删除它们。 After that i tried to remove the vertex w. 之后,我尝试删除顶点w。

But there come some Exceptions and Errors while starting the program. 但是在启动程序时会出现一些异常和错误。

Have somebody an hint for me to remove and Vertex w with all of his neighbours from an Graph? 有没有人要我从“图形”中删除所有Vertex W及其所有邻居的提示?

Update: Now I understand why the code above won't work (I'm Using VertexList=vecS). 更新:现在我明白了为什么上面的代码不起作用(我正在使用VertexList = vecS)。 I now try to mark the Vertex as "removed" and want to remove all edges. 现在,我尝试将顶点标记为“已删除”,并希望删除所有边缘。

Graph: 图形:

0     1
o-----o
|     |
|     |
o-----o
2     3

Code: 码:

typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, Vertex, Edge> MyGraph;
[...]
// *w is Vertex "1"
boost::graph_traits<MyGraph>::adjacency_iterator n_iter, n_end, next;
for (tr1::tie(n_iter, n_end) = boost::adjacent_vertices (*w, G1); n_iter != n_end; ++n_iter)
{
    cout << G1[*n_iter].p_index << endl;
    G1[*n_iter].Graph_Part = Graph_Part::R;
    // boost::clear_vertex(*n_iter, G1); <-- problem
 }
cout << endl << "----" << endl;

If i uncomment the clear_vertex method, the output is: 如果我取消注释clear_vertex方法,则输出为:

0
3

If the program remove the edges of *n_iter, the output is only: 如果程序删除了* n_iter的边缘,则输出仅为:

0

- the loop ends after one iteration. -循环在一次迭代后结束。

Have a look here . 在这里看看。 remove_vertex will not change any edges. remove_vertex不会改变任何边缘。 You need to clear_vertex it first. 您需要先clear_vertex

A general hint: Don't use qualified calls to the boost::graph library, call them unqualified. 一个一般提示:不要对boost::graph库使用合格的调用,不要将它们称为不合格的。 I'd also suggest Boost.Range to handle the iteration in such simple cases. 我还建议Boost.Range在这种简单情况下处理迭代。 It keeps the scope cleaner and is a lot prettier. 它使示波器保持清洁,并且更漂亮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM