Boost logo

Boost Users :

Subject: [Boost-users] Basic question about using remove_edge() in boost graph library
From: Yi-Ping Huang (Yiping.Huang_at_[hidden])
Date: 2014-08-11 19:34:07


Dear boost-users:

I just start learning boost graph library and have a basic problem on using
the remove_edge function in boost graph library.

I would like to remove an edge in a multigraph. For a graph with two edges
connect vertice 0 and 1, I would like to remove one of them, but kept the
other.

According to the user guide, remove_edge(u ,v , g) removes all occurrences
of (u ,v). So I should use remove_edge(e ,g) instead. Here, e is a valid
edge descriptor.

For a single graph, g, I am able to perform both remove_edge(u ,v , g) and
remove_edge(e ,g) operation.

However, when I want to generalize the implementation to fit my need by
putting graphs in a vector container, I got segmentation fault for
remove_edge(e ,graph_list[0]).

I really want to make remove_edge(e,g) and vector container work at the
same time. So any suggestion that can help my bypass this difficulty is
appreciated.

Thanks a lot!!
Yi-Ping

*************************Here is the complete code*************************
#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>

#include "boost/graph/graph_traits.hpp"
#include "boost/graph/adjacency_list.hpp"

using namespace boost;

typedef adjacency_list<vecS, vecS, undirectedS> UndirectedGraph;
typedef boost::graph_traits<UndirectedGraph>::edge_iterator edge_iterator;

int graph_show(UndirectedGraph &g);

int main(int argc, char *argv[])
{
    UndirectedGraph g;

    typedef boost::graph_traits<UndirectedGraph>::edge_descriptor
edge_descriptor;
    edge_descriptor ed;

    std::vector<edge_descriptor> edge_list;
    std::vector<std::vector<edge_descriptor> > graph_edge_list;
    std::vector<UndirectedGraph> graph_list;

    int nb=0;
    int Nl=4;

    bool inserted;
    while(nb<Nl)
    {
        tie(ed,inserted)=add_edge(nb,nb+1,g);
        edge_list.push_back(ed);
        tie(ed,inserted)=add_edge(nb,nb+1,g);
        edge_list.push_back(ed);
        graph_edge_list.push_back(edge_list);
        nb=nb+1;
        graph_list.push_back(g);
    }

    std::cout<<"size of the graph vector is:
"<<graph_list.size()<<std::endl;

    remove_edge(graph_edge_list[0][0],graph_list[0]);//This is where the
problem shows.

//I got Segmentation fault (core dumped)
    //remove_edge(0,1,graph_list[0]);
    /*Remove edges by assigning vertices works fine, but that is not what I
need.*/

    for (int ig = 0; ig < Nl; ++ig) {
        std::cout<<"graph#"<<ig<<std::endl;
        std::cout<<"Size of edge_list is:
"<<graph_edge_list[ig].size()<<std::endl;
        graph_show(graph_list[ig]);
    }
    std::cout<<"Success"<<std::endl;
    return 0;
}

int graph_show(UndirectedGraph &g)
{
    std::cout<<"Number of edges is : "<<boost::num_edges(g)<<std::endl;
    std::cout<<"Number of vertices is :
"<<boost::num_vertices(g)<<std::endl;
    std::pair<edge_iterator,edge_iterator> ei=edges(g);

    for (edge_iterator edge_iter = ei.first; edge_iter!=ei.second;
++edge_iter) {
            std::cout<<"("<<
boost::source(*edge_iter,g)<<","<<boost::target(*edge_iter,g)<<")"<<std::endl;
    }

    typedef boost::graph_traits<UndirectedGraph>::vertex_iterator iter_v;

    std::cout<<"vertices(g)={ ";
    for (std::pair<iter_v,iter_v> p = vertices(g); p.first != p.second;
++p.first) {
            std::cout<< *p.first;
                std::cout<<" ";
    }
    std::cout<<"}"<<std::endl;

    return 0;
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net