|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Bidirectional BFS for finding shortest unweighted paths. More...
#include <Bidirectional_BFS.H>
Classes | |
| struct | BFS_Info |
| class | BiBFS_Init_Guard |
| RAII guard for BiBFS initialization and cleanup. More... | |
| struct | No_Index |
Public Types | |
| using | Node = typename GT::Node |
| using | Arc = typename GT::Arc |
Public Member Functions | |
| Bidirectional_BFS (SA &&__sa=SA()) | |
| Constructor. | |
| Bidirectional_BFS (const SA &__sa) | |
| Constructor with lvalue arc filter, const or not. | |
| bool | find_path (const GT &g, Node *start, Node *end, Path< GT > &path) |
| Finds the shortest unweighted path between start and end. | |
| bool | operator() (const GT &g, Node *start, Node *end, Path< GT > &path) |
| Finds shortest path (operator interface with path output). | |
| Path< GT > | operator() (const GT &g, Node *start, Node *end) |
| Finds shortest path (operator interface returning path). | |
Private Types | |
| using | Backward_Index = std::conditional_t< backward_by_index, std::optional< In_Arcs_Index< GT, SA > >, No_Index > |
Private Member Functions | |
| void | init (const GT &g) |
| void | cleanup (const GT &g) |
| bool | is_seen (Node *p, const int dir) const noexcept |
| void | mark_seen (Node *p, const int dir) noexcept |
| size_t | get_dist (Node *p, const int dir) const noexcept |
| void | set_dist (Node *p, const int dir, const size_t d) noexcept |
| Node *& | parent_ref (Node *p, const int dir) noexcept |
| Arc *& | parent_arc_ref (Node *p, const int dir) noexcept |
| void | visit_backward (Node *curr, const size_t curr_dist, Arc *arc, Node *tgt, DynListQueue< Node * > &next_frontier, size_t &best_len, Node *&best_meeting) |
| template<template< typename, class > class Itor> | |
| void | expand_frontier (const GT &g, DynListQueue< Node * > &frontier, const int my_dir, const int other_dir, size_t &best_len, Node *&best_meeting) |
| void | expand_backward (const GT &g, DynListQueue< Node * > &frontier, size_t &best_len, Node *&best_meeting, Backward_Index &index) |
| void | build_path (const GT &g, Node *start, Node *meeting, Node *end, Path< GT > &path) |
Private Attributes | |
| SA | sa |
Static Private Attributes | |
| static constexpr bool | backward_by_index |
| static constexpr int | UNSEEN = 0 |
| static constexpr int | FORWARD = 1 |
| static constexpr int | BACKWARD = 2 |
| static constexpr size_t | INF_DIST = std::numeric_limits<size_t>::max() |
Bidirectional BFS for finding shortest unweighted paths.
This class finds the shortest path (in terms of number of edges) between two nodes by simultaneously running BFS from both the source and the target. The search terminates when the two frontiers meet.
For undirected graphs, both iterators default to Node_Arc_Iterator. For directed graphs, use Out_Iterator for forward and In_Iterator for backward traversal. With In_Iterator, on a digraph type, the backward search takes the arcs into each node from an In_Arcs_Index built at its first step, in \(O(V + E)\).
The class receives the following template parameters:
GT: graph type.Fwd_Itor: arc iterator for forward expansion (from start).Bck_Itor: arc iterator for backward expansion (from end).SA: arc filter for internal iterators.Definition at line 144 of file Bidirectional_BFS.H.
| using Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::Arc = typename GT::Arc |
Definition at line 148 of file Bidirectional_BFS.H.
|
private |
Definition at line 163 of file Bidirectional_BFS.H.
| using Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::Node = typename GT::Node |
Definition at line 147 of file Bidirectional_BFS.H.
|
inline |
Constructor.
| [in] | __sa | Arc filter for iterators, which it moves. |
Definition at line 447 of file Bidirectional_BFS.H.
|
inline |
Constructor with lvalue arc filter, const or not.
| [in] | __sa | Arc filter for iterators, which it copies. |
Definition at line 453 of file Bidirectional_BFS.H.
|
inlineprivate |
Definition at line 404 of file Bidirectional_BFS.H.
References ah_runtime_error_if, Aleph::Path< GT >::append(), BIBFS_BCK_PARC, BIBFS_BCK_PARENT, BIBFS_FWD_PARC, BIBFS_FWD_PARENT, Aleph::Path< GT >::empty(), LocateFunctions< Container, Type >::get_it(), Aleph::DynList< T >::insert(), and Aleph::Path< GT >::set_graph().
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::find_path().
|
inlineprivate |
Definition at line 245 of file Bidirectional_BFS.H.
References BIBFS_INFO, and NODE_COOKIE.
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::BiBFS_Init_Guard::~BiBFS_Init_Guard(), and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::init().
|
inlineprivate |
Definition at line 380 of file Bidirectional_BFS.H.
References Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::BACKWARD, Aleph::DynListQueue< T >::get(), GraphCommon< GT, Node, Arc >::get_connected_node(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::get_dist(), Aleph::DynListQueue< T >::is_empty(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::sa, Aleph::DynListQueue< T >::swap(), and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::visit_backward().
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::find_path().
|
inlineprivate |
Definition at line 330 of file Bidirectional_BFS.H.
References Aleph::and, Aleph::DynListQueue< T >::get(), GraphCommon< GT, Node, Arc >::get_connected_node(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::get_dist(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::INF_DIST, Aleph::DynListQueue< T >::is_empty(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::is_seen(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::mark_seen(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::parent_arc_ref(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::parent_ref(), Aleph::DynListQueue< T >::put(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::sa, Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::set_dist(), and Aleph::DynListQueue< T >::swap().
|
inline |
Finds the shortest unweighted path between start and end.
Performs bidirectional BFS by expanding one complete frontier level at a time. At each iteration the smaller frontier is expanded (Fwd_Itor from start or Bck_Itor from end). Meeting detection uses forward/backward distance maps and stops only when no shorter meeting point can still exist.
| [in] | g | The graph. |
| [in] | start | The starting node. |
| [in] | end | The destination node. |
| [out] | path | The shortest path (empty if no path exists). |
| domain_error | If start or end is nullptr, or g is empty. |
| bad_alloc | If there is not enough memory. |
In_Iterator on a digraph type, O(V + E) for the index of the arcs into each node, once the search goes backward. Definition at line 476 of file Bidirectional_BFS.H.
References ah_domain_error_if, Aleph::and, Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::BACKWARD, Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::backward_by_index, Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::build_path(), Aleph::Path< GT >::empty(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::expand_backward(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::FORWARD, GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::INF_DIST, Aleph::init, GraphCommon< GT, Node, Arc >::is_digraph(), Aleph::DynListQueue< T >::is_empty(), Aleph::lower_bound(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::mark_seen(), Aleph::DynListQueue< T >::put(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::set_dist(), Aleph::Path< GT >::set_graph(), and Aleph::DynListQueue< T >::size().
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::operator()(), and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::operator()().
|
inlineprivatenoexcept |
Definition at line 265 of file Bidirectional_BFS.H.
References BIBFS_BCK_DIST, BIBFS_FWD_DIST, and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::FORWARD.
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::expand_backward(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::expand_frontier(), and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::visit_backward().
|
inlineprivate |
Definition at line 225 of file Bidirectional_BFS.H.
References Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::cleanup(), and NODE_COOKIE.
|
inlineprivatenoexcept |
Definition at line 255 of file Bidirectional_BFS.H.
References BIBFS_DIR.
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::expand_frontier(), and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::visit_backward().
|
inlineprivatenoexcept |
Definition at line 260 of file Bidirectional_BFS.H.
References BIBFS_DIR.
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::expand_frontier(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::find_path(), and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::visit_backward().
|
inline |
Finds shortest path (operator interface returning path).
| [in] | g | The graph. |
| [in] | start | The starting node. |
| [in] | end | The destination node. |
Definition at line 578 of file Bidirectional_BFS.H.
References Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::find_path().
|
inline |
Finds shortest path (operator interface with path output).
| [in] | g | The graph. |
| [in] | start | The starting node. |
| [in] | end | The destination node. |
| [out] | path | The shortest path. |
Definition at line 566 of file Bidirectional_BFS.H.
References Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::find_path().
|
inlineprivatenoexcept |
Definition at line 285 of file Bidirectional_BFS.H.
References BIBFS_BCK_PARC, BIBFS_FWD_PARC, and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::FORWARD.
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::expand_frontier(), and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::visit_backward().
|
inlineprivatenoexcept |
Definition at line 278 of file Bidirectional_BFS.H.
References BIBFS_BCK_PARENT, BIBFS_FWD_PARENT, and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::FORWARD.
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::expand_frontier(), and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::visit_backward().
|
inlineprivatenoexcept |
Definition at line 270 of file Bidirectional_BFS.H.
References BIBFS_BCK_DIST, BIBFS_FWD_DIST, and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::FORWARD.
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::expand_frontier(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::find_path(), and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::visit_backward().
|
inlineprivate |
Definition at line 295 of file Bidirectional_BFS.H.
References Aleph::and, Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::BACKWARD, Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::FORWARD, Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::get_dist(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::INF_DIST, Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::is_seen(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::mark_seen(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::parent_arc_ref(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::parent_ref(), Aleph::DynListQueue< T >::put(), and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::set_dist().
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::expand_backward().
|
staticconstexprprivate |
|
staticconstexprprivate |
Definition at line 157 of file Bidirectional_BFS.H.
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::find_path().
|
staticconstexprprivate |
Definition at line 168 of file Bidirectional_BFS.H.
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::find_path(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::get_dist(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::parent_arc_ref(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::parent_ref(), Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::set_dist(), and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::visit_backward().
|
staticconstexprprivate |
|
private |
Definition at line 151 of file Bidirectional_BFS.H.
Referenced by Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::expand_backward(), and Aleph::Bidirectional_BFS< GT, Fwd_Itor, Bck_Itor, SA >::expand_frontier().
|
staticconstexprprivate |
Definition at line 167 of file Bidirectional_BFS.H.