This repository was archived by the owner on Jun 18, 2026. It is now read-only.
perf: track infected nodes in SIR simulation instead of scanning all vertices - #107
Merged
Merged
Conversation
…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.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Optimizes the SIR epidemic simulation by maintaining an explicit \currentlyInfected\ set instead of scanning all V vertices each round.
Problem
The previous implementation had three O(V) passes per simulation round:
For large graphs (thousands of nodes) with low infection rates, most of these iterations were wasted checking susceptible/recovered nodes.
Solution
Track the set of currently infected nodes incrementally:
This reduces per-round cost from O(V) to O(|infected| * avg_degree) for spreading + O(|infected|) for recovery, which is significant when |infected| << V.
Impact
Especially impactful during Monte Carlo simulations (\monteCarlo()) where \simulateSIR\ is called thousands of times. For a 10,000-node graph with 100 trials, this eliminates millions of unnecessary state lookups.