Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_kgraph_example.cc
Go to the documentation of this file.
1
33#include <iostream>
34#include <tpl_graph.H>
35
36using namespace std;
37using namespace Aleph;
38
39int main()
40{
41 cout << "=== K-Connected Graphs: Educational Examples ===\n\n";
42
43 // =========================================================================
44 // EXAMPLE 1: Understanding 1-Connectivity (Bridges)
45 // =========================================================================
46 {
47 cout << "--- Example 1: 1-Connected Graph (Has Bridges) ---\n\n";
48
50 GT network;
51
52 cout << "SCENARIO: Simple network with weak link\n";
53 cout << "=======================================\n\n";
54
55 // Build network with a bridge
56 auto cluster1_a = network.insert_node("A");
57 auto cluster1_b = network.insert_node("B");
58 auto cluster2_c = network.insert_node("C");
59 auto cluster2_d = network.insert_node("D");
60
61 cout << "Network topology:\n";
62 cout << " Cluster 1: A --- B\n";
63 cout << " |\n";
64 cout << " BRIDGE (single connection)\n";
65 cout << " |\n";
66 cout << " Cluster 2: C --- D\n\n";
67
68 // Connections within clusters
69 network.insert_arc(cluster1_a, cluster1_b);
70 network.insert_arc(cluster1_b, cluster1_a);
71
72 network.insert_arc(cluster2_c, cluster2_d);
73 network.insert_arc(cluster2_d, cluster2_c);
74
75 // BRIDGE: Single connection between clusters
76 network.insert_arc(cluster1_b, cluster2_c);
77 network.insert_arc(cluster2_c, cluster1_b);
78
79 cout << "CONNECTIVITY ANALYSIS:\n";
80 cout << " * Edge connectivity: 1\n";
81 cout << " * Meaning: Removing 1 edge (the bridge) disconnects graph\n";
82 cout << " * Risk: Single point of failure!\n\n";
83
84 cout << "WHAT HAPPENS IF BRIDGE FAILS?\n";
85 cout << " Cluster 1 (A,B) is isolated from Cluster 2 (C,D)\n";
86 cout << " Network splits into two components\n";
87 cout << " No redundancy - catastrophic failure!\n\n";
88
89 cout << "REAL-WORLD: Internet backbone with single link between regions\n";
90 cout << " One cable cut = complete regional isolation\n\n";
91 }
92
93 // =========================================================================
94 // EXAMPLE 2: 2-Connected Graph (More Robust)
95 // =========================================================================
96 {
97 cout << "--- Example 2: 2-Connected Graph (No Bridges) ---\n\n";
98
100 GT network;
101
102 cout << "SCENARIO: Improved network with redundancy\n";
103 cout << "==========================================\n\n";
104
105 auto a = network.insert_node("A");
106 auto b = network.insert_node("B");
107 auto c = network.insert_node("C");
108 auto d = network.insert_node("D");
109
110 cout << "Network topology (cycle with diameter):\n";
111 cout << " A --- B\n";
112 cout << " | \\ |\n";
113 cout << " | \\ | (diagonal A-C adds redundancy)\n";
114 cout << " D --- C\n\n";
115
116 // Create cycle
117 network.insert_arc(a, b);
118 network.insert_arc(b, c);
119 network.insert_arc(c, d);
120 network.insert_arc(d, a);
121
122 // Add diagonal for 2-connectivity
123 network.insert_arc(a, c);
124
125 // Make undirected
126 network.insert_arc(b, a);
127 network.insert_arc(c, b);
128 network.insert_arc(d, c);
129 network.insert_arc(a, d);
130 network.insert_arc(c, a);
131
132 cout << "CONNECTIVITY ANALYSIS:\n";
133 cout << " * Edge connectivity: 2\n";
134 cout << " * Meaning: Need to remove 2 edges to disconnect\n";
135 cout << " * Benefit: Can survive any single edge failure!\n\n";
136
137 cout << "FAILURE SCENARIOS:\n";
138 cout << " 1. Edge A-B fails:\n";
139 cout << " Alternative path: A → D → C → B\n";
140 cout << " Or: A → C → B\n";
141 cout << " Network stays connected ✓\n\n";
142
143 cout << " 2. Edge B-C fails:\n";
144 cout << " Alternative: B → A → C (via diagonal)\n";
145 cout << " Or: B → A → D → C\n";
146 cout << " Network stays connected ✓\n\n";
147
148 cout << "KEY INSIGHT: 2-connectivity = no single point of failure\n";
149 cout << " Every node reachable via multiple paths\n\n";
150 }
151
152 // =========================================================================
153 // EXAMPLE 3: Comparing Connectivity Levels
154 // =========================================================================
155 {
156 cout << "--- Example 3: Connectivity Level Comparison ---\n\n";
157
158 cout << "CONNECTIVITY LEVELS:\n";
159 cout << "===================\n\n";
160
161 cout << "k=0 (DISCONNECTED):\n";
162 cout << " A B C D\n";
163 cout << " Completely separate components\n";
164 cout << " Fault tolerance: None\n";
165 cout << " Use case: Independent subsystems\n\n";
166
167 cout << "k=1 (TREE/BRIDGE):\n";
168 cout << " A---B C---D\n";
169 cout << " \\ /\n";
170 cout << " BRIDGE\n";
171 cout << " Minimal connectivity\n";
172 cout << " Fault tolerance: Very low (any edge failure = disconnect)\n";
173 cout << " Use case: Hierarchical networks, cost-critical designs\n\n";
174
175 cout << "k=2 (CYCLE):\n";
176 cout << " A---B\n";
177 cout << " | |\n";
178 cout << " D---C\n";
179 cout << " Survives 1 edge failure\n";
180 cout << " Fault tolerance: Good (backup paths exist)\n";
181 cout << " Use case: Most practical networks\n\n";
182
183 cout << "k=3 (HIGHLY CONNECTED):\n";
184 cout << " A---B\n";
185 cout << " |\\ /|\n";
186 cout << " |X X| (many cross-connections)\n";
187 cout << " |/ \\|\n";
188 cout << " D---C\n";
189 cout << " Survives 2 edge failures\n";
190 cout << " Fault tolerance: Excellent\n";
191 cout << " Use case: Critical infrastructure, data centers\n\n";
192
193 cout << "TRADE-OFF:\n";
194 cout << " Higher k → More reliable BUT More expensive\n";
195 cout << " More edges → Higher cost (cables, maintenance)\n";
196 cout << " Design choice depends on criticality vs budget\n\n";
197 }
198
199 // =========================================================================
200 // EXAMPLE 4: Calculating Edge Connectivity
201 // =========================================================================
202 {
203 cout << "--- Example 4: Computing Edge Connectivity ---\n\n";
204
206 GT g;
207
208 cout << "ALGORITHM: Uses maximum flow\n";
209 cout << "============================\n\n";
210
211 // Create a simple graph
212 auto n1 = g.insert_node(1);
213 auto n2 = g.insert_node(2);
214 auto n3 = g.insert_node(3);
215 auto n4 = g.insert_node(4);
216
217 // Create 2-connected graph
218 g.insert_arc(n1, n2);
219 g.insert_arc(n2, n3);
220 g.insert_arc(n3, n4);
221 g.insert_arc(n4, n1);
222 g.insert_arc(n1, n3); // diagonal
223
224 cout << "Graph: Square with diagonal\n";
225 cout << " 1---2\n";
226 cout << " |\\ |\n";
227 cout << " | \\ |\n";
228 cout << " 4---3\n\n";
229
230 cout << "COMPUTATION STEPS:\n";
231 cout << "1. Pick a source node (say, node 1)\n";
232 cout << "2. For each other node as sink:\n";
233 cout << " a. Build unit-capacity network\n";
234 cout << " b. Compute max-flow from source to sink\n";
235 cout << " c. Max-flow = number of edge-disjoint paths\n";
236 cout << "3. Minimum over all sinks = edge connectivity\n\n";
237
238 cout << "EXAMPLE CALCULATION:\n";
239 cout << " Node 1 to Node 2: max-flow = 2 (direct + via 4 or 3)\n";
240 cout << " Node 1 to Node 3: max-flow = 2 (direct + via 2 or 4)\n";
241 cout << " Node 1 to Node 4: max-flow = 2 (direct + via 2-3)\n";
242 cout << " Minimum = 2\n";
243 cout << " Edge connectivity = 2 ✓\n\n";
244
245 cout << "COMPLEXITY:\n";
246 cout << " * Need O(V) max-flow computations\n";
247 cout << " * Each max-flow: O(V * E^2) with Edmonds-Karp\n";
248 cout << " * Total: O(V^2 * E^2)\n";
249 cout << " * Practical for graphs with thousands of nodes\n\n";
250 }
251
252 // =========================================================================
253 // EXAMPLE 5: Real-World Network Design
254 // =========================================================================
255 {
256 cout << "--- Example 5: Designing Reliable Networks ---\n\n";
257
258 cout << "CASE STUDY: Data Center Network\n";
259 cout << "===============================\n\n";
260
261 cout << "REQUIREMENTS:\n";
262 cout << " * 100 servers must stay connected\n";
263 cout << " * Network must survive any 2 simultaneous failures\n";
264 cout << " * Minimize number of switches (cost)\n\n";
265
266 cout << "SOLUTION: 3-Connected Topology\n";
267 cout << " * Each server connects to 3 switches\n";
268 cout << " * Switches form highly connected mesh\n";
269 cout << " * Any 2 links can fail, connectivity preserved\n\n";
270
271 cout << "DESIGN CHOICES:\n\n";
272
273 cout << "Option A: Tree (k=1)\n";
274 cout << " Pros: Minimal cost (n-1 edges)\n";
275 cout << " Cons: No fault tolerance\n";
276 cout << " Decision: ✗ Too risky for data center\n\n";
277
278 cout << "Option B: Ring (k=2)\n";
279 cout << " Pros: Moderate cost, survives 1 failure\n";
280 cout << " Cons: Still vulnerable to 2 failures\n";
281 cout << " Decision: ✗ Insufficient for requirements\n\n";
282
283 cout << "Option C: 3-Connected Mesh (k=3)\n";
284 cout << " Pros: Survives 2 failures (meets requirement!)\n";
285 cout << " Cons: Higher cost (more cables)\n";
286 cout << " Decision: ✓ Best fit for critical infrastructure\n\n";
287
288 cout << "COST-BENEFIT ANALYSIS:\n";
289 cout << " Extra cost: ~50% more cables than ring\n";
290 cout << " Benefit: Can survive 2 simultaneous failures\n";
291 cout << " ROI: Downtime costs far exceed cable costs\n";
292 cout << " Conclusion: Worth the investment\n\n";
293 }
294
295 cout << "=== SUMMARY: K-Connected Graphs ===\n";
296 cout << "\n1. DEFINITION:\n";
297 cout << " k-connected: Removing any k-1 vertices keeps graph connected\n";
298 cout << " Edge connectivity: Min edges to remove to disconnect\n";
299 cout << " Higher k = more robust network\n";
300 cout << "\n2. CONNECTIVITY LEVELS:\n";
301 cout << " k=1: Tree-like, has bridges (weak)\n";
302 cout << " k=2: No bridges, survives 1 failure (good)\n";
303 cout << " k=3+: Highly redundant, very robust (excellent)\n";
304 cout << "\n3. HOW TO COMPUTE:\n";
305 cout << " Use maximum flow algorithm\n";
306 cout << " Build unit-capacity network\n";
307 cout << " Compute min-cut = edge connectivity\n";
308 cout << " Time: O(V^2 * E^2)\n";
309 cout << "\n4. DESIGN PRINCIPLES:\n";
310 cout << " * Critical systems: k ≥ 3 (data centers, hospitals)\n";
311 cout << " * Important systems: k = 2 (corporate networks)\n";
312 cout << " * Non-critical: k = 1 acceptable (home networks)\n";
313 cout << " * Always consider cost vs reliability trade-off\n";
314 cout << "\n5. REAL-WORLD APPLICATIONS:\n";
315 cout << " ✓ Internet backbone design\n";
316 cout << " ✓ Power grid planning\n";
317 cout << " ✓ Transportation networks\n";
318 cout << " ✓ Data center topologies\n";
319 cout << " ✓ Telecommunications infrastructure\n";
320 cout << "\n6. FAILURE ANALYSIS:\n";
321 cout << " k=1: Any edge failure → disconnect\n";
322 cout << " k=2: Survives 1 edge failure\n";
323 cout << " k=3: Survives 2 edge failures\n";
324 cout << " General: Survives k-1 failures\n";
325 cout << "\n7. COST CONSIDERATIONS:\n";
326 cout << " Tree (k=1): n-1 edges (minimum)\n";
327 cout << " Cycle (k=2): n edges (+1)\n";
328 cout << " Complete (k=n-1): n*(n-1)/2 edges (maximum)\n";
329 cout << " Practical: k=2 or k=3 for most applications\n";
330
331 return 0;
332}
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
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 graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Generic graph and digraph implementations.
int main()