Searches and paths¶
vefr/algorithms.hpp.
The algorithms are written on a graph’s ranges: its vertices(), and each vertex’s out_edges(u) where it
numbers its edges, else its neighbours(u). As in the Boost Graph Library, a search reports what it does to a
visitor, and the visitor may stop it. Unlike there, a visitor is any object with the events it wants, visitors compose,
and the parameters are plain arguments: sources are any range of vertices, a weight is a function of an edge or of its
number, and results are urd Stores keyed by vertex. None recurses: deep graphs do not overflow the stack.
// Shortest paths from 0, stopping once 7's distance is final; `record` sees each vertex settled.
struct
{
std::vector<V> settled;
void examine_vertex(V v) { settled.push_back(v); }
} record;
const auto length = [&](const vefr::Edge<V>& e) { return distance(e.source, e.target); };
const auto paths = vefr::dijkstra_shortest_paths(g, std::array { V { 0 } }, length,
vefr::visitors(record, vefr::stop_at(std::array { V { 7 } })));
const auto route = vefr::path_to(paths, V { 7 });
Edges, data and weights¶
-
template<typename V>
struct Edge¶ An edge as the algorithms hand it out:
source,targetandid, its number, orEdge::nonewhere the graph numbers no edges.
-
template<urd::adjacency_graph G>
auto out_edges_of(const G &g, vertex_t<G> u)¶ The edges leaving
u, asEdge.
-
template<typename T, typename V>
using VertexMap = urd::Store<urd::sequence<T, V>>¶ Data per vertex, keyed by vertex.
-
template<typename T, urd::adjacency_graph G>
VertexMap<T, vertex_t<G>> vertex_map(const G &g, const T &value = T{})¶ Sized
vertex_bound(), every entryvalue: a graph that lost vertices leaves holes.
-
template<typename R, typename G>
concept vertex_range¶ An input range of vertices of
G: a container, a span, a view.
-
template<typename F, typename G>
concept edge_weight¶ A function of an
Edge(a length from its endpoints, say), or of its number, which the graph must then give (urd::incidence_graph).
-
template<typename G, typename F>
using weight_t¶ What a weight
Fgives onG’s edges: the type of a distance.
Visitors¶
A visitor is any object with the events it wants, each taking a vertex or an Edge. Events it does not have
are not called. An event may return ControlFlow: ControlFlow::stop ends the search where it is.
-
struct null_visitor¶
No events: the default.
-
template<typename ...Visitors>
class visitors¶ Visitors called one after the other on each event; the search stops where any of them asks. A named visitor is held by reference, a temporary by value:
vefr::visitors(record, vefr::stop_at(targets)).
-
template<typename V>
class stop_at¶ Stops a search once it has examined every vertex of the targets given: in breadth-first search and Dijkstra’s, where their distances are then final.
Searches¶
-
template<urd::adjacency_graph G, vertex_range<G> R, typename Visitor>
void breadth_first_visit(const G &g, R &&sources, Visitor &&visitor)¶ Events:
start_vertex,discover_vertex,examine_vertex,examine_edge,tree_edge,non_tree_edge,gray_target,black_target,finish_vertex.
-
template<urd::adjacency_graph G, vertex_range<G> R, typename Visitor>
void depth_first_visit(const G &g, R &&sources, Visitor &&visitor)¶ A source at a time, skipping those already reached. Events:
start_vertex,discover_vertex,examine_edge,tree_edge,back_edge,forward_or_cross_edge,finish_edge,finish_vertex.
-
template<typename V, typename D>
struct Paths¶
-
template<urd::adjacency_graph G, vertex_range<G> R, typename Visitor>
auto breadth_first_search(const G &g, R &&sources, Visitor &&visitor = {})¶ Shortest paths in edges,
Paths<V, std::optional<std::size_t>>;visitorsees the search too, and may stop it.
Structure¶
-
template<urd::adjacency_graph G>
auto topological_sort(const G &g)¶ The vertices in topological order, each before the vertices its edges lead to; a cycle throws
not_a_dag, astd::invalid_argument.
-
struct not_a_dag¶
Shortest paths¶
Distances not reached are infinite where the type has an infinity, else its largest value.
-
template<urd::adjacency_graph G, vertex_range<G> R, edge_weight<G> F, typename Visitor>
void dijkstra_visit(const G &g, R &&sources, const F &weight, Paths<vertex_t<G>, weight_t<G, F>> &paths, Visitor &&visitor = {})¶ Dijkstra’s algorithm into
paths, which the caller keeps across searches (made withvertex_map()) and which it resets. No weight is negative; O((n + m) log n). Events:discover_vertex(a distance first found),examine_vertex(settled: its distance is final),examine_edge,edge_relaxed,edge_not_relaxed,finish_vertex.
-
template<urd::adjacency_graph G, vertex_range<G> R, edge_weight<G> F, typename Visitor>
auto dijkstra_shortest_paths(const G &g, R &&sources, const F &weight, Visitor &&visitor = {})¶ The same into new
Paths<V, D>,Dwhatweightreturns. Withstop_at, the targets’ distances and paths are final; a vertex not settled may have only a bound.
-
template<urd::adjacency_graph G, edge_weight<G> F>
auto floyd_warshall_all_pairs_shortest_paths(const G &g, const F &weight)¶ The distance between every two vertices,
result[u][v]; O(n3). A negative cycle throwsstd::domain_error.