Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
net_utils.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
78#ifndef NET_UTILS_H
79#define NET_UTILS_H
80
81#include <fstream>
82#include <sstream>
83#include <random>
84#include <chrono>
85#include <iomanip>
86#include <tpl_net.H>
87#include <tpl_netcost.H>
88#include <tpl_dynMapTree.H>
89#include <tpl_array.H>
90
91namespace Aleph
92{
93
94//==============================================================================
95// NETWORK GENERATORS
96//==============================================================================
97
102{
103 size_t num_nodes = 10;
104 size_t num_arcs = 20;
105 double min_capacity = 1.0;
106 double max_capacity = 100.0;
107 double min_cost = 0.0;
108 double max_cost = 10.0;
109 bool ensure_connected = true;
110 unsigned seed = 0;
111};
112
134template <class Net>
136{
137 using Node = typename Net::Node;
138 using Flow_Type = typename Net::Flow_Type;
139
140 Net net;
141
142 // Initialize RNG
143 unsigned seed = params.seed;
144 if (seed == 0)
145 seed = static_cast<unsigned>(
146 std::chrono::system_clock::now().time_since_epoch().count());
147
148 std::mt19937 gen(seed);
149 std::uniform_real_distribution<double> cap_dist(params.min_capacity,
150 params.max_capacity);
151 std::uniform_int_distribution<size_t> node_dist(0, params.num_nodes - 1);
152
153 // Create nodes
154 std::vector<Node*> nodes(params.num_nodes);
155 for (size_t i = 0; i < params.num_nodes; ++i)
156 nodes[i] = net.insert_node();
157
158 // Set source and sink
159 // Sources and sinks are detected automatically by Net_Graph
160 // based on which nodes have no incoming/outgoing arcs
161
162 // Ensure connectivity: create a path from source to sink
163 if (params.ensure_connected)
164 {
165 for (size_t i = 0; i < params.num_nodes - 1; ++i)
166 {
167 Flow_Type cap = static_cast<Flow_Type>(cap_dist(gen));
168 net.insert_arc(nodes[i], nodes[i + 1], cap);
169 }
170 }
171
172 // Add random arcs
173 size_t arcs_to_add = params.num_arcs;
174 if (params.ensure_connected)
175 arcs_to_add -= (params.num_nodes - 1);
176
177 for (size_t i = 0; i < arcs_to_add; ++i)
178 {
179 size_t src = node_dist(gen);
180 size_t tgt = node_dist(gen);
181
182 // Avoid self-loops
183 if (src == tgt)
184 {
185 tgt = (tgt + 1) % params.num_nodes;
186 }
187
188 Flow_Type cap = static_cast<Flow_Type>(cap_dist(gen));
189 net.insert_arc(nodes[src], nodes[tgt], cap);
190 }
191
192 return net;
193}
194
207template <class Net>
208Net generate_random_network(size_t num_nodes, size_t num_arcs,
209 double min_cap = 1.0, double max_cap = 100.0,
210 unsigned seed = 0)
211{
212 NetworkGenParams params;
213 params.num_nodes = num_nodes;
214 params.num_arcs = num_arcs;
215 params.min_capacity = min_cap;
216 params.max_capacity = max_cap;
217 params.seed = seed;
218 return generate_random_network<Net>(params);
219}
220
244template <class Net>
246 typename Net::Flow_Type capacity,
247 bool bidirectional = true)
248{
249 using Node = typename Net::Node;
250
251 Net net;
252 std::vector<std::vector<Node*>> nodes(rows, std::vector<Node*>(cols));
253
254 // Create nodes
255 for (size_t i = 0; i < rows; ++i)
256 for (size_t j = 0; j < cols; ++j)
257 nodes[i][j] = net.insert_node();
258
259 // Create horizontal arcs
260 for (size_t i = 0; i < rows; ++i)
261 for (size_t j = 0; j < cols - 1; ++j)
262 {
263 net.insert_arc(nodes[i][j], nodes[i][j+1], capacity);
264 if (bidirectional)
265 net.insert_arc(nodes[i][j+1], nodes[i][j], capacity);
266 }
267
268 // Create vertical arcs
269 for (size_t i = 0; i < rows - 1; ++i)
270 for (size_t j = 0; j < cols; ++j)
271 {
272 net.insert_arc(nodes[i][j], nodes[i+1][j], capacity);
273 if (bidirectional)
274 net.insert_arc(nodes[i+1][j], nodes[i][j], capacity);
275 }
276
277 // Source (top-left) and sink (bottom-right) are detected automatically
278 // since nodes[0][0] has no incoming arcs and nodes[rows-1][cols-1] has no outgoing arcs
279
280 return net;
281}
282
301template <class Net>
302Net generate_bipartite_network(size_t left_size, size_t right_size,
303 double edge_prob = 0.5,
304 unsigned seed = 0)
305{
306 using Node = typename Net::Node;
307 using Flow_Type = typename Net::Flow_Type;
308
309 Net net;
310
311 if (seed == 0)
312 seed = static_cast<unsigned>(
313 std::chrono::system_clock::now().time_since_epoch().count());
314
315 std::mt19937 gen(seed);
316 std::uniform_real_distribution<double> prob_dist(0.0, 1.0);
317
318 // Create source and sink
319 Node* source = net.insert_node();
320 Node* sink = net.insert_node();
321
322 // Create left nodes
323 std::vector<Node*> left_nodes(left_size);
324 for (size_t i = 0; i < left_size; ++i)
325 {
326 left_nodes[i] = net.insert_node();
327 net.insert_arc(source, left_nodes[i], Flow_Type{1});
328 }
329
330 // Create right nodes
331 std::vector<Node*> right_nodes(right_size);
332 for (size_t i = 0; i < right_size; ++i)
333 {
334 right_nodes[i] = net.insert_node();
335 net.insert_arc(right_nodes[i], sink, Flow_Type{1});
336 }
337
338 // Create random edges between left and right
339 for (size_t i = 0; i < left_size; ++i)
340 for (size_t j = 0; j < right_size; ++j)
341 if (prob_dist(gen) < edge_prob)
342 net.insert_arc(left_nodes[i], right_nodes[j], Flow_Type{1});
343
344 // Source and sink are detected automatically
345
346 return net;
347}
348
363template <class Net>
364Net generate_layered_network(const std::vector<size_t>& layer_sizes,
365 typename Net::Flow_Type capacity,
366 double edge_prob = 0.5,
367 unsigned seed = 0)
368{
369 using Node = typename Net::Node;
370
371 Net net;
372
373 if (layer_sizes.size() < 2)
374 return net;
375
376 for (size_t l = 1; l + 1 < layer_sizes.size(); ++l)
377 ah_invalid_argument_if(layer_sizes[l] == 0)
378 << "Intermediate layers must be non-empty";
379
380
381 if (seed == 0)
382 seed = static_cast<unsigned>(
383 std::chrono::system_clock::now().time_since_epoch().count());
384
385 std::mt19937 gen(seed);
386 std::uniform_real_distribution<double> prob_dist(0.0, 1.0);
387
388 // Create nodes for each layer
389 std::vector<std::vector<Node*>> layers(layer_sizes.size());
390 for (size_t l = 0; l < layer_sizes.size(); ++l)
391 {
392 layers[l].resize(layer_sizes[l]);
393 for (size_t i = 0; i < layer_sizes[l]; ++i)
394 layers[l][i] = net.insert_node();
395 }
396
397 // Connect adjacent layers
398 for (size_t l = 0; l < layer_sizes.size() - 1; ++l)
399 {
400 // Track which nodes in layer l+1 received at least one incoming arc
401 // so we can guarantee no intermediate node has in-degree 0 (which
402 // would make it an extra source and break is_single_source()).
403 std::vector<bool> reached(layers[l+1].size(), false);
404
405 for (size_t i = 0; i < layers[l].size(); ++i)
406 {
407 bool connected = false;
408 for (size_t j = 0; j < layers[l+1].size(); ++j)
409 if (prob_dist(gen) < edge_prob)
410 {
411 net.insert_arc(layers[l][i], layers[l+1][j], capacity);
412 connected = true;
413 reached[j] = true;
414 }
415
416 // Ensure every node in layer l has at least one outgoing arc
417 if (not connected and not layers[l+1].empty())
418 {
419 size_t j = gen() % layers[l+1].size();
420 net.insert_arc(layers[l][i], layers[l+1][j], capacity);
421 reached[j] = true;
422 }
423 }
424
425 // Ensure every node in layer l+1 has at least one incoming arc;
426 // otherwise it would be an extra source and fail is_single_source().
427 if (not layers[l].empty())
428 for (size_t j = 0; j < layers[l+1].size(); ++j)
429 if (not reached[j])
430 {
431 size_t i = gen() % layers[l].size();
432 net.insert_arc(layers[l][i], layers[l+1][j], capacity);
433 }
434 }
435
436 // Source and sink are detected automatically
437
438 return net;
439}
440
441
442//==============================================================================
443// DOT/GRAPHVIZ EXPORT
444//==============================================================================
445
450{
451 bool show_flow = true;
452 bool show_capacity = true;
453 bool show_cost = false;
455 bool highlight_empty = false;
456 bool use_colors = true;
457 std::string graph_name = "Network";
458 std::string source_color = "green";
459 std::string sink_color = "red";
460 std::string saturated_color = "blue";
461 std::string empty_color = "gray";
462};
463
484template <class Net>
485void export_network_to_dot(const Net& net, const std::string& filename,
487{
488 using Node = typename Net::Node;
489
490 std::ofstream out(filename);
491 if (not out)
492 return;
493
494 // Build node ID map
496 size_t id = 0;
497 for (Node_Iterator<Net> it(net); it.has_curr(); it.next_ne())
498 node_ids[it.get_curr()] = id++;
499
500 out << "digraph " << options.graph_name << " {\n";
501 out << " rankdir=LR;\n";
502 out << " node [shape=circle];\n";
503
504 // Export nodes
505 for (Node_Iterator<Net> it(net); it.has_curr(); it.next_ne())
506 {
507 Node* p = it.get_curr();
508 out << " " << node_ids[p];
509
510 std::string color;
511 if (net.is_single_source() and p == net.get_source())
512 color = options.source_color;
513 else if (net.is_single_sink() and p == net.get_sink())
514 color = options.sink_color;
515
516 if (not color.empty() and options.use_colors)
517 out << " [style=filled, fillcolor=" << color << "]";
518
519 out << ";\n";
520 }
521
522 // Export arcs
523 for (Arc_Iterator<Net> it(net); it.has_curr(); it.next_ne())
524 {
525 auto arc = it.get_curr();
526 Node* src = net.get_src_node(arc);
527 Node* tgt = net.get_tgt_node(arc);
528
529 out << " " << node_ids[src] << " -> " << node_ids[tgt];
530
531 // Build label
532 std::ostringstream label;
533 if (options.show_flow or options.show_capacity)
534 {
535 label << "\"";
536 if (options.show_flow)
537 label << arc->flow;
538 if (options.show_flow and options.show_capacity)
539 label << "/";
540 if (options.show_capacity)
541 label << arc->cap;
542 label << "\"";
543 }
544
545 // Build style
546 std::string color;
547 if (options.use_colors)
548 {
549 if (options.highlight_saturated and arc->flow == arc->cap)
550 color = options.saturated_color;
551 else if (options.highlight_empty and arc->flow == 0)
552 color = options.empty_color;
553 }
554
555 out << " [";
556 if (not label.str().empty())
557 out << "label=" << label.str();
558 if (not color.empty())
559 {
560 if (not label.str().empty())
561 out << ", ";
562 out << "color=" << color;
563 }
564 out << "];\n";
565 }
566
567 out << "}\n";
568}
569
579template <class Net>
580std::string network_to_dot_string(const Net& net,
582{
583 using Node = typename Net::Node;
584
585 std::ostringstream out;
586
587 // Build node ID map
589 size_t id = 0;
590 for (Node_Iterator<Net> it(net); it.has_curr(); it.next_ne())
591 node_ids[it.get_curr()] = id++;
592
593 out << "digraph " << options.graph_name << " {\n";
594 out << " rankdir=LR;\n";
595 out << " node [shape=circle];\n";
596
597 // Export nodes
598 for (Node_Iterator<Net> it(net); it.has_curr(); it.next_ne())
599 {
600 Node* p = it.get_curr();
601 out << " " << node_ids[p];
602
603 std::string color;
604 if (net.is_single_source() and p == net.get_source())
605 color = options.source_color;
606 else if (net.is_single_sink() and p == net.get_sink())
607 color = options.sink_color;
608
609 if (not color.empty() and options.use_colors)
610 out << " [style=filled, fillcolor=" << color << "]";
611
612 out << ";\n";
613 }
614
615 // Export arcs
616 for (Arc_Iterator<Net> it(net); it.has_curr(); it.next_ne())
617 {
618 auto arc = it.get_curr();
619 Node* src = net.get_src_node(arc);
620 Node* tgt = net.get_tgt_node(arc);
621
622 out << " " << node_ids[src] << " -> " << node_ids[tgt];
623
624 std::ostringstream label;
625 if (options.show_flow or options.show_capacity)
626 {
627 label << "\"";
628 if (options.show_flow)
629 label << arc->flow;
630 if (options.show_flow and options.show_capacity)
631 label << "/";
632 if (options.show_capacity)
633 label << arc->cap;
634 label << "\"";
635 }
636
637 std::string color;
638 if (options.use_colors)
639 {
640 if (options.highlight_saturated and arc->flow == arc->cap)
641 color = options.saturated_color;
642 else if (options.highlight_empty and arc->flow == 0)
643 color = options.empty_color;
644 }
645
646 out << " [";
647 if (not label.str().empty())
648 out << "label=" << label.str();
649 if (not color.empty())
650 {
651 if (not label.str().empty())
652 out << ", ";
653 out << "color=" << color;
654 }
655 out << "];\n";
656 }
657
658 out << "}\n";
659
660 return out.str();
661}
662
663
664//==============================================================================
665// JSON SERIALIZATION
666//==============================================================================
667
694template <class Net>
695void export_network_to_json(const Net& net, const std::string& filename)
696{
697 using Node = typename Net::Node;
698
699 std::ofstream out(filename);
700 if (not out)
701 return;
702
703 // Build node ID map
705 size_t id = 0;
706 for (Node_Iterator<Net> it(net); it.has_curr(); it.next_ne())
707 node_ids[it.get_curr()] = id++;
708
709 out << "{\n";
710
711 // Nodes
712 out << " \"num_nodes\": " << net.vsize() << ",\n";
713 out << " \"num_arcs\": " << net.esize() << ",\n";
714
715 // Source and sink
716 if (net.is_single_source())
717 out << " \"source\": " << node_ids[net.get_source()] << ",\n";
718 if (net.is_single_sink())
719 out << " \"sink\": " << node_ids[net.get_sink()] << ",\n";
720
721 // Arcs
722 out << " \"arcs\": [\n";
723 bool first = true;
724 for (Arc_Iterator<Net> it(net); it.has_curr(); it.next_ne())
725 {
726 if (not first)
727 out << ",\n";
728 first = false;
729
730 auto arc = it.get_curr();
731 out << " {"
732 << "\"src\": " << node_ids[net.get_src_node(arc)] << ", "
733 << "\"tgt\": " << node_ids[net.get_tgt_node(arc)] << ", "
734 << "\"cap\": " << arc->cap << ", "
735 << "\"flow\": " << arc->flow
736 << "}";
737 }
738 out << "\n ]\n";
739 out << "}\n";
740}
741
750template <class Net>
751std::string network_to_json_string(const Net& net)
752{
753 using Node = typename Net::Node;
754
755 std::ostringstream out;
756
757 // Build node ID map
759 size_t id = 0;
760 for (Node_Iterator<Net> it(net); it.has_curr(); it.next_ne())
761 node_ids[it.get_curr()] = id++;
762
763 out << "{\n";
764 out << " \"num_nodes\": " << net.vsize() << ",\n";
765 out << " \"num_arcs\": " << net.esize() << ",\n";
766
767 if (net.is_single_source())
768 out << " \"source\": " << node_ids[net.get_source()] << ",\n";
769 if (net.is_single_sink())
770 out << " \"sink\": " << node_ids[net.get_sink()] << ",\n";
771
772 out << " \"arcs\": [\n";
773 bool first = true;
774 for (Arc_Iterator<Net> it(net); it.has_curr(); it.next_ne())
775 {
776 if (not first)
777 out << ",\n";
778 first = false;
779
780 auto arc = it.get_curr();
781 out << " {"
782 << "\"src\": " << node_ids[net.get_src_node(arc)] << ", "
783 << "\"tgt\": " << node_ids[net.get_tgt_node(arc)] << ", "
784 << "\"cap\": " << arc->cap << ", "
785 << "\"flow\": " << arc->flow
786 << "}";
787 }
788 out << "\n ]\n";
789 out << "}\n";
790
791 return out.str();
792}
793
794
795//==============================================================================
796// DIMACS FORMAT
797//==============================================================================
798
820template <class Net>
821void export_network_to_dimacs(const Net& net, const std::string& filename)
822{
823 using Node = typename Net::Node;
824
825 std::ofstream out(filename);
826 if (not out)
827 return;
828
829 // Build node ID map (1-based for DIMACS)
831 size_t id = 1; // DIMACS is 1-indexed
832 for (Node_Iterator<Net> it(net); it.has_curr(); it.next_ne())
833 node_ids[it.get_curr()] = id++;
834
835 // Header
836 out << "c Network exported from Aleph-w\n";
837 out << "p max " << net.vsize() << " " << net.esize() << "\n";
838
839 // Source and sink
840 if (net.is_single_source())
841 out << "n " << node_ids[net.get_source()] << " s\n";
842 if (net.is_single_sink())
843 out << "n " << node_ids[net.get_sink()] << " t\n";
844
845 // Arcs
846 for (Arc_Iterator<Net> it(net); it.has_curr(); it.next_ne())
847 {
848 auto arc = it.get_curr();
849 out << "a " << node_ids[net.get_src_node(arc)] << " "
850 << node_ids[net.get_tgt_node(arc)] << " "
851 << static_cast<long long>(arc->cap) << "\n";
852 }
853}
854
868template <class Net>
869Net import_network_from_dimacs(const std::string& filename)
870{
871 using Node = typename Net::Node;
872 using Flow_Type = typename Net::Flow_Type;
873
874 std::ifstream in(filename);
875 ah_runtime_error_if(not in) << "Cannot open file: " << filename;
876
877 // The node numbered i is nodes(i - 1). Every read and every number is
878 // checked: a malformed line left its fields unset, and a number out of
879 // range indexed past the nodes
880 Net net;
882 bool has_problem = false;
883 size_t line_number = 0;
884 auto node_of = [&](const size_t id)
885 {
886 ah_runtime_error_if(id == 0 or id > nodes.size())
887 << filename << ":" << line_number << ": node " << id
888 << " is not in [1, " << nodes.size() << "]";
889 return nodes(id - 1);
890 };
891
892 std::string line;
893 while (std::getline(in, line))
894 {
895 ++line_number;
896 std::istringstream iss(line);
897 char type = 0;
898 if (not (iss >> type) or type == 'c') // a blank line or a comment
899 continue;
900
901 if (type == 'p')
902 {
903 std::string problem;
904 size_t n = 0, m = 0;
905 ah_runtime_error_if(has_problem)
906 << filename << ":" << line_number << ": a second problem line";
907 ah_runtime_error_if(not (iss >> problem >> n >> m))
908 << filename << ":" << line_number << ": malformed problem line";
909 nodes.reserve(n);
910 for (size_t i = 0; i < n; ++i)
911 nodes.append(net.insert_node());
912 has_problem = true;
913 }
914 else if (type == 'n' or type == 'a')
915 {
916 ah_runtime_error_if(not has_problem)
917 << filename << ":" << line_number << ": a '" << type
918 << "' line before the problem line";
919 if (type == 'n')
920 {
921 size_t id = 0;
922 char designator = 0;
923 ah_runtime_error_if(not (iss >> id >> designator)
924 or (designator != 's' and designator != 't'))
925 << filename << ":" << line_number << ": malformed node line";
926 (void) node_of(id); // Net_Graph finds the source and the sink by topology
927 }
928 else
929 {
930 size_t src = 0, tgt = 0;
931 long long cap = 0;
932 ah_runtime_error_if(not (iss >> src >> tgt >> cap))
933 << filename << ":" << line_number << ": malformed arc line";
934 net.insert_arc(node_of(src), node_of(tgt), static_cast<Flow_Type>(cap));
935 }
936 }
937 }
938
939 return net;
940}
941
942
943//==============================================================================
944// BENCHMARKING UTILITIES
945//==============================================================================
946
950template <typename Flow_Type>
957
969template <class Net, class Algo>
971benchmark_maxflow(Net& net, Algo algo, const std::string& name)
972{
974 result.algorithm_name = name;
975
976 auto start = std::chrono::high_resolution_clock::now();
977 result.flow_value = algo(net);
978 auto end = std::chrono::high_resolution_clock::now();
979
980 result.elapsed_ms = std::chrono::duration<double, std::milli>(
981 end - start).count();
982
983 return result;
984}
985
989template <typename Flow_Type>
991 const std::vector<MaxFlowBenchmarkResult<Flow_Type>>& results)
992{
993 std::cout << "\n=== Max-Flow Algorithm Benchmark ===\n";
994 std::cout << std::left << std::setw(25) << "Algorithm"
995 << std::setw(15) << "Flow"
996 << std::setw(15) << "Time (ms)" << "\n";
997 std::cout << std::string(55, '-') << "\n";
998
999 for (const auto& r : results)
1000 std::cout << std::left << std::setw(25) << r.algorithm_name
1001 << std::setw(15) << r.flow_value
1002 << std::setw(15) << std::fixed << std::setprecision(3)
1003 << r.elapsed_ms << "\n";
1004}
1005
1006} // namespace Aleph
1007
1008#endif // NET_UTILS_H
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:415
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
WeightedDigraph::Node Node
int num_nodes
Definition btreepic.C:410
size_t size_t int32_t * out
Definition ca-c-api.h:120
size_t * rows
Definition ca-c-api.h:112
size_t cols
Definition ca-c-api.h:105
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
Generic key-value map implemented on top of a binary search tree.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1287
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
constexpr size_t vsize() const noexcept
See get_num_nodes().
Definition graph-dry.H:984
size_t esize() const noexcept
Return the total of arcs of graph.
Definition graph-dry.H:1078
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
Key * append(const Key &key)
Alias for insert() (copy version).
Definition hashDry.H:733
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
double Flow_Type
Main namespace for Aleph-w library functions.
Net import_network_from_dimacs(const std::string &filename)
Import network from DIMACS max-flow format.
Definition net_utils.H:869
void export_network_to_dimacs(const Net &net, const std::string &filename)
Export network to DIMACS max-flow format.
Definition net_utils.H:821
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
void export_network_to_dot(const Net &net, const std::string &filename, const DotExportOptions &options=DotExportOptions())
Export network to DOT format for GraphViz visualization.
Definition net_utils.H:485
Net generate_random_network(const NetworkGenParams &params)
Generate a random flow network.
Definition net_utils.H:135
and
Check uniqueness with explicit hash + equality functors.
Net generate_bipartite_network(size_t left_size, size_t right_size, double edge_prob=0.5, unsigned seed=0)
Generate a bipartite network for matching.
Definition net_utils.H:302
void export_network_to_json(const Net &net, const std::string &filename)
Export network to JSON format.
Definition net_utils.H:695
std::string network_to_json_string(const Net &net)
Export network to JSON string.
Definition net_utils.H:751
MaxFlowBenchmarkResult< typename Net::Flow_Type > benchmark_maxflow(Net &net, Algo algo, const std::string &name)
Run and time a max-flow algorithm.
Definition net_utils.H:971
Net generate_grid_network(size_t rows, size_t cols, typename Net::Flow_Type capacity, bool bidirectional=true)
Generate a grid network.
Definition net_utils.H:245
void print_benchmark_results(const std::vector< MaxFlowBenchmarkResult< Flow_Type > > &results)
Print benchmark results.
Definition net_utils.H:990
std::string network_to_dot_string(const Net &net, const DotExportOptions &options=DotExportOptions())
Generate DOT string for network (instead of file).
Definition net_utils.H:580
Net generate_layered_network(const std::vector< size_t > &layer_sizes, typename Net::Flow_Type capacity, double edge_prob=0.5, unsigned seed=0)
Generate a layered network.
Definition net_utils.H:364
static long & color(typename GT::Node *p)
static struct argp_option options[]
Definition ntreepic.C:1890
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Options for DOT export.
Definition net_utils.H:450
bool highlight_saturated
Highlight saturated arcs.
Definition net_utils.H:454
std::string source_color
Definition net_utils.H:458
std::string empty_color
Definition net_utils.H:461
bool use_colors
Use colors for flow visualization.
Definition net_utils.H:456
bool show_capacity
Show capacity values on arcs.
Definition net_utils.H:452
bool show_flow
Show flow values on arcs.
Definition net_utils.H:451
bool highlight_empty
Highlight empty arcs.
Definition net_utils.H:455
std::string saturated_color
Definition net_utils.H:460
bool show_cost
Show cost values (for cost networks)
Definition net_utils.H:453
Result of running a max-flow algorithm.
Definition net_utils.H:952
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
constexpr bool is_single_source() const noexcept
Return true if the network has a single source.
Definition tpl_net.H:429
Node * get_source() const
Return an arbitrary source node.
Definition tpl_net.H:733
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
Node * get_sink() const
Return an arbitrary sink node.
Definition tpl_net.H:739
typename Arc::Flow_Type Flow_Type
Capacity/flow numeric type.
Definition tpl_net.H:328
constexpr bool is_single_sink() const noexcept
Return true if the network has a single sink.
Definition tpl_net.H:435
NodeT Node
Node type.
Definition tpl_net.H:325
Parameters for random network generation.
Definition net_utils.H:102
double max_capacity
Maximum arc capacity.
Definition net_utils.H:106
size_t num_arcs
Target number of arcs.
Definition net_utils.H:104
bool ensure_connected
Ensure source can reach sink.
Definition net_utils.H:109
double min_cost
Minimum arc cost (for cost networks)
Definition net_utils.H:107
double min_capacity
Minimum arc capacity.
Definition net_utils.H:105
unsigned seed
Random seed (0 = use time)
Definition net_utils.H:110
double max_cost
Maximum arc cost.
Definition net_utils.H:108
size_t num_nodes
Number of nodes.
Definition net_utils.H:103
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
ValueArg< size_t > seed
Definition testHash.C:53
Gsl_Compatible_Rng r
Dynamic array container with automatic resizing.
Dynamic key-value map based on balanced binary search trees.
Network flow graph structures.
Maximum flow minimum cost network algorithms.
DynList< int > l