Boost logo

Boost :

From: B.T. Adams (bta_at_[hidden])
Date: 2001-01-25 17:27:56


The documentation provided in the 2nd paragraph of the section "Adding
Some Color to your Graph" of
http://www.boost.org/libs/graph/doc/quick_tour.html indicates that
the vertex index property map is automatically assigned when a vecS
container is specified for VertexList. I have found this does not
occur, as illustrated by the output:

vertex indices w/ put(index) = 0 1 2 3
vertex indices w/o put(index) = 0 0 0 0

vertex indices w/ put(index) = 0 1 2 3
vertex indices w/o put(index) = 0 0 0 0

of the following code. Shouldn't the indices be automatically assigned
(i.e., without an explicit put()) for this case?

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map.hpp>

int main()
{
    // Set the graph characteristics
    typedef boost::vertex_index_t vertex_index_t;
    typedef boost::vertex_degree_t vertex_degree_t;
    typedef boost::vertex_in_degree_t vertex_in_degree_t;
    typedef boost::vertex_out_degree_t vertex_out_degree_t;
    typedef boost::property<vertex_index_t, int,
                boost::property<vertex_degree_t, int,
                    boost::property<vertex_in_degree_t, int,
                        boost::property<vertex_out_degree_t, int
> > > > VertexProperty;
    typedef boost::edge_index_t edge_index_t;
    typedef boost::edge_weight_t edge_weight_t;
    typedef boost::property<boost::edge_index_t, int,
                boost::property<boost::edge_weight_t, int> >
EdgeProperty;
    typedef boost::adjacency_list<boost::vecS, boost::setS,
                boost::bidirectionalS, VertexProperty, EdgeProperty>
Graph;
    typedef Graph::vertex_descriptor vertex_descriptor;
    typedef Graph::vertex_iterator vertex_iterator;
    typedef Graph::edge_descriptor edge_descriptor;

    // Declare some graph variables.
    vertex_descriptor v_desc;
    edge_descriptor e_desc;

    const int nnodes = 4;
    Graph graph1;
    Graph graph2;
    boost::property_map<Graph, vertex_index_t>::type v_index =
        get(boost::vertex_index, graph1);
    // Add a vertex corresponding to each input node.
    std::pair<vertex_iterator, vertex_iterator> vp;
    for (int n = 0; n < nnodes; n++)
    {
        v_desc = boost::add_vertex(graph1);
        put(v_index, v_desc, n);
        v_desc = boost::add_vertex(graph2);
    }
    std::cout << "vertex indices w/ put(index) = ";
    for (vp = vertices(graph1); vp.first != vp.second; ++vp.first)
      std::cout << v_index[*vp.first] << " ";
    std::cout << std::endl;
    v_index = get(boost::vertex_index, graph2);
    std::cout << "vertex indices w/o put(index) = ";
    for (vp = vertices(graph2); vp.first != vp.second; ++vp.first)
      std::cout << v_index[*vp.first] << " ";
    std::cout << std::endl;
    std::cout << std::endl;

    // Use a constructor with the number of vertices predefined.
    Graph graph3(nnodes);
    v_index = get(boost::vertex_index, graph3);
    Graph graph4(nnodes);
    int n = 0;
    for (vp = vertices(graph3); vp.first != vp.second; ++vp.first)
    {
        put(v_index, *vp.first, n);
        ++n;
    }
    std::cout << "vertex indices w/ put(index) = ";
    for (vp = vertices(graph3); vp.first != vp.second; ++vp.first)
        std::cout << v_index[*vp.first] << " ";
    std::cout << std::endl;
    v_index = get(boost::vertex_index, graph4);
    std::cout << "vertex indices w/o put(index) = ";
    for (vp = vertices(graph4); vp.first != vp.second; ++vp.first)
        std::cout << v_index[*vp.first] << " ";
    std::cout << std::endl;
    std::cout << std::endl;

    return 0;
}


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