Skip to content
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
sauravbhattacharya001 merged 1 commit into
masterfrom
perf/sir-infected-tracking
Mar 28, 2026
Merged

sauravbhattacharya001 merged 1 commit into
masterfrom
perf/sir-infected-tracking

Conversation

@sauravbhattacharya001

Copy link
Copy Markdown
Owner

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:

  1. Find infected nodes for spreading
  2. Find infected nodes for recovery
  3. Check if any infected remain

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:

  • Add newly infected nodes to the set
  • Remove recovered nodes from the set
  • Loop terminates when the set is empty (O(1) check)

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.

…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.
@github-actions github-actions Bot added the visualization Graph visualization and UI label Mar 22, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@sauravbhattacharya001
sauravbhattacharya001 merged commit b7be624 into master Mar 28, 2026
4 of 8 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size/s visualization Graph visualization and UI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant