97namespace johnson_detail
109 std::uint64_t
hi = 0;
110 std::uint64_t
lo = 0;
113 template <std::
integral T>
114 [[nodiscard]]
static constexpr Wide of(
const T x)
noexcept
116 if constexpr (std::is_signed_v<T>)
117 return Wide{x < 0 ?
~std::uint64_t(0) : std::uint64_t(0),
118 static_cast<std::uint64_t
>(
static_cast<std::int64_t
>(x))};
120 return Wide{0,
static_cast<std::uint64_t
>(x)};
126 const std::uint64_t
lo = a.
lo + b.lo;
127 return Wide{a.
hi + b.hi + (
lo < a.lo ? 1 : 0),
lo};
133 return Wide{a.
hi - b.hi - (a.lo < b.lo ? 1 : 0), a.lo - b.lo};
143 return a.hi < b.hi or (a.hi == b.hi
and a.lo < b.lo);
149 constexpr std::uint64_t sign = std::uint64_t(1) << 63;
150 return Wide{a.
hi ^ sign, a.lo} <
Wide{b.
hi ^ sign, b.lo};
210template <AlephGraph
GT,
225 static constexpr bool Integer = std::is_integral_v<Distance_Type>
226 and not std::is_same_v<std::remove_cv_t<Distance_Type>,
bool>;
232 typename std::conditional_t<
Integer,
233 std::make_unsigned<std::remove_cv_t<Distance_Type>>,
234 std::type_identity<Distance_Type>>::type;
244 static constexpr size_t None = std::numeric_limits<size_t>::max();
269 std::vector<Distance_Type>
h;
314 std::vector<std::pair<Weight, size_t>>
heap;
364 :
node(std::exchange(other.node,
None))
371 node = std::exchange(other.node,
None);
381 <<
"Node not found in mapping (not from original graph?)";
401 if (b >
Weight(std::numeric_limits<Weight>::max() - a))
412 return not (
sum < a);
419 if constexpr (std::is_assignable_v<typename GT::Arc_Type &, const Distance_Type &>)
428 copy->get_info() =
w;
435 if constexpr (std::is_assignable_v<typename GT::Arc_Type &, const Distance_Type &>)
448 if constexpr (std::numeric_limits<Distance_Type>::has_infinity)
449 return std::numeric_limits<Distance_Type>::infinity();
451 return std::numeric_limits<Distance_Type>::max();
467 Weight(
Weight(std::numeric_limits<Distance_Type>::max()) + minus_ht);
470 <<
"Integer overflow: a distance is above max()";
475 return d_prime - hs + ht;
482 const Wide d = d_prime - hs + ht;
484 <<
"Integer overflow: a distance is above max()";
486 <<
"Integer overflow: a distance is below lowest()";
521 auto & stack = heap_of<W>();
522 using Key =
typename std::remove_reference_t<
decltype(stack)>::value_type::first_type;
525 for (
size_t v = 0; v <
state.size(); ++v)
527 stack.emplace_back(Key{}, v);
529 while (not stack.empty())
531 const size_t u = stack.back().second;
537 stack.emplace_back(Key{}, v);
549 [[gnu::always_inline]]
inline void paint(
size_t s)
551 using Key =
typename std::remove_reference_t<decltype(weights_of<W>())>::value_type;
553 const auto & weight = weights_of<W>();
554 auto & dist = reach_of<W>();
555 auto & pairs = heap_of<W>();
557 const auto later = [] (
const std::pair<Key, size_t> & a,
558 const std::pair<Key, size_t> & b)
560 return b.first < a.first;
566 bool too_far =
false;
571 pairs.emplace_back(Key{}, s);
572 while (not pairs.empty())
574 std::pop_heap(pairs.begin(), pairs.end(), later);
575 const size_t u = pairs.back().second;
577 if (
state[u] == Done)
581 const Key du = dist[u];
585 if (
state[v] == Done)
589 if (not
add(du, weight[e], dv))
606 pairs.emplace_back(dv, v);
607 std::push_heap(pairs.begin(), pairs.end(), later);
618 [[gnu::noinline]]
void paint_wide(
size_t s) { paint<true>(s); }
667 std::vector<typename Potential_Graph::Node *> pnodes;
669 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
671 Node * p = it.get_curr_ne();
673 pnodes.push_back(pg.insert_node(
nodes.size()));
681 std::vector<Shown_Arc> shown;
682 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
684 Arc * a = it.get_curr_ne();
693 if (not directed
and u != v)
694 shown.push_back(
Shown_Arc{v, u,
w, a,
nullptr});
703 pg.insert_arc(pnodes[x.u], pnodes[x.v], x.w);
711 catch (
const std::overflow_error &)
728 for (
size_t i = 0; i < n; ++i)
732 if (wide) [[unlikely]]
746 if (x.copy !=
nullptr)
751 const size_t e =
next[x.u]++;
771 const std::vector<typename Potential_Graph::Node *> & pnodes)
775 h.reserve(pnodes.size());
776 for (
size_t i = 0; i < pnodes.size(); ++i)
777 h.push_back(potentials.find(pnodes[i]));
787 const size_t n =
nodes.size();
789 for (
size_t pass = 1; ; ++pass)
791 bool changed =
false;
811 const size_t n =
nodes.size();
820 if (x.copy !=
nullptr)
825 const size_t e =
next[x.u]++;
838 for (
size_t i = 0; i < n; ++i)
886 requires std::is_convertible_v<typename GT::Arc_Type, Distance_Type>
889 ah_domain_error_if(src ==
nullptr or tgt ==
nullptr) <<
"Source or target node is null";
914 requires std::is_convertible_v<typename GT::Arc_Type, Distance_Type>
917 ah_domain_error_if(src ==
nullptr or tgt ==
nullptr) <<
"Source or target node is null";
937 std::vector<Arc*>
arcs;
938 for (
size_t v = t; v != s; v =
parent[v])
942 for (
auto it =
arcs.rbegin(); it !=
arcs.rend(); ++it)
966 for (
size_t s = 0; s <
nodes.size(); ++s)
972 for (
size_t t = 0; t <
nodes.size(); ++t)
998 <<
"Integer overflow: a potential is below lowest()";
Bellman-Ford algorithm for single-source shortest paths.
Dijkstra's shortest path algorithm.
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_overflow_error()
Throws std::overflow_error unconditionally.
#define ah_domain_error_if(C)
Throws std::domain_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
size_t size_t int32_t value
Bellman-Ford algorithm for shortest paths with negative weights.
DynMapTree< Node *, Distance_Type > compute_nodes_weights()
Returns a mapping with the shortest distances to each node obtained after executing the Bellman-Ford ...
Default distance accessor for arc weights.
Generic directed graph (digraph) wrapper template.
Generic key-value map implemented on top of a binary search tree.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
Johnson's algorithm for all-pairs shortest paths.
static Weight reweight(const Distance_Type &w, const Distance_Type &hu, const Distance_Type &hv)
std::vector< Weight > reach
std::vector< size_t > via
Path< GT > find_path(Node *src, Node *tgt)
Find the shortest path from source to target.
Johnson(const Johnson &)=delete
Johnson is not copyable: a copy would point to the nodes of the copy of the graph that this one keeps...
std::vector< Wide > wide_weight
static constexpr bool Integer
Johnson(Johnson &&)=default
Move constructor: the tree that is kept moves too.
void paint_wide(size_t s)
Distance_Type distance_to(size_t s, size_t t) const
void paint_from(size_t s)
std::vector< Node * > nodes
The node of each number.
size_t number_of(Node *p) const
GT & get_reweighted_graph() noexcept
Get access to the internal reweighted graph.
static Distance_Type wide_unweighted(const Wide &d_prime, const Wide &hs, const Wide &ht)
static constexpr Distance_Type unreachable() noexcept
bool is_initialized() const noexcept
Check if the algorithm has been initialized.
std::vector< size_t > out_first
DynMapTree< std::pair< Node *, Node * >, Distance_Type > compute_all_pairs_distances()
Compute the shortest distance for all pairs.
std::vector< std::pair< Wide, size_t > > wide_heap
std::vector< std::pair< Weight, size_t > > heap
auto & reach_of() noexcept
std::vector< Weight > out_weight
static bool add(const Weight &a, const Weight &b, Weight &sum)
void wide_potentials(const std::vector< Shown_Arc > &shown)
Distance_Type get_potential(Node *node) const
Get the node potential (h value) for a node.
static void write_info(Arc *copy, const Wide &w)
void wide_arcs(const std::vector< Shown_Arc > &shown)
GT reweighted
Copy of the graph.
std::vector< unsigned char > state
std::vector< Wide > wide_reach
static bool add(const Wide &a, const Wide &b, Wide &sum)
typename std::conditional_t< Integer, std::make_unsigned< std::remove_cv_t< Distance_Type > >, std::type_identity< Distance_Type > >::type Weight
static constexpr size_t None
Johnson(const GT &g, Distance d=Distance(), SA __sa=SA())
Construct a Johnson's all-pairs shortest paths executor.
Distance_Type distance_apart(size_t s, size_t t) const
auto & weights_of() noexcept
static Distance_Type unweighted(const Weight &d_prime, const Distance_Type &hs, const Distance_Type &ht)
std::vector< size_t > parent
typename Distance::Distance_Type Distance_Type
std::vector< Arc * > out_arc
Johnson()=default
Default constructor (uninitialized state)
std::vector< Node * > copies
Its copy in the reweighted graph.
struct Aleph::Johnson::Kept_Source kept
DynMapTree< Node *, size_t > number
Of each node of the graph, from 0.
auto & heap_of() noexcept
Node * get_copy_node(Node *orig) const
Get the copy of a node in the reweighted graph.
Distance_Type get_distance(Node *src, Node *tgt)
Get the shortest distance from source to target.
const GT & get_reweighted_graph() const noexcept
Get const access to the internal reweighted graph.
Johnson & operator=(const Johnson &)=delete
Johnson is not copyable: a copy would point to the nodes of the copy of the graph that this one keeps...
void narrow_potentials(Potential_Graph &pg, const std::vector< typename Potential_Graph::Node * > &pnodes)
static constexpr bool Signed_Integer
static void write_info(Arc *copy, const Weight &w)
std::vector< size_t > out_target
std::vector< Distance_Type > h
Its potential, from Bellman-Ford.
std::vector< Wide > wide_h
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
void init(Node *start_node)
Set the first node of a 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.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
bool is_digraph() const noexcept
Return true if the graph this is directed.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
size_t resize(size_t new_size)
Resizes the hash table to a new capacity.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
DynArray< Graph::Arc * > arcs
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Filtered iterator on all the arcs of a graph.
Default filter for filtered iterators on arcs.
Arc of graph implemented with double-linked adjacency lists.
Kept_Source(Kept_Source &&other) noexcept
Kept_Source & operator=(Kept_Source &&other) noexcept
Filtered iterator of adjacent arcs of a node.
Represents a missing value.
An integer of 128 bits, in two words of 64.
friend constexpr Wide operator-(const Wide &a, const Wide &b) noexcept
a - b, modulo 2^128
static constexpr Wide of(const T x) noexcept
The integer x, in two's complement.
friend constexpr Wide operator+(const Wide &a, const Wide &b) noexcept
a + b, modulo 2^128
std::uint64_t hi
The high word.
friend constexpr bool operator<(const Wide &a, const Wide &b) noexcept
Whether a < b, as unsigned integers.
friend constexpr bool operator==(const Wide &a, const Wide &b) noexcept=default
Whether a and b are the same integer.
std::uint64_t lo
The low word.
static constexpr bool signed_less(const Wide &a, const Wide &b) noexcept
Whether a < b, as integers in two's complement.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Alias for htlist.H (DynList implementation).
Dynamic key-value map based on balanced binary search trees.
Generic graph and digraph implementations.