Boost Graph Library - Accessing a vertex

Discussion in 'C++' started by imported_Muthuveerappan, Jun 23, 2010.

  1. imported_Muthuveerappan

    imported_Muthuveerappan New Member

    Joined:
    Jun 23, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I am a newbie to boost graph library (BGL).

    1) To build a graph with vertices with unique ids - Able to do this
    2) To get the vertex containing a specific idsI have created a graph with vertices that have a custom property. - how can I do this ?

    The way I have tried to do is to iterate through all the vertices and checked each vertex's property value. Is there a better way to do this ?

    Note - This field is a unique Id field

    Can it be indexed based on the unique id, so that it can be accessed directly ?

    Given below is a small example:
    Code:
    /*
    Aim -
    -----
    1) To build a graph with vertices with unique ids - Able to do this
    2) To get the vertex containing a specific ids - How do I do this ?
    
    
    */
    
    #include "boost/graph/adjacency_list.hpp"
    using namespace boost;
    
    class VertId
    {
        public:
            typedef boost :: vertex_property_tag kind;
    };
    
    
    
    int main()
    {
    
        typedef property<VertId, std :: string> VertIdProp;
    
        typedef adjacency_list<
                               vecS,
                               vecS,
                               directedS,
                               VertIdProp
                             > GraphType;
    
        GraphType g1;
    
    
        std :: string vp1 = "a100",
                      vp2 = "a200",
                      vp3 = "a300";
    
        add_vertex(vp1, g1);
        add_vertex(vp2, g1);
        add_vertex(vp3, g1);
    
        property_map<GraphType, VertId> :: type propMap1;
    
        propMap1 = get(VertId(), g1);
    
        graph_traits<GraphType> :: vertex_iterator vertCurrItr, vertEndItr;
    
        // search for vertex containing the id a300
    
        for(
            tie(vertCurrItr, vertEndItr) = vertices(g1);
            vertCurrItr != vertEndItr;
            vertCurrItr++
           )
        {
            if(propMap1[*vertCurrItr] == "a300")
                std :: cout << "found it!!\n";
        }
    
    
        return (0);
    }
    Thanks
    Muthu
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice