This repository was archived by the owner on Jun 18, 2026. It is now read-only.
Commit 6797ec6
committed
perf: track infected nodes in SIR simulation instead of scanning all vertices
The SIR simulateSIR() method previously iterated all V vertices twice per
round: once to find infected nodes for spreading, and once for recovery
checks, plus a third pass to check if any infected remain.
Replace with an explicit currentlyInfected set that is maintained
incrementally. This reduces per-round cost from O(V) to O(|infected|)
for the recovery check and termination test, which is significant for
large sparse graphs where only a small fraction of nodes are infected
at any given time. The infection spreading loop also benefits since it
now only iterates currently infected nodes.
This is especially impactful during Monte Carlo simulations where
simulateSIR is called thousands of times.1 parent e7a2803 commit 6797ec6
1 file changed
Lines changed: 16 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
315 | 315 | | |
316 | 316 | | |
317 | 317 | | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
318 | 327 | | |
319 | 328 | | |
320 | 329 | | |
321 | | - | |
322 | | - | |
| 330 | + | |
323 | 331 | | |
324 | 332 | | |
325 | 333 | | |
326 | 334 | | |
327 | 335 | | |
328 | 336 | | |
329 | 337 | | |
330 | | - | |
331 | | - | |
| 338 | + | |
332 | 339 | | |
333 | 340 | | |
334 | 341 | | |
| |||
341 | 348 | | |
342 | 349 | | |
343 | 350 | | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
349 | 354 | | |
350 | 355 | | |
351 | 356 | | |
352 | 357 | | |
353 | 358 | | |
| 359 | + | |
354 | 360 | | |
355 | 361 | | |
356 | 362 | | |
357 | 363 | | |
358 | 364 | | |
359 | 365 | | |
360 | 366 | | |
| 367 | + | |
361 | 368 | | |
362 | 369 | | |
363 | 370 | | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | 371 | | |
370 | 372 | | |
371 | 373 | | |
| |||
0 commit comments