33 cout <<
"=== Node Indexing: Educational Examples ===\n\n";
39 cout <<
"--- Example 1: Fast Node Lookup ---\n\n";
48 cout <<
"Creating social network with unique usernames...\n";
61 cout <<
"Added users: Alice, Bob, Charlie, Diana\n\n";
64 cout <<
"SEARCH DEMONSTRATIONS:\n";
68 cout <<
" search(\"Charlie\"): FOUND (O(log n) time)\n";
72 cout <<
" search(\"Eve\"): FOUND\n";
74 cout <<
" search(\"Eve\"): NOT FOUND (O(log n) time)\n";
77 <<
" nodes linearly\n";
78 cout <<
"WITH INDEXING: Binary search tree lookup\n\n";
85 cout <<
"--- Example 2: Custom Node Comparison ---\n\n";
111 cout <<
"User database indexed by ID:\n";
114 auto u1 =
user_idx.insert_in_graph({1001,
"Alice", 30});
115 auto u2 =
user_idx.insert_in_graph({1003,
"Bob", 25});
116 auto u3 =
user_idx.insert_in_graph({1002,
"Charlie", 35});
121 cout <<
" Added: Alice(1001), Bob(1003), Charlie(1002)\n";
122 cout <<
" Index maintains sorted order by ID\n\n";
130 cout <<
"Found user 1002: " <<
found->get_info().name <<
"\n";
132 cout <<
"\nKEY FEATURE: Can index by ANY field (ID, name, email, etc.)\n";
133 cout <<
" Just provide appropriate comparator\n\n";
140 cout <<
"--- Example 3: Enforcing Uniqueness ---\n\n";
146 cout <<
"Inserting node with ID 100...\n";
147 auto n1 =
node_idx.insert_in_graph(100);
151 cout <<
"Attempting to insert duplicate ID 100...\n";
155 cout <<
" PREVENTED: ID 100 already exists\n";
159 cout <<
"\nREAL-WORLD APPLICATIONS:\n";
160 cout <<
" * Database primary keys\n";
161 cout <<
" * User registration (unique usernames/emails)\n";
162 cout <<
" * File systems (unique paths)\n";
163 cout <<
" * Network routing (unique IP addresses)\n\n";
170 cout <<
"--- Example 4: Performance Analysis ---\n\n";
178 cout <<
"Creating graph with " <<
N <<
" nodes...\n";
179 for (
int i = 0; i <
N; ++i)
182 cout <<
"\nSEARCH COMPLEXITY COMPARISON:\n\n";
184 cout <<
"Linear Search (without index):\n";
185 cout <<
" Best case: O(1) - found immediately\n";
186 cout <<
" Average: O(n/2) - scan half the nodes\n";
187 cout <<
" Worst case: O(n) - scan all nodes\n";
188 cout <<
" For n=100: ~50 comparisons average\n\n";
190 cout <<
"Indexed Search (with IndexNode):\n";
191 cout <<
" All cases: O(log n) - binary search tree\n";
192 cout <<
" For n=100: ~7 comparisons always\n\n";
194 cout <<
"SPEEDUP: 50/7 = ~7x faster on average!\n";
195 cout <<
" Grows with graph size: O(n) vs O(log n)\n\n";
202 cout <<
"--- Example 5: Ordered Traversal ---\n\n";
209 int values[] = {50, 30, 70, 20, 40, 60, 80};
210 cout <<
"Inserting in order: ";
219 cout <<
"Internal index maintains BST order:\n";
220 cout <<
" Allows efficient range queries\n";
221 cout <<
" Enables sorted iteration\n";
222 cout <<
" Supports predecessor/successor queries\n\n";
224 cout <<
"ADVANCED FEATURES (if needed):\n";
225 cout <<
" * Find all nodes in range [a, b]\n";
226 cout <<
" * Find k smallest/largest values\n";
227 cout <<
" * Iterate nodes in sorted order\n\n";
230 cout <<
"=== SUMMARY: IndexNode Best Practices ===\n";
231 cout <<
"\n1. WHEN TO USE:\n";
232 cout <<
" ✓ Need fast node lookup by value\n";
233 cout <<
" ✓ Graph has unique node identifiers\n";
234 cout <<
" ✓ Frequent search operations\n";
235 cout <<
" ✓ Large graphs (n > 100)\n";
236 cout <<
"\n2. DESIGN PATTERNS:\n";
237 cout <<
" - Social networks: Index by username\n";
238 cout <<
" - Databases: Index by primary key\n";
239 cout <<
" - IP networks: Index by IP address\n";
240 cout <<
" - File systems: Index by path\n";
241 cout <<
"\n3. PERFORMANCE TIPS:\n";
242 cout <<
" - Always search before insert (check duplicates)\n";
243 cout <<
" - Use custom comparator for complex types\n";
244 cout <<
" - Index is self-balancing (treaps)\n";
245 cout <<
"\n4. MEMORY OVERHEAD:\n";
246 cout <<
" - Extra O(n) space for search tree\n";
247 cout <<
" - Typical: ~24 bytes per node pointer\n";
248 cout <<
" - Negligible for graphs > 100 nodes\n";
249 cout <<
"\n5. COMPLEXITY SUMMARY:\n";
250 cout <<
" Insert: O(log n) - add to tree\n";
251 cout <<
" Search: O(log n) - tree lookup\n";
252 cout <<
" Remove: O(log n) - tree deletion\n";
253 cout <<
" Memory: O(n) - tree overhead\n";
bool operator<(const Time &l, const Time &r)
Builds an index of nodes for fast search and retrieval.
GT_Node * insert_in_graph(const GT_Node_Type &info)
Creates a new node with generic content info, inserts it into the graph, and then into the index.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
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.
Node indexing for fast O(log n) lookup by key.