Boost logo

Boost :

Subject: [boost] [BGL] How to provide a vertex_index property for my graph
From: ShNaYkHs ShNaYkHs (shnaykhs_at_[hidden])
Date: 2011-11-02 15:20:56


Hello,

Since my graph use setS as a VertexList, I have to either provide a
vertex_index property map for my graph to be able to use some functions
requiring a vertex_index (e.g. write_graphviz). My graph is defined as:
typedef adjacency_list<setS, setS, undirectedS, NodeData, EdgeData> Graph;
Where NodeData and EdgeData are structures. Can you please give me a very
simple example of how to provide a vertex_index property map for my graph ?
I mean, since an adjacency_list that uses listS or setS as the vertex
container does not automatically provide this vertex_id property, how can I
add it to the code bellow ?

I tried to use an associative_property_map to include index for vertices,
but it doesn't work (errors):

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

using namespace std;
using namespace boost;

struct NodeData
{
    int label;
    float influance;
    /* etc ... */
};

struct EdgeData
{
    int age;
    /* etc ... */
};

typedef map<vecS, size_t> IndexMap;
IndexMap mapIndex;
associative_property_map<IndexMap> propmapIndex(mapIndex);

typedef adjacency_list<setS, setS, undirectedS, NodeData, EdgeData> Graph;
typedef Graph::vertex_descriptor NodeID;
typedef Graph::edge_descriptor EdgeID;

int main()
{
    Graph g;

    NodeID n0 = add_vertex(g); g[n0].label = -1;
    NodeID n1 = add_vertex(g); g[n1].label = -1;

    EdgeID edge; bool ok;
    tie(edge, ok) = boost::add_edge(n0, n1, g);
    if (ok) g[edge].age = 10;

    int i=0;
    BGL_FORALL_VERTICES(v, g, Graph)
    {
        put(propmapIndex, v, i++);
    }

    return 0;
}

Thanks for answer.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk