Search code examples
c++boostgrapha-star

Using iterators to access data. Boost graph


I am new to C++ and the boost graph library. I am trying to use iterators to access information already stored within my graph "lattuce", more specifically, the weight an edge between two specific nodes.

This data will then be used by a A* algorithm (not the one in Boost graph). I am not sure if iterators are the solution to this either, so any guidance or criticism would be appreciated.

struct Point {//struct point with vertex properties
    int x, y;
    int parentx, parenty;
    double g;
    double h;
     friend std::ostream& operator<<(std::ostream& os, Point p) {
      return os << "[" << p.x << "," << p.y << "]";
    }
};

int main() {
//declarations
typedef property < edge_weight_t, double >Weight;
using std::vector;//?
using Graph = adjacency_list<setS, vecS, undirectedS, Point, Weight>;//graph includes our created point struct property<edge_weight_t
using vertex_descriptor = Graph::vertex_descriptor;
Graph lattuce;

//lattuce graph is created with weighted edges value 1 or 1,41 if diagonal. The functions used on a loop are:
//add_edge(nodes[p.x][p.y],nodes[neighbour.x][neighbour.y], Weight(1.0), lattuce);
//add_edge(nodes[p.x][p.y],nodes[neighbour.x][neighbour.y], Weight(1.4), lattuce);

If more information about the code that generates the graph is needed I'll provide it. Thanks


Solution

  • It is possible to obtain link edge weights in directed and undirected graphs by means of the boost::property_map:

    boost::property_map<UndirectedGraph, boost::edge_weight_t>::type EdgeWeightMap = get(boost::edge_weight_t(), g);
    

    Example implementation given below, that first builds the following simple graph (specifically a tree with no cycles):

    enter image description here

    ... then uses the boost::property_map to obtain the weight of each edge, and prints it out:

    #include <iostream>
    #include <boost/graph/graph_traits.hpp>
    #include <boost/graph/adjacency_list.hpp>
    
    typedef boost::property<boost::edge_weight_t, double> EdgeWeight;
    typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, boost::no_property, EdgeWeight> UndirectedGraph;
    typedef boost::graph_traits<UndirectedGraph>::edge_iterator edge_iterator;
    
    int main(int, char*[])
    {
        // 1. Undirected graph - print out the edge weights
        UndirectedGraph g;
    
        boost::add_edge(0, 1, 8, g);
        boost::add_edge(0, 5, 2, g);
        boost::add_edge(5, 6, 1, g);
        boost::add_edge(4, 5, 5, g);
        boost::add_edge(3, 5, 7, g);
    
        boost::property_map<UndirectedGraph, boost::edge_weight_t>::type EdgeWeightMap = get(boost::edge_weight_t(), g);
    
        std::pair<edge_iterator, edge_iterator> edgePair;
        for (edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first)
        {
            std::cout << *edgePair.first << " " << EdgeWeightMap[*edgePair.first] << std::endl;
        }   
    
        return 0;
    }
    

    Giving the following console output, showing the edges as (start,end) node pairs plus their respective weights:

    enter image description here