39 cout <<
"=== Arc Indexing: Educational Examples ===\n\n";
45 cout <<
"--- Example 1: Fast Arc Lookup ---\n\n";
59 cout <<
"Road network: NYC, Boston, DC, Philadelphia\n\n";
62 cout <<
"Adding roads (directed):\n";
70 cout <<
" NYC <-> Boston: 215 miles\n";
71 cout <<
" NYC <-> Philadelphia: 95 miles\n";
72 cout <<
" NYC <-> DC: 225 miles\n";
73 cout <<
" Philadelphia <-> DC: 140 miles\n\n";
76 cout <<
"QUERY: Is there a direct road from NYC to Boston?\n";
79 cout <<
" YES! Distance: " <<
road->get_info() <<
" miles (O(log m) lookup)\n";
81 cout <<
" NO direct road\n";
83 cout <<
"\nQUERY: Is there a direct road from Boston to DC?\n";
86 cout <<
" YES! Distance: " <<
no_road->get_info() <<
" miles\n";
88 cout <<
" NO direct road (O(log m) lookup)\n";
90 cout <<
"\nKEY BENEFIT: Without index, would iterate through all arcs of source node\n";
91 cout <<
" With index: Direct O(log m) binary search tree lookup\n\n";
98 cout <<
"--- Example 2: Querying Arc Weights ---\n\n";
110 cout <<
"Network topology with bandwidth (Gbps):\n";
117 cout <<
" All servers connected to router at 10 Gbps\n\n";
120 cout <<
"Checking connection bandwidth:\n";
123 cout <<
" Server1 -> Router: " <<
link1->get_info() <<
" Gbps\n";
127 cout <<
" Server2 -> Router: " <<
link2->get_info() <<
" Gbps\n";
132 cout <<
" Server1 -> Server2: Direct link\n";
134 cout <<
" Server1 -> Server2: No direct link (must route through router)\n";
136 cout <<
"\nAPPLICATION: Network topology queries, routing decisions\n\n";
143 cout <<
"--- Example 3: Parallel Arc Detection ---\n\n";
152 cout <<
"Transportation network between two cities:\n";
156 cout <<
" Added: Highway\n";
161 cout <<
" WARNING: Arc A->B already exists!\n";
162 cout <<
" Cannot add parallel arc with IndexArc (simple graph assumption)\n";
165 cout <<
"\nIMPORTANT: IndexArc assumes SIMPLE GRAPH (no parallel arcs)\n";
166 cout <<
" One arc per (source, target) pair\n";
167 cout <<
" Use multi-graph if parallel arcs needed\n\n";
174 cout <<
"--- Example 4: Performance Analysis ---\n\n";
181 cout <<
"Creating dense graph: " <<
N <<
" nodes...\n";
185 for (
int i = 0; i <
N; ++i)
190 for (
int i = 0; i <
N; ++i)
191 for (
int j = i + 1; j <
N; ++j)
192 if ((i + j) % 2 == 0)
198 cout <<
" Nodes: " <<
N <<
"\n";
202 cout <<
"SEARCH COMPLEXITY:\n\n";
204 cout <<
"Without Index (iterate outgoing arcs):\n";
205 cout <<
" Best: O(1) - arc is first\n";
206 cout <<
" Average: O(deg/2) - scan half of arcs\n";
207 cout <<
" Worst: O(deg) - scan all arcs\n";
210 cout <<
"With IndexArc:\n";
211 cout <<
" All cases: O(log m) - binary search tree\n";
215 cout <<
"SPEEDUP: ~" << (
int)
speedup <<
"x faster for arc queries!\n\n";
222 cout <<
"--- Example 5: Dynamic Arc Management ---\n\n";
232 cout <<
"Building graph dynamically...\n";
235 arc_idx.insert_in_graph(a, b, 10);
236 arc_idx.insert_in_graph(b, c, 20);
237 cout <<
" Added: A->B(10), B->C(20)\n";
242 cout <<
" Found arc A->B with weight " <<
arc_ab->get_info() <<
"\n";
245 arc_idx.insert_in_graph(a, c, 30);
246 cout <<
" Added: A->C(30)\n";
249 cout <<
"\nIndex automatically maintains balance\n";
250 cout <<
"All arc queries remain O(log m) after updates\n\n";
253 cout <<
"=== SUMMARY: IndexArc Best Practices ===\n";
254 cout <<
"\n1. WHEN TO USE:\n";
255 cout <<
" ✓ Dense graphs (high average degree)\n";
256 cout <<
" ✓ Frequent 'does arc exist?' queries\n";
257 cout <<
" ✓ Need arc weight/data lookup\n";
258 cout <<
" ✓ Adjacency matrix-like access pattern\n";
259 cout <<
"\n2. DESIGN PATTERNS:\n";
260 cout <<
" - Road networks: Query route existence\n";
261 cout <<
" - Social graphs: Check friendship status\n";
262 cout <<
" - Network topology: Verify link existence\n";
263 cout <<
" - Dependency graphs: Check direct dependency\n";
264 cout <<
"\n3. LIMITATIONS:\n";
265 cout <<
" ✗ Assumes SIMPLE GRAPH (no parallel arcs)\n";
266 cout <<
" ✗ Extra memory for index: O(m)\n";
267 cout <<
" ✗ Slightly slower insertions: O(log m) vs O(1)\n";
268 cout <<
"\n4. PERFORMANCE:\n";
269 cout <<
" Without index: O(degree) arc iteration\n";
270 cout <<
" With index: O(log m) tree lookup\n";
271 cout <<
" Speedup: degree / log(m) times faster\n";
272 cout <<
"\n5. COMPLEXITY SUMMARY:\n";
273 cout <<
" Insert arc: O(log m) - add to tree\n";
274 cout <<
" Search arc: O(log m) - tree lookup\n";
275 cout <<
" Remove arc: O(log m) - tree deletion\n";
276 cout <<
" Memory: O(m) - tree overhead\n";
277 cout <<
"\n6. BEST PRACTICES:\n";
278 cout <<
" - Use with IndexNode for complete indexing\n";
279 cout <<
" - Check arc existence before insert\n";
280 cout <<
" - Ideal for graphs with many arcs per node\n";
281 cout <<
" - Not needed for sparse graphs (low degree)\n";
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Append a new item by copy.
Index for fast arc lookup by its endpoint nodes.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log2_function > > log2(const __gmp_expr< T, U > &expr)
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of graph implemented with double-linked adjacency lists.
Generic graph and digraph implementations.
Arc indexing for fast lookup by endpoint nodes.