Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
karger_example.cc
Go to the documentation of this file.
1
33#include <iostream>
34#include <Karger.H>
35#include <tpl_graph.H>
36
37using namespace std;
38using namespace Aleph;
39
40int main()
41{
42 cout << "=== Karger's Min-Cut Algorithm: Educational Examples ===\n\n";
43
44 cout << "WHAT IS KARGER'S ALGORITHM?\n";
45 cout << "===========================\n";
46 cout << "Randomized algorithm to find minimum cut in undirected graphs.\n";
47 cout << "Repeatedly contracts random edges until 2 super-nodes remain.\n\n";
48
49 cout << "KEY CONCEPTS:\n";
50 cout << "-------------\n";
51 cout << "1. MIN-CUT: Partition vertices into 2 sets minimizing edges between them\n";
52 cout << "2. EDGE CONTRACTION: Merge two nodes connected by an edge\n";
53 cout << "3. RANDOMIZATION: Success probability ≥ 1/n² per iteration\n";
54 cout << "4. REPETITION: O(n² log n) iterations give high success probability\n\n";
55
56 cout << "APPLICATIONS:\n";
57 cout << "-------------\n";
58 cout << "* Network reliability: Find critical connections (bridges)\n";
59 cout << "* Graph clustering: Partition graphs into communities\n";
60 cout << "* VLSI design: Minimize wire crossings\n";
61 cout << "* Image segmentation: Partition images into regions\n\n";
62
63 cout << "EXAMPLE SCENARIO: Network Bottleneck Detection\n";
64 cout << "===============================================\n\n";
65
66 cout << "Consider a network with two clusters:\n";
67 cout << " Cluster 1: [A-B] (well-connected internally)\n";
68 cout << " Cluster 2: [C-D] (well-connected internally)\n";
69 cout << " Bridge: B <--> C (single connection between clusters)\n\n";
70
71 cout << "GRAPH STRUCTURE:\n";
72 cout << " Nodes: A, B, C, D\n";
73 cout << " Edges: A-B, B-A, C-D, D-C, B-C, C-B (6 arcs total)\n\n";
74
75 cout << "EXPECTED MIN-CUT:\n";
76 cout << " Size: 1 (the bridge B-C)\n";
77 cout << " Partition 1: {A, B}\n";
78 cout << " Partition 2: {C, D}\n\n";
79
80 cout << "ALGORITHM STEPS:\n";
81 cout << "----------------\n";
82 cout << "1. Start with original graph\n";
83 cout << "2. Randomly select an edge and contract it\n";
84 cout << "3. Repeat until only 2 super-nodes remain\n";
85 cout << "4. Count edges between the 2 super-nodes (cut size)\n";
86 cout << "5. Repeat entire process O(n² log n) times\n";
87 cout << "6. Return minimum cut found\n\n";
88
89 cout << "COMPLEXITY:\n";
90 cout << "-----------\n";
91 cout << "* Per iteration: O(n²)\n";
92 cout << "* Total with repetition: O(n⁴ log n)\n";
93 cout << "* Karger-Stein variant: O(n² log³ n)\n\n";
94
95 cout << "ADVANTAGES:\n";
96 cout << "-----------\n";
97 cout << "+ Simple to implement\n";
98 cout << "+ Works on general graphs\n";
99 cout << "+ Provably correct with high probability\n\n";
100
101 cout << "DISADVANTAGES:\n";
102 cout << "--------------\n";
103 cout << "- Randomized (may need multiple runs)\n";
104 cout << "- Slower than deterministic algorithms for dense graphs\n\n";
105
106 cout << "USAGE IN CODE:\n";
107 cout << "==============\n";
108 cout << "```cpp\n";
109 cout << "// 1. Define graph type\n";
110 cout << "using GT = List_Graph<Graph_Node<string>, Graph_Arc<int>>;\n";
111 cout << "GT g;\n\n";
112 cout << "// 2. Build graph (nodes and arcs)\n";
113 cout << "auto a = g.insert_node(\"A\");\n";
114 cout << "auto b = g.insert_node(\"B\");\n";
115 cout << "g.insert_arc(a, b);\n\n";
116 cout << "// 3. Create Karger solver\n";
117 cout << "Karger_Min_Cut<GT> karger;\n\n";
118 cout << "// 4. Run algorithm\n";
119 cout << "DynList<GT::Node*> S, T;\n";
120 cout << "DynList<GT::Arc*> cut_arcs;\n";
121 cout << "size_t min_cut = karger(g, S, T, cut_arcs);\n\n";
122 cout << "// 5. Analyze results\n";
123 cout << "// S and T contain the two partitions\n";
124 cout << "// cut_arcs contains edges crossing the cut\n";
125 cout << "// min_cut is the size of the minimum cut\n";
126 cout << "```\n\n";
127
128 cout << "IMPROVING ACCURACY:\n";
129 cout << "===================\n";
130 cout << "Since Karger's algorithm is randomized:\n";
131 cout << "* Run it MULTIPLE times\n";
132 cout << "* Keep the MINIMUM cut found\n";
133 cout << "* For n nodes, O(n² log n) runs give high probability\n\n";
134
135 cout << "Example: Multiple iterations\n";
136 cout << "```cpp\n";
137 cout << "size_t best_cut = infinity;\n";
138 cout << "for (int i = 0; i < n*n*log(n); ++i) {\n";
139 cout << " size_t cut = karger(g, S, T, cut_arcs);\n";
140 cout << " if (cut < best_cut)\n";
141 cout << " best_cut = cut;\n";
142 cout << "}\n";
143 cout << "```\n\n";
144
145 {
146 cout << "COMPARISON WITH OTHER MIN-CUT ALGORITHMS:\n";
147 cout << "=========================================\n";
148 cout << "\nKarger (this algorithm):\n";
149 cout << " Time: O(n⁴ log n) with repetition\n";
150 cout << " Type: Randomized\n";
151 cout << " Advantage: Simple implementation\n";
152 cout << " Disadvantage: Slower for dense graphs\n\n";
153
154 cout << "Karger-Stein (improved variant):\n";
155 cout << " Time: O(n² log³ n)\n";
156 cout << " Type: Randomized\n";
157 cout << " Advantage: Much faster than basic Karger\n";
158 cout << " Disadvantage: More complex implementation\n\n";
159
160 cout << "Stoer-Wagner:\n";
161 cout << " Time: O(nm + n² log n)\n";
162 cout << " Type: Deterministic\n";
163 cout << " Advantage: Always correct, faster for sparse graphs\n";
164 cout << " Disadvantage: More complex\n\n";
165
166 cout << "Max-Flow Min-Cut:\n";
167 cout << " Time: O(n³) or better with advanced algorithms\n";
168 cout << " Type: Deterministic\n";
169 cout << " Advantage: Well-studied, many optimizations\n";
170 cout << " Disadvantage: Requires choosing source and sink\n\n";
171 }
172
173 cout << "\n=== SUMMARY: Key Takeaways ===\n";
174 cout << "\n1. WHAT IS MIN-CUT?\n";
175 cout << " Minimum number of edges to remove to disconnect the graph\n";
176 cout << "\n2. WHY USE KARGER'S ALGORITHM?\n";
177 cout << " - Simple randomized approach (contract random edges)\n";
178 cout << " - Works on any undirected graph\n";
179 cout << " - No need to specify source/sink like max-flow algorithms\n";
180 cout << "\n3. PRACTICAL TIPS:\n";
181 cout << " - Run multiple iterations for higher accuracy\n";
182 cout << " - Use Karger_Min_Cut::fast() for large graphs (n > 100)\n";
183 cout << " - Min-cut = 1 means BRIDGE exists (critical connection)\n";
184 cout << "\n4. REAL-WORLD APPLICATIONS:\n";
185 cout << " * Network reliability analysis (find weak points)\n";
186 cout << " * Community detection in social networks\n";
187 cout << " * Image segmentation (computer vision)\n";
188 cout << " * VLSI circuit design (minimize wire crossings)\n";
189 cout << "\n5. COMPLEXITY SUMMARY:\n";
190 cout << " Standard: O(n^2*m) per iteration, O(n^2 log n) iterations\n";
191 cout << " Karger-Stein: O(n^2 log^3 n) total\n";
192
193 return 0;
194}
Karger's randomized min-cut algorithm.
int main()
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Generic graph and digraph implementations.