Search code examples
c++qtboostgraphmining

C++ BOOST library and bundled properties


Iam trying to make a graph mining program using Boost, so I started by graph structure, here is the code tha I make:

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
using namespace std;
using namespace boost;



//vertex
struct VertexProperties
{
    int id;
    int label;
    VertexProperties()= default;
    VertexProperties(unsigned i, unsigned l) : id(i), label(l) {}
};



//edge
struct EdgeProperties
{
    unsigned id;
    unsigned label;
    EdgeProperties()= default;
    EdgeProperties(unsigned i, unsigned l) : id(i), label(l) {}
};

//Graph
struct GraphProperties
{
    unsigned id;
    unsigned label;
    GraphProperties()= default;
    GraphProperties(unsigned i, unsigned l) : id(i), label(l) {}
};


//adjency list
typedef boost::adjacency_list<
    boost::vecS, boost::vecS, boost::directedS,
    VertexProperties,
    EdgeProperties,
    GraphProperties
> Graph;


//iterators
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
typedef boost::graph_traits<Graph>::edge_descriptor edge_t;
/***********************************************/
int main()
{
    Graph g;
    vertex_t v1 = boost::add_vertex(VertexProperties(1,10),g);
    vertex_t v2 = boost::add_vertex(VertexProperties(2,20),g);
    //edge_t e1= boost::add_edge(EdgeProperties(3,55),g);
    std::cout << "Vertice: " <<num_vertices(g) << std::endl;
    std::cout << "edges: " <<num_edges(g) << std::endl;

    return 0;
}

there is a problem in this line:

edge_t e1= boost::add_edge(EdgeProperties(3,55),g);

How can I create this edge ? PS: please tell if the code is right (I mean the conception pont of vue)


Solution

  • Note: I'm using GCC 4.8 (with the -std=c++11 flag) and Boost 1.48.

    There are two issues I see with your code. First, the first two arguments passed to boost::add_edge(...) should be the vertices associated with the edge. So in your case, the call would be

    edge_t e1= boost::add_edge(v1, v2, EdgeProperties(3,55),g);
    

    Second, e1's type as I understand it should be

    std::pair<edge_descriptor, bool>
    

    so your second typedef would be

    typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
    

    Alternatively, you can just use the auto keyword to describe the type of e1. For example,

    auto e1= boost::add_edge(v1, v2, EdgeProperties(3,55),g);