83# ifndef BIDIRECTIONAL_BFS_H
84# define BIDIRECTIONAL_BFS_H
90# include <type_traits>
99namespace bidirectional_bfs_detail
102 template <
template <
typename,
class>
class Itor>
164 std::conditional_t<backward_by_index, std::optional<In_Arcs_Index<GT, SA>>,
No_Index>;
170 static constexpr size_t INF_DIST = std::numeric_limits<size_t>::max();
183#define BIBFS_INFO(p) (static_cast<BFS_Info *>(NODE_COOKIE(p)))
184#define BIBFS_DIR(p) (BIBFS_INFO(p)->dir)
185#define BIBFS_FWD_PARENT(p) (BIBFS_INFO(p)->fwd_parent)
186#define BIBFS_BCK_PARENT(p) (BIBFS_INFO(p)->bck_parent)
187#define BIBFS_FWD_PARC(p) (BIBFS_INFO(p)->fwd_parent_arc)
188#define BIBFS_BCK_PARC(p) (BIBFS_INFO(p)->bck_parent_arc)
189#define BIBFS_FWD_DIST(p) (BIBFS_INFO(p)->fwd_dist)
190#define BIBFS_BCK_DIST(p) (BIBFS_INFO(p)->bck_dist)
204 :
owner(&__owner),
g(&__g)
227 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next())
232 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next())
234 auto p = it.get_curr();
247 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next())
249 auto p = it.get_curr();
255 [[nodiscard]]
bool is_seen(
Node * p,
const int dir)
const noexcept
297 size_t & best_len,
Node *& best_meeting)
300 constexpr int other_dir =
FORWARD;
302 const bool seen_my =
is_seen(tgt, my_dir);
308 set_dist(tgt, my_dir, curr_dist + 1);
309 next_frontier.
put(tgt);
314 const auto my_dist =
get_dist(tgt, my_dir);
315 const auto other_dist =
get_dist(tgt, other_dir);
319 const auto candidate_len = my_dist + other_dist;
320 if (candidate_len < best_len)
322 best_len = candidate_len;
329 template <
template <
typename,
class>
class Itor>
331 const int my_dir,
const int other_dir,
332 size_t & best_len,
Node *& best_meeting)
338 auto curr = frontier.
get();
339 const auto curr_dist =
get_dist(curr, my_dir);
341 for (Itor<GT, SA> it(curr,
sa); it.has_curr(); it.next())
343 auto arc = it.get_current_arc();
346 const bool seen_my =
is_seen(tgt, my_dir);
352 set_dist(tgt, my_dir, curr_dist + 1);
353 next_frontier.
put(tgt);
358 const auto my_dist =
get_dist(tgt, my_dir);
359 const auto other_dist =
get_dist(tgt, other_dir);
363 const auto candidate_len = my_dist + other_dist;
364 if (candidate_len < best_len)
366 best_len = candidate_len;
374 frontier.
swap(next_frontier);
384 index.emplace(g,
sa);
390 auto curr = frontier.
get();
393 for (
auto it = index->get_it(curr); it.has_curr(); it.next_ne())
395 auto arc = it.get_curr_ne();
397 next_frontier, best_len, best_meeting);
401 frontier.
swap(next_frontier);
422 <<
"Bidirectional_BFS: inconsistent forward parent chain";
424 <<
"Bidirectional_BFS: missing forward parent arc";
428 for (
auto it = fwd_arcs.
get_it(); it.has_curr(); it.next())
429 path.
append(it.get_curr());
435 <<
"Bidirectional_BFS: inconsistent backward parent chain";
437 <<
"Bidirectional_BFS: missing backward parent arc";
494 Node * meeting =
nullptr;
504 fwd_frontier.
put(start);
505 bck_frontier.
put(end);
507 size_t fwd_depth = 0;
508 size_t bck_depth = 0;
516 const bool expand_forward = fwd_frontier.
size() <= bck_frontier.
size();
542 if (meeting !=
nullptr)
544 const size_t min_depth = fwd_depth < bck_depth ? fwd_depth : bck_depth;
551 found = meeting !=
nullptr;
587#undef BIBFS_FWD_PARENT
588#undef BIBFS_BCK_PARENT
#define BIBFS_FWD_DIST(p)
#define BIBFS_BCK_PARENT(p)
#define BIBFS_BCK_DIST(p)
#define BIBFS_FWD_PARENT(p)
#define BIBFS_BCK_PARC(p)
#define BIBFS_FWD_PARC(p)
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
RAII guard for BiBFS initialization and cleanup.
BiBFS_Init_Guard(const BiBFS_Init_Guard &)=delete
Deleted copy constructor.
BiBFS_Init_Guard & operator=(const BiBFS_Init_Guard &)=delete
Deleted copy assignment.
Bidirectional_BFS * owner
~BiBFS_Init_Guard() noexcept
Destructor.
BiBFS_Init_Guard(BiBFS_Init_Guard &&)=delete
Deleted move constructor.
BiBFS_Init_Guard(Bidirectional_BFS &__owner, const GT &__g) noexcept
Constructor.
Bidirectional BFS for finding shortest unweighted paths.
Bidirectional_BFS(const SA &__sa)
Constructor with lvalue arc filter, const or not.
Path< GT > operator()(const GT &g, Node *start, Node *end)
Finds shortest path (operator interface returning path).
void build_path(const GT &g, Node *start, Node *meeting, Node *end, Path< GT > &path)
static constexpr bool backward_by_index
Node *& parent_ref(Node *p, const int dir) noexcept
Bidirectional_BFS(SA &&__sa=SA())
Constructor.
bool is_seen(Node *p, const int dir) const noexcept
static constexpr int UNSEEN
static constexpr int BACKWARD
void mark_seen(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)
void cleanup(const GT &g)
std::conditional_t< backward_by_index, std::optional< In_Arcs_Index< GT, SA > >, No_Index > Backward_Index
void expand_frontier(const GT &g, DynListQueue< Node * > &frontier, const int my_dir, const int other_dir, size_t &best_len, Node *&best_meeting)
static constexpr size_t INF_DIST
static constexpr int FORWARD
bool find_path(const GT &g, Node *start, Node *end, Path< GT > &path)
Finds the shortest unweighted path between start and end.
Arc *& parent_arc_ref(Node *p, const int dir) noexcept
void set_dist(Node *p, const int dir, const size_t d) noexcept
void expand_backward(const GT &g, DynListQueue< Node * > &frontier, size_t &best_len, Node *&best_meeting, Backward_Index &index)
size_t get_dist(Node *p, const int dir) const noexcept
bool operator()(const GT &g, Node *start, Node *end, Path< GT > &path)
Finds shortest path (operator interface with path output).
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
T get()
Remove the oldest item of the queue.
void swap(DynListQueue &__q) noexcept
Swap this with __q in constant time.
size_t size() const noexcept
Return the number of elements.
bool is_empty() const noexcept
Return true if this is empty.
Doubly-linked list (defined in tpl_dynList.H).
T & insert(const T &item)
Filtered iterator for incoming arcs of a node.
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
void empty()
Clean the path: all the nodes and arc are removed.
void set_graph(const GT &__g, Node *start_node=nullptr)
Set the graph of the path.
void append(Arc *arc)
Append an arc to the path.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
bool is_digraph() const noexcept
Return true if the graph this is directed.
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
A graph usable by the graph algorithms.
An arc filter (SA) for graph GT.
A graph type built with the Digraph template.
#define NODE_COOKIE(p)
Return the node cookie
Main namespace for Aleph-w library functions.
Itor lower_bound(Itor beg, Itor end, const T &value)
Find lower bound in a sorted range.
and
Check uniqueness with explicit hash + equality functors.
static std::atomic< bool > init
Default filter for filtered iterators on arcs.
Filtered iterator of adjacent arcs of a node.
Whether the iterator template is not In_Iterator.
Dynamic queue implementation based on linked lists.
Utility algorithms and operations for graphs.