145 seed =
static_cast<unsigned>(
146 std::chrono::system_clock::now().time_since_epoch().count());
148 std::mt19937 gen(
seed);
149 std::uniform_real_distribution<double> cap_dist(params.
min_capacity,
151 std::uniform_int_distribution<size_t> node_dist(0, params.
num_nodes - 1);
155 for (
size_t i = 0; i < params.
num_nodes; ++i)
165 for (
size_t i = 0; i < params.
num_nodes - 1; ++i)
173 size_t arcs_to_add = params.
num_arcs;
177 for (
size_t i = 0; i < arcs_to_add; ++i)
179 size_t src = node_dist(gen);
180 size_t tgt = node_dist(gen);
209 double min_cap = 1.0,
double max_cap = 100.0,
218 return generate_random_network<Net>(params);
247 bool bidirectional =
true)
252 std::vector<std::vector<Node*>>
nodes(
rows, std::vector<Node*>(
cols));
255 for (
size_t i = 0; i <
rows; ++i)
256 for (
size_t j = 0; j <
cols; ++j)
260 for (
size_t i = 0; i <
rows; ++i)
261 for (
size_t j = 0; j <
cols - 1; ++j)
269 for (
size_t i = 0; i <
rows - 1; ++i)
270 for (
size_t j = 0; j <
cols; ++j)
303 double edge_prob = 0.5,
312 seed =
static_cast<unsigned>(
313 std::chrono::system_clock::now().time_since_epoch().count());
315 std::mt19937 gen(
seed);
316 std::uniform_real_distribution<double> prob_dist(0.0, 1.0);
323 std::vector<Node*> left_nodes(left_size);
324 for (
size_t i = 0; i < left_size; ++i)
331 std::vector<Node*> right_nodes(right_size);
332 for (
size_t i = 0; i < right_size; ++i)
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)
366 double edge_prob = 0.5,
373 if (layer_sizes.size() < 2)
376 for (
size_t l = 1;
l + 1 < layer_sizes.
size(); ++
l)
378 <<
"Intermediate layers must be non-empty";
382 seed =
static_cast<unsigned>(
383 std::chrono::system_clock::now().time_since_epoch().count());
385 std::mt19937 gen(
seed);
386 std::uniform_real_distribution<double> prob_dist(0.0, 1.0);
389 std::vector<std::vector<Node*>> layers(layer_sizes.size());
390 for (
size_t l = 0;
l < layer_sizes.
size(); ++
l)
392 layers[
l].resize(layer_sizes[
l]);
393 for (
size_t i = 0; i < layer_sizes[
l]; ++i)
398 for (
size_t l = 0;
l < layer_sizes.
size() - 1; ++
l)
403 std::vector<bool> reached(layers[
l+1].
size(),
false);
405 for (
size_t i = 0; i < layers[
l].size(); ++i)
407 bool connected =
false;
408 for (
size_t j = 0; j < layers[
l+1].size(); ++j)
409 if (prob_dist(gen) < edge_prob)
417 if (not connected
and not layers[
l+1].empty())
419 size_t j = gen() % layers[
l+1].size();
427 if (not layers[
l].empty())
428 for (
size_t j = 0; j < layers[
l+1].size(); ++j)
431 size_t i = gen() % layers[
l].size();
490 std::ofstream
out(filename);
498 node_ids[it.get_curr()] =
id++;
500 out <<
"digraph " <<
options.graph_name <<
" {\n";
501 out <<
" rankdir=LR;\n";
502 out <<
" node [shape=circle];\n";
507 Node* p = it.get_curr();
508 out <<
" " << node_ids[p];
517 out <<
" [style=filled, fillcolor=" <<
color <<
"]";
525 auto arc = it.get_curr();
529 out <<
" " << node_ids[src] <<
" -> " << node_ids[tgt];
532 std::ostringstream label;
549 if (
options.highlight_saturated
and arc->flow == arc->cap)
551 else if (
options.highlight_empty
and arc->flow == 0)
556 if (not label.str().empty())
557 out <<
"label=" << label.str();
558 if (not
color.empty())
560 if (not label.str().empty())
585 std::ostringstream
out;
591 node_ids[it.get_curr()] =
id++;
593 out <<
"digraph " <<
options.graph_name <<
" {\n";
594 out <<
" rankdir=LR;\n";
595 out <<
" node [shape=circle];\n";
600 Node* p = it.get_curr();
601 out <<
" " << node_ids[p];
610 out <<
" [style=filled, fillcolor=" <<
color <<
"]";
618 auto arc = it.get_curr();
622 out <<
" " << node_ids[src] <<
" -> " << node_ids[tgt];
624 std::ostringstream label;
640 if (
options.highlight_saturated
and arc->flow == arc->cap)
642 else if (
options.highlight_empty
and arc->flow == 0)
647 if (not label.str().empty())
648 out <<
"label=" << label.str();
649 if (not
color.empty())
651 if (not label.str().empty())
699 std::ofstream
out(filename);
707 node_ids[it.get_curr()] =
id++;
712 out <<
" \"num_nodes\": " << net.
vsize() <<
",\n";
713 out <<
" \"num_arcs\": " << net.
esize() <<
",\n";
717 out <<
" \"source\": " << node_ids[net.
get_source()] <<
",\n";
719 out <<
" \"sink\": " << node_ids[net.
get_sink()] <<
",\n";
722 out <<
" \"arcs\": [\n";
730 auto arc = it.get_curr();
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
755 std::ostringstream
out;
761 node_ids[it.get_curr()] =
id++;
764 out <<
" \"num_nodes\": " << net.
vsize() <<
",\n";
765 out <<
" \"num_arcs\": " << net.
esize() <<
",\n";
768 out <<
" \"source\": " << node_ids[net.
get_source()] <<
",\n";
770 out <<
" \"sink\": " << node_ids[net.
get_sink()] <<
",\n";
772 out <<
" \"arcs\": [\n";
780 auto arc = it.get_curr();
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
825 std::ofstream
out(filename);
833 node_ids[it.get_curr()] =
id++;
836 out <<
"c Network exported from Aleph-w\n";
837 out <<
"p max " << net.
vsize() <<
" " << net.
esize() <<
"\n";
848 auto arc = it.get_curr();
851 <<
static_cast<long long>(arc->cap) <<
"\n";
874 std::ifstream in(filename);
882 bool has_problem =
false;
883 size_t line_number = 0;
884 auto node_of = [&](
const size_t id)
887 << filename <<
":" << line_number <<
": node " <<
id
888 <<
" is not in [1, " <<
nodes.size() <<
"]";
889 return nodes(
id - 1);
893 while (std::getline(in, line))
896 std::istringstream iss(line);
898 if (not (iss >> type) or type ==
'c')
906 << filename <<
":" << line_number <<
": a second problem line";
908 << filename <<
":" << line_number <<
": malformed problem line";
910 for (
size_t i = 0; i < n; ++i)
914 else if (type ==
'n' or type ==
'a')
917 << filename <<
":" << line_number <<
": a '" << type
918 <<
"' line before the problem line";
924 or (designator !=
's' and designator !=
't'))
925 << filename <<
":" << line_number <<
": malformed node line";
930 size_t src = 0, tgt = 0;
933 << filename <<
":" << line_number <<
": malformed arc line";
950template <
typename Flow_Type>
969template <
class Net,
class Algo>
976 auto start = std::chrono::high_resolution_clock::now();
978 auto end = std::chrono::high_resolution_clock::now();
980 result.
elapsed_ms = std::chrono::duration<double, std::milli>(
981 end - start).count();
989template <
typename Flow_Type>
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";
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";
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
WeightedDigraph::Node Node
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
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.
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.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t vsize() const noexcept
See get_num_nodes().
size_t esize() const noexcept
Return the total of arcs of graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Key * append(const Key &key)
Alias for insert() (copy version).
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
Net import_network_from_dimacs(const std::string &filename)
Import network from DIMACS max-flow format.
void export_network_to_dimacs(const Net &net, const std::string &filename)
Export network to DIMACS max-flow format.
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.
Net generate_random_network(const NetworkGenParams ¶ms)
Generate a random flow network.
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.
void export_network_to_json(const Net &net, const std::string &filename)
Export network to JSON format.
std::string network_to_json_string(const Net &net)
Export network to JSON string.
MaxFlowBenchmarkResult< typename Net::Flow_Type > benchmark_maxflow(Net &net, Algo algo, const std::string &name)
Run and time a max-flow algorithm.
Net generate_grid_network(size_t rows, size_t cols, typename Net::Flow_Type capacity, bool bidirectional=true)
Generate a grid network.
void print_benchmark_results(const std::vector< MaxFlowBenchmarkResult< Flow_Type > > &results)
Print benchmark results.
std::string network_to_dot_string(const Net &net, const DotExportOptions &options=DotExportOptions())
Generate DOT string for network (instead of file).
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.
static long & color(typename GT::Node *p)
static struct argp_option options[]
Filtered iterator on all the arcs of a graph.
bool highlight_saturated
Highlight saturated arcs.
bool use_colors
Use colors for flow visualization.
bool show_capacity
Show capacity values on arcs.
bool show_flow
Show flow values on arcs.
bool highlight_empty
Highlight empty arcs.
std::string saturated_color
bool show_cost
Show cost values (for cost networks)
Result of running a max-flow algorithm.
std::string algorithm_name
Flow network implemented with adjacency lists.
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
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.
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.
Node * get_sink() const
Return an arbitrary sink node.
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.
Parameters for random network generation.
double max_capacity
Maximum arc capacity.
size_t num_arcs
Target number of arcs.
bool ensure_connected
Ensure source can reach sink.
double min_cost
Minimum arc cost (for cost networks)
double min_capacity
Minimum arc capacity.
unsigned seed
Random seed (0 = use time)
double max_cost
Maximum arc cost.
size_t num_nodes
Number of nodes.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
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.