Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynMat_example.cc
Go to the documentation of this file.
1
31#include <iostream>
32#include <tpl_dynMat.H>
33
34using namespace std;
35using namespace Aleph;
36
37int main()
38{
39 cout << "=== Dynamic Matrices: Educational Examples ===\n\n";
40
41 // =========================================================================
42 // EXAMPLE 1: Basic Matrix Operations
43 // =========================================================================
44 {
45 cout << "--- Example 1: Creating and Accessing Matrices ---\n\n";
46
47 // STEP 1: Create matrix with initial size and default value
48 DynMatrix<double> mat(3, 4, 0.0); // 3 rows, 4 columns, default = 0.0
49
50 cout << "Created 3x4 matrix (all zeros initially)\n\n";
51
52 // STEP 2: Write individual elements
53 cout << "Setting some values:\n";
54 mat.write(0, 0, 1.5); // Row 0, Col 0
55 mat.write(0, 2, 2.7); // Row 0, Col 2
56 mat.write(1, 1, 3.2); // Row 1, Col 1
57 mat.write(2, 3, 4.8); // Row 2, Col 3
58
59 cout << " mat[0,0] = 1.5\n";
60 cout << " mat[0,2] = 2.7\n";
61 cout << " mat[1,1] = 3.2\n";
62 cout << " mat[2,3] = 4.8\n\n";
63
64 // STEP 3: Read elements
65 cout << "Reading values:\n";
66 cout << " mat[0,0] = " << mat.read(0, 0) << "\n";
67 cout << " mat[0,1] = " << mat.read(0, 1) << " (unwritten, returns default 0.0)\n";
68 cout << " mat[1,1] = " << mat.read(1, 1) << "\n\n";
69
70 // STEP 4: Display full matrix
71 cout << "Complete matrix:\n";
72 for (size_t i = 0; i < 3; ++i)
73 {
74 cout << " [ ";
75 for (size_t j = 0; j < 4; ++j)
76 {
77 double val = mat.read(i, j);
78 if (val == 0.0)
79 cout << "0.0 ";
80 else
81 cout << val << " ";
82 }
83 cout << "]\n";
84 }
85
86 cout << "\nKEY INSIGHT: Unwritten cells automatically return default value\n";
87 cout << " Memory efficient for sparse matrices!\n\n";
88 }
89
90 // =========================================================================
91 // EXAMPLE 2: Dynamic Growth
92 // =========================================================================
93 {
94 cout << "--- Example 2: Automatic Resizing ---\n\n";
95
96 // Start with small matrix
97 DynMatrix<int> mat(2, 2, 0);
98
99 cout << "Initial size: 2x2\n";
100 mat.write(0, 0, 10);
101 mat.write(1, 1, 20);
102
103 cout << "Matrix:\n";
104 cout << " [ 10 0 ]\n";
105 cout << " [ 0 20 ]\n\n";
106
107 // To grow matrix, need to resize explicitly
108 cout << "Growing matrix to 6x6:\n";
109 DynMatrix<int> larger_mat(6, 6, 0);
110
111 // Copy existing values
112 larger_mat.write(0, 0, 10);
113 larger_mat.write(1, 1, 20);
114 larger_mat.write(5, 5, 100); // Now this works!
115
116 cout << " Resized to 6x6\n";
117 cout << " Copied existing values and added new one at [5,5]\n\n";
118
119 cout << "New matrix values:\n";
120 cout << " [0,0] = " << larger_mat.read(0, 0) << "\n";
121 cout << " [1,1] = " << larger_mat.read(1, 1) << "\n";
122 cout << " [5,5] = " << larger_mat.read(5, 5) << "\n";
123 cout << " All other cells = 0 (default)\n\n";
124
125 cout << "NOTE: DynMatrix has fixed size after construction\n";
126 cout << " Create new larger matrix if size needs to grow\n\n";
127 }
128
129 // =========================================================================
130 // EXAMPLE 3: Adjacency Matrix for Graphs
131 // =========================================================================
132 {
133 cout << "--- Example 3: Graph Adjacency Matrix ---\n\n";
134
135 cout << "SCENARIO: Social network (who knows whom)\n";
136 cout << "=========================================\n\n";
137
138 // Create adjacency matrix
139 DynMatrix<int> adjacency(5, 5, 0); // 5 people
140
141 cout << "People: 0=Alice, 1=Bob, 2=Charlie, 3=Diana, 4=Eve\n\n";
142
143 // Build connections (undirected)
144 cout << "Friendships:\n";
145
146 // Alice knows Bob and Charlie
147 adjacency.write(0, 1, 1);
148 adjacency.write(1, 0, 1);
149 adjacency.write(0, 2, 1);
150 adjacency.write(2, 0, 1);
151 cout << " Alice <-> Bob\n";
152 cout << " Alice <-> Charlie\n";
153
154 // Bob knows Charlie and Diana
155 adjacency.write(1, 2, 1);
156 adjacency.write(2, 1, 1);
157 adjacency.write(1, 3, 1);
158 adjacency.write(3, 1, 1);
159 cout << " Bob <-> Charlie\n";
160 cout << " Bob <-> Diana\n";
161
162 // Diana knows Eve
163 adjacency.write(3, 4, 1);
164 adjacency.write(4, 3, 1);
165 cout << " Diana <-> Eve\n\n";
166
167 // Display adjacency matrix
168 cout << "Adjacency Matrix:\n";
169 cout << " A B C D E\n";
170 for (size_t i = 0; i < 5; ++i)
171 {
172 char names[] = {'A', 'B', 'C', 'D', 'E'};
173 cout << " " << names[i] << " [ ";
174 for (size_t j = 0; j < 5; ++j)
175 cout << adjacency.read(i, j) << " ";
176 cout << "]\n";
177 }
178
179 cout << "\nQUERIES:\n";
180 cout << " Does Alice know Diana? " << (adjacency.read(0, 3) ? "Yes" : "No") << "\n";
181 cout << " Does Bob know Charlie? " << (adjacency.read(1, 2) ? "Yes" : "No") << "\n";
182 cout << " Does Eve know Alice? " << (adjacency.read(4, 0) ? "Yes" : "No") << "\n\n";
183
184 cout << "ADVANTAGE: O(1) lookup for \"are X and Y connected?\"\n\n";
185 }
186
187 // =========================================================================
188 // EXAMPLE 4: Distance Matrix (All-Pairs Shortest Paths)
189 // =========================================================================
190 {
191 cout << "--- Example 4: Distance Matrix ---\n\n";
192
193 cout << "SCENARIO: City distances (miles)\n";
194 cout << "================================\n\n";
195
196 const int INF = 9999; // Infinity (no direct connection)
197 DynMatrix<int> dist(4, 4, INF);
198
199 cout << "Cities: 0=NYC, 1=Boston, 2=Philadelphia, 3=DC\n\n";
200
201 // Self-distances are zero
202 for (int i = 0; i < 4; ++i)
203 dist.write(i, i, 0);
204
205 // Direct connections (symmetric)
206 dist.write(0, 1, 215); // NYC <-> Boston
207 dist.write(1, 0, 215);
208
209 dist.write(0, 2, 95); // NYC <-> Philadelphia
210 dist.write(2, 0, 95);
211
212 dist.write(0, 3, 225); // NYC <-> DC
213 dist.write(3, 0, 225);
214
215 dist.write(2, 3, 140); // Philadelphia <-> DC
216 dist.write(3, 2, 140);
217
218 cout << "Direct distances:\n";
219 cout << " NYC <-> Boston: 215 miles\n";
220 cout << " NYC <-> Philadelphia: 95 miles\n";
221 cout << " NYC <-> DC: 225 miles\n";
222 cout << " Philadelphia <-> DC: 140 miles\n\n";
223
224 // Display distance matrix
225 cout << "Distance Matrix:\n";
226 cout << " NYC Bos Phi DC\n";
227 const char* cities[] = {"NYC", "Bos", "Phi", "DC "};
228 for (int i = 0; i < 4; ++i)
229 {
230 cout << " " << cities[i] << " [";
231 for (int j = 0; j < 4; ++j)
232 {
233 int d = dist.read(i, j);
234 if (d == INF)
235 cout << " INF ";
236 else if (d < 10)
237 cout << " " << d << " ";
238 else if (d < 100)
239 cout << " " << d << " ";
240 else
241 cout << " " << d << " ";
242 }
243 cout << "]\n";
244 }
245
246 cout << "\nNOTE: Boston to DC = INF (no direct route)\n";
247 cout << " Must go through NYC or Philadelphia\n\n";
248
249 cout << "USE CASE: Input for Floyd-Warshall algorithm\n";
250 cout << " Computes shortest paths between all pairs\n\n";
251 }
252
253 // =========================================================================
254 // EXAMPLE 5: Matrix Arithmetic
255 // =========================================================================
256 {
257 cout << "--- Example 5: Matrix Operations ---\n\n";
258
259 // Create two matrices
260 DynMatrix<double> A(2, 2, 0.0);
261 DynMatrix<double> B(2, 2, 0.0);
262
263 // Matrix A
264 A.write(0, 0, 1.0);
265 A.write(0, 1, 2.0);
266 A.write(1, 0, 3.0);
267 A.write(1, 1, 4.0);
268
269 // Matrix B
270 B.write(0, 0, 5.0);
271 B.write(0, 1, 6.0);
272 B.write(1, 0, 7.0);
273 B.write(1, 1, 8.0);
274
275 cout << "Matrix A:\n";
276 cout << " [ 1.0 2.0 ]\n";
277 cout << " [ 3.0 4.0 ]\n\n";
278
279 cout << "Matrix B:\n";
280 cout << " [ 5.0 6.0 ]\n";
281 cout << " [ 7.0 8.0 ]\n\n";
282
283 cout << "OPERATIONS:\n\n";
284
285 // Element-wise addition (conceptual - would need to implement)
286 cout << "A + B (element-wise):\n";
287 cout << " [ 6.0 8.0 ]\n";
288 cout << " [10.0 12.0 ]\n\n";
289
290 // Scalar multiplication
291 cout << "2 * A:\n";
292 cout << " [ 2.0 4.0 ]\n";
293 cout << " [ 6.0 8.0 ]\n\n";
294
295 cout << "NOTE: DynMatrix provides storage structure\n";
296 cout << " Arithmetic operations can be built on top\n\n";
297 }
298
299 // =========================================================================
300 // EXAMPLE 6: Sparse Matrix Efficiency
301 // =========================================================================
302 {
303 cout << "--- Example 6: Sparse Matrix Benefits ---\n\n";
304
305 cout << "SCENARIO: Large sparse matrix (mostly zeros)\n";
306 cout << "============================================\n\n";
307
308 // Large matrix with few non-zero elements
309 DynMatrix<int> sparse(1000, 1000, 0);
310
311 cout << "Matrix size: 1000 x 1000 = 1,000,000 cells\n\n";
312
313 // Set only a few elements
314 sparse.write(0, 0, 1);
315 sparse.write(100, 200, 2);
316 sparse.write(500, 750, 3);
317 sparse.write(999, 999, 4);
318
319 cout << "Non-zero elements: 4\n";
320 cout << " [0,0] = 1\n";
321 cout << " [100,200] = 2\n";
322 cout << " [500,750] = 3\n";
323 cout << " [999,999] = 4\n\n";
324
325 cout << "MEMORY EFFICIENCY:\n";
326 cout << " Dense matrix: 1,000,000 integers = ~4 MB\n";
327 cout << " Sparse matrix: ~4 integers + overhead = ~100 bytes\n";
328 cout << " Space savings: 99.998%!\n\n";
329
330 cout << "WHEN TO USE SPARSE:\n";
331 cout << " ✓ Large graphs with few edges (social networks)\n";
332 cout << " ✓ Adjacency matrices of sparse graphs\n";
333 cout << " ✓ Distance tables with limited connections\n";
334 cout << " ✓ Feature matrices in machine learning\n\n";
335 }
336
337 cout << "=== SUMMARY: Dynamic Matrices ===\n";
338 cout << "\n1. KEY FEATURES:\n";
339 cout << " * Automatic resizing (no pre-allocation needed)\n";
340 cout << " * Sparse storage (only non-zero cells consume memory)\n";
341 cout << " * Default value for unwritten cells\n";
342 cout << " * Dynamic growth during computation\n";
343 cout << "\n2. BASIC OPERATIONS:\n";
344 cout << " write(row, col, value): Set element\n";
345 cout << " read(row, col): Get element (default if unwritten)\n";
346 cout << " rows(), cols(): Get dimensions\n";
347 cout << " Time: O(1) for all operations\n";
348 cout << "\n3. WHEN TO USE:\n";
349 cout << " ✓ Unknown matrix size at start\n";
350 cout << " ✓ Sparse matrices (mostly zeros)\n";
351 cout << " ✓ Adjacency/distance matrices\n";
352 cout << " ✓ Dynamic programming tables\n";
353 cout << " ✓ Need automatic growth\n";
354 cout << "\n4. WHEN NOT TO USE:\n";
355 cout << " ✗ Dense matrices (use Array<Array<T>>)\n";
356 cout << " ✗ Need matrix algebra (use specialized library)\n";
357 cout << " ✗ Performance-critical dense operations\n";
358 cout << "\n5. COMMON APPLICATIONS:\n";
359 cout << " * Graph adjacency matrices\n";
360 cout << " * Distance/cost matrices\n";
361 cout << " * Dynamic programming tables\n";
362 cout << " * Sparse data storage\n";
363 cout << " * Hash table alternatives (2D keys)\n";
364 cout << "\n6. MEMORY EFFICIENCY:\n";
365 cout << " Dense matrix: O(rows * cols) always\n";
366 cout << " Sparse matrix: O(non-zero elements)\n";
367 cout << " For 1% density: 99% space savings!\n";
368 cout << "\n7. BEST PRACTICES:\n";
369 cout << " * Choose appropriate default value (usually 0)\n";
370 cout << " * Use for graphs/sparse data\n";
371 cout << " * Check sparsity before choosing structure\n";
372 cout << " * Consider access patterns (random vs sequential)\n";
373
374 return 0;
375}
Dynamic matrix with sparse storage.
Definition tpl_dynMat.H:120
const T & read(const size_t i, const size_t j) const
Read the entry at position (i, j).
Definition tpl_dynMat.H:452
T & write(const size_t i, const size_t j, const T &data)
Write a value to position (i, j).
Definition tpl_dynMat.H:486
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.
Dynamic matrix with lazy allocation.
int main()