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())
142 total_cap += it.get_curr()->cap;
162 cout <<
"\n=== " << title <<
" ===" << endl;
169 double max_arcs = n * (n - 1);
170 double density = (max_arcs > 0) ? 100.0 *
m / max_arcs : 0;
172 cout <<
"Density: " << fixed << setprecision(1) << density <<
"%" << endl;
176 for (
auto it = net.
get_arc_it(); it.has_curr(); it.next())
177 total_cap += it.get_curr()->cap;
179 cout <<
"Total capacity: " << total_cap << endl;
187 cout <<
"\n" << string(60,
'=') << endl;
188 cout <<
"Demo 1: Random Network Generation" << endl;
189 cout << string(60,
'=') << 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;
231 cout << string(60,
'=') << 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})
239 Net net = generate_grid_network<Net>(
size,
size, 10.0,
false);
242 title <<
size <<
"x" <<
size <<
" Grid";
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 "
251 << fixed << setprecision(3) << ms <<
" ms)" << endl;
260 cout <<
"\n" << string(60,
'=') << endl;
261 cout <<
"Demo 3: Layered Network Generation" << endl;
262 cout << string(60,
'=') << 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};
269 Net net = generate_layered_network<Net>(layers, 20.0, 0.7);
271 cout <<
"Layer structure: ";
272 for (
size_t l : layers)
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;
294 cout << string(60,
'=') << endl;
296 cout <<
"\nCreating a small network and exporting to DOT format..." << endl;
317 cout <<
"Max flow computed: " << flow << endl;
327 cout <<
"\nDOT output:" << endl;
328 cout << string(40,
'-') << endl;
330 cout << string(40,
'-') << endl;
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;
347 cout << string(60,
'=') << endl;
349 cout <<
"\nExporting network to JSON format..." << endl;
358 cout <<
"\nJSON output:" << endl;
359 cout << string(40,
'-') << endl;
360 cout << json << endl;
361 cout << string(40,
'-') << 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;
376 cout << string(60,
'=') << 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)"
385 << setw(10) <<
"Flow" << endl;
386 cout << string(74,
'-') << endl;
388 auto benchmark = [](
Net& net,
const string& name) {
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();
408 cout << setw(20) << left << name
411 << setw(16) << fixed << setprecision(3) << ek_ms
412 << setw(12) << dinic_ms
413 << setw(10) << setprecision(0) << f1 << endl;
419 benchmark(net,
"Random Sparse");
425 benchmark(net,
"Random Dense");
430 Net net = generate_grid_network<Net>(8, 8, 50.0,
false);
431 benchmark(net,
"Grid 8x8");
436 vector<size_t> layers = {1, 5, 8, 8, 5, 1};
437 Net net = generate_layered_network<Net>(layers, 50.0, 0.6);
438 benchmark(net,
"Layered (6 layers)");
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;
459 cout <<
"Summary" << endl;
460 cout << 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
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
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
See compute_cardinality_rec(Node * root).
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).
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.
bool show_capacity
Show capacity values on arcs.
bool show_flow
Show flow values on arcs.
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.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Advanced maximum flow algorithms.
Network flow graph structures.