Boost logo

Boost Users :

From: Stephan Diederich (stephan.diederich_at_[hidden])
Date: 2006-09-12 11:36:50


Hi Daniel,

2006/9/12, boost_at_[hidden] <boost_at_[hidden]>:
> Hello Stephan,
>
> Am 12.09.2006 um 12:42 Uhr haben Sie geschrieben:
> > typedef adjacency_list < vecS, vecS, undirectedS, tVertex> tGraph;
> >
> > after that you get a property map from the graph:
> > typedef property_map<graph_t, long tVertex::*>::type tDistanceMap;
>
> I think "graph_t" should be "tGraph", shouldn't ist?

Yep, typo. Sorry. Actually there was another one in the visitor. So,
here again a full example of how I would do it (including the
exception thrown if a max-distance was discovered)

Please try this one (it's the altered bfs-example):

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/pending/integer_range.hpp>
#include <iostream>

using namespace boost;

//tag for throwing inside bfs_distance_visitor
struct max_distance_reached{};
template < typename DistanceMap >
class bfs_distance_visitor:public default_bfs_visitor {
  typedef typename property_traits < DistanceMap >::value_type T;

public:
  bfs_distance_visitor(DistanceMap dmap, T
max_distance):m_distance_map(dmap),m_max_distance(max_distance){ }
  template < typename Edge, typename Graph >
      void tree_edge(Edge e, const Graph & g) const
  {
    typename graph_traits < Graph >::vertex_descriptor
    s = source(e, g), t = target(e, g);
    m_distance_map[t] = m_distance_map[s] + 1;
    if (m_distance_map[t] >= m_max_distance)
      throw max_distance_reached();
  }
  DistanceMap m_distance_map;
  T m_max_distance;
};

//own vertex, bundles properties style
struct Vertex{
  long distance;
  double probability;
};

int
main()
{
  // Select the graph type we wish to use
  typedef adjacency_list < vecS, vecS, undirectedS, Vertex> graph_t;
  // Set up the vertex IDs and names
  enum { r, s, t, u, v, w, x, y, N };
  // Specify the edges in the graph
  typedef std::pair < int, int >E;
  E edge_array[] = { E(r, s), E(r, v), E(s, w), E(w, r), E(w, t),
    E(w, x), E(x, t), E(t, u), E(x, y), E(u, y)
  };

  // Create the graph object
  const int n_edges = sizeof(edge_array) / sizeof(E);
  typedef graph_traits<graph_t>::vertices_size_type v_size_t;
  graph_t g(edge_array, edge_array + n_edges, v_size_t(N));

  // Typedefs
  typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor ;
  typedef graph_traits < graph_t >::vertices_size_type vertices_size_type;

  // get the distance property map from the graph
  typedef property_map<graph_t, long Vertex::*>::type tDistanceMap;
  tDistanceMap distance_map = get(&Vertex::distance, g);

  //create a bfs visitor which calculates all distances smaller than 3
  bfs_distance_visitor<tDistanceMap> vis(distance_map, 3);
  try{
    breadth_first_search(g, vertex(s, g), visitor(vis));
  } catch(max_distance_reached& ){}
  return EXIT_SUCCESS;
}

If this won't compile, please specify also which version of boost and
VC++ you are using.

HTH,
Stephan


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