Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_net_sup_dem.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
96#ifndef TPL_NET_SUP_DEM_H
97#define TPL_NET_SUP_DEM_H
98
99#include <cmath>
100#include <limits>
101#include <type_traits>
102#include <utility>
103#include <tpl_net.H>
104#include <ah-errors.H>
105
106namespace Aleph
107{
108
122template <typename Node_Info, typename F_Type = long>
123class Net_Sup_Dem_Node : public Graph_Anode<Node_Info>
124{
125public:
127 using Flow_Type = F_Type;
128
131
134
136 Flow_Type out_cap = std::numeric_limits<Flow_Type>::max();
137
139 Flow_Type in_cap = std::numeric_limits<Flow_Type>::max();
140
143
146
151 [[nodiscard]] Flow_Type& get_supply_flow() noexcept { return supply_flow; }
152
157 [[nodiscard]] const Flow_Type& get_supply_flow() const noexcept
158 {
159 return supply_flow;
160 }
161
164
174 : Base(node_info)
175 {}
176
185 : Base(*node),
187 out_cap(node->out_cap),
188 in_cap(node->in_cap),
189 out_flow(node->out_flow),
190 in_flow(node->in_flow)
191 {}
192};
193
194
237template <class NodeT = Net_Sup_Dem_Node<Empty_Class, double>,
238 class ArcT = Net_Arc<Empty_Class, double>>
239class Net_Sup_Dem_Graph : public Net_Graph<NodeT, ArcT>
240{
241public:
245 using Arc = ArcT;
247 using Node = NodeT;
249 using Flow_Type = typename Arc::Flow_Type;
251 using Node_Type = typename Node::Node_Type;
253 using Arc_Type = typename Arc::Arc_Type;
256
257private:
258 Node* super_source = nullptr;
259 Node* super_sink = nullptr;
260
279 static bool filled(const Arc *a) noexcept
280 {
281 if constexpr (std::is_floating_point_v<Flow_Type>)
282 return a->flow == a->cap
283 or (std::isfinite(a->cap)
284 and std::abs(a->cap - a->flow) <= Net_Class::node_tolerance(a->cap, a->flow));
285 else
286 return a->flow == a->cap;
287 }
288
289protected:
302 void swapped() noexcept final
303 {
304 super_source = nullptr;
305 super_sink = nullptr;
306 }
307
308public:
311
313 Net_Sup_Dem_Graph() = default;
314
324 : Net_Class(other)
325 {
326 // The copy of the network builds the nodes from their infos, in the
327 // order of other
329 for (Node_Iterator<Net_Sup_Dem_Graph> jt(other); jt.has_curr();
330 it.next_ne(), jt.next_ne())
331 {
332 Node * p = it.get_curr();
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;
339 if (q == other.super_source)
340 super_source = p;
341 if (q == other.super_sink)
342 super_sink = p;
343 }
344 }
345
348 : Net_Class(),
349 super_source(std::exchange(other.super_source, nullptr)),
350 super_sink(std::exchange(other.super_sink, nullptr))
351 {
352 // not the move of Net_Graph, which tells other that its nodes went
354 }
355
364 {
365 if (this != &other)
366 {
367 Net_Sup_Dem_Graph tmp(other);
368 tmp.get_cookie() = this->get_cookie();
369 swap(tmp);
370 }
371 return *this;
372 }
373
376 {
377 if (this != &other)
378 swap(other);
379 return *this;
380 }
381
383 void swap(Net_Sup_Dem_Graph & other) noexcept
384 {
385 Net_Class::swap_contents(other); // not swap(), which forgets those of the two
386 std::swap(super_source, other.super_source);
387 std::swap(super_sink, other.super_sink);
388 }
389
391
403 void remove_node(Node *p) override
404 {
405 const bool source = p == super_source;
406 const bool sink = p == super_sink;
408 if (source)
409 super_source = nullptr;
410 if (sink)
411 super_sink = nullptr;
412 }
413
414 // Bring base class insert_node overloads into scope; those below with
415 // an info hide that of Net_Graph, which a call with an info would find
416 // as good
418
421
433 Node* insert_node(const Node_Type& node_info, const Flow_Type& supply)
434 {
435 Node* p = Net_Class::insert_node(node_info);
436 p->supply_flow = supply;
437 return p;
438 }
439
447 Node* insert_node(const Node_Type& node_info)
448 {
449 return insert_node(node_info, Flow_Type{0});
450 }
451
462 Node* insert_node(const Flow_Type& supply)
463 requires (not std::is_same_v<Node_Type, Flow_Type>)
464 {
465 return insert_node(Node_Type(), supply);
466 }
467
469
472
477 [[nodiscard]] bool exist_aux_net() const noexcept
478 {
479 return super_source != nullptr or super_sink != nullptr;
480 }
481
502 {
504 << "Auxiliary net is already computed";
505
506 super_source = Net_Class::insert_node(); // auxiliary source
507 super_sink = Net_Class::insert_node(); // auxiliary sink
508
509 // Connect super-source/sink based on supply/demand values
510 for (Node_Iterator<Net_Sup_Dem_Graph> it(*this); it.has_curr(); it.next_ne())
511 {
512 Node* p = it.get_curr();
513 if (p->supply_flow > 0) // supply node?
514 {
515 ah_range_error_if(p->out_cap < p->supply_flow)
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);
519 }
520 else if (p->supply_flow < 0) // demand node?
521 {
522 ah_range_error_if(p->in_cap < -p->supply_flow)
523 << "Supply flow in node at " << static_cast<void*>(p)
524 << " is smaller than in capacity";
525 this->insert_arc(p, super_sink, -p->supply_flow);
526 }
527 }
528
529 // Remove super-source if no supply nodes
530 if (this->get_out_degree(super_source) == 0)
531 {
532 this->remove_node(super_source);
533 super_source = nullptr;
534 }
535
536 // Remove super-sink if no demand nodes
537 if (this->get_in_degree(super_sink) == 0)
538 {
539 this->remove_node(super_sink);
540 super_sink = nullptr;
541 }
542
543 if (super_source == nullptr or super_sink == nullptr)
544 return this;
545
546 // The other sources and sinks, through arcs of capacity 0
547 DynList<Node*> sources, sinks;
548 for (auto it = this->get_src_nodes().get_it(); it.has_curr(); it.next_ne())
549 if (it.get_curr() != super_source)
550 sources.append(it.get_curr());
551 for (auto it = this->get_sink_nodes().get_it(); it.has_curr(); it.next_ne())
552 if (it.get_curr() != super_sink)
553 sinks.append(it.get_curr());
554 for (auto it = sources.get_it(); it.has_curr(); it.next_ne())
555 this->insert_arc(super_source, it.get_curr(), Flow_Type{0});
556 for (auto it = sinks.get_it(); it.has_curr(); it.next_ne())
557 this->insert_arc(it.get_curr(), super_sink, Flow_Type{0});
558
559 return this;
560 }
561
566 [[nodiscard]] Net_Sup_Dem_Graph* get_aux_net() noexcept
567 {
568 return exist_aux_net() ? this : nullptr;
569 }
570
575 [[nodiscard]] Node* get_super_source() const noexcept { return super_source; }
576
581 [[nodiscard]] Node* get_super_sink() const noexcept { return super_sink; }
582
594 {
596 << "Auxiliary net has not been computed";
597
598 if (super_source != nullptr)
599 {
600 this->remove_node(super_source);
601 super_source = nullptr;
602 }
603
604 if (super_sink != nullptr)
605 {
606 this->remove_node(super_sink);
607 super_sink = nullptr;
608 }
609 }
610
612
615
634 [[nodiscard]] bool is_feasible() const
635 {
637 << "Auxiliary net has not been computed";
638
639 if (super_source == nullptr or super_sink == nullptr)
640 return false;
641
642 for (typename Net_Class::Node_Arc_Iterator it(super_source); it.has_curr(); it.next_ne())
643 if (not filled(it.get_curr()))
644 return false; // a supply node does not ship all its supply
645
646 for (typename Net_Class::Node_Arc_Iterator it(super_sink); it.has_curr(); it.next_ne())
647 if (not filled(it.get_curr()))
648 return false; // a demand node does not receive all its demand
649
650 return true;
651 }
652
677 DynDlist<Node*>& demand_list)
678 {
680 << "Auxiliary net has not been computed";
681
682 if (super_source == nullptr or super_sink == nullptr)
683 {
684 // nothing is shipped: no supply leaves and no demand is met, also
685 // if a flow fills the arcs of the super node that there is
686 for (Node_Iterator<Net_Sup_Dem_Graph> it(*this); it.has_curr(); it.next_ne())
687 if (Node* p = it.get_curr(); p->supply_flow > 0)
688 supply_list.append(p);
689 else if (p->supply_flow < 0)
690 demand_list.append(p);
691 return;
692 }
693
694 for (typename Net_Class::Node_Arc_Iterator it(super_source); it.has_curr(); it.next_ne())
695 if (Arc* a = it.get_curr(); not filled(a))
696 supply_list.append(this->get_tgt_node(a));
697
698 for (typename Net_Class::Node_Arc_Iterator it(super_sink); it.has_curr(); it.next_ne())
699 if (Arc* a = it.get_curr(); not filled(a))
700 demand_list.append(this->get_src_node(a));
701 }
702
704
707
720 void set_supply_flow(Node* p, const Flow_Type& supply)
721 {
722 ah_range_error_if(supply > 0 and p->out_cap < supply)
723 << "Supply flow in node at " << static_cast<void*>(p)
724 << " is greater than out capacity";
725 ah_range_error_if(supply < 0 and p->in_cap < -supply)
726 << "Supply flow in node at " << static_cast<void*>(p)
727 << " is smaller than in capacity";
728
729 p->supply_flow = supply;
730 }
731
738 [[nodiscard]] Flow_Type get_supply_flow(Node* p) const noexcept
739 {
740 return p->supply_flow;
741 }
742
744
747
752 [[nodiscard]] size_t count_supply_nodes() const noexcept
753 {
754 size_t count = 0;
755 for (Node_Iterator<Net_Sup_Dem_Graph> it(*this); it.has_curr(); it.next_ne())
756 if (it.get_curr()->supply_flow > 0)
757 ++count;
758 return count;
759 }
760
765 [[nodiscard]] size_t count_demand_nodes() const noexcept
766 {
767 size_t count = 0;
768 for (Node_Iterator<Net_Sup_Dem_Graph> it(*this); it.has_curr(); it.next_ne())
769 if (it.get_curr()->supply_flow < 0)
770 ++count;
771 return count;
772 }
773
778 [[nodiscard]] Flow_Type total_supply() const noexcept
779 {
780 Flow_Type total = 0;
781 for (Node_Iterator<Net_Sup_Dem_Graph> it(*this); it.has_curr(); it.next_ne())
782 {
783 const Flow_Type& sf = it.get_curr()->supply_flow;
784 if (sf > 0)
785 total += sf;
786 }
787 return total;
788 }
789
794 [[nodiscard]] Flow_Type total_demand() const noexcept
795 {
796 Flow_Type total = 0;
797 for (Node_Iterator<Net_Sup_Dem_Graph> it(*this); it.has_curr(); it.next_ne())
798 {
799 const Flow_Type& sf = it.get_curr()->supply_flow;
800 if (sf < 0)
801 total -= sf; // Add absolute value
802 }
803 return total;
804 }
805
813 [[nodiscard]] bool is_balanced() const noexcept
814 {
815 return total_supply() == total_demand();
816 }
817
819};
820
821} // end namespace Aleph
822
823#endif // TPL_NET_SUP_DEM_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:356
string Node_Info
Iterator over the arcs of a node.
Definition tpl_agraph.H:354
bool has_curr() const noexcept
Return true if the iterator is on an arc.
Definition tpl_agraph.H:387
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).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Node of Array_Graph
Definition tpl_agraph.H:64
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.
Definition tpl_net.H:756
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.
Definition graph-dry.H:931
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
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.
Definition ahAlgo.H:127
Flow network implemented with adjacency lists.
Definition tpl_net.H:311
const DynSetTree< Node * > & get_sink_nodes() const noexcept
Return the set of sink nodes.
Definition tpl_net.H:595
const DynSetTree< Node * > & get_src_nodes() const noexcept
Return the set of source nodes.
Definition tpl_net.H:589
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.
Definition tpl_net.H:453
size_t get_out_degree(Node *p) const noexcept
Return the out-degree of p (number of outgoing arcs).
Definition tpl_net.H:393
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.
Definition tpl_net.H:799
void remove_node(Node *p) override
Remove node p and all its arcs from the network.
Definition tpl_net.H:933
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,...
Definition tpl_net.H:995
Node * insert_node()
Insert a new node with default info.
Definition tpl_net.H:756
size_t get_in_degree(Node *p) const noexcept
Return the in-degree of p (number of incoming arcs).
Definition tpl_net.H:387
Node * get_curr() const
Return the current node.
Definition graph-dry.H:522
Network flow graph structures.