Winfred Huang
Winfred Huang

Reputation: 372

How to modify properties while proceeding BFS in Boost Graph Library?

I'm using bundled property for a graph. Definitions are as follows:

class Node
{
    void AssignPlane(Plane& p)
    Plane* dp;
    double errors;
}

void Node::AssignPlane(Plane& p)
{
    dp=&p;
    errors=p.a+p.b+p.c;// simplified
}

typedef adjacency_list<vecS,vecS,bidirectionalS,Node,float> NGraph;

//...

struct NVisitor: default_bfs_visitor
{
    void discover_vertex(VertexDesc u, const NGraph& g) const
    {
        // CAN'T MODIFY G
    }
}

But I can't simply call g[u].AssignPlane(p) to modify a vertex, nor can I get the pointer to the vertex, which are both vital to me.
Though this question may seem to be stupid, as a novice of Boost, and have fought two weeks to adapt to the convoluted style of Boost codes, I really need help.
Do not try to answer "You need to use something other than BGL", please, as I can find nothing to support my work other than BGL.
And I must also say, the official documentation is not intended to explain their great work in a simpler way. Since I have read the documentation for dozens of times, do not suggest me to reread the documentation.
I will appreciate any useful help, and say thank you in advance.

Upvotes: 1

Views: 3577

Answers (2)

Bradley Rothenberg
Bradley Rothenberg

Reputation: 41

Have you looked into event visitors?

It looks like the graph is passed in by non-const reference in this example:

Depth-First Search Event Visitor

Boost Event Visitors

Upvotes: 0

sehe
sehe

Reputation: 394054

You could make the fields mutable

class Node
{
    void AssignPlane(Plane& p) const;
    Plane* mutable dp;
    double mutable errors;
}

void Node::AssignPlane(Plane& p) const
{
    dp=&p;
    errors=p.a+p.b+p.c;// simplified
}

Otherwise, consider holding a non-const "reference" to the graph inside the visitor:

struct NVisitor: default_bfs_visitor
{
    NGraph* gref_;
    NVisitor(NGraph& g) : gref_(&g) {}

    void discover_vertex(VertexDesc u, const NGraph& g) const
    {
        Plane* p = /*get it somewhere*/;
        (*gref_)[u].AssignPlane(p);
    }
}

Be careful not to break the invariants for BFS (like, don't edit edges while traversing).

Upvotes: 1

Related Questions