簡體   English   中英

使用Boost Graph Library刪除頂點后無法刪除邊

[英]Can't remove an edge after having removed a vertex with Boost Graph Library

我開始研究BGL ,它看起來很棒,但是我發現很難使用。

當我玩這個圖形示例時,事情開始變得更加清晰。 但是我現在遇到了一個問題:移除頂點后,無法移除圖形中的邊。 到目前為止,這似乎使我對BGL的理解無效:(

這是我必須使問題清楚顯示的最短代碼。

(我確定了具有整數[1..5]的頂點和具有字母[a..g]的邊)

#include <iostream>
#include <boost/graph/adjacency_list.hpp>

using namespace boost;

//==============================================================
// TL;DR : Skip to the end of `main()` where the real problem is
//==============================================================

// lazy iterate over the graph: // {{{
#define VERTEXITERATE(nameV, graph, ...)                                       \
    {                                                                          \
    auto GRAPHITERATOR(vertices(graph));                                       \
    auto& nameV(GRAPHITERATOR.first);                                          \
    for(; GRAPHITERATOR.first != GRAPHITERATOR.second; ++GRAPHITERATOR.first) {\
        __VA_ARGS__                                                            \
    }                                                                          \
    }
#define EDGEITERATE(nameE, graph, ...)                                         \
    {                                                                          \
    auto GRAPHITERATOR(edges(graph));                                          \
    auto& nameE(GRAPHITERATOR.first);                                          \
    for(; GRAPHITERATOR.first != GRAPHITERATOR.second; ++GRAPHITERATOR.first) {\
        __VA_ARGS__                                                            \
    }                                                                          \
    }
    // }}}

// Properties of the graph // {{{
struct VertexProperties {
    VertexProperties() : id(0) {};
    VertexProperties(int id) : id(id) {};
    int id;
};
struct EdgeProperties {
    EdgeProperties() : id(0) {};
    EdgeProperties(char id) : id(id) {};
    char id;
};
struct GraphProperties {
    GraphProperties() : id(0) {};
    GraphProperties(std::string id) : id(id) {};
    std::string id;
};
// }}}

// Type definitions // {{{
typedef adjacency_list<
      hash_setS // vecS would allow parallel edges, which I don't want.
    , vecS
    , bidirectionalS
    , VertexProperties
    , EdgeProperties
    , GraphProperties
    > Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef graph_traits<Graph>::edge_descriptor Edge;
// }}}

// Glance at the state of the graph
void printGraph(Graph const& g) { // {{{
    // Print graph properties
    std::cout << get_property(g).id << " : |V| = " << num_vertices(g);
    std::cout << ", |E| =  " << num_edges(g) << std::endl;
    std::cout << "vertices: " << std::endl;
    VERTEXITERATE(v, g,
            Vertex vertex(*v);
            std::cout << g[vertex].id << " :";
            std::cout << " in " << in_degree(vertex, g);
            std::cout << ", out " << out_degree(vertex, g);
            std::cout << std::endl;
        );
    std::cout << "edges: " << std::endl;
    EDGEITERATE(e, g,
            Edge edge(*e);
            std::cout << g[edge].id << " :";
            std::cout << g[source(edge, g)].id << " \u2192 ";
            std::cout << g[target(edge, g)].id;
            std::cout << std::endl;
            );
    std::cout << std::endl;
}
// }}}

int main() {

    // Build the graph
    Graph g(GraphProperties("Graph"));
    const int nV(5); // number of vertices
    const int nE(7); // number of edges
    std::cout << "Created." << std::endl;
    // should be empty:
    printGraph(g);

    // Vertices
    std::array<Vertex, nV> V;
    int vId(0);
    for (Vertex& v : V)
        v = add_vertex(VertexProperties(++vId), g);

    // Edges
    typedef struct {         // store here everything we need to define an edge:
        Vertex source, target;
        EdgeProperties props;
    } BuildEdge;
    std::array<BuildEdge, nE> builds { {
           {V[0], V[1], 'a'} // define the graph topology in this initializer
        ,  {V[0], V[2], 'b'}
        ,  {V[0], V[3], 'c'}
        ,  {V[2], V[3], 'd'}
        ,  {V[1], V[4], 'e'}
        ,  {V[2], V[4], 'f'}
        ,  {V[3], V[4], 'g'}
    }};
    for(auto p : builds)
        add_edge(p.source, p.target, p.props, g);

    // See what happened:
    std::cout << "Filled up." << std::endl;
    printGraph(g); // ok.

    //==============================================================
    // HERE is the interesting part :
    // remove an edge by its vertices:
    std::array<Vertex, 2> toRemove {{V[0], V[1]}};
    remove_edge(toRemove[0], toRemove[1], g);
    std::cout << "Edge removed." << std::endl;
    printGraph(g); // ok.

    // remove a vertex:
    toRemove[0] = V[3];
    clear_vertex(toRemove[0], g);
    remove_vertex(toRemove[0], g);
    std::cout << "Vertex removed" << std::endl;
    printGraph(g); // success.

    // remove another vertex:
    toRemove = {{ V[2], V[4] }};
    remove_edge(toRemove[0], toRemove[1], g);
    std::cout << "Edge removed." << std::endl;
    printGraph(g); // FAIL!

    // Why does this fail?
    // Is `V` outdated after a call to remove_edge?
    //==============================================================

    return EXIT_SUCCESS;

}

為什么最后一次清除沒有發生? 當然,刪除中間塊(即保留第四個頂點)將允許正確刪除邊。

我知道在刪除一個邊界的頂點之一之后刪除邊沒有多大意義,但是在這里情況並非如此。

痛擊! 只是在這個主題上找到了一個非常有趣的頁面 這是官方的Boost文檔。

VertexList = vecS定義adjacency_list會導致所有頂點描述符通過調用remove_vertex無效。 選擇其他數據結構(例如hash_setS將使它們保持有效。

因此:重寫typedef:

typedef adjacency_list<
      hash_setS // vecS would allow parallel edges, which I don't want.
    , hash_setS // vecS would make the descriptor invalid on graph change
    , bidirectionalS
    , VertexProperties
    , EdgeProperties
    , GraphProperties
    > Graph;

解決了問題。

它不僅會這樣做,還會影響adjacency_list其他屬性,例如基礎算法的復雜性。 對我來說,這仍然是相當復雜的,因為我是BGL的新手,但請參閱docs :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM