39 cout <<
"=== Dynamic Matrices: Educational Examples ===\n\n";
45 cout <<
"--- Example 1: Creating and Accessing Matrices ---\n\n";
50 cout <<
"Created 3x4 matrix (all zeros initially)\n\n";
53 cout <<
"Setting some values:\n";
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";
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";
71 cout <<
"Complete matrix:\n";
72 for (
size_t i = 0; i < 3; ++i)
75 for (
size_t j = 0; j < 4; ++j)
77 double val = mat.
read(i, j);
86 cout <<
"\nKEY INSIGHT: Unwritten cells automatically return default value\n";
87 cout <<
" Memory efficient for sparse matrices!\n\n";
94 cout <<
"--- Example 2: Automatic Resizing ---\n\n";
99 cout <<
"Initial size: 2x2\n";
104 cout <<
" [ 10 0 ]\n";
105 cout <<
" [ 0 20 ]\n\n";
108 cout <<
"Growing matrix to 6x6:\n";
116 cout <<
" Resized to 6x6\n";
117 cout <<
" Copied existing values and added new one at [5,5]\n\n";
119 cout <<
"New matrix values:\n";
123 cout <<
" All other cells = 0 (default)\n\n";
125 cout <<
"NOTE: DynMatrix has fixed size after construction\n";
126 cout <<
" Create new larger matrix if size needs to grow\n\n";
133 cout <<
"--- Example 3: Graph Adjacency Matrix ---\n\n";
135 cout <<
"SCENARIO: Social network (who knows whom)\n";
136 cout <<
"=========================================\n\n";
141 cout <<
"People: 0=Alice, 1=Bob, 2=Charlie, 3=Diana, 4=Eve\n\n";
144 cout <<
"Friendships:\n";
151 cout <<
" Alice <-> Bob\n";
152 cout <<
" Alice <-> Charlie\n";
159 cout <<
" Bob <-> Charlie\n";
160 cout <<
" Bob <-> Diana\n";
165 cout <<
" Diana <-> Eve\n\n";
168 cout <<
"Adjacency Matrix:\n";
169 cout <<
" A B C D E\n";
170 for (
size_t i = 0; i < 5; ++i)
172 char names[] = {
'A',
'B',
'C',
'D',
'E'};
174 for (
size_t j = 0; j < 5; ++j)
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";
184 cout <<
"ADVANTAGE: O(1) lookup for \"are X and Y connected?\"\n\n";
191 cout <<
"--- Example 4: Distance Matrix ---\n\n";
193 cout <<
"SCENARIO: City distances (miles)\n";
194 cout <<
"================================\n\n";
196 const int INF = 9999;
199 cout <<
"Cities: 0=NYC, 1=Boston, 2=Philadelphia, 3=DC\n\n";
202 for (
int i = 0; i < 4; ++i)
206 dist.
write(0, 1, 215);
207 dist.
write(1, 0, 215);
209 dist.
write(0, 2, 95);
210 dist.
write(2, 0, 95);
212 dist.
write(0, 3, 225);
213 dist.
write(3, 0, 225);
215 dist.
write(2, 3, 140);
216 dist.
write(3, 2, 140);
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";
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)
231 for (
int j = 0; j < 4; ++j)
233 int d = dist.
read(i, j);
237 cout <<
" " << d <<
" ";
239 cout <<
" " << d <<
" ";
241 cout <<
" " << d <<
" ";
246 cout <<
"\nNOTE: Boston to DC = INF (no direct route)\n";
247 cout <<
" Must go through NYC or Philadelphia\n\n";
249 cout <<
"USE CASE: Input for Floyd-Warshall algorithm\n";
250 cout <<
" Computes shortest paths between all pairs\n\n";
257 cout <<
"--- Example 5: Matrix Operations ---\n\n";
275 cout <<
"Matrix A:\n";
276 cout <<
" [ 1.0 2.0 ]\n";
277 cout <<
" [ 3.0 4.0 ]\n\n";
279 cout <<
"Matrix B:\n";
280 cout <<
" [ 5.0 6.0 ]\n";
281 cout <<
" [ 7.0 8.0 ]\n\n";
283 cout <<
"OPERATIONS:\n\n";
286 cout <<
"A + B (element-wise):\n";
287 cout <<
" [ 6.0 8.0 ]\n";
288 cout <<
" [10.0 12.0 ]\n\n";
292 cout <<
" [ 2.0 4.0 ]\n";
293 cout <<
" [ 6.0 8.0 ]\n\n";
295 cout <<
"NOTE: DynMatrix provides storage structure\n";
296 cout <<
" Arithmetic operations can be built on top\n\n";
303 cout <<
"--- Example 6: Sparse Matrix Benefits ---\n\n";
305 cout <<
"SCENARIO: Large sparse matrix (mostly zeros)\n";
306 cout <<
"============================================\n\n";
311 cout <<
"Matrix size: 1000 x 1000 = 1,000,000 cells\n\n";
315 sparse.write(100, 200, 2);
316 sparse.write(500, 750, 3);
317 sparse.write(999, 999, 4);
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";
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";
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";
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";
Dynamic matrix with sparse storage.
const T & read(const size_t i, const size_t j) const
Read the entry at position (i, j).
T & write(const size_t i, const size_t j, const T &data)
Write a value to position (i, j).
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Dynamic matrix with lazy allocation.