import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
SEED = 7
# Model parameters
beta = 0.20
mu = 0.10
# Simulation parameters
steps = 120
initial_infected = 5
N = 100SIR on Networks
Module 5: Complex Networks
The SIR (Susceptible-Infected-Recovered) model adds permanent immunity to SIS: once a node recovers, it cannot be infected again (Kermack and McKendrick 1927; Keeling and Rohani 2008). You met its ODE version in Compartmental Models. On a network, the final size of an SIR outbreak is closely related to bond percolation, and the mapping is exact when every infected node stays infectious for the same time (newman2002spread?). This links the page to Robustness and Percolation.
This page reuses the graph models and node attributes of the SIS pages and focuses on what is new:
- the new Recovered (R) state,
- the fact that infection cannot return once it disappears,
- the notion of final epidemic size.
What is the SIR Model?
- States: Each node can be Susceptible (S), Infected (I), or Recovered (R).
- Transitions:
- \(S \to I\): a susceptible node becomes infected through infected neighbors.
- \(I \to R\): an infected node recovers and becomes permanently immune.
- Permanent immunity: \(R\) is an absorbing state (once recovered, always recovered).
Discrete-Time Update Rule
You simulate in discrete time steps \(t = 0, 1, 2, \dots\).
If a susceptible node has \(m\) infected neighbors, use the same “independent trials” rule as SIS (Keeling and Rohani 2008):
\[ \mathbb{P}(S \to I) = 1 - (1 - \beta)^m. \]
If a node is infected, it recovers with probability:
\[ \mathbb{P}(I \to R) = \mu. \]
Recovered nodes stay recovered:
\[ \mathbb{P}(R \to R) = 1. \]
In SIS, \(I \to S\) (you become susceptible again). In SIR, \(I \to R\) (you become immune). That single change alters the long-term behaviour.
Absorbing States
On a finite graph with \(\mu > 0\), the process reaches a time \(T\) such that there are no infected nodes (\(I(T)=0\)), and after that nothing can ever change (Keeling and Rohani 2008; Pastor-Satorras et al. 2015).
Each node can become infected at most once (because \(R\) is permanent), so there can be at most \(N\) infection events. Since recovery happens with positive probability, infections cannot persist forever.
This means SIR always ends in a “frozen” configuration containing only \(S\) and \(R\) nodes.
Imports and Parameters
Choose a Network
Reuse the helper from SIS Model on Networks.
G = make_graph("random", n=N, seed=SEED)
print("N, E =", G.number_of_nodes(), G.number_of_edges())N, E = 100 317
Initialize States
As in SIS, store states on the graph using a node attribute called state.
initialize_state(G, initial_infected=initial_infected, seed=SEED)
print(
"Initial counts (S, I, R) =",
sum(G.nodes[u]["state"] == "S" for u in G.nodes()),
sum(G.nodes[u]["state"] == "I" for u in G.nodes()),
sum(G.nodes[u]["state"] == "R" for u in G.nodes()),
)Initial counts (S, I, R) = 95 5 0
What’s Next?
The next page implements the SIR update and plots the epidemic curve.