distance_recorder
Records the distance of each vertex from the source vertex into a property map.
When applied to edge e = (u, v), the distance of v is recorded as one more
than the distance of u.
Defined in: <boost/graph/visitors.hpp>
Models: EventVisitor
Typical event: on_tree_edge or on_relax_edge (edge events only; cannot be
used with vertex events)
To use it, wrap it in an algorithm adaptor and optionally combine it with other event visitors; see the visitors overview.
Synopsis
template <typename DistanceMap, typename EventTag>
struct distance_recorder {
typedef EventTag event_filter;
distance_recorder(DistanceMap pa);
template <class Edge, class Graph>
void operator()(Edge e, const Graph& g);
};
template <typename DistanceMap, typename EventTag>
distance_recorder<DistanceMap, EventTag>
record_distances(DistanceMap pa, EventTag);
Template Parameters
| Parameter | Description |
|---|---|
|
A WritablePropertyMap whose key type is the graph’s vertex descriptor and whose value type is the distance (a numeric type that supports adding one). |
|
The event at which to record. Must be an edge event (e.g. |
Associated Types
| Type | Description |
|---|---|
|
The event tag the visitor responds to. Same type as |
Member Functions
| Member | Description |
|---|---|
|
Constructs a recorder writing distances into the property map |
|
Given edge |
Non-Member Functions
| Function | Description |
|---|---|
|
Convenience factory that creates a |
Example
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/visitors.hpp>
#include <iostream>
#include <vector>
int main() {
using namespace boost;
using Graph = adjacency_list<vecS, vecS, undirectedS>;
Graph g(5);
add_edge(0, 1, g);
add_edge(0, 2, g);
add_edge(1, 3, g);
add_edge(3, 4, g);
std::vector<int> dist(num_vertices(g), 0);
auto vis = make_bfs_visitor(
record_distances(dist.data(), on_tree_edge())
);
breadth_first_search(g, vertex(0, g), visitor(vis));
for (std::size_t v = 0; v < num_vertices(g); ++v) {
std::cout << "vertex " << v << " distance=" << dist[v] << "\n";
}
}
vertex 0 distance=0
vertex 1 distance=1
vertex 2 distance=1
vertex 3 distance=2
vertex 4 distance=3