Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_netcapgraph.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
71#ifndef TPL_NETCAPGRAPH_H
72#define TPL_NETCAPGRAPH_H
73
74#include <tpl_net.H>
75#include <ah-errors.H>
76
77namespace Aleph {
78
97template <typename Node_Info = Empty_Class, typename F_Type = double>
98class Net_Cap_Node : public Graph_Anode<Node_Info>
99{
101
102public:
104 using Flow_Type = F_Type;
105
108
111
114
117
119 Net_Cap_Node() noexcept
120 : Base(), max_cap(std::numeric_limits<Flow_Type>::max())
121 {
122 // empty
123 }
124
133 : Base(node_info),
134 max_cap(std::numeric_limits<Flow_Type>::max())
135 {
136 // empty
137 }
138
143 explicit Net_Cap_Node(Node_Info&& node_info) noexcept
144 : Base(std::move(node_info)),
145 max_cap(std::numeric_limits<Flow_Type>::max())
146 {
147 // empty
148 }
149
157 : Base(node->get_info()),
158 max_cap(node->max_cap),
159 in_flow(node->in_flow),
160 out_flow(node->out_flow)
161 {
162 // empty
163 }
164
170 : Base(other),
171 max_cap(other.max_cap),
172 in_flow(other.in_flow),
173 out_flow(other.out_flow)
174 {
175 // empty
176 }
177
184 {
185 if (this != &other)
186 {
187 Base::operator=(other);
188 max_cap = other.max_cap;
189 in_flow = other.in_flow;
190 out_flow = other.out_flow;
191 }
192 return *this;
193 }
194
199 [[nodiscard]] const Flow_Type& get_max_cap() const noexcept
200 {
201 return max_cap;
202 }
203
209 void set_max_cap(const Flow_Type& cap)
210 {
212 << "Node capacity cannot be negative";
213 max_cap = cap;
214 }
215};
216
217
275template <class NodeT = Net_Cap_Node<Empty_Class, double>,
276 class ArcT = Net_Arc<Empty_Class, double>>
277class Net_Cap_Graph : public Net_Graph<NodeT, ArcT>
278{
279public:
282
284 using Arc = ArcT;
285
287 using Node = NodeT;
288
290 using Flow_Type = typename Arc::Flow_Type;
291
293 using Node_Type = typename Node::Node_Type;
294
296 using Arc_Type = typename Arc::Arc_Type;
297
298 Node * insert_node(Node *p) override
299 {
300 return Net_Class::insert_node(p);
301 }
302
312 void remove_arc(Arc *arc) override
313 {
314 aux_stamp.stale = true; // before, since it may go and then throw
316 }
317
327 void remove_node(Node *p) override
328 {
329 aux_stamp.stale = true;
331 }
332
342 void disconnect_arc(Arc *arc) override
343 {
344 aux_stamp.stale = true; // before, as remove_arc()
346 }
347
360 Node* insert_node(const Node_Type& node_info,
361 const Flow_Type& cap = std::numeric_limits<Flow_Type>::max())
362 {
364 << "Node capacity cannot be negative";
365
366 Node* p = Net_Class::insert_node(node_info);
367 p->max_cap = cap;
368 return p;
369 }
370
379 {
380 return insert_node(Node_Type{}, cap);
381 }
382
389 {
390 return insert_node(Node_Type{}, std::numeric_limits<Flow_Type>::max());
391 }
392
412
413private:
414 Aux_Net* aux_net = nullptr;
415
416 // What compute_aux_net() saw: the auxiliary network maps to the network
417 // while no node or arc went and the numbers of nodes and of arcs are
418 // these. remove_arc(), remove_node() and disconnect_arc() are virtual,
419 // so that they mark it also when called through a base, as clear_graph()
420 // does.
422 {
423 bool stale = false;
424 size_t num_nodes = 0;
425 size_t num_arcs = 0;
427
428 // The swap(), a move or an assignment of Net_Graph, through a reference
429 // to it, exchanged the nodes and the arcs, and not the auxiliary
430 // network, which maps to those that went. Final: an override in a
431 // network derived from this one, if it did not call this, would leave
432 // the auxiliary network unmarked
433 void swapped() noexcept final { aux_stamp.stale = true; }
434
435public:
440 Net_Cap_Graph() noexcept
441 : Net_Class(), aux_net(nullptr)
442 {
443 // empty
444 }
445
454 : Net_Class(other), aux_net(nullptr)
455 {
456 // The copy of the network builds the nodes from their infos, in the
457 // order of other; aux_net is not copied, and must be recomputed
459 for (Node_Iterator<Net_Cap_Graph> jt(other); jt.has_curr(); it.next_ne(), jt.next_ne())
460 {
461 Node* p = it.get_curr();
462 const Node* q = jt.get_curr();
463 p->max_cap = q->max_cap;
464 p->in_flow = q->in_flow;
465 p->out_flow = q->out_flow;
466 }
467 }
468
473 Net_Cap_Graph(Net_Cap_Graph&& other) noexcept
474 : Net_Class(), aux_net(other.aux_net), aux_stamp(other.aux_stamp)
475 {
476 // not the move of Net_Graph, which tells other that its nodes and
477 // arcs went without its auxiliary network: here it goes with them
479 other.aux_net = nullptr;
480 }
481
491 {
492 if (this != &other)
493 {
494 Net_Cap_Graph tmp(other);
495 tmp.get_cookie() = this->get_cookie();
496 swap(tmp); // tmp frees the old auxiliary network
497 }
498 return *this;
499 }
500
510 void swap(Net_Cap_Graph& other) noexcept
511 {
512 Net_Class::swap_contents(other); // not swap(), which marks the two
513 std::swap(aux_net, other.aux_net);
514 std::swap(aux_stamp, other.aux_stamp);
515 }
516
523 {
524 if (this != &other)
525 {
526 Net_Class::swap_contents(other); // as the move of Net_Graph, unmarked
527 delete aux_net; // ~Array_Graph() frees without allocating
528 aux_net = other.aux_net;
529 aux_stamp = other.aux_stamp;
530 other.aux_net = nullptr;
531 }
532 return *this;
533 }
534
540 {
541 delete aux_net; // ~Array_Graph() frees without allocating
542 }
543
548 [[nodiscard]] Aux_Net* get_aux_net() noexcept
549 {
550 return aux_net;
551 }
552
557 [[nodiscard]] const Aux_Net* get_aux_net() const noexcept
558 {
559 return aux_net;
560 }
561
566 [[nodiscard]] bool has_aux_net() const noexcept
567 {
568 return aux_net != nullptr;
569 }
570
596 {
597 ah_domain_error_if(aux_net != nullptr)
598 << "Auxiliary network has already been computed";
599
600 aux_net = new Aux_Net;
601
602 try
603 {
604 // Phase 1: Create node pairs and capacity arcs
605 for (Node_Iterator<Net_Cap_Graph> it(*this); it.has_curr(); it.next_ne())
606 {
607 Node* p = it.get_curr();
608
609 // Create source and target nodes for this node
610 typename Aux_Net::Node* src = aux_net->insert_node();
611 typename Aux_Net::Node* tgt = aux_net->insert_node();
612
613 // Create arc representing node capacity
614 // info = true means this arc represents a node
615 typename Aux_Net::Arc* arc =
616 aux_net->insert_arc(src, tgt, p->max_cap, Flow_Type{0}, true);
617
618 // Establish bidirectional mapping
619 NODE_COOKIE(p) = arc;
620 ARC_COOKIE(arc) = p;
621 }
622
623 // Phase 2: Create arcs corresponding to original edges
624 for (Arc_Iterator<Net_Cap_Graph> it(*this); it.has_curr(); it.next_ne())
625 {
626 Arc* arc = it.get_curr();
627
628 // Get the auxiliary arcs for source and target nodes
629 typename Aux_Net::Arc* src_arc =
630 static_cast<typename Aux_Net::Arc*>(NODE_COOKIE(this->get_src_node(arc)));
631 typename Aux_Net::Arc* tgt_arc =
632 static_cast<typename Aux_Net::Arc*>(NODE_COOKIE(this->get_tgt_node(arc)));
633
634 // Connect tgt of src_arc to src of tgt_arc
635 // info = false means this arc represents an original edge
636 typename Aux_Net::Arc* a =
638 aux_net->get_src_node(tgt_arc),
639 arc->cap, arc->flow, false);
640
641 // Establish bidirectional mapping
642 ARC_COOKIE(arc) = a;
643 ARC_COOKIE(a) = arc;
644 }
645
646 aux_stamp = {false, this->get_num_nodes(), this->get_num_arcs()};
647 return aux_net;
648 }
649 catch (...)
650 {
651 // Clean up on failure
652 if (aux_net != nullptr)
653 {
655 delete aux_net;
656 aux_net = nullptr;
657 }
658 throw;
659 }
660 }
661
679 void update()
680 {
681 ah_domain_error_if(aux_net == nullptr)
682 << "Auxiliary network has not been computed";
683
684 ah_domain_error_if(aux_stamp.stale or aux_stamp.num_nodes != this->get_num_nodes()
685 or aux_stamp.num_arcs != this->get_num_arcs())
686 << "The network changed after compute_aux_net(): free its auxiliary"
687 << " network and compute it again";
688
689 for (typename Aux_Net::Arc_Iterator it(*aux_net); it.has_curr(); it.next_ne())
690 {
691 typename Aux_Net::Arc* arc = it.get_curr();
692
693 if (arc->get_info()) // This arc represents a node
694 {
695 Node* p = static_cast<Node*>(ARC_COOKIE(arc));
696 p->in_flow = arc->flow;
697 p->out_flow = arc->flow;
698 }
699 else // This arc represents an original edge
700 {
701 Arc* a = static_cast<Arc*>(ARC_COOKIE(arc));
702 a->flow = arc->flow;
703 }
704 }
705 }
706
716 {
717 ah_domain_error_if(aux_net == nullptr)
718 << "Auxiliary network has not been computed";
719
720 delete aux_net;
721 aux_net = nullptr;
722 }
723
734 [[nodiscard]] Flow_Type get_total_flow() const
735 {
736 ah_domain_error_if(aux_net == nullptr)
737 << "Auxiliary network has not been computed";
738
739 return this->flow_value();
740 }
741
749 [[nodiscard]] bool check_node_capacities() const noexcept
750 {
751 for (Node_Iterator<Net_Cap_Graph> it(*this); it.has_curr(); it.next_ne())
752 {
753 Node* p = it.get_curr();
754 if (p->in_flow > p->max_cap || p->out_flow > p->max_cap)
755 return false;
756 }
757 return true;
758 }
759
764 void reset_flows() noexcept
765 {
766 // Reset arc flows
767 for (Arc_Iterator<Net_Cap_Graph> it(*this); it.has_curr(); it.next_ne())
768 it.get_curr()->flow = Flow_Type{0};
769
770 // Reset node flows
771 for (Node_Iterator<Net_Cap_Graph> it(*this); it.has_curr(); it.next_ne())
772 {
773 Node* p = it.get_curr();
774 p->in_flow = Flow_Type{0};
775 p->out_flow = Flow_Type{0};
776 }
777 }
778
784 [[nodiscard]] static Flow_Type get_node_cap(const Node* node) noexcept
785 {
786 return node->max_cap;
787 }
788
796 static void set_node_cap(Node* node, const Flow_Type& cap)
797 {
799 << "Node capacity cannot be negative";
800 ah_overflow_error_if(node->in_flow > cap || node->out_flow > cap)
801 << "Current flow exceeds new capacity";
802 node->max_cap = cap;
803 }
804};
805
806} // end namespace Aleph
807
808#endif // TPL_NETCAPGRAPH_H
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
string Node_Info
Node of Array_Graph
Definition tpl_agraph.H:64
Graph_Anode & operator=(const Graph_Anode &node)
Definition tpl_agraph.H:109
Capacitated network with node capacity constraints.
Net_Cap_Graph & operator=(const Net_Cap_Graph &other)
Copy assignment operator.
static void set_node_cap(Node *node, const Flow_Type &cap)
Set the capacity of a node.
Net_Cap_Graph(const Net_Cap_Graph &other)
Copy constructor.
~Net_Cap_Graph()
Destructor.
bool has_aux_net() const noexcept
Check if auxiliary network has been computed.
void remove_arc(Arc *arc) override
Remove the arc arc from the network, as Net_Graph::remove_arc() does.
const Aux_Net * get_aux_net() const noexcept
Get const pointer to the auxiliary network.
Aux_Net * get_aux_net() noexcept
Get pointer to the auxiliary network.
Node * insert_node(const Node_Type &node_info, const Flow_Type &cap=std::numeric_limits< Flow_Type >::max())
Insert a new capacitated node into the network.
typename Node::Node_Type Node_Type
Type of information stored in nodes.
void swapped() noexcept final
What swap() calls on each of its two networks, once their nodes and arcs are exchanged: nothing,...
Net_Cap_Graph & operator=(Net_Cap_Graph &&other) noexcept
Move assignment operator.
struct Aleph::Net_Cap_Graph::Aux_Stamp aux_stamp
void disconnect_arc(Arc *arc) override
Disconnect the arc arc from the network without deleting it, as Net_Graph::disconnect_arc() does.
Node * insert_node(const Flow_Type &cap)
Insert a new node with default info and specified capacity.
static Flow_Type get_node_cap(const Node *node) noexcept
Get the capacity of a node.
void remove_node(Node *p) override
Remove the node p and its arcs from the network, as Net_Graph::remove_node() does.
typename Arc::Arc_Type Arc_Type
Type of information stored in arcs.
Net_Graph< Net_Node< Empty_Class >, Net_Arc< bool, Flow_Type > > Aux_Net
Auxiliary network type for solving node-capacitated networks.
Net_Cap_Graph(Net_Cap_Graph &&other) noexcept
Move constructor.
Node * insert_node()
Insert a new node with default info and unlimited capacity.
Net_Graph< NodeT, ArcT > Net_Class
Base network class type.
void free_aux_net()
Free the auxiliary network.
Flow_Type get_total_flow() const
Get the total flow value of the network.
void update()
Update flow values from auxiliary network to this network.
void swap(Net_Cap_Graph &other) noexcept
Swap contents with another network, with their auxiliary networks.
Node * insert_node(Node *p) override
bool check_node_capacities() const noexcept
Check if all node capacity constraints are satisfied.
void reset_flows() noexcept
Reset all flow values to zero.
Aux_Net * compute_aux_net()
Compute the equivalent arc-capacitated auxiliary network.
Net_Cap_Graph() noexcept
Default constructor.
typename Arc::Flow_Type Flow_Type
Type representing capacity and flow values.
Node with capacity constraint for flow networks.
F_Type Flow_Type
Type representing flow and capacity values.
Net_Cap_Node(const Net_Cap_Node &other)
Copy constructor.
void set_max_cap(const Flow_Type &cap)
Set the maximum capacity of this node.
Net_Cap_Node(Node_Info &&node_info) noexcept
Move constructor from node info.
Node_Info Node_Type
Type of information stored in the node.
Net_Cap_Node & operator=(const Net_Cap_Node &other)
Copy assignment operator.
Flow_Type in_flow
Tracked incoming flow (updated by update() after auxiliary net computation).
const Flow_Type & get_max_cap() const noexcept
Get the maximum capacity of this node.
Graph_Anode< Node_Info > Base
Flow_Type out_flow
Tracked outgoing flow (updated by update() after auxiliary net computation).
Net_Cap_Node(const Node_Info &node_info)
Construct a node with the given information.
Flow_Type max_cap
Maximum flow capacity that can pass through this node.
Net_Cap_Node() noexcept
Default constructor. Sets max_cap to the maximum possible value.
Net_Cap_Node(const Net_Cap_Node *node)
Copy constructor from another Net_Cap_Node.
NodeInfo node_info
Definition graph-dry.H:702
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
void *& get_cookie() noexcept
Return a modifiable reference to graph's cookie.
Definition graph-dry.H:931
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
__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)
Definition gmpfrxx.h:4188
#define ARC_COOKIE(p)
Return the arc cookie
#define NODE_COOKIE(p)
Return the node cookie
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:4776
Main namespace for Aleph-w library functions.
STL namespace.
Iterator over arcs of a graph.
Definition tpl_agraph.H:334
Arc of a flow network implemented with adjacency lists.
Definition tpl_net.H:160
Flow network implemented with adjacency lists.
Definition tpl_net.H:311
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
Definition tpl_net.H:750
void remove_arc(Arc *arc) override
Remove arc arc from the network.
Definition tpl_net.H:906
virtual void disconnect_arc(Arc *arc)
Disconnect arc arc from the graph without deleting it.
Definition tpl_net.H:921
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
ArcT Arc
Arc type.
Definition tpl_net.H:322
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...
Definition tpl_net.H:1139
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
NodeT Node
Node type.
Definition tpl_net.H:325
Node * insert_node()
Insert a new node with default info.
Definition tpl_net.H:756
Node * get_curr() const
Return the current node.
Definition graph-dry.H:522
Network flow graph structures.