142 unsigned seed =
params.seed;
144 seed =
static_cast<unsigned>(
145 std::chrono::system_clock::now().time_since_epoch().count());
147 std::mt19937
gen(seed);
148 std::uniform_real_distribution<double>
cap_dist(
params.min_capacity,
150 std::uniform_int_distribution<size_t>
node_dist(0,
params.num_nodes - 1);
154 for (
size_t i = 0; i <
params.num_nodes; ++i)
162 if (
params.ensure_connected)
164 for (
size_t i = 0; i <
params.num_nodes - 1; ++i)
173 if (
params.ensure_connected)
184 tgt = (tgt + 1) %
params.num_nodes;
208 double min_cap = 1.0,
double max_cap = 100.0,
213 params.num_arcs = num_arcs;
215 params.max_capacity = max_cap;
251 std::vector<std::vector<Node*>>
nodes(rows, std::vector<Node*>(cols));
254 for (
size_t i = 0; i < rows; ++i)
255 for (
size_t j = 0; j < cols; ++j)
259 for (
size_t i = 0; i < rows; ++i)
260 for (
size_t j = 0; j < cols - 1; ++j)
268 for (
size_t i = 0; i < rows - 1; ++i)
269 for (
size_t j = 0; j < cols; ++j)
311 seed =
static_cast<unsigned>(
312 std::chrono::system_clock::now().time_since_epoch().count());
314 std::mt19937
gen(seed);
315 std::uniform_real_distribution<double> prob_dist(0.0, 1.0);
323 for (
size_t i = 0; i < left_size; ++i)
331 for (
size_t i = 0; i < right_size; ++i)
338 for (
size_t i = 0; i < left_size; ++i)
339 for (
size_t j = 0; j < right_size; ++j)
376 seed =
static_cast<unsigned>(
377 std::chrono::system_clock::now().time_since_epoch().count());
379 std::mt19937
gen(seed);
380 std::uniform_real_distribution<double> prob_dist(0.0, 1.0);
467 std::ofstream
out(filename);
477 out <<
"digraph " <<
options.graph_name <<
" {\n";
478 out <<
" rankdir=LR;\n";
479 out <<
" node [shape=circle];\n";
484 Node* p = it.get_curr();
494 out <<
" [style=filled, fillcolor=" <<
color <<
"]";
502 auto arc = it.get_curr();
509 std::ostringstream label;
526 if (
options.highlight_saturated
and arc->flow == arc->cap)
528 else if (
options.highlight_empty
and arc->flow == 0)
534 out <<
"label=" << label.str();
562 std::ostringstream
out;
570 out <<
"digraph " <<
options.graph_name <<
" {\n";
571 out <<
" rankdir=LR;\n";
572 out <<
" node [shape=circle];\n";
577 Node* p = it.get_curr();
587 out <<
" [style=filled, fillcolor=" <<
color <<
"]";
595 auto arc = it.get_curr();
601 std::ostringstream label;
617 if (
options.highlight_saturated
and arc->flow == arc->cap)
619 else if (
options.highlight_empty
and arc->flow == 0)
625 out <<
"label=" << label.str();
676 std::ofstream
out(filename);
689 out <<
" \"num_nodes\": " << net.
vsize() <<
",\n";
690 out <<
" \"num_arcs\": " << net.
esize() <<
",\n";
699 out <<
" \"arcs\": [\n";
707 auto arc = it.get_curr();
711 <<
"\"cap\": " << arc->cap <<
", "
712 <<
"\"flow\": " << arc->flow
732 std::ostringstream
out;
741 out <<
" \"num_nodes\": " << net.
vsize() <<
",\n";
742 out <<
" \"num_arcs\": " << net.
esize() <<
",\n";
749 out <<
" \"arcs\": [\n";
757 auto arc = it.get_curr();
761 <<
"\"cap\": " << arc->cap <<
", "
762 <<
"\"flow\": " << arc->flow
802 std::ofstream
out(filename);
813 out <<
"c Network exported from Aleph-w\n";
814 out <<
"p max " << net.
vsize() <<
" " << net.
esize() <<
"\n";
825 auto arc = it.get_curr();
828 <<
static_cast<long long>(arc->cap) <<
"\n";
849 std::ifstream
in(filename);
851 throw std::runtime_error(
"Cannot open file: " + filename);
853 std::vector<Node*>
nodes;
857 while (std::getline(
in, line))
859 if (line.empty()
or line[0] ==
'c')
862 std::istringstream
iss(line);
873 for (
size_t i = 1; i <= n; ++i)
876 else if (type ==
'n')
887 else if (type ==
'a')
891 iss >> src >> tgt >> cap;
914template <
typename Flow_Type>
933template <
class Net,
class Algo>
940 auto start = std::chrono::high_resolution_clock::now();
942 auto end = std::chrono::high_resolution_clock::now();
944 result.
elapsed_ms = std::chrono::duration<double, std::milli>(
945 end - start).count();
953template <
typename Flow_Type>
957 std::cout <<
"\n=== Max-Flow Algorithm Benchmark ===\n";
958 std::cout << std::left << std::setw(25) <<
"Algorithm"
959 << std::setw(15) <<
"Flow"
960 << std::setw(15) <<
"Time (ms)" <<
"\n";
961 std::cout << std::string(55,
'-') <<
"\n";
965 << std::setw(15) << r.flow_value
966 << std::setw(15) << std::fixed << std::setprecision(3)
967 << r.elapsed_ms <<
"\n";
WeightedDigraph::Node Node
void empty() noexcept
empty the list
Generic key-value map implemented on top of a binary search tree.
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
size_t size() const noexcept
Count the number of elements of the list.
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
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)
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.
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.
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)
DynList< T > maps(const C &c, Op op)
Classic map operation.
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.
Dynamic key-value map based on balanced binary search trees.
Network flow graph structures.
Maximum flow minimum cost network algorithms.