41 cout <<
"=== Capacity Networks: Educational Examples ===\n\n";
47 cout <<
"--- Example 1: Simple Max Flow Problem ---\n\n";
53 cout <<
"PROBLEM: Pipeline network\n";
54 cout <<
"========================\n\n";
57 auto source =
network.insert_node(
"Source");
58 auto a =
network.insert_node(
"A");
59 auto b =
network.insert_node(
"B");
60 auto sink =
network.insert_node(
"Sink");
62 cout <<
"Network topology:\n";
70 cout <<
"Arc capacities (gallons/minute):\n";
71 network.insert_arc(source, a, 10);
72 network.insert_arc(source, b, 8);
73 network.insert_arc(a, sink, 12);
77 cout <<
" Source -> A: 10\n";
78 cout <<
" Source -> B: 8\n";
79 cout <<
" A -> Sink: 12\n";
80 cout <<
" B -> Sink: 9\n";
81 cout <<
" A -> B: 5 (allows redistribution)\n\n";
83 cout <<
"QUESTION: What is maximum flow from Source to Sink?\n\n";
85 cout <<
"SOLUTION PATHS:\n";
86 cout <<
" Path 1: Source -> A -> Sink (limited by 10)\n";
87 cout <<
" Path 2: Source -> B -> Sink (limited by 8)\n";
88 cout <<
" Path 3: Source -> A -> B -> Sink (limited by 5)\n\n";
90 cout <<
"MAXIMUM FLOW CALCULATION:\n";
91 cout <<
" 1. Send 10 through Source -> A -> Sink\n";
92 cout <<
" BUT: A -> Sink has capacity 12, can handle it\n";
93 cout <<
" 2. Send 8 through Source -> B -> Sink\n";
94 cout <<
" 3. Could send more through A -> B -> Sink\n";
95 cout <<
" But Source -> A already at capacity (10)\n\n";
97 cout <<
"EXPECTED MAX FLOW: 17 units\n";
98 cout <<
" (Limited by source output: 10 + 8 = 18, but)\n";
99 cout <<
" (Actually limited by sink input: 12 + 9 = 21)\n";
100 cout <<
" (Real limit: 10 + 7 = 17, using A->B path)\n\n";
107 cout <<
"--- Example 2: Finding Bottlenecks ---\n\n";
112 cout <<
"SCENARIO: Data center network\n";
113 cout <<
"============================\n\n";
121 cout <<
"Network path: Internet -> Router -> Switches -> Servers\n\n";
130 cout <<
"Link capacities:\n";
131 cout <<
" Internet -> Router: 100 Gbps\n";
132 cout <<
" Router -> Switch1: 40 Gbps\n";
133 cout <<
" Router -> Switch2: 40 Gbps\n";
134 cout <<
" Switch1 -> Servers: 50 Gbps\n";
135 cout <<
" Switch2 -> Servers: 50 Gbps\n\n";
137 cout <<
"BOTTLENECK ANALYSIS:\n";
138 cout <<
" 1. Internet connection: 100 Gbps (not a bottleneck)\n";
139 cout <<
" 2. Router output: 40 + 40 = 80 Gbps (BOTTLENECK!)\n";
140 cout <<
" 3. Server input: 50 + 50 = 100 Gbps (not a bottleneck)\n\n";
142 cout <<
"MAXIMUM THROUGHPUT: 80 Gbps\n";
143 cout <<
" Limited by router's total output capacity\n";
144 cout <<
" Even though internet and servers can handle 100 Gbps\n\n";
146 cout <<
"UPGRADE RECOMMENDATION:\n";
147 cout <<
" Add another 40 Gbps link from router to switches\n";
148 cout <<
" This would increase capacity to 100 Gbps\n\n";
155 cout <<
"--- Example 3: Minimum Cut Problem ---\n\n";
160 cout <<
"MAX-FLOW MIN-CUT THEOREM:\n";
161 cout <<
"=========================\n";
162 cout <<
"The maximum flow equals the capacity of the minimum cut\n";
163 cout <<
"Cut = partition of nodes into two sets (S and T)\n";
164 cout <<
"Cut capacity = sum of arc capacities from S to T\n\n";
167 auto s =
network.insert_node(1);
168 auto a =
network.insert_node(2);
169 auto b =
network.insert_node(3);
170 auto t =
network.insert_node(4);
178 cout <<
"Network:\n";
179 cout <<
" s -> a: 10\n";
180 cout <<
" s -> b: 10\n";
181 cout <<
" a -> b: 2\n";
182 cout <<
" a -> t: 4\n";
183 cout <<
" b -> t: 8\n\n";
185 cout <<
"POSSIBLE CUTS:\n\n";
187 cout <<
"Cut 1: S={s}, T={a,b,t}\n";
188 cout <<
" Capacity: 10 + 10 = 20\n\n";
190 cout <<
"Cut 2: S={s,a}, T={b,t}\n";
191 cout <<
" Capacity: 10 + 4 = 14\n\n";
193 cout <<
"Cut 3: S={s,b}, T={a,t}\n";
194 cout <<
" Capacity: 10 + 8 = 18\n\n";
196 cout <<
"Cut 4: S={s,a,b}, T={t}\n";
197 cout <<
" Capacity: 4 + 8 = 12 (MINIMUM CUT!)\n\n";
199 cout <<
"CONCLUSION:\n";
200 cout <<
" Minimum cut capacity: 12\n";
201 cout <<
" Maximum flow: 12 (by theorem)\n";
202 cout <<
" The cut identifies the bottleneck!\n\n";
209 cout <<
"--- Example 4: Ford-Fulkerson Algorithm Concept ---\n\n";
211 cout <<
"RESIDUAL GRAPH:\n";
212 cout <<
"===============\n";
213 cout <<
"Shows remaining capacity after current flow\n\n";
215 cout <<
"Original arc: A --10--> B\n";
216 cout <<
"Current flow: 7 units flowing\n\n";
218 cout <<
"Residual graph has TWO arcs:\n";
219 cout <<
" 1. A --3--> B (forward: remaining capacity = 10 - 7 = 3)\n";
220 cout <<
" 2. B --7--> A (backward: can 'undo' flow = 7)\n\n";
222 cout <<
"WHY BACKWARD ARCS?\n";
223 cout <<
" Allow algorithm to 'change its mind'\n";
224 cout <<
" Redirect flow along better paths\n";
225 cout <<
" Essential for finding optimal solution\n\n";
227 cout <<
"AUGMENTING PATH:\n";
228 cout <<
"================\n";
229 cout <<
"Path from source to sink in residual graph\n\n";
231 cout <<
"FORD-FULKERSON ALGORITHM:\n";
232 cout <<
"1. Start with zero flow\n";
233 cout <<
"2. While augmenting path exists in residual graph:\n";
234 cout <<
" a. Find path from source to sink\n";
235 cout <<
" b. Determine bottleneck (min capacity on path)\n";
236 cout <<
" c. Augment flow by bottleneck amount\n";
237 cout <<
" d. Update residual graph\n";
238 cout <<
"3. No more augmenting paths → maximum flow found!\n\n";
240 cout <<
"COMPLEXITY:\n";
241 cout <<
" Time: O(E * |max_flow|)\n";
242 cout <<
" Can be slow for large flows\n";
243 cout <<
" Edmonds-Karp improves this to O(V * E^2) using BFS\n\n";
250 cout <<
"--- Example 5: Practical Applications ---\n\n";
252 cout <<
"1. NETWORK ROUTING:\n";
253 cout <<
" ==================\n";
254 cout <<
" * Internet traffic routing\n";
255 cout <<
" * Telecommunication networks\n";
256 cout <<
" * Data center load balancing\n";
257 cout <<
" Goal: Maximize throughput\n\n";
259 cout <<
"2. TRANSPORTATION:\n";
260 cout <<
" ===============\n";
261 cout <<
" * Road traffic management\n";
262 cout <<
" * Railway scheduling\n";
263 cout <<
" * Airline route planning\n";
264 cout <<
" Goal: Maximize vehicles/passengers moved\n\n";
266 cout <<
"3. SUPPLY CHAIN:\n";
267 cout <<
" =============\n";
268 cout <<
" * Distribution networks\n";
269 cout <<
" * Manufacturing pipelines\n";
270 cout <<
" * Inventory management\n";
271 cout <<
" Goal: Maximize delivery capacity\n\n";
273 cout <<
"4. BIPARTITE MATCHING:\n";
274 cout <<
" ====================\n";
275 cout <<
" * Job assignment (workers to tasks)\n";
276 cout <<
" * Stable marriage problem\n";
277 cout <<
" * Resource allocation\n";
278 cout <<
" Goal: Maximum matching size\n\n";
280 cout <<
"5. IMAGE SEGMENTATION:\n";
281 cout <<
" ===================\n";
282 cout <<
" * Computer vision\n";
283 cout <<
" * Medical imaging\n";
284 cout <<
" * Object detection\n";
285 cout <<
" Goal: Optimal foreground/background separation\n\n";
288 cout <<
"=== SUMMARY: Capacity Networks ===\n";
289 cout <<
"\n1. CORE CONCEPTS:\n";
290 cout <<
" * Capacity: Maximum flow through arc\n";
291 cout <<
" * Flow: Actual amount flowing\n";
292 cout <<
" * Conservation: Flow in = Flow out (except source/sink)\n";
293 cout <<
"\n2. MAXIMUM FLOW PROBLEM:\n";
294 cout <<
" Input: Network with capacities, source, sink\n";
295 cout <<
" Output: Maximum amount from source to sink\n";
296 cout <<
" Time: O(V * E^2) with Edmonds-Karp\n";
297 cout <<
"\n3. MINIMUM CUT PROBLEM:\n";
298 cout <<
" Input: Same as max flow\n";
299 cout <<
" Output: Partition with minimum cut capacity\n";
300 cout <<
" Result: Max flow = Min cut (famous theorem!)\n";
301 cout <<
"\n4. KEY ALGORITHMS:\n";
302 cout <<
" Ford-Fulkerson: O(E * max_flow), simple\n";
303 cout <<
" Edmonds-Karp: O(V * E^2), uses BFS\n";
304 cout <<
" Dinic: O(V^2 * E), level graphs\n";
305 cout <<
" Push-Relabel: O(V^2 * sqrt(E)), fastest\n";
306 cout <<
"\n5. RESIDUAL GRAPH:\n";
307 cout <<
" * Forward arcs: Remaining capacity\n";
308 cout <<
" * Backward arcs: Can reduce flow\n";
309 cout <<
" * Essential for finding optimal solution\n";
310 cout <<
"\n6. APPLICATIONS:\n";
311 cout <<
" ✓ Network design and optimization\n";
312 cout <<
" ✓ Resource allocation\n";
313 cout <<
" ✓ Scheduling problems\n";
314 cout <<
" ✓ Image processing\n";
315 cout <<
" ✓ Bipartite matching\n";
316 cout <<
"\n7. DESIGN PRINCIPLES:\n";
317 cout <<
" * Add capacity where needed most\n";
318 cout <<
" * Identify bottlenecks with min-cut\n";
319 cout <<
" * Redundancy improves robustness\n";
320 cout <<
" * Balance capacities across network\n";
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of a flow network implemented with adjacency lists.
Flow network implemented with adjacency lists.
Network flow graph structures.
Network flow graph with capacities.