109template <
typename Flow_Type>
141 "NODE_COOKIE not initialized; call dinic_maximum_flow() instead");
162 "NODE_COOKIE not initialized; call dinic_maximum_flow() instead");
181 "NODE_COOKIE not initialized; call dinic_maximum_flow() instead");
231 auto p = it.get_curr_ne();
232 assert(is_dinic_cookie_valid<Net>(p) &&
233 "NODE_COOKIE not initialized for Dinic's algorithm. "
234 "Call dinic_maximum_flow() instead of build_level_graph() directly, "
235 "or initialize Dinic_Node_Info for all nodes before calling.");
236 dinic_level<Net>(p) = -1;
237 dinic_blocked<Net>(p) =
false;
238 dinic_current_arc<Net>(p) = 0;
243 dinic_level<Net>(source) = 0;
254 auto arc = it.get_curr_ne();
258 if (dinic_level<Net>(
next) >= 0)
264 residual = arc->cap - arc->flow;
266 residual = arc->flow;
270 dinic_level<Net>(
next) = dinic_level<Net>(curr) + 1;
276 return dinic_level<Net>(sink) >= 0;
303 const size_t n = net.
vsize();
318 Node *curr = stack(depth - 1);
323 Flow_Type bottleneck = std::numeric_limits<Flow_Type>::max();
324 size_t bottleneck_pos = 0;
325 for (
size_t i = 0; i + 1 < depth; ++i)
328 path_fwd(i) ? (path_arcs(i)->cap - path_arcs(i)->flow) : path_arcs(i)->flow;
329 if (res < bottleneck)
338 for (
size_t i = 0; i + 1 < depth; ++i)
346 total_flow += bottleneck;
351 depth = bottleneck_pos + 1;
356 size_t &ca = dinic_current_arc<Net>(curr);
357 bool advanced =
false;
359 while (ca < curr->num_arcs)
361 Arc *arc =
static_cast<Arc *
>(curr->arc_array[ca]);
365 if (dinic_level<Net>(
next) != dinic_level<Net>(curr) + 1)
372 if (dinic_blocked<Net>(
next))
382 const bool is_forward = (net.
get_src_node(arc) == curr);
383 Flow_Type residual = is_forward ? (arc->cap - arc->flow) : arc->flow;
392 path_arcs(depth - 1) = arc;
393 path_fwd(depth - 1) = is_forward ? 1 : 0;
403 dinic_blocked<Net>(curr) =
true;
461template <FlowNetwork Net>
468 <<
"Network must have single source and single sink";
501template <FlowNetwork Net>
572template <FlowNetwork Net>
580 <<
"Network must have single source and single sink";
589 bool infinite_cap =
false;
592 const Flow_Type cap = it.get_curr()->cap;
593 if constexpr (std::numeric_limits<Flow_Type>::has_infinity)
594 if (cap == std::numeric_limits<Flow_Type>::infinity())
599 max_cap = std::max(max_cap, cap);
607 while (delta <= max_cap / 2)
616 bool found_path =
true;
627 parent[source] =
nullptr;
636 Arc *arc = it.get_curr();
646 residual = arc->cap - arc->flow;
648 residual = arc->flow;
650 if (residual >= min_residual
and residual >
Flow_Type{0})
653 parent_arc[
next] = arc;
654 is_forward[
next] = forward;
660 if (parent.
has(sink))
664 Flow_Type path_flow = std::numeric_limits<Flow_Type>::max();
665 for (
Node *n = sink; n != source; n = parent[n])
667 Arc *arc = parent_arc[n];
668 Flow_Type residual = is_forward[n] ? (arc->cap - arc->flow) : arc->flow;
669 path_flow = std::min(path_flow, residual);
672 for (
Node *n = sink; n != source; n = parent[n])
674 Arc *arc = parent_arc[n];
681 phase_flow += path_flow;
690 (void) bfs_augment(delta);
709template <FlowNetwork Net>
775 [[nodiscard]]
size_t length() const noexcept
826 for (
auto it =
paths.get_it(); it.has_curr(); it.next_ne())
827 sum += it.get_curr().flow;
901template <FlowNetwork Net>
909 <<
"Network must have single source and single sink";
917 Arc *arc = it.get_curr();
928 Arc *start_arc =
nullptr;
931 Arc *arc = it.get_curr();
939 if (start_arc ==
nullptr)
959 Arc *next_arc =
nullptr;
962 Arc *arc = it.get_curr();
970 if (next_arc ==
nullptr)
977 <<
"decompose_flow(): the flow is not conserved at a node, which receives " << in
978 <<
" and sends " <<
out;
989 const size_t cycle_start = visited[next_node];
998 for (
size_t i = 0; i < cycle_start; ++i)
1005 while (arc_it.has_curr())
1021 for (
auto it = cycle.
arcs.
get_it(); it.has_curr(); it.next_ne())
1025 result.
cycles.append(std::move(cycle));
1048 for (
auto it = path.arcs.get_it(); it.has_curr(); it.next_ne())
1050 result.
paths.append(std::move(path));
1058 Node *start = it.get_curr();
1064 Arc *first_arc =
nullptr;
1067 Arc *arc = ait.get_curr();
1075 if (first_arc ==
nullptr)
1084 path_nodes.
append(start);
1087 Node *curr = net.get_tgt_node(first_arc);
1088 path_arcs.
append(first_arc);
1091 bool found_cycle =
false;
1093 while (not found_cycle)
1098 const size_t cycle_start = visited[curr];
1099 cycle.
flow = std::numeric_limits<Flow_Type>::max();
1102 auto node_it = path_nodes.
get_it();
1103 auto arc_it = path_arcs.
get_it();
1104 for (
size_t i = 0; i < cycle_start; ++i)
1111 while (arc_it.has_curr())
1126 visited[curr] = path_nodes.
size() - 1;
1129 Arc *next_arc =
nullptr;
1132 Arc *arc = ait.get_curr();
1140 if (next_arc ==
nullptr)
1143 path_arcs.
append(next_arc);
1144 curr = net.get_tgt_node(next_arc);
1151 for (
auto cit = cycle.
arcs.
get_it(); cit.has_curr(); cit.next_ne())
1153 result.cycles.append(std::move(cycle));
1166template <FlowNetwork Net>
1197template <
typename Flow_Type,
bool = std::is_
floating_po
int_v<Flow_Type>>
1212template <
typename Flow_Type>
1217 bool in_bucket =
false;
1268template <FlowNetwork Net>
1276 <<
"Network must have single source and single sink";
1280 const size_t n_nodes = net.
vsize();
1281 const long n =
static_cast<long>(n_nodes);
1304 hlpp_height<Net>(it.get_curr_ne()) = n;
1306 hlpp_height<Net>(sink) = 0;
1316 auto arc = ait.get_curr_ne();
1319 if (
next == source or hlpp_height<Net>(
next) != n)
1327 hlpp_height<Net>(
next) = hlpp_height<Net>(curr) + 1;
1335 hlpp_height<Net>(source) = n;
1340 constexpr bool floating = std::is_floating_point_v<Flow_Type>;
1341 [[maybe_unused]]
auto info = [](
Node *p)
1345 if constexpr (floating)
1348 Arc *arc = it.get_curr_ne();
1357 [[maybe_unused]]
bool bounded =
false;
1362 if constexpr (std::numeric_limits<Flow_Type>::has_infinity)
1363 if (full == std::numeric_limits<Flow_Type>::infinity())
1370 full = std::max(bound, arc->flow);
1373 if constexpr (floating)
1375 info(source)->out.replace(arc->flow, full);
1376 info(net.
get_tgt_node(arc))->in.replace(arc->flow, full);
1380 hlpp_excess<Net>(source) -= delta;
1382 if constexpr (floating)
1385 Node *p = it.get_curr_ne();
1386 hlpp_excess<Net>(p) = info(p)->in.minus(info(p)->
out);
1391 auto in_a_bucket = [](
Node *p) ->
bool
1393 if constexpr (floating)
1396 return hlpp_excess<Net>(p) >
Flow_Type{0};
1398 auto set_in_a_bucket = [](
Node *p,
bool in)
1400 if constexpr (floating)
1403 (
void) p, (void) in;
1407 const size_t bucket_count =
static_cast<size_t>(2 * n + 1);
1409 long max_height = 0;
1414 auto p = it.get_curr_ne();
1417 long h = hlpp_height<Net>(p);
1418 buckets(
h).append(p);
1419 set_in_a_bucket(p,
true);
1420 max_height = std::max(max_height,
h);
1425 while (max_height >= 0)
1427 if (buckets(max_height).is_empty())
1433 Node *u = buckets(max_height).remove_first();
1434 set_in_a_bucket(u,
false);
1439 if constexpr (floating)
1444 bool pushed =
false;
1445 long min_height = 2 * n;
1450 Arc *arc = it.get_curr_ne();
1456 residual = arc->cap - arc->flow;
1458 residual = arc->flow;
1462 if (hlpp_height<Net>(u) == hlpp_height<Net>(v) + 1)
1466 [[maybe_unused]]
const Flow_Type delta = std::min(hlpp_excess<Net>(u), residual);
1467 if constexpr (floating)
1475 info(v)->in.replace(before, arc->flow);
1477 info(v)->out.replace(before, arc->flow);
1478 hlpp_excess<Net>(u) = info(u)->in.minus(info(u)->
out);
1480 else if (is_forward)
1485 const bool was_inactive = (v != source
and v != sink
and not in_a_bucket(v));
1487 if constexpr (floating)
1488 hlpp_excess<Net>(v) = info(v)->in.minus(info(v)->
out);
1491 hlpp_excess<Net>(u) -= delta;
1492 hlpp_excess<Net>(v) += delta;
1495 if (was_inactive
and hlpp_excess<Net>(v) >
Flow_Type{0})
1497 buckets(hlpp_height<Net>(v)).append(v);
1498 set_in_a_bucket(v,
true);
1499 max_height = std::max(max_height, hlpp_height<Net>(v));
1505 min_height = std::min(min_height, hlpp_height<Net>(v));
1509 if (hlpp_excess<Net>(u) > residue)
1513 if (min_height >= 2 * n)
1515 hlpp_height<Net>(u) = min_height + 1;
1517 buckets(hlpp_height<Net>(u)).append(u);
1518 set_in_a_bucket(u,
true);
1519 max_height = std::max(max_height, hlpp_height<Net>(u));
1532template <FlowNetwork Net>
1562template <
typename Flow_Type>
1584 std::ostringstream oss;
1585 oss <<
"=== Flow Statistics ===\n"
1591 <<
"Utilization: " << std::fixed << std::setprecision(2) << (
utilization * 100) <<
"%\n";
1631 auto arc = it.get_curr();
1634 if (arc->flow == arc->cap)
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
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.
Simple dynamic array with automatic resizing and functional operations.
static Array create(const size_t n)
Create an array with n logical elements.
RAII guard that saves and restores graph cookies.
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.
T & front()
Return a modifiable reference to the oldest item in the queue.
bool is_empty() const noexcept
Return true if this is empty.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
T & get_last()
Return the last item of the list.
size_t size() const noexcept
Return the number of items of the list, in constant time.
Generic key-value map implemented on top of a binary search tree.
bool contains(const Key &key) const noexcept(nothrow_search)
Same as has().
bool has(const Key &key) const noexcept(nothrow_search)
Return whether key is in the map.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
constexpr bool is_empty() const noexcept
Filtered iterator on the nodes of a graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
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().
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
RAII guards for graph node/arc cookies.
#define NODE_COOKIE(p)
Return the node cookie
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 subtract_flow(Arc *arc, const Flow_Type &slack) noexcept
Net::Flow_Type infinite_arc_preflow(const Net &net)
Main namespace for Aleph-w library functions.
Net::Flow_Type remaining_flow(typename Net::Node *src, typename Net::Arc *a) noexcept
Return the remaining flow of a as seen from src.
FlowStatistics< typename Net::Flow_Type > compute_flow_statistics(const Net &net)
Compute statistics about the current flow in a network.
bool is_dinic_cookie_valid(typename Net::Node *p) noexcept
Check if a node's cookie is properly initialized for Dinic's algorithm.
Net::Flow_Type dinic_maximum_flow(Net &net)
Compute maximum flow using Dinic's algorithm.
long & hlpp_height(typename Net::Node *p) noexcept
Access the height label stored in the node's cookie.
bool build_level_graph(Net &net, typename Net::Node *source, typename Net::Node *sink)
Build level graph using BFS from source.
long & dinic_level(typename Net::Node *p) noexcept
Access the node level in Dinic's algorithm.
FlowDecomposition< Net > decompose_flow(const Net &net)
Decompose network flow into paths and cycles.
Net::Flow_Type capacity_scaling_maximum_flow(Net &net)
Compute maximum flow using capacity scaling.
bool & dinic_blocked(typename Net::Node *p) noexcept
Check if a node is blocked in Dinic's algorithm.
size_t & dinic_current_arc(typename Net::Node *p) noexcept
Access the current arc index in Dinic's algorithm.
Net::Flow_Type hlpp_maximum_flow(Net &net)
Compute maximum flow using Highest-Label Preflow-Push.
and
Check uniqueness with explicit hash + equality functors.
Net::Flow_Type & hlpp_excess(typename Net::Node *p) noexcept
Access the excess flow stored in the node's cookie.
void next()
Advance all underlying iterators (bounds-checked).
Net::Flow_Type dinic_blocking_flow(Net &net, typename Net::Node *source, typename Net::Node *sink)
Find all blocking flows using iterative DFS with current-arc optimization.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Filtered iterator on all the arcs of a graph.
Functor wrapper for capacity scaling.
Net::Flow_Type operator()(Net &net) const
Invoke capacity scaling maximum flow algorithm on the given network.
Functor wrapper for flow decomposition.
FlowDecomposition< Net > operator()(const Net &net) const
Invoke flow decomposition on the given network.
Functor wrapper for Dinic's algorithm.
Net::Flow_Type operator()(Net &net) const
Invoke Dinic's maximum flow algorithm on the given network.
Level information for Dinic's algorithm.
size_t current_arc
Current arc index for current-arc optimization.
bool blocked
Whether the node is blocked in the current phase.
long level
BFS level from source (-1 = unreachable)
Represents a flow cycle in the network.
typename Net::Flow_Type Flow_Type
Flow_Type flow
Flow on this cycle.
DynList< Node * > nodes
Nodes in the cycle.
DynList< Arc * > arcs
Arcs in the cycle.
Result of flow decomposition.
DynList< FlowCycle< Net > > cycles
Cycles (if any)
typename Net::Flow_Type Flow_Type
Flow_Type total_flow() const noexcept
Total flow (sum of path flows).
size_t num_paths() const noexcept
Number of paths.
size_t num_cycles() const noexcept
Number of cycles.
DynList< FlowPath< Net > > paths
Source-to-sink paths.
Represents a flow path from source to sink.
DynList< Node * > nodes
Nodes in the path.
bool is_empty() const noexcept
Check if a path is empty.
Flow_Type flow
Flow on this path.
typename Net::Flow_Type Flow_Type
size_t length() const noexcept
Get path length (number of arcs).
DynList< Arc * > arcs
Arcs in the path (source to sink order)
Statistics about a network flow.
std::string to_string() const
Format statistics as a string.
size_t num_saturated_arcs
Arcs with flow = capacity.
size_t num_empty_arcs
Arcs with flow = 0.
Flow_Type total_capacity
Sum of all capacities.
Flow_Type total_flow
Total flow value.
void print() const
Print statistics to standard output.
size_t num_partial_arcs
Arcs with 0 < flow < capacity.
double utilization
flow / capacity ratio
Functor wrapper for HLPP.
Net::Flow_Type operator()(Net &net) const
Invoke the Highest-Label Preflow-Push algorithm on the given network.
net_detail::Flow_Sum< Flow_Type > in
The flow into the node.
net_detail::Flow_Sum< Flow_Type > out
The flow out of the node.
Node info for HLPP algorithm.
Flow network implemented with adjacency lists.
Flow_Type get_out_flow(Node *node) const noexcept
Return total outgoing flow of node.
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.
constexpr bool is_single_source() const noexcept
Return true if the network has a single source.
Node * get_source() const
Return an arbitrary source 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.
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...
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.
A sum of floating flows that keeps the error of its rounding, as hi + lo, by the error-free addition ...
Dynamic array container with automatic resizing.
Dynamic doubly linked list implementation.
Dynamic queue implementation based on linked lists.
Network flow graph structures.