115using namespace Aleph;
127 const double max_arcs =
static_cast<double>(n) * (n - 1);
128 size_t m =
static_cast<size_t>(density *
max_arcs);
137 for (
auto it = net.
get_node_it(); it.has_curr(); it.next())
138 nodes.push_back(it.get_curr());
141 for (
auto it = net.
get_arc_it(); it.has_curr(); it.next())
176 for (
auto it = net.
get_arc_it(); it.has_curr(); it.next())
187 cout <<
"\n" << string(60,
'=') <<
endl;
188 cout <<
"Demo 1: Random Network Generation" <<
endl;
191 cout <<
"\nGenerating random networks with different parameters..." <<
endl;
200 cout <<
"Max flow: " << flow <<
endl;
210 cout <<
"Max flow: " << flow <<
endl;
220 cout <<
"Max flow: " << flow <<
endl;
229 cout <<
"\n" << string(60,
'=') <<
endl;
230 cout <<
"Demo 2: Grid Network Generation" <<
endl;
233 cout <<
"\nGrid networks are useful for benchmarking." <<
endl;
234 cout <<
"Source is top-left, sink is bottom-right.\n" <<
endl;
237 for (
int size : {3, 5, 8})
245 auto start = chrono::high_resolution_clock::now();
247 auto end = chrono::high_resolution_clock::now();
249 double ms = chrono::duration<double, milli>(end - start).count();
250 cout <<
"Max flow: " << flow <<
" (computed in "
260 cout <<
"\n" << string(60,
'=') <<
endl;
261 cout <<
"Demo 3: Layered Network Generation" <<
endl;
264 cout <<
"\nLayered networks have nodes in discrete layers." <<
endl;
265 cout <<
"Edges only go from one layer to the next (DAG structure).\n" <<
endl;
267 vector<size_t>
layers = {1, 3, 4, 3, 1};
271 cout <<
"Layer structure: ";
279 cout <<
"Max flow: " << flow <<
endl;
281 cout <<
"\nLayered networks model:" <<
endl;
282 cout <<
" - Assembly lines (stages of production)" <<
endl;
283 cout <<
" - Communication protocols (network layers)" <<
endl;
284 cout <<
" - Project scheduling (phases)" <<
endl;
292 cout <<
"\n" << string(60,
'=') <<
endl;
293 cout <<
"Demo 4: GraphViz DOT Export" <<
endl;
296 cout <<
"\nCreating a small network and exporting to DOT format..." <<
endl;
317 cout <<
"Max flow computed: " << flow <<
endl;
321 opts.graph_name =
"sample_network";
322 opts.show_capacity =
true;
323 opts.show_flow =
true;
332 cout <<
"\nTo visualize, save to .dot file and run:" <<
endl;
333 cout <<
" dot -Tpng network.dot -o network.png" <<
endl;
335 cout <<
"\nCommands to visualize DOT files:" <<
endl;
336 cout <<
" dot -Tpng network.dot -o network.png" <<
endl;
337 cout <<
" dot -Tsvg network.dot -o network.svg" <<
endl;
345 cout <<
"\n" << string(60,
'=') <<
endl;
346 cout <<
"Demo 5: JSON Serialization" <<
endl;
349 cout <<
"\nExporting network to JSON format..." <<
endl;
363 cout <<
"\nJSON format is useful for:" <<
endl;
364 cout <<
" - Web visualization (D3.js, vis.js)" <<
endl;
365 cout <<
" - Data exchange between systems" <<
endl;
366 cout <<
" - Storing network configurations" <<
endl;
374 cout <<
"\n" << string(60,
'=') <<
endl;
375 cout <<
"Demo 6: Algorithm Benchmarking" <<
endl;
378 cout <<
"\nComparing max-flow algorithms on different network types...\n" <<
endl;
380 cout <<
setw(20) << left <<
"Network Type"
381 <<
setw(10) <<
"Nodes"
382 <<
setw(10) <<
"Arcs"
383 <<
setw(16) <<
"F-F (DFS) (ms)"
384 <<
setw(12) <<
"Dinic (ms)"
395 auto t1 = chrono::high_resolution_clock::now();
397 auto t2 = chrono::high_resolution_clock::now();
398 double ek_ms = chrono::duration<double, milli>(
t2 -
t1).count();
403 t1 = chrono::high_resolution_clock::now();
405 t2 = chrono::high_resolution_clock::now();
406 double dinic_ms = chrono::duration<double, milli>(
t2 -
t1).count();
436 vector<size_t>
layers = {1, 5, 8, 8, 5, 1};
441 cout <<
"\nNote: Times may vary based on random network structure." <<
endl;
442 cout <<
"Dinic is generally faster, especially on dense networks." <<
endl;
447 cout <<
"=== Network Utilities ===" <<
endl;
448 cout <<
"Generation, Visualization, and Serialization\n" <<
endl;
458 cout <<
"\n" << string(60,
'=') <<
endl;
463Network Utilities in Aleph-w:
466 - generate_random_network(n, m, min_cap, max_cap)
467 - generate_grid_network(rows, cols, capacity, bidirectional)
468 - generate_layered_network(layers, capacity, edge_prob)
469 - generate_bipartite_network(left, right, edge_prob)
471Visualization (DOT/GraphViz):
472 - export_network_to_dot(net, filename, options)
473 - network_to_dot_string(net, options)
475 Visualize with: dot -Tpng network.dot -o network.png
478 - network_to_json_string(net)
479 - export_network_to_dimacs(net, filename)
480 - import_network_from_dimacs<Net>(filename)
483 - Algorithm testing and benchmarking
484 - Educational demonstrations
485 - Network visualization
486 - Data exchange between systems
WeightedDigraph::Node Node
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
constexpr size_t get_num_arcs() const noexcept
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
Net::Flow_Type dinic_maximum_flow(Net &net)
Compute maximum flow using Dinic's algorithm.
std::string network_to_json_string(const Net &net)
Export network to JSON string.
Net::Flow_Type ford_fulkerson_maximum_flow(Net &net)
Maximize flow using the Ford-Fulkerson algorithm.
std::string network_to_dot_string(const Net &net, const DotExportOptions &options=DotExportOptions())
Generate DOT string for network (instead of file).
DynList< T > maps(const C &c, Op op)
Classic map operation.
Network flow utilities: generators, visualization, serialization.
void demo_benchmarking()
Demo 6: Benchmarking.
void demo_json_export()
Demo 5: JSON Serialization.
void demo_random_networks()
Demo 1: Random Network Generation.
static void add_super_source_and_sink(Net &net)
static size_t density_to_num_arcs(size_t n, double density)
void demo_dot_export()
Demo 4: DOT Export for Visualization.
void demo_layered_networks()
Demo 3: Layered Network Generation.
void print_network_stats(Net &net, const string &title)
Print network statistics.
void demo_grid_networks()
Demo 2: Grid Network Generation.
Arc of a flow network implemented with adjacency lists.
Flow network implemented with adjacency lists.
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
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.
Advanced maximum flow algorithms.
Network flow graph structures.