41using namespace std::chrono;
45 cout <<
"=== Indexed Graphs: Educational Examples ===\n\n";
53 cout <<
"--- Example 1: Basic Operations ---\n\n";
63 cout <<
"Creating indexed graph with integer nodes...\n";
73 cout <<
"Inserted nodes: 100, 200, 150, 50\n";
74 cout <<
"Index maintains sorted order for O(log n) search\n\n";
77 cout <<
"SEARCH DEMONSTRATION:\n";
81 cout <<
" search_node(150): FOUND (O(log n) time!)\n";
83 cout <<
" search_node(150): NOT FOUND\n";
87 cout <<
" search_node(999): FOUND\n";
89 cout <<
" search_node(999): NOT FOUND (O(log n) time!)\n";
95 cout <<
"\nInserted 2 arcs\n";
100 cout <<
"Found arc 100->200 with weight: " << arc->get_info() <<
"\n\n";
103 cout <<
"KEY INSIGHT: Without indexing, search_node() would scan ALL nodes\n";
104 cout <<
" With indexing: O(log n) binary search tree lookup\n";
105 cout <<
" For n=1000 nodes: 1000 vs ~10 comparisons!\n\n";
114 cout <<
"--- Example 2: Automatic Duplicate Prevention ---\n\n";
121 cout <<
"Inserting unique nodes...\n";
129 cout <<
" Inserted: Alice, Bob, Charlie\n";
133 cout <<
"Attempting to insert duplicate 'Bob'...\n";
137 cout <<
" PREVENTED: 'Bob' already exists in index\n";
138 cout <<
" Returned pointer to existing node\n";
144 cout <<
"REAL-WORLD APPLICATION:\n";
145 cout <<
" Social Network: Usernames must be unique\n";
146 cout <<
" Database: Primary keys must be unique\n";
147 cout <<
" Directory: File names in same folder must be unique\n\n";
155 cout <<
"--- Example 3: Performance Comparison ---\n\n";
160 cout <<
"Creating graph with " <<
N <<
" nodes...\n\n";
167 for (
int i = 0; i <
N; ++i)
171 cout <<
"BENCHMARK: Searching for 100 random nodes\n\n";
174 auto start = high_resolution_clock::now();
176 for (
int i = 0; i < 100; ++i)
182 auto end = high_resolution_clock::now();
185 cout <<
"Indexed Graph (O(log n) search):\n";
193 cout <<
"THEORETICAL SPEEDUP:\n";
194 cout <<
" Linear search: O(n) = ~" << (
N/2) <<
" comparisons average\n";
195 cout <<
" Indexed search: O(log n) = ~" << (
int)
log2(
N) <<
" comparisons\n";
198 cout <<
"CONCLUSION: For large graphs (n > 1000), indexing is ESSENTIAL\n\n";
206 cout <<
"--- Example 4: Dynamic Graph Updates ---\n\n";
213 cout <<
"Initial graph: ";
214 for (
int i = 1; i <= 5; ++i)
217 cout << (i*10) <<
" ";
222 cout <<
"\nAdding nodes 25, 35, 45...\n";
228 cout <<
"All nodes still searchable in O(log n)\n";
232 for (
int val : {10, 20, 25, 30, 35, 40, 45, 50})
239 cout <<
"KEY FEATURE: Index self-balances (uses treaps by default)\n";
240 cout <<
" Maintains O(log n) search even after many updates\n\n";
243 cout <<
"=== SUMMARY: When to Use Index_Graph ===\n";
244 cout <<
"\n1. GRAPH SIZE:\n";
245 cout <<
" - Small graphs (n < 100): Regular graph is fine\n";
246 cout <<
" - Medium graphs (100 < n < 1000): Indexing helps\n";
247 cout <<
" - Large graphs (n > 1000): Indexing is ESSENTIAL\n";
248 cout <<
"\n2. ACCESS PATTERN:\n";
249 cout <<
" Use indexing if you frequently:\n";
250 cout <<
" * Search for nodes by value\n";
251 cout <<
" * Check if arc exists between two nodes\n";
252 cout <<
" * Need to prevent duplicate nodes\n";
253 cout <<
"\n3. TRADE-OFFS:\n";
254 cout <<
" Benefits: O(log n) search vs O(n)\n";
255 cout <<
" Cost: Extra memory + slightly slower inserts\n";
256 cout <<
"\n4. REAL-WORLD EXAMPLES:\n";
257 cout <<
" * Social networks (find user by name)\n";
258 cout <<
" * Road networks (find city by name)\n";
259 cout <<
" * Dependency graphs (find package by name)\n";
260 cout <<
" * Database relations (indexed foreign keys)\n";
261 cout <<
"\n5. COMPLEXITY SUMMARY:\n";
262 cout <<
" Operation | Regular | Indexed | Speedup\n";
263 cout <<
" ------------------|---------|----------|--------\n";
264 cout <<
" Insert node | O(1) | O(log n) | Slower\n";
265 cout <<
" Search node | O(n) | O(log n) | n/log n\n";
266 cout <<
" Search arc | O(deg) | O(log m) | High\n";
267 cout <<
" Memory | O(n+m) | O(n+m) | Same\n";
Construye índices de nodos y arcos para su rápida búsqueda y recuperación.
GT_Node * search_node(GT_Node *p)
Busca en el índice un nodo.
GT_Arc * search_arc(GT_Node *src, GT_Node *tgt)
Busca en el índice un arco dados dos nodos.
GT_Node * insert_node(const GT_Node_Type &info)
Crea un nuevo nodo y lo inserta en grafo y en el índice.
GT_Arc * insert_arc(GT_Node *src, GT_Node *tgt, const GT_Arc_Type &info=GT_Arc_Type())
Crea un nuevo arco entre dos nodos y lo inserta en el grafo y en el índice.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log2_function > > log2(const __gmp_expr< T, U > &expr)
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.
Graph indexing utilities for O(log n) node/arc lookup.