Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_indexNode_example.cc
Go to the documentation of this file.
1
24#include <iostream>
25#include <tpl_indexNode.H>
26#include <tpl_graph.H>
27
28using namespace std;
29using namespace Aleph;
30
31int main()
32{
33 cout << "=== Node Indexing: Educational Examples ===\n\n";
34
35 // =========================================================================
36 // EXAMPLE 1: Basic Node Indexing
37 // =========================================================================
38 {
39 cout << "--- Example 1: Fast Node Lookup ---\n\n";
40
42 GT g;
43
44 // STEP 1: Create node index
45 // IndexNode maintains a search tree of nodes by their value
47
48 cout << "Creating social network with unique usernames...\n";
49
50 // STEP 2: Insert nodes through index
51 // This adds to BOTH graph and search tree
52 auto alice = node_idx.insert_in_graph("Alice");
53 auto bob = node_idx.insert_in_graph("Bob");
54 auto charlie = node_idx.insert_in_graph("Charlie");
55 auto diana = node_idx.insert_in_graph("Diana");
56 (void)alice;
57 (void)bob;
59 (void)diana;
60
61 cout << "Added users: Alice, Bob, Charlie, Diana\n\n";
62
63 // STEP 3: Fast O(log n) search by username
64 cout << "SEARCH DEMONSTRATIONS:\n";
65
66 auto found = node_idx.search("Charlie");
67 if (found)
68 cout << " search(\"Charlie\"): FOUND (O(log n) time)\n";
69
70 auto not_found = node_idx.search("Eve");
71 if (not_found)
72 cout << " search(\"Eve\"): FOUND\n";
73 else
74 cout << " search(\"Eve\"): NOT FOUND (O(log n) time)\n";
75
76 cout << "\nWITHOUT INDEXING: Would scan all " << g.get_num_nodes()
77 << " nodes linearly\n";
78 cout << "WITH INDEXING: Binary search tree lookup\n\n";
79 }
80
81 // =========================================================================
82 // EXAMPLE 2: Custom Comparator (Advanced)
83 // =========================================================================
84 {
85 cout << "--- Example 2: Custom Node Comparison ---\n\n";
86
87 // Define custom struct for nodes
88 struct User {
89 int id;
90 string name;
91 int age;
92
93 bool operator<(const User& other) const {
94 return id < other.id; // Compare by ID
95 }
96 };
97
99 GT g;
100
101 // Custom comparator to index by user ID
102 struct UserIdCmp {
103 bool operator()(GT::Node* a, GT::Node* b) const {
104 return a->get_info().id < b->get_info().id;
105 }
106 };
107
108 // Create index with custom comparator
110
111 cout << "User database indexed by ID:\n";
112
113 // Insert users
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});
117 (void)u1;
118 (void)u2;
119 (void)u3;
120
121 cout << " Added: Alice(1001), Bob(1003), Charlie(1002)\n";
122 cout << " Index maintains sorted order by ID\n\n";
123
124 // Search by creating a probe node
125 User probe{1002, "", 0}; // Only ID matters for search
127
128 auto found = user_idx.search(&probe_node);
129 if (found)
130 cout << "Found user 1002: " << found->get_info().name << "\n";
131
132 cout << "\nKEY FEATURE: Can index by ANY field (ID, name, email, etc.)\n";
133 cout << " Just provide appropriate comparator\n\n";
134 }
135
136 // =========================================================================
137 // EXAMPLE 3: Duplicate Prevention
138 // =========================================================================
139 {
140 cout << "--- Example 3: Enforcing Uniqueness ---\n\n";
141
143 GT g;
145
146 cout << "Inserting node with ID 100...\n";
147 auto n1 = node_idx.insert_in_graph(100);
148 (void)n1;
149 cout << " Success! Node count: " << g.get_num_nodes() << "\n\n";
150
151 cout << "Attempting to insert duplicate ID 100...\n";
152 // Check first to avoid duplicate
153 if (node_idx.search(100))
154 {
155 cout << " PREVENTED: ID 100 already exists\n";
156 cout << " Node count unchanged: " << g.get_num_nodes() << "\n";
157 }
158
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";
164 }
165
166 // =========================================================================
167 // EXAMPLE 4: Performance Comparison
168 // =========================================================================
169 {
170 cout << "--- Example 4: Performance Analysis ---\n\n";
171
173 GT g;
174 IndexNode<GT> idx(g);
175
176 const int N = 100;
177
178 cout << "Creating graph with " << N << " nodes...\n";
179 for (int i = 0; i < N; ++i)
180 idx.insert_in_graph(i);
181
182 cout << "\nSEARCH COMPLEXITY COMPARISON:\n\n";
183
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";
189
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";
193
194 cout << "SPEEDUP: 50/7 = ~7x faster on average!\n";
195 cout << " Grows with graph size: O(n) vs O(log n)\n\n";
196 }
197
198 // =========================================================================
199 // EXAMPLE 5: Range Queries (Bonus)
200 // =========================================================================
201 {
202 cout << "--- Example 5: Ordered Traversal ---\n\n";
203
205 GT g;
206 IndexNode<GT> idx(g);
207
208 // Insert nodes in random order
209 int values[] = {50, 30, 70, 20, 40, 60, 80};
210 cout << "Inserting in order: ";
211 for (int v : values)
212 {
213 idx.insert_in_graph(v);
214 cout << v << " ";
215 }
216 cout << "\n\n";
217
218 // Index maintains sorted order internally
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";
223
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";
228 }
229
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";
254
255 return 0;
256}
bool operator<(const Time &l, const Time &r)
Definition ah-time.H:142
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.
Definition graph-dry.H:494
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
#define N
Definition fib.C:294
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.
Node indexing for fast O(log n) lookup by key.
int main()