96#ifndef TPL_NET_SUP_DEM_H
97#define TPL_NET_SUP_DEM_H
101#include <type_traits>
122template <
typename Node_Info,
typename F_Type =
long>
237template <
class NodeT = Net_Sup_Dem_Node<Empty_Class,
double>,
238 class ArcT = Net_Arc<Empty_Class,
double>>
281 if constexpr (std::is_floating_point_v<Flow_Type>)
282 return a->flow == a->cap
283 or (std::isfinite(a->cap)
286 return a->flow == a->cap;
330 it.next_ne(), jt.next_ne())
333 const Node * q = jt.get_curr();
334 p->supply_flow = q->supply_flow;
335 p->out_cap = q->out_cap;
336 p->in_cap = q->in_cap;
337 p->out_flow = q->out_flow;
338 p->in_flow = q->in_flow;
349 super_source(std::exchange(other.super_source,
nullptr)),
350 super_sink(std::exchange(other.super_sink,
nullptr))
436 p->supply_flow = supply;
463 requires (not std::is_same_v<Node_Type, Flow_Type>)
504 <<
"Auxiliary net is already computed";
512 Node* p = it.get_curr();
513 if (p->supply_flow > 0)
516 <<
"Supply flow in node at " <<
static_cast<void*
>(p)
517 <<
" is greater than out capacity";
518 this->
insert_arc(super_source, p, p->supply_flow);
520 else if (p->supply_flow < 0)
523 <<
"Supply flow in node at " <<
static_cast<void*
>(p)
524 <<
" is smaller than in capacity";
548 for (
auto it = this->
get_src_nodes().get_it(); it.has_curr(); it.next_ne())
550 sources.
append(it.get_curr());
551 for (
auto it = this->
get_sink_nodes().get_it(); it.has_curr(); it.next_ne())
553 sinks.
append(it.get_curr());
554 for (
auto it = sources.
get_it(); it.has_curr(); it.next_ne())
556 for (
auto it = sinks.
get_it(); it.has_curr(); it.next_ne())
596 <<
"Auxiliary net has not been computed";
637 <<
"Auxiliary net has not been computed";
643 if (not
filled(it.get_curr()))
647 if (not
filled(it.get_curr()))
680 <<
"Auxiliary net has not been computed";
687 if (
Node* p = it.get_curr(); p->supply_flow > 0)
689 else if (p->supply_flow < 0)
695 if (
Arc* a = it.get_curr(); not
filled(a))
696 supply_list.
append(this->get_tgt_node(a));
699 if (
Arc* a = it.get_curr(); not
filled(a))
700 demand_list.
append(this->get_src_node(a));
723 <<
"Supply flow in node at " <<
static_cast<void*
>(p)
724 <<
" is greater than out capacity";
726 <<
"Supply flow in node at " <<
static_cast<void*
>(p)
727 <<
" is smaller than in capacity";
729 p->supply_flow = supply;
740 return p->supply_flow;
756 if (it.get_curr()->supply_flow > 0)
769 if (it.get_curr()->supply_flow < 0)
783 const Flow_Type& sf = it.get_curr()->supply_flow;
799 const Flow_Type& sf = it.get_curr()->supply_flow;
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Iterator over the arcs of a node.
bool has_curr() const noexcept
Return true if the iterator is on an arc.
Dynamic doubly linked list with O(1) size and bidirectional access.
T & append(const T &item)
Append a copied item at the end of the list.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Network graph with supply and demand nodes.
void non_feasible_nodes(DynDlist< Node * > &supply_list, DynDlist< Node * > &demand_list)
Get lists of nodes with unsatisfied supply/demand.
typename Node::Node_Type Node_Type
Type of information stored in nodes.
void swap(Net_Sup_Dem_Graph &other) noexcept
Swaps the networks, with their super nodes. O(1).
Node * get_super_sink() const noexcept
Get the super-sink node.
Node * super_sink
Super-sink for auxiliary network.
Node * get_super_source() const noexcept
Get the super-source node.
bool exist_aux_net() const noexcept
Check if the auxiliary network has been computed.
void free_aux_net()
Free the auxiliary network structures.
bool is_feasible() const
Check if the current flow is feasible.
typename Arc::Arc_Type Arc_Type
Type of information stored in arcs.
Node * insert_node(const Node_Type &node_info)
Insert a node with info and no supply nor demand.
Node * super_source
Super-source for auxiliary network.
Flow_Type total_demand() const noexcept
Calculate total demand in the network.
Net_Sup_Dem_Graph()=default
Default constructor.
typename Arc::Flow_Type Flow_Type
Type representing capacity and flow values.
Flow_Type get_supply_flow(Node *p) const noexcept
Get the supply/demand value for a node.
bool is_balanced() const noexcept
Check if the network is balanced.
Net_Graph< NodeT, ArcT > Net_Class
The base network class type.
Net_Sup_Dem_Graph & operator=(const Net_Sup_Dem_Graph &other)
Copy assignment, as the copy constructor; the network keeps its cookie.
Net_Sup_Dem_Graph * compute_aux_net()
Compute the auxiliary capacitated network.
Net_Sup_Dem_Graph(Net_Sup_Dem_Graph &&other) noexcept
Move constructor. O(1).
size_t count_demand_nodes() const noexcept
Count the number of demand nodes.
Net_Sup_Dem_Graph(const Net_Sup_Dem_Graph &other)
Copy constructor.
size_t count_supply_nodes() const noexcept
Count the number of supply nodes.
void set_supply_flow(Node *p, const Flow_Type &supply)
Set the supply/demand value for a node.
Node * insert_node(const Node_Type &node_info, const Flow_Type &supply)
Insert a node with info and supply/demand value.
Node * insert_node(const Flow_Type &supply)
Insert a node with supply/demand value only.
void swapped() noexcept final
Forgets the super nodes: Net_Graph calls it once the nodes and the arcs of the network were exchanged...
Flow_Type total_supply() const noexcept
Calculate total supply in the network.
void remove_node(Node *p) override
Remove node p and all its arcs from the network, as Net_Graph::remove_node() does.
Node * insert_node()
Insert a new node with default info.
static bool filled(const Arc *a) noexcept
Whether the flow fills an arc of the super-source or of the super-sink: whether its node ships all it...
Net_Sup_Dem_Graph * get_aux_net() noexcept
Get the auxiliary network if it exists.
Node with supply/demand flow value.
const Flow_Type & get_supply_flow() const noexcept
Get the supply/demand flow value (const version).
Flow_Type & get_supply_flow() noexcept
Get the supply/demand flow value.
Net_Sup_Dem_Node(const Node_Info &node_info)
Construct node with info.
Flow_Type out_flow
Outgoing flow, for the user: the network does not write it.
Flow_Type in_flow
Incoming flow, for the user: the network does not write it.
Net_Sup_Dem_Node(Net_Sup_Dem_Node *node)
Copy constructor from pointer.
Flow_Type out_cap
The most that the node may supply; unlimited unless set.
Flow_Type supply_flow
Supply flow value: positive = supply, negative = demand, zero = transit.
Flow_Type in_cap
The most that the node may demand; unlimited unless set.
F_Type Flow_Type
Type representing flow values.
Net_Sup_Dem_Node()
Default constructor: no supply, unlimited capacities and no flows.
void *& get_cookie() noexcept
Return a modifiable reference to graph's cookie.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Flow network implemented with adjacency lists.
const DynSetTree< Node * > & get_sink_nodes() const noexcept
Return the set of sink nodes.
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.
size_t get_out_degree(Node *p) const noexcept
Return the out-degree of p (number of outgoing arcs).
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.
void remove_node(Node *p) override
Remove node p and all its arcs from the network.
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,...
Node * insert_node()
Insert a new node with default info.
size_t get_in_degree(Node *p) const noexcept
Return the in-degree of p (number of incoming arcs).
Node * get_curr() const
Return the current node.
Network flow graph structures.