Boost logo

Boost Users :

Subject: Re: [Boost-users] [BGL] How to provide a vertex_index property for my graph
From: gongyiling (gongyiling2010_at_[hidden])
Date: 2011-11-01 22:13:04


"ShNaYkHs ShNaYkHs" 写入消息
news:CA+GAXM2Kt7KjUamMuXUeo2_SL-GTWg0k2UjerJTNQi7sizUipQ_at_mail.gmail.com...

> 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.

--------------------------------------------------------------------------------
you can using vertex bundle property as you vertex index,
here is the code I modified according to above:

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

using namespace std;
using namespace boost;

struct NodeData
{
    int label;
    float influance;
    int index; //add by gyl
    /* etc ... */
};

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

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; g[n0].index = 0; //add
by gyl, init index 0.
    NodeID n1 = add_vertex(g); g[n1].label = -1; g[n1].index = 1; //add
by gyl, init index 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(boost::get(&NodeData::index, g)/*by gyl get bundle property
map*/, v, i++);
    }

    return 0;
}

at last, I think you should post this to boost.user maillist instead here.

_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost


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