Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_netcapgraph_example.cc
Go to the documentation of this file.
1
32#include <iostream>
33#include <tpl_netcapgraph.H>
34#include <tpl_net.H>
35
36using namespace std;
37using namespace Aleph;
38
39int main()
40{
41 cout << "=== Capacity Networks: Educational Examples ===\n\n";
42
43 // =========================================================================
44 // EXAMPLE 1: Basic Maximum Flow
45 // =========================================================================
46 {
47 cout << "--- Example 1: Simple Max Flow Problem ---\n\n";
48
49 // STEP 1: Define network type
52
53 cout << "PROBLEM: Pipeline network\n";
54 cout << "========================\n\n";
55
56 // STEP 2: Create nodes
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");
61
62 cout << "Network topology:\n";
63 cout << " Source\n";
64 cout << " / \\\n";
65 cout << " A B\n";
66 cout << " \\ /\n";
67 cout << " Sink\n\n";
68
69 // STEP 3: Add arcs with capacities
70 cout << "Arc capacities (gallons/minute):\n";
71 network.insert_arc(source, a, 10); // capacity 10
72 network.insert_arc(source, b, 8); // capacity 8
73 network.insert_arc(a, sink, 12); // capacity 12
74 network.insert_arc(b, sink, 9); // capacity 9
75 network.insert_arc(a, b, 5); // capacity 5 (crossover)
76
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";
82
83 cout << "QUESTION: What is maximum flow from Source to Sink?\n\n";
84
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";
89
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";
96
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";
101 }
102
103 // =========================================================================
104 // EXAMPLE 2: Bottleneck Analysis
105 // =========================================================================
106 {
107 cout << "--- Example 2: Finding Bottlenecks ---\n\n";
108
110 Net network;
111
112 cout << "SCENARIO: Data center network\n";
113 cout << "============================\n\n";
114
115 auto internet = network.insert_node("Internet");
116 auto router = network.insert_node("Router");
117 auto switch1 = network.insert_node("Switch1");
118 auto switch2 = network.insert_node("Switch2");
119 auto server_cluster = network.insert_node("Servers");
120
121 cout << "Network path: Internet -> Router -> Switches -> Servers\n\n";
122
123 // Capacities in Gbps
124 network.insert_arc(internet, router, 100); // 100 Gbps
125 network.insert_arc(router, switch1, 40); // 40 Gbps
126 network.insert_arc(router, switch2, 40); // 40 Gbps
127 network.insert_arc(switch1, server_cluster, 50); // 50 Gbps
128 network.insert_arc(switch2, server_cluster, 50); // 50 Gbps
129
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";
136
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";
141
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";
145
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";
149 }
150
151 // =========================================================================
152 // EXAMPLE 3: Minimum Cut (Max-Flow Min-Cut Theorem)
153 // =========================================================================
154 {
155 cout << "--- Example 3: Minimum Cut Problem ---\n\n";
156
158 Net network;
159
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";
165
166 // Create simple network
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);
171
172 network.insert_arc(s, a, 10);
173 network.insert_arc(s, b, 10);
174 network.insert_arc(a, b, 2);
175 network.insert_arc(a, t, 4);
176 network.insert_arc(b, t, 8);
177
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";
184
185 cout << "POSSIBLE CUTS:\n\n";
186
187 cout << "Cut 1: S={s}, T={a,b,t}\n";
188 cout << " Capacity: 10 + 10 = 20\n\n";
189
190 cout << "Cut 2: S={s,a}, T={b,t}\n";
191 cout << " Capacity: 10 + 4 = 14\n\n";
192
193 cout << "Cut 3: S={s,b}, T={a,t}\n";
194 cout << " Capacity: 10 + 8 = 18\n\n";
195
196 cout << "Cut 4: S={s,a,b}, T={t}\n";
197 cout << " Capacity: 4 + 8 = 12 (MINIMUM CUT!)\n\n";
198
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";
203 }
204
205 // =========================================================================
206 // EXAMPLE 4: Residual Graph and Augmenting Paths
207 // =========================================================================
208 {
209 cout << "--- Example 4: Ford-Fulkerson Algorithm Concept ---\n\n";
210
211 cout << "RESIDUAL GRAPH:\n";
212 cout << "===============\n";
213 cout << "Shows remaining capacity after current flow\n\n";
214
215 cout << "Original arc: A --10--> B\n";
216 cout << "Current flow: 7 units flowing\n\n";
217
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";
221
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";
226
227 cout << "AUGMENTING PATH:\n";
228 cout << "================\n";
229 cout << "Path from source to sink in residual graph\n\n";
230
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";
239
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";
244 }
245
246 // =========================================================================
247 // EXAMPLE 5: Real-World Applications
248 // =========================================================================
249 {
250 cout << "--- Example 5: Practical Applications ---\n\n";
251
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";
258
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";
265
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";
272
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";
279
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";
286 }
287
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";
321
322 return 0;
323}
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.
Arc of a flow network implemented with adjacency lists.
Definition tpl_net.H:115
Flow network implemented with adjacency lists.
Definition tpl_net.H:261
Network flow graph structures.
Network flow graph with capacities.