69 cout <<
" " << name <<
": { ";
73 if (!first) cout <<
", ";
74 cout << node->get_info();
86 cout <<
" Cut edges:\n";
90 <<
" --(" << arc->get_info() <<
")-- "
104 cout <<
"╔════════════════════════════════════════════════════════════════╗\n";
105 cout <<
"║ EXAMPLE 1: Network Reliability Analysis ║\n";
106 cout <<
"╚════════════════════════════════════════════════════════════════╝\n\n";
108 cout <<
"SCENARIO: A company has 6 offices connected by network links.\n";
109 cout <<
" We need to find the minimum number of links that,\n";
110 cout <<
" if cut, would split the network into two parts.\n\n";
116 cout <<
"STEP 1: Building network topology\n";
117 cout <<
" Offices: HQ, Branch1, Branch2, Branch3, Remote1, Remote2\n\n";
139 cout <<
" Network structure:\n";
140 cout <<
" Remote1\n";
142 cout <<
" Branch1--+--HQ--Branch3--Remote2\n";
144 cout <<
" Branch2-------+\n\n";
146 cout <<
" Total: " << network.
get_num_nodes() <<
" offices, "
150 cout <<
"STEP 2: Finding minimum cut with Karger-Stein\n";
151 cout <<
" (Running 20 iterations for accuracy)\n\n";
157 size_t min_cut = ks(network, S,
T, cut, 20);
160 cout <<
" Minimum cut size: " <<
min_cut / 2 <<
" links\n";
161 print_partition<GT>(
"Partition 1", S);
162 print_partition<GT>(
"Partition 2",
T);
164 cout <<
"\nINTERPRETATION:\n";
165 cout <<
" The network can be split by cutting just " <<
min_cut / 2 <<
" link(s).\n";
166 cout <<
" RECOMMENDATION: Add redundant links to vulnerable offices.\n\n";
178 cout <<
"╔════════════════════════════════════════════════════════════════╗\n";
179 cout <<
"║ EXAMPLE 2: Weighted Network - Bandwidth Analysis ║\n";
180 cout <<
"╚════════════════════════════════════════════════════════════════╝\n\n";
182 cout <<
"SCENARIO: A data center has servers connected with varying bandwidth.\n";
183 cout <<
" Find the minimum total bandwidth that could bottleneck.\n\n";
195 cout <<
"STEP 1: Network topology with bandwidth (Gbps)\n\n";
204 cout <<
" Web1 --10-- App --20-- Cache --5-- Database\n";
205 cout <<
" Web2 --10-- | |\n";
206 cout <<
" +-------2---------+\n\n";
209 cout <<
"STEP 2: Finding minimum cut with Stoer-Wagner\n\n";
215 int min_bandwidth = sw(datacenter, S,
T, cut);
218 cout <<
" Minimum cut bandwidth: " << min_bandwidth / 2 <<
" Gbps\n";
219 print_partition<GT>(
"Partition 1", S);
220 print_partition<GT>(
"Partition 2",
T);
221 cout <<
"\n Bottleneck links:\n";
226 <<
" (" << arc->get_info() <<
" Gbps)\n";
229 cout <<
"\nINTERPRETATION:\n";
230 cout <<
" The database access is the bottleneck at " << min_bandwidth / 2 <<
" Gbps.\n";
231 cout <<
" RECOMMENDATION: Upgrade Cache-Database link or add more paths.\n\n";
243 cout <<
"╔════════════════════════════════════════════════════════════════╗\n";
244 cout <<
"║ EXAMPLE 3: Community Detection in Social Network ║\n";
245 cout <<
"╚════════════════════════════════════════════════════════════════╝\n\n";
247 cout <<
"SCENARIO: A social network with two friend groups loosely connected.\n";
248 cout <<
" Find the natural division between communities.\n\n";
263 cout <<
"STEP 1: Building social connections\n\n";
278 cout <<
" Group 1 (Tech): Group 2 (Sports):\n";
279 cout <<
" Alice--Bob Dave--Eve\n";
280 cout <<
" \\ / \\ /\n";
281 cout <<
" Carol ---- Dave Frank\n";
282 cout <<
" (weak link)\n\n";
285 cout <<
"STEP 2: Running both algorithms\n\n";
291 size_t ks_result = ks(social, ks_S, ks_T, ks_cut, 30);
297 int sw_result = sw(social, sw_S, sw_T, sw_cut);
299 cout <<
"KARGER-STEIN RESULT:\n";
300 cout <<
" Min-cut size: " << ks_result / 2 <<
"\n";
301 print_partition<GT>(
"Community 1", ks_S);
302 print_partition<GT>(
"Community 2", ks_T);
304 cout <<
"\nSTOER-WAGNER RESULT:\n";
305 cout <<
" Min-cut size: " << sw_result / 2 <<
"\n";
306 print_partition<GT>(
"Community 1", sw_S);
307 print_partition<GT>(
"Community 2", sw_T);
309 cout <<
"\nINTERPRETATION:\n";
310 cout <<
" Both algorithms identify the two communities correctly.\n";
311 cout <<
" The Carol-Dave connection is the bridge between groups.\n\n";
320 cout <<
"╔════════════════════════════════════════════════════════════════╗\n";
321 cout <<
"║ EXAMPLE 4: Algorithm Comparison ║\n";
322 cout <<
"╚════════════════════════════════════════════════════════════════╝\n\n";
324 cout <<
"SCENARIO: Compare Karger-Stein vs Stoer-Wagner on the same graph.\n\n";
330 const int CLIQUE_SIZE = 5;
331 const int BRIDGE_COUNT = 3;
333 vector<GT::Node*> left(CLIQUE_SIZE), right(CLIQUE_SIZE);
335 for (
int i = 0; i < CLIQUE_SIZE; ++i)
342 for (
int i = 0; i < CLIQUE_SIZE; ++i)
343 for (
int j = i + 1; j < CLIQUE_SIZE; ++j)
350 for (
int i = 0; i < CLIQUE_SIZE; ++i)
351 for (
int j = i + 1; j < CLIQUE_SIZE; ++j)
358 for (
int i = 0; i < BRIDGE_COUNT; ++i)
364 cout <<
"GRAPH: Two K" << CLIQUE_SIZE <<
" cliques connected by "
365 << BRIDGE_COUNT <<
" edges\n";
366 cout <<
" Expected min-cut: " << BRIDGE_COUNT <<
"\n";
371 cout <<
"KARGER-STEIN (varying iterations):\n";
374 for (
int iters : {1, 5, 10, 20, 50})
378 size_t result = ks(g, S,
T, cut, iters);
379 cout <<
" " << setw(2) << iters <<
" iterations: min-cut = "
380 << result / 2 <<
"\n";
384 cout <<
"\nSTOER-WAGNER (deterministic):\n";
388 int result = sw(g, S,
T, cut);
389 cout <<
" Result: min-cut = " << result / 2 <<
"\n";
391 cout <<
"\nCONCLUSION:\n";
392 cout <<
" - Karger-Stein converges to correct answer with more iterations\n";
393 cout <<
" - Stoer-Wagner always gives exact answer in one run\n";
394 cout <<
" - Choose based on: graph size, accuracy needs, weighted/unweighted\n\n";
403 cout <<
"╔════════════════════════════════════════════════════════════════╗\n";
404 cout <<
"║ EXAMPLE 5: API Usage Patterns ║\n";
405 cout <<
"╚════════════════════════════════════════════════════════════════╝\n\n";
420 cout <<
"PATTERN 1: Basic single-run (Karger-Stein)\n";
421 cout <<
"-------------------------------------------\n";
423 Karger_Stein_Min_Cut<GT> ks;
424 DynList<GT::Node*> S, T;
425 DynList<GT::Arc*> cut;
426 size_t result = ks(g, S, T, cut);
433 size_t result = ks(g, S,
T, cut);
434 cout <<
" Result: " << result <<
"\n\n";
437 cout <<
"PATTERN 2: Multiple iterations for accuracy (Karger-Stein)\n";
438 cout <<
"-----------------------------------------------------------\n";
440 size_t result = ks(g, S, T, cut, 20);
447 size_t result = ks(g, S,
T, cut, 20);
448 cout <<
" Result: " << result <<
"\n\n";
451 cout <<
"PATTERN 3: Reproducible results with seed\n";
452 cout <<
"------------------------------------------\n";
454 Karger_Stein_Min_Cut<GT> ks(12345); // Seed = 12345
455 // Or: ks.set_seed(12345);
463 size_t r1 = ks1(g, S1, T1, cut1);
464 size_t r2 = ks2(g, S2, T2, cut2);
465 cout <<
" Same seed → same result: " << (r1 == r2 ?
"YES" :
"NO") <<
"\n\n";
468 cout <<
"PATTERN 4: Weighted graph (Stoer-Wagner)\n";
469 cout <<
"-----------------------------------------\n";
471 Stoer_Wagner_Min_Cut<GT> sw;
472 int weight = sw(g, S, T, cut); // Returns total weight of cut edges
479 int weight = sw(g, S,
T, cut);
480 cout <<
" Result: " << weight <<
"\n\n";
483 cout <<
"PATTERN 5: Just the weight, no partition (Stoer-Wagner)\n";
484 cout <<
"--------------------------------------------------------\n";
486 int weight = sw.min_cut_weight(g); // Slightly faster
492 cout <<
" Result: " << weight <<
"\n\n";
495 cout <<
"PATTERN 6: Unweighted graph with Stoer-Wagner\n";
496 cout <<
"----------------------------------------------\n";
498 Stoer_Wagner_Min_Cut<GT, Unit_Weight<GT>> sw;
499 size_t num_edges = sw(g, S, T, cut); // Counts edges, ignores weights
506 size_t num_edges = sw(g, S,
T, cut);
507 cout <<
" Result: " << num_edges <<
" edges\n\n";
517 cout <<
"╔════════════════════════════════════════════════════════════════╗\n";
518 cout <<
"║ EXAMPLE 6: Algorithm Selection Guide ║\n";
519 cout <<
"╚════════════════════════════════════════════════════════════════╝\n\n";
521 cout <<
"DECISION FLOWCHART:\n";
522 cout <<
"═══════════════════\n\n";
524 cout <<
" ┌─────────────────────────────────────────┐\n";
525 cout <<
" │ Do you need the EXACT minimum cut? │\n";
526 cout <<
" └────────────────┬────────────────────────┘\n";
528 cout <<
" ┌─────────┴─────────┐\n";
533 cout <<
" ┌──────────────┐ ┌──────────────────────┐\n";
534 cout <<
" │ STOER-WAGNER │ │ Is the graph large? │\n";
535 cout <<
" │ Deterministic│ │ (n > 100) │\n";
536 cout <<
" └──────────────┘ └──────────┬───────────┘\n";
538 cout <<
" ┌─────────┴─────────┐\n";
543 cout <<
" ┌──────────────┐ ┌──────────────┐\n";
544 cout <<
" │ KARGER-STEIN │ │ STOER-WAGNER │\n";
545 cout <<
" │ O(n² log³ n) │ │ Simple cases │\n";
546 cout <<
" └──────────────┘ └──────────────┘\n\n";
548 cout <<
"COMPARISON TABLE:\n";
549 cout <<
"═════════════════\n\n";
551 cout <<
" ┌────────────────┬───────────────────┬───────────────────┐\n";
552 cout <<
" │ Criterion │ Karger-Stein │ Stoer-Wagner │\n";
553 cout <<
" ├────────────────┼───────────────────┼───────────────────┤\n";
554 cout <<
" │ Time │ O(n² log³ n) │ O(nm + n² log n) │\n";
555 cout <<
" │ Space │ O(n + m) │ O(n²) │\n";
556 cout <<
" │ Deterministic? │ No (randomized) │ Yes │\n";
557 cout <<
" │ Weighted? │ No │ Yes │\n";
558 cout <<
" │ Exact? │ High probability │ Always │\n";
559 cout <<
" │ Large graphs │ ✓ Better │ OK │\n";
560 cout <<
" │ Small graphs │ OK │ ✓ Better │\n";
561 cout <<
" └────────────────┴───────────────────┴───────────────────┘\n\n";
563 cout <<
"PRACTICAL RECOMMENDATIONS:\n";
564 cout <<
"══════════════════════════\n\n";
566 cout <<
" 1. NETWORK RELIABILITY → Stoer-Wagner (exact answer matters)\n";
567 cout <<
" 2. BANDWIDTH ANALYSIS → Stoer-Wagner (needs weights)\n";
568 cout <<
" 3. COMMUNITY DETECTION → Either (approximate OK)\n";
569 cout <<
" 4. LARGE SOCIAL GRAPH → Karger-Stein (faster)\n";
570 cout <<
" 5. VLSI DESIGN → Stoer-Wagner (precision critical)\n\n";
580 cout <<
"████████████████████████████████████████████████████████████████████\n";
582 cout <<
"██ MINIMUM CUT ALGORITHMS - EDUCATIONAL EXAMPLES ██\n";
583 cout <<
"██ Karger-Stein (Randomized) & Stoer-Wagner (Deterministic) ██\n";
585 cout <<
"████████████████████████████████████████████████████████████████████\n";
595 cout <<
"════════════════════════════════════════════════════════════════════\n";
596 cout <<
" All examples completed successfully!\n";
597 cout <<
"════════════════════════════════════════════════════════════════════\n\n";
Karger-Stein randomized min-cut algorithm (alias for Karger.H fast method).
Stoer-Wagner deterministic min-cut algorithm.
Doubly-linked list (defined in tpl_dynList.H).
Karger's randomized minimum cut algorithm.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Stoer-Wagner deterministic minimum cut algorithm.
Weight min_cut_weight(GT &g)
Find only the minimum cut weight (faster, no partition info).
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
void example_weighted_bandwidth()
void example_community_detection()
void print_cut_edges(GT &g, const DynList< typename GT::Arc * > &cut)
void example_algorithm_comparison()
void print_partition(const string &name, const DynList< typename GT::Node * > &partition)
void example_network_reliability()
void example_algorithm_selection()
void example_api_patterns()
Main namespace for Aleph-w library functions.
std::pair< TgtContainer< typename SrcContainer::Item_Type >, TgtContainer< typename SrcContainer::Item_Type > > partition(const SrcContainer &c, std::function< bool(const typename SrcContainer::Item_Type &)> operation)
Partition a container into two based on a predicate.
std::decay_t< typename HeadC::Item_Type > T
Net::Flow_Type min_cut(Net &net, DynSetTree< typename Net::Node * > &vs, DynSetTree< typename Net::Node * > &vt, DynList< typename Net::Arc * > &cuts, DynList< typename Net::Arc * > &cutt)
Compute max flow and the corresponding minimum cut.
Arc of graph implemented with double-linked adjacency lists.
Generic graph and digraph implementations.