85template <
typename Arc_Info,
typename Flow_Type>
105namespace net_detail {
125[[deprecated(
"float as the type of a flow is deprecated: its precision is below what the "
126 "tolerances of the floating flows need; use double. It is rejected in 8.0.0")]]
158template <
typename Arc_Info,
typename F_Type =
double>
202 *
static_cast<Base *
>(
this) = arc;
214 assert(a->src_node == src or a->tgt_node == src);
215 return a->tgt_node == src;
244 assert(a->src_node ==
p or a->tgt_node ==
p);
245 auto src =
static_cast<typename
Net::Node *
>(a->src_node);
247 return a->cap - a->flow > 0;
249 assert(is_residual<Net>(
p, a));
263 assert(a->src_node ==
p or a->tgt_node ==
p);
264 return static_cast<typename
Net::Node *
>(a->src_node ==
p ? a->tgt_node : a->src_node);
272template <
class Net,
class Show_Arc = Dft_Show_Arc<Net>>
285 return is_residual<Net>(src, a) ? a->flow : a->cap - a->flow;
289template <
typename Node_Info = Empty_Class>
309template <
class NodeT = Net_Node<Empty_Class>,
class ArcT = Net_Arc<Empty_Class,
double>>
341 return Aleph::out_arcs<Net_Graph>(p);
356 return Aleph::in_arcs<Net_Graph>(p);
373 sum += it.get_curr()->cap;
382 sum += it.get_curr()->cap;
403 sum += it.get_curr()->flow;
412 sum += it.get_curr()->flow;
455 if constexpr (std::is_floating_point_v<Flow_Type>)
456 return std::max(std::abs(in), std::abs(
out)) * 1e-9;
479 const auto nearly_zero = [eps](
auto x)
481 return std::abs(x) <= eps;
483 const auto nearly_equal = [eps](
auto a,
auto b)
485 return a == b or std::abs(a - b) <= eps;
489 return nearly_zero(out_flow)
and in_flow >= -eps;
492 return nearly_zero(in_flow)
and out_flow >= -eps;
494 return nearly_equal(out_flow, in_flow);
515 if constexpr (std::is_floating_point_v<Flow_Type>)
516 if (std::isinf(in) or std::isinf(
out))
540 if constexpr (std::is_floating_point_v<Flow_Type>)
612 sources.
append(it.get_curr());
617 for (
auto it = sources.
get_it(); it.has_curr(); it.next_ne())
665 sinks.
append(it.get_curr());
670 for (
auto it = sinks.
get_it(); it.has_curr(); it.next_ne())
719 if (not had_super_source)
768 template <
typename... Args>
800 const typename Arc::Arc_Type &arc_info =
Arc_Type())
816 template <
typename... Args>
821 return insert_arc(src_node, tgt_node, cap, flow,
Arc_Type(std::forward<Args>(args)...));
859 requires(not std::is_same_v<Arc_Type, Flow_Type>
and not std::is_arithmetic_v<Arc_Type>)
861 return insert_arc(src_node, tgt_node, 0, 0, arc_info);
872 requires(std::is_same_v<Arc_Type, Flow_Type> or std::is_arithmetic_v<Arc_Type>)
885 const bool new_source =
960 using Pair = std::pair<typename Net_Graph::Arc *, typename Net_Graph::Arc *>;
962 .for_each([](
const Pair &p)
965 auto asrc = p.second;
966 atgt->cap = asrc->cap;
967 atgt->flow = asrc->flow;
1002 std::swap(
Infinity, other.Infinity);
1102 it.get_curr()->flow = 0;
1123 return this->
nodes().all(
1131 return a->check_arc();
1142 <<
"network has no source or sink nodes";
1166 return s <<
"Path is Empty";
1174 s <<
"(" << a->cap <<
"," << a->flow <<
")"
1188using Parc = std::tuple<typename Net::Arc *, bool>;
1198using SemiPath = std::tuple<bool, typename Net::Flow_Type, DynList<Parc<Net>>>;
1206 std::cout <<
"Semi path is Empty";
1210 for (
auto it = sp.get_it(); it.has_curr(); it.next_ne())
1213 auto a = get<0>(pa);
1214 auto s =
static_cast<typename
Net::Node *
>(a->src_node);
1215 auto t =
static_cast<typename
Net::Node *
>(a->tgt_node);
1216 std::cout << s->get_info() <<
"(" << a->flow <<
"," << a->cap <<
")" << t->get_info() <<
" "
1217 << (get<1>(pa) ?
"Normal" :
"Reduced") <<
'\n';
1221namespace net_detail {
1226template <
class Arc,
typename Flow_Type>
1229 if constexpr (std::is_floating_point_v<Flow_Type>)
1230 if (arc->cap - arc->flow <= slack)
1232 arc->flow = arc->cap;
1249template <
typename Flow_Type>
1252 if constexpr (std::is_floating_point_v<Flow_Type>)
1253 return std::numeric_limits<Flow_Type>::epsilon() *
Flow_Type(
arcs + 2) * std::max(in,
out);
1271 if constexpr (std::is_floating_point_v<typename Net::Flow_Type>)
1282template <
class Arc,
typename Flow_Type>
1285 if constexpr (std::is_floating_point_v<Flow_Type>)
1289 if (arc->flow <= 256 * std::numeric_limits<Flow_Type>::epsilon() * std::max(before, slack))
1303template <
typename Flow_Type>
1306#if defined(__FAST_MATH__) || defined(_M_FP_FAST)
1307 static_assert(not std::is_floating_point_v<Flow_Type>,
1308 "The floating flows of tpl_net.H need strict floating "
1309 "arithmetic, which -ffast-math and /fp:fast are not");
1320 lo += (
hi - (s - b)) + (x - b);
1342 return d.
hi + (d.
lo - other.lo);
1353 auto arc = it.get_curr_ne();
1354 if (net.get_src_node(arc) == p)
1356 if (net.get_tgt_node(arc) == p)
1369template <
class Arc,
typename Flow_Type>
1379 arc->flow = balanced >= arc->cap ? arc->cap : balanced;
1382 with.
add(arc->flow);
1413 if constexpr (std::is_floating_point_v<Flow_Type>)
1428 Node *p = it.get_curr_ne();
1432 Arc *arc = a.get_curr_ne();
1441 received = in -
out;
1453 long from_source = -1;
1455 Arc *parent =
nullptr;
1472 auto at = [](
Node *p) -> Info &
1478 Arc *a = it.get_curr_ne();
1487 a->flow = d >= before ?
Flow_Type{0} : before - d;
1493 return p != source
and p != sink;
1495 auto excess = [&](
Node *p)
1497 return at(p).in.minus(at(p).
out);
1502 auto search = [&](
Node *start,
Node *stop,
bool forward,
long Info::*depth)
1505 at(start).*depth = 0;
1507 for (
size_t k = 0;
k < order.
size(); ++
k)
1514 Arc *a = it.get_curr_ne();
1519 at(v).*depth = at(u).*depth + 1;
1529 auto tolerance_of = [&](
Node *p)
1533 auto off = [&](
Node *p)
1535 return std::abs(excess(p)) > tolerance_of(p);
1541 search(source, sink,
true, &Info::from_source);
1542 for (
size_t k = order.
size();
k-- > 1;)
1547 Arc *a = it.get_curr_ne();
1550 at(u).from_source >= 0
and at(u).from_source < at(v).from_source)
1551 lower(a, excess(v));
1556 search(sink, source,
false, &Info::to_sink);
1557 for (
size_t k = order.
size();
k-- > 1;)
1562 Arc *a = it.get_curr_ne();
1565 at(
w).to_sink >= 0
and at(
w).to_sink < at(v).to_sink)
1566 lower(a, -excess(v));
1587 for (
Node *v = nt.get_curr_ne();
between(v)
and off(v)
and budget > 0; --budget)
1589 const bool keeps = excess(v) >
Flow_Type{0};
1590 const Flow_Type d = std::abs(excess(v));
1591 auto opposite = [&](
Node *
w)
1596 auto takes_all = [&](
Node *
w)
1598 return not
between(
w) or std::abs(excess(
w)) + d <= tolerance_of(
w);
1606 at(v).stamp = stamp;
1609 Node *
w =
nullptr, *some =
nullptr;
1610 for (
size_t k = 0;
k < queue.
size()
and w ==
nullptr; ++
k)
1613 Arc *a = it.get_curr_ne();
1616 if (near != queue(
k) or x == near or not (a->flow >
Flow_Type{0}) or
1617 at(x).stamp == stamp)
1619 at(x).stamp = stamp;
1621 if (takes_all(x) or (opposite(x)
and off(x)))
1626 if (some ==
nullptr and opposite(x))
1636 Arc *a = at(x).parent;
1641 m = std::min(
m, at(x).parent->flow);
1642 if (not takes_all(
w))
1643 m = std::min(
m, std::abs(excess(
w)));
1644 for (
Node *x =
w; x != v;)
1646 Arc *a = at(x).parent;
1663 for (
const Info &x : info)
1665 const Flow_Type sent = at(source).out.minus(at(source).in);
1666 const Flow_Type received = at(sink).in.minus(at(sink).
out);
1667 const Flow_Type apart = sent - received;
1669 std::abs(apart) <= tolerance)
1679 return a->flow < b->flow;
1682 for (
Arc *a : end_arcs)
1688 left -= before - a->flow;
1717 using Tuple = std::tuple<typename Net::Node *, typename Net::Arc *>;
1722 Tuple t = it.get_tuple_ne();
1724 auto arc = get<1>(t);
1725 const auto w = remaining_flow<Net>(p, arc);
1733 auto t = it.get_tuple_ne();
1735 auto arc = get<1>(t);
1737 if (is_residual<Net>(p, arc))
1742 assert(arc->check_arc());
1768 for (
auto it = semi_path.get_it(); it.has_curr(); it.next_ne())
1770 auto p = it.get_curr();
1771 auto arc = get<0>(p);
1777 assert(arc->check_arc());
1779 if constexpr (not std::is_floating_point_v<typename Net::Flow_Type>)
1801 for (
auto it = semi_path.get_it(); it.has_curr(); it.next_ne())
1803 auto p = it.get_curr();
1804 auto arc = get<0>(p);
1810 assert(arc->check_arc());
1820template <
class Net,
template <
typename T>
class Q>
1837 Q<typename Net::Arc *> q;
1838 for (Itor it(start); it.has_curr(); it.next_ne())
1840 auto a = it.get_curr();
1841 if (remaining_flow<Net>(start, a) < min_slack)
1850 while (not q.is_empty())
1870 for (Itor it(curr); it.has_curr(); it.next_ne())
1872 auto a = it.get_curr();
1878 if (remaining_flow<Net>(curr, a) < min_slack)
1901 auto curr =
search(start, end, min_slack);
1907 assert(curr == end);
1910 while (curr != start)
1924 auto t =
search(start, end, min_slack);
1931 auto m = std::numeric_limits<typename Net::Flow_Type>::max();
1936 bool normal = a->tgt_node == t;
1937 auto slack = normal ? a->cap - a->flow : a->flow;
1938 m = std::min(
m, slack);
1939 semi_path.insert(std::make_tuple(a, normal));
1943 return std::make_tuple(
true,
m, std::move(
semi_path));
1969 return find(start, end, min_slack);
1988 auto result =
find_path(start, end, min_slack);
1989 if (not get<0>(result))
1993 return get<1>(result);
2017template <
class Net,
template <
typename T>
class Q>
2029 return find_aumenting_path<Net, DynListStack>(net, min_slack);
2036 return find_aumenting_path<Net, DynListQueue>(net, min_slack);
2047template <
class Net,
template <
typename T>
class Q>
2057 return find_aumenting_semi_path<Net, DynListStack>(net, slack);
2064 return find_aumenting_semi_path<Net, DynListQueue>(net, slack);
2075template <
class Net,
template <
typename T>
class Q>
2085 return find_decrementing_path<Net, DynListStack>(net, slack);
2092 return find_decrementing_path<Net, DynListQueue>(net, slack);
2138 return img ==
nullptr;
2160 auto src =
static_cast<typename Rnet::Node *
>(
NODE_COOKIE(s));
2161 auto tgt =
static_cast<typename Rnet::Node *
>(
NODE_COOKIE(t));
2163 auto arc = rnet.insert_arc(src, tgt);
2164 auto dup = rnet.insert_arc(tgt, src);
2168 arc->flow = a->flow;
2171 dup->cap = arc->cap;
2172 dup->flow = arc->cap - arc->flow;
2177template <
class Rnet>
2180 Res_F(
typename Rnet::Node *)
noexcept {}
2185 return a->cap > a->flow;
2203 auto p = it.get_curr();
2204 auto q = rnet.insert_node();
2216 return std::make_tuple(std::move(rnet),
2222template <
class Rnet>
2225 for (
typename Rnet::Arc_Iterator it(rnet); it.has_curr(); it.next_ne())
2227 auto arc = it.get_curr();
2228 auto img = arc->img;
2231 img->flow = arc->flow;
2257template <
class Net,
template <
class>
class Find_Path>
2261 <<
"Network is not single source and single sink";
2266 if (not get<0>(semi_path))
2272 auto slack = get<1>(semi_path);
2276 increase_flow<Net>(net, get<2>(semi_path), slack);
2308 if constexpr (std::is_floating_point_v<typename Net::Flow_Type>)
2309 return aumenting_path_maximum_flow<Net, Find_Aumenting_Path_BFS>(net);
2311 return aumenting_path_maximum_flow<Net, Find_Aumenting_Path_DFS>(net);
2338 return aumenting_path_maximum_flow<Net, Find_Aumenting_Path_BFS>(net);
2351 return edmonds_karp_maximum_flow<Net>(net);
2363template <
class Rnet>
2366 return p->in_flow != p->out_flow;
2384 node_height<Net>(p) = node_height<Net>(net.
get_tgt_node(a)) + 1;
2390template <
class Q_Type>
2400template <
class Q_Type>
2410template <
class Q_Type>
2418namespace net_detail {
2431 if constexpr (std::numeric_limits<Flow_Type>::has_infinity)
2433 constexpr Flow_Type inf = std::numeric_limits<Flow_Type>::infinity();
2435 bool leaves =
false;
2437 if (
auto a = it.get_curr(); net.
get_src_node(a) == source
and a->cap == inf)
2443 if (not get<0>(semi_path))
2445 increase_flow<Net>(net, get<2>(semi_path), inf);
2465 constexpr Flow_Type inf = std::numeric_limits<Flow_Type>::infinity();
2470 if (
auto a = it.get_curr(); net.
get_tgt_node(a) == sink)
2481 if (
auto a = it.get_curr(); a->cap != inf)
2484 return std::min(bound, std::numeric_limits<Flow_Type>::max());
2525template <
class Net,
class Q_Type>
2529 <<
"Network is not single source and single sink";
2540 node_height<Net>(source) = net.
vsize();
2541 constexpr auto Max = std::numeric_limits<long>::max();
2548 [[maybe_unused]]
bool bounded =
false;
2549 for (Itor it(source); it.has_curr(); it.next_ne())
2551 auto arc = it.get_curr();
2552 arc->flow = arc->cap;
2553 if constexpr (std::numeric_limits<Flow_Type>::has_infinity)
2554 if (arc->cap == std::numeric_limits<Flow_Type>::infinity())
2567 while (not q.is_empty())
2575 if constexpr (std::is_floating_point_v<Flow_Type>)
2587 bool was_eligible_arc =
false;
2588 for (Itor it(src); it.has_curr()
and excess > residue; it.next_ne())
2590 auto arc = it.get_curr();
2592 if (node_height<Net>(src) != node_height<Net>(tgt) + 1)
2594 m = std::min(
m, node_height<Net>(tgt));
2598 was_eligible_arc =
true;
2599 if constexpr (std::is_floating_point_v<Flow_Type>)
2610 if (is_residual<Net>(src, arc))
2612 flow_to_push = std::min(arc->flow, excess);
2613 arc->flow -= flow_to_push;
2617 flow_to_push = std::min(arc->cap - arc->flow, excess);
2618 arc->flow += flow_to_push;
2620 excess -= flow_to_push;
2622 if (tgt != source
and tgt != sink)
2629 if (excess > residue)
2631 if (not was_eligible_arc)
2632 node_height<Net>(src) =
m + 1;
2648template <
class Rnet>
2652 sink, [&rnet](
typename Rnet::Node *p,
typename Rnet::Arc *a)
2655 node_height<Rnet>(p) = node_height<Rnet>(rnet.get_src_node(a)) + 1;
2671 return generic_preflow_vertex_push_maximum_flow<Net, DynListQueue<Node *>>(net);
2694 return node_height<Net>(n1) > node_height<Net>(n2);
2709 return generic_preflow_vertex_push_maximum_flow<Net, DynBinHeap<Node *, Compare_Height<Net>>>(net);
2741 return generic_preflow_vertex_push_maximum_flow<Net, Random_Set<Node *>>(net);
2761 return generic_preflow_vertex_push_maximum_flow<Net, Random_Set<Node *>>(net, std::move(q));
2789template <
class Net,
template <
class>
class Maxflow>
2795 Maxflow<Net>()(net);
2809 if (
auto p = it.get_curr(); p->state() ==
Unprocessed)
2814 auto arc = it.get_curr();
2823 if (arc->flow == arc->cap)
2845 return min_cut<Net, Maxflow>(net, vs, vt, cuts, cutt);
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
WeightedDigraph::Node Node
size_t size_t int32_t * out
Iterator over the arcs of a node.
bool has_curr() const noexcept
Return true if the iterator is on an arc.
virtual void remove_arc(Arc *a)
virtual Node * insert_node(Node *p)
Inserts the node p, made with new, which the graph takes.
Arc * disconnect_arc(Arc *arc)
Dlink & get_arc_dlink() noexcept
Returns reference to internal arc Dlink for sorting operations.
virtual void remove_node(Node *p)
Arc * insert_arc(Node *src, Node *tgt, void *a)
Arc * connect_arc(Arc *arc)
Dlink & get_node_dlink() noexcept
Returns reference to internal node Dlink for sorting operations.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void clear() noexcept
Empties the container.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
auto next_ne() noexcept(noexcept(std::declval< I & >().next_ne())) -> decltype(std::declval< I & >().next_ne())
next() without checks.
RAII guard that saves and restores graph cookies.
Filtered iterator on directed graphs.
bool has_curr() const noexcept
Return true the iterator has an current arc.
Generic directed graph (digraph) wrapper template.
typename BaseGraph::Node Node
void swap(Dlink *link) noexcept
Swap this with list whose header is link.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
T & insert(const T &item)
Dynamic set backed by balanced binary search trees with automatic memory management.
const Key & get_item() const
Returns any element of the set.
const size_t & size() const
Returns the cardinality of the set.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
void swap(DynSetTree &dset) noexcept(noexcept(tree.swap(dset.tree)) and noexcept(std::swap(num_nodes, dset.num_nodes)) and noexcept(std::swap(arena_allocator, dset.arena_allocator)))
Exchange all elements of this set with dset in constant time (and extremely fast).
size_t remove(const Key &key)
Removes a key from the dynamic set.
bool contains(const Key &key) const
Checks if a key exists in the set.
bool is_empty() const
returns true if the set is empty
Generic filter iterator wrapper.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Augmenting-path search over a directed network.
Path< Net > operator()(typename Net::Node *start, typename Net::Node *end, typename Net::Flow_Type min_slack=0)
Find a concrete Path with at least min_slack.
Find_Aumenting_Path(const Net &__g)
Construct a finder for net.
Net::Flow_Type semi_path(typename Net::Node *start, typename Net::Node *end, DynList< Parc< Net > > &semi_path, const typename Net::Flow_Type &min_slack=0)
Fill semi_path and return the slack value.
Path< Net > find(typename Net::Node *start, typename Net::Node *end, const typename Net::Flow_Type &min_slack=typename Net::Flow_Type{0})
Build a Path from start to end with minimum slack.
SemiPath< Net > find_dec_path(typename Net::Flow_Type min_slack=0.0)
Find a decrementing semi-path with at least min_slack.
SemiPath< Net > find_aum_path(typename Net::Flow_Type min_slack=0.0)
Find an augmenting semi-path with at least min_slack.
SemiPath< Net > find_path(typename Net::Node *start, typename Net::Node *end, typename Net::Flow_Type min_slack=typename Net::Flow_Type{0})
Build a SemiPath from start to end with minimum slack.
Net::Node * search(typename Net::Node *start, typename Net::Node *end, typename Net::Flow_Type min_slack)
Filtered iterator on the nodes of a graph.
Iterator on nodes and arcs of a path.
Node * get_current_node_ne() const noexcept
Node * get_current_node() const
Return the current node of a path.
Arc * get_current_arc_ne() const noexcept
bool has_current_arc() const noexcept
Return true if iterator has current arc.
bool is_empty() const noexcept
Return true if the path is empty.
const GT & get_graph() const noexcept
Get a constant reference to the graph.
void set_seed(unsigned long seed) const
Seeds the Mersenne Twister of the set, as gsl_rng_set() did: the positions are from then on those of ...
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Container< Arc * > arcs() const
Return a container with all the arcs of the graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
std::tuple< Arc *, Node * > ArcPair
Pair of arc and node (topologically related)
void *& get_cookie() noexcept
Return a modifiable reference to graph's cookie.
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
void common_swap(GT &g) noexcept
void reset_bit_arcs(int bit) const noexcept
Reset bit to zero for all the arcs of graph.
size_t out_degree(Node *p) const noexcept
Compute the output degree of a node.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
constexpr size_t vsize() const noexcept
See get_num_nodes().
size_t esize() const noexcept
Return the total of arcs of graph.
void reset_nodes() const
Reset all the nodes of graph (the control bits, the state, the counter and the cookie)
size_t in_degree(Node *p) const noexcept
Compute the input degree of a node.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Container< Node * > nodes() const
Return a container with all the nodes of the graph.
Traverse a graph depth-first or breadth-first and execute a visit function.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
RAII guards for graph node/arc cookies.
__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)
Graph traversal algorithms (DFS, BFS).
DynArray< Graph::Arc * > arcs
const unsigned char Processed
The node or arc has already been processed.
const unsigned char Processing
The node are being processed; probably it is inside a queue, stack or heap.
#define NODE_COUNTER(p)
Get the counter of a node.
#define NODE_COOKIE(p)
Return the node cookie
const unsigned char Unprocessed
The node have not bees processed.
#define NODE_BITS(p)
Get the control bits of a node.
void copy_graph(GT >gt, const GT &gsrc, bool cookie_map=false)
Explicit copy of graph.
bool not_short(const Net &net, typename Net::Node *p)
void return_dust(Net &net, typename Net::Node *source, typename Net::Node *sink)
Returns the dust of a floating flow that a preflow algorithm left in net, from source to sink: what e...
void add_flow(Arc *arc, const Flow_Type &slack) noexcept
bool fill_infinite_path(Net &net)
void push_excess(Arc *arc, bool forward, Flow_Sum< Flow_Type > &in, Flow_Sum< Flow_Type > &out) noexcept
Flow_Type node_residue(const Flow_Type &in, const Flow_Type &out, size_t arcs) noexcept
void node_flows(const Net &net, typename Net::Node *p, Flow_Sum< typename Net::Flow_Type > &in, Flow_Sum< typename Net::Flow_Type > &out) noexcept
void subtract_flow(Arc *arc, const Flow_Type &slack) noexcept
constexpr bool flow_type_ok(const float *) noexcept
Whether float can be the type of a flow: it can, but it is deprecated, and this overload,...
Net::Flow_Type infinite_arc_preflow(const Net &net)
Main namespace for Aleph-w library functions.
SemiPath< Net > find_aumenting_semi_path_dfs(const Net &net, const typename Net::Flow_Type &slack)
Find an augmenting semi-path using DFS.
static void remove_from_active_queue(Q_Type &q, typename Q_Type::Item_Type &p)
Remove a node from the active queue and clear its queue mark.
Net::Flow_Type remaining_flow(typename Net::Node *src, typename Net::Arc *a) noexcept
Return the remaining flow of a as seen from src.
Net::Flow_Type generic_preflow_vertex_push_maximum_flow(Net &net, Q_Type q=Q_Type())
Generic preflow-push maximum flow (vertex-oriented).
void decrease_flow(Net &net, const DynList< Parc< Net > > &semi_path, const typename Net::Flow_Type slack)
Decrease flow along a semi-path by a given slack.
Net::Flow_Type edmonds_karp_maximum_flow(Net &net)
Maximize flow using the Edmonds-Karp algorithm.
bool is_residual(typename Net::Node *src, typename Net::Arc *a) noexcept
Return true if arc a is residual with respect to src.
Net::Flow_Type random_preflow_maximum_flow(Net &net)
Preflow-push maximum flow using a random active-node queue.
Path< Net > find_aumenting_path_dfs(const Net &net, const typename Net::Flow_Type &min_slack)
Find an augmenting path using DFS.
SemiPath< Net > find_decrementing_path(const Net &net, const typename Net::Flow_Type &slack)
Find a decrementing semi-path using DFS or BFS based on Q.
Net::Flow_Type heap_preflow_maximum_flow(Net &net)
Preflow-push maximum flow using a height-ordered heap.
SemiPath< Net > find_aumenting_semi_path_bfs(const Net &net, const typename Net::Flow_Type &slack)
Find an augmenting semi-path using BFS.
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zip(const Container1 &a, const Container2 &b)
Zip two containers into a list of pairs.
static Q_Type::Item_Type get_from_active_queue(Q_Type &q)
Dequeue an active node and clear its queue mark.
Net::Flow_Type increase_flow(Net &net, const Path< Net > &path)
Increase flow along an augmenting path.
SemiPath< Net > find_decrementing_path_dfs(const Net &net, const typename Net::Flow_Type &slack)
Find a decrementing semi-path using DFS.
void create_residual_arc(const Net &net, PP_Res_Net< Net > &rnet, typename Net::Arc *a)
Create residual arcs for a in the residual network.
std::tuple< PP_Res_Net< Net >, typename PP_Res_Net< Net >::Node *, typename PP_Res_Net< Net >::Node * > preflow_create_residual_net(Net &net)
Build the residual network for preflow-push algorithms.
Path< Net > find_aumenting_path_bfs(const Net &net, const typename Net::Flow_Type &min_slack)
Find an augmenting path using BFS.
and
Check uniqueness with explicit hash + equality functors.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
static long & node_height(typename Net::Node *p)
Access the height label stored in NODE_COUNTER.
std::tuple< bool, typename Net::Flow_Type, DynList< Parc< Net > > > SemiPath
Semi-path tuple:
SemiPath< Net > find_aumenting_semi_path(const Net &net, const typename Net::Flow_Type &slack)
Find an augmenting semi-path using DFS or BFS based on Q.
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
Net::Flow_Type fifo_preflow_maximum_flow(Net &net)
Preflow-push maximum flow using a FIFO queue of active nodes.
Path< Net > find_aumenting_path(const Net &net, const typename Net::Flow_Type &min_slack)
Find an augmenting path using DFS or BFS based on Q.
static bool is_node_active(const Net &net, typename Net::Node *p)
Return true if p has excess flow in net.
Net::Flow_Type ford_fulkerson_maximum_flow(Net &net)
Maximize flow using the Ford-Fulkerson algorithm.
static void init_height_in_nodes(Net &net)
Initialize node heights with BFS distance to the sink.
SemiPath< Net > find_decrementing_path_bfs(const Net &net, const typename Net::Flow_Type &slack)
Find a decrementing semi-path using BFS.
void update_flow(const Rnet &rnet)
Propagate residual arc flows back to their original arcs.
static void put_in_active_queue(Q_Type &q, typename Q_Type::Item_Type &p)
Enqueue an active node if it is not already enqueued.
void next()
Advance all underlying iterators (bounds-checked).
bool between(const Point &a, const Point &b, const Point &c)
Net::Flow_Type aumenting_path_maximum_flow(Net &net)
Maximize flow using repeated augmenting-path searches.
void introsort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using introsort (introspective sort).
Net::Flow_Type min_cut(Net &net, DynSetTree< typename Net::Node * > &vs, DynSetTree< typename Net::Node * > &vt, DynList< typename Net::Arc * > &cuts, DynList< typename Net::Arc * > &cutt)
Compute max flow and the corresponding minimum cut.
std::tuple< typename Net::Arc *, bool > Parc
Arc entry for a semi-path.
@ Tuple
Tuple type such as (Int, Bool).
void print(const DynList< Parc< Net > > &sp)
Print a semi-path to stdout.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Filtered iterator on all the arcs of a graph.
Iterator over arcs of a graph.
Compare nodes by height (higher first).
bool operator()(typename Net::Node *n1, typename Net::Node *n2) const
Functor wrapper for edmonds_karp_maximum_flow().
Net::Flow_Type operator()(Net &net) const
Invoke edmonds_karp_maximum_flow().
Empty placeholder class with no data members.
Functor wrapper for fifo_preflow_maximum_flow().
Net::Flow_Type operator()(Net &net) const
Invoke fifo_preflow_maximum_flow().
Functor wrapper for ford_fulkerson_maximum_flow().
Net::Flow_Type operator()(Net &net) const
Invoke ford_fulkerson_maximum_flow().
Functor wrapper for heap_preflow_maximum_flow().
Net::Flow_Type operator()(Net &net) const
Invoke heap_preflow_maximum_flow().
Functor wrapper for min_cut().
Net::Flow_Type operator()(Net &net, DynSetTree< typename Net::Node * > &vs, DynSetTree< typename Net::Node * > &vt, DynList< typename Net::Arc * > &cuts, DynList< typename Net::Arc * > &cutt)
Invoke min_cut().
Arc info extension that stores capacity and flow values.
Flow_Type flow
Flow value.
Flow_Type cap
Capacity value.
Net_Arc_Info()=default
Default constructor.
Arc of a flow network implemented with adjacency lists.
bool check_arc() const noexcept
Return true if flow satisfies 0 <= flow <= cap.
Net_Arc()
Default constructor.
Flow_Type cap
Capacity value.
Net_Arc(const Arc_Info &info)
Construct from arc info.
Net_Arc & operator=(const Net_Arc &arc)
Copy assignment.
Flow_Type flow
Flow value.
Net_Arc(const Net_Arc &arc)
Copy constructor.
Ftype Flow_Type
Type representing flow, capacity, and cost values.
Arc filter for residual traversal in flow networks.
Net::Node * get_node(typename Net::Arc *a) const noexcept
Return the opposite endpoint of arc a with respect to p.
Net_Filt(typename Net::Node *s=nullptr) noexcept
Build a filter rooted at node s.
void set_cookie(void *__cookie) noexcept
Store an opaque cookie for compatibility with other iterators.
bool operator()(typename Net::Arc *a) const noexcept
Return true if arc a has residual capacity with respect to p.
Flow network implemented with adjacency lists.
static Flow_Type check_tolerance(const Flow_Type &in, const Flow_Type &out) noexcept
Node * emplace_node(Args &&...args)
Construct a node in-place and insert it into the network.
Flow_Type get_out_flow(Node *node) const noexcept
Return total outgoing flow of node.
typename Arc::Arc_Type Arc_Type
Arc info type.
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
void set_cap(Arc *arc, const Flow_Type &cap)
Set the capacity of an arc.
const DynSetTree< Node * > & get_sink_nodes() const noexcept
Return the set of sink nodes.
Node * insert_node(Node_Type &&info)
Insert a new node by moving info.
bool check_network() const
Validate flow-conservation and capacity constraints.
bool is_connected(Node *p) const noexcept
Return true if p is connected (used for validation).
bool with_super_sink
True if the network has a super-sink.
const DynSetTree< Node * > & get_src_nodes() const noexcept
Return the set of source nodes.
static Flow_Type node_tolerance(const Flow_Type &in, const Flow_Type &out) noexcept
The tolerance of rounding of the flow of a node whose flows into and out of it are in and out.
typename Node::Node_Type Node_Type
Node info type.
Arc * connect_arc(Arc *arc)
Connect a previously disconnected arc.
Arc * emplace_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap, const Flow_Type &flow, Args &&...args)
Construct arc info in-place and insert the arc.
Net_Graph()
Default constructor.
Net_Graph(const Net_Graph &net)
Copy-construct a network. Throws bad_alloc on allocation failure.
bool same_totals(const Flow_Type &out, const Flow_Type &in) const
void remove_arc(Arc *arc) override
Remove arc arc from the network.
void set_flow(Arc *arc, const Flow_Type &flow)
Set the flow of an arc. Throws if flow exceeds capacity.
Flow_Type nodes_tolerance() const
virtual void disconnect_arc(Arc *arc)
Disconnect arc arc from the graph without deleting it.
void make_super_source()
Convert a multi-source network into a single super-source network.
void make_super_sink()
Convert a multi-sink network into a single super-sink network.
size_t get_out_degree(Node *p) const noexcept
Return the out-degree of p (number of outgoing arcs).
void register_ends_of_leaving(Arc *arc)
Array_Graph< NodeT, ArcT > Base
constexpr bool is_single_source() const noexcept
Return true if the network has a single source.
Node * register_node(Node *p)
Insert p into source/sink sets with rollback on failure.
Flow_Type total_sink_in_flow() const
Return total incoming flow to all sinks.
bool is_source(Node *node) const noexcept
Return true if node is a source.
Node * get_source() const
Return an arbitrary source node.
friend std::ostream & operator<<(std::ostream &s, const Path< Net_Graph > &path)
Stream a path with arc (cap,flow) tuples.
Flow_Type get_out_cap(Node *node) const noexcept
Return total outgoing capacity of node.
Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap, const Flow_Type &flow, const typename Arc::Arc_Type &arc_info=Arc_Type())
Insert a capacitated arc with an initial flow.
DynSetTree< Node * > src_nodes
bool with_super_source
True if the network has a super-source.
Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap)
Insert an arc with capacity cap and zero flow.
DynList< Node * > out_nodes(Node *p) const noexcept
Return nodes reachable from p through outgoing arcs.
Node * insert_node(Node *p) override
Insert the node p, allocated by the caller.
void unmake_super_nodes()
Restore a super-source/super-sink network to its original form.
void remove_node(Node *p) override
Remove node p and all its arcs from the network.
DynList< Arc * > out_arcs(Node *p) const noexcept
Return arcs outgoing from p (as a DynList).
Flow_Type total_source_out_flow() const
Return total outgoing flow from all sources.
virtual void swapped() noexcept
What swap() calls on each of its two networks, once their nodes and arcs are exchanged: nothing,...
void unmake_super_sink()
Restore a super-sink network to its original multi-sink form, by removing the super sink that make_su...
Flow_Type get_in_cap(Node *node) const noexcept
Return total incoming capacity of node.
Flow_Type get_in_flow(Node *node) const noexcept
Return total incoming flow of node.
Node * get_sink() const
Return an arbitrary sink node.
bool is_sink(Node *node) const noexcept
Return true if node is a sink.
Flow_Type flow_value() const
Return the total flow value of the network: the flow out of its sources, which a valid flow also take...
Arc * insert_arc(Node *src_node, Node *tgt_node)
Insert an arc with zero capacity and flow, for an arc info that is arithmetic or of the type of the f...
typename Arc::Flow_Type Flow_Type
Capacity/flow numeric type.
constexpr bool is_single_sink() const noexcept
Return true if the network has a single sink.
void swap_contents(Net_Graph &other) noexcept
Exchange the nodes, the arcs and the rest of the state of this network with those of other,...
Net_Graph & operator=(Net_Graph &&other) noexcept
Move-assign a network. O(1) operation.
void make_super_nodes()
Convert a multi-source/multi-sink network into super-source/super-sink.
DynSetTree< Node * > sink_nodes
DynList< Node * > in_nodes(Node *p) const noexcept
Return nodes that can reach p through incoming arcs.
Net_Graph(Net_Graph &&other) noexcept
Move-construct a network. O(1) operation.
const Flow_Type & get_cap(Arc *arc) const noexcept
Return the capacity value of an arc.
const Flow_Type & get_flow(Arc *arc) const noexcept
Return the flow value of an arc.
DynList< Arc * > in_arcs(Node *p) const noexcept
Return arcs incoming to p (as a DynList).
Node * insert_node()
Insert a new node with default info.
void unmake_super_source()
Restore a super-source network to its original multi-source form, by removing the super source that m...
void reset()
Reset all arc flows to zero.
bool check_node(Node *node) const noexcept
Return true if node satisfies flow conservation constraints.
size_t get_in_degree(Node *p) const noexcept
Return the in-degree of p (number of incoming arcs).
void swap(Net_Graph &other) noexcept
Swap contents with another network.
Arc * insert_arc(Node *src_node, Node *tgt_node, const Arc_Type &arc_info=Arc_Type())
Insert an arc with zero capacity and flow.
Residual node used by preflow-push algorithms.
PP_Res_Node()
Default constructor.
PP_Res_Node(const Empty_Class &info)
Constructor from node info.
PP_Res_Node(Empty_Class &&info)
Move constructor from node info.
Functor wrapper for random_preflow_maximum_flow().
Net::Flow_Type operator()(Net &net) const
Invoke random_preflow_maximum_flow().
Residual arc filter: keep arcs with residual capacity.
Res_F(typename Rnet::Node *) noexcept
bool operator()(typename Rnet::Arc *a) const
Arc of the residual network of the preflow-push algorithms.
__Res_Arc(const Empty_Class &info)
Constructor from arc info.
bool is_residual() const
Return true if this is a residual arc.
__Res_Arc(Empty_Class &&info)
Move constructor from arc info.
__Res_Arc()
Default constructor.
A sum of floating flows that keeps the error of its rounding, as hi + lo, by the error-free addition ...
Flow_Type value() const noexcept
The sum, rounded once.
void replace(const Flow_Type &from, const Flow_Type &to) noexcept
Replaces the term from, which the sum has, by to.
void add(const Flow_Type &x) noexcept
Adds x.
Flow_Type minus(const Flow_Sum &other) const noexcept
This sum less other, rounded once but for the errors of lo.
Flow_Type hi
The sum, rounded.
Flow_Type lo
What its rounding lost.
Node * get_curr() const
Return the current node.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.
Dynamic binary heap with node-based storage.
Dynamic doubly linked list implementation.
Dynamic stack implementation based on linked lists.
Dynamic set implementations based on hash tables.
Dynamic set implementations based on balanced binary search trees.
Path finding algorithms in graphs.
Utility algorithms and operations for graphs.
Random access queue (bag) with O(1) random pop.
Comprehensive sorting algorithms and search utilities for Aleph-w.