Figures
Abstract
Epidemic models on complex networks are widely used to assess how the social structure of a population affects epidemic spreading. However, their numerical simulation can be computationally heavy, especially for large networks. In this paper, we introduce NEXT-Net: a flexible implementation of the next reaction method for simulating epidemic spreading on both static and temporal weighted networks. We find that NEXT-Net is substantially faster than alternative algorithms, while being exact. It permits, in particular, to efficiently simulate epidemics on networks with millions of nodes on a standard computer. It also permits simulating a broad range of epidemic models on temporal networks, including scenarios in which the network structure changes in response to the epidemic. NEXT-Net is implemented in C++ and accessible from Python and R, thus combining speed with user friendliness. These features make our algorithm an ideal tool for a broad range of applications.
Author summary
Human social structures tend to be quite heterogeneous, with some individuals having many more social interactions than others. These social structures profoundly affect the spreading of epidemics and can be conveniently conceptualized as networks, in which nodes represent individuals and links represent contacts. However, computer simulations of epidemic models on networks can be slow, and efficient numerical methods are understudied. This prevents computer simulations of epidemics on realistically large networks. In this paper, we present NEXT-Net: an algorithm to efficiently simulate epidemic spreading on networks. Our algorithm can simulate a broad class of models, including networks whose structure evolves in time. Its versatility, ease of use, and performance make it broadly useful for epidemiological studies.
Citation: Cure S, Pflug FG, Pigolotti S (2025) Fast and exact stochastic simulations of epidemics on static and temporal networks. PLoS Comput Biol 21(9): e1013490. https://doi.org/10.1371/journal.pcbi.1013490
Editor: Katharina Kusejko, University Hospital Zurich, SWITZERLAND
Received: June 6, 2025; Accepted: September 4, 2025; Published: September 15, 2025
Copyright: © 2025 Cure et al. This is an open access article distributed under the terms of the Creative Commons Attribution License, which permits unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited.
Data Availability: The code is available at https://github.com/OIST/NEXTNet.
Funding: The author(s) received no specific funding for this work.
Competing interests: The authors have declared that no competing interests exist.
Introduction
Mathematical models are invaluable tools to rationalize the spreading of epidemics. The simplest models assume that epidemics spread in well-mixed populations [1]. However, this simplifying assumption neglects fundamental aspects such as the heterogeneity of contacts in a population and the presence of social structures. A common and powerful way to include these factors is to model epidemic spreading as a process taking place on a network [2–5], where nodes represent individuals and links represent contacts. In this class of models, infected individuals can infect their contacts according to certain stochastic rules. Computer simulations of these models are, unfortunately, computationally demanding on large networks [2]. Efficient numerical methods are thus crucial.
In an epidemic, the infectiousness of individuals, i.e., their propensity to spread the disease, depends on the time since they were infected in a disease-specific manner [1]. This time dependence strongly affects epidemic spreading and therefore has to be taken into account in models. In the literature, models with time-dependent rates are often referred to as “non-Markovian” [1,2]. Non-Markovian models can not be simulated using the standard Gillespie algorithm [6].
Several algorithms for simulating non-Markovian epidemic models on networks have been proposed [7–9]. Each possesses its own advantages and disadvantages. The non-Markovian Gillespie Algorithm (nMGA) [7] generalizes the Gillespie algorithm to arbitrary infection time distributions. However, the time it takes for nMGA to process a single infection scales linearly with the number of infected nodes, making simulations infeasible for large networks. In addition, the nMGA is exact only in the limit of a large number of infected nodes. The Laplace Gillespie algorithm [8] is exact and has a lower computational complexity than nMGA. However, it can only be used for monotonically decreasing infection time distributions, which excludes most realistic cases. The Rejection-based Gillespie for non-Markovian Reactions (REGIR) algorithm [9] efficiently simulates epidemics for arbitrary distributions, but, like nMGA, it is an approximate algorithm. Finally, the next reaction method is a flexible and exact algorithm, originating from chemical physics [10,11], that has been applied to simulate epidemic spreading [3,12]. However, it has been doubted whether it can be used effectively for large networks [2].
The algorithms we surveyed can be used to simulate epidemic spreading on static networks. However, the social structures that affect epidemic spreading may evolve over time, thus requiring a description in terms of temporal networks [13]. The network evolution can be independent of the disease, or can arise as a response to the disease itself. For example, infected individuals may behave differently than susceptible ones [14] and the whole population may change its behavior as the disease spreads [15,16]. Efficient numerical algorithms to study epidemic spreading on temporal networks have received little attention so far.
In this paper, we present NEXT-Net (Next-reaction-based Epidemics eXtended to Temporal NETworks), a simulation algorithm for epidemics on both static and temporal weighted networks. Our algorithm is based on a combination of the next reaction scheme and rejection sampling to handle temporal networks efficiently. By a systematic comparison in the static network case, we find that NEXT-Net is much faster than alternatives (nMGA and REGIR) in all the examples we considered. In particular, NEXT-Net scales approximately linearly with the network size, thus allowing efficient simulations of epidemic models on networks with millions of nodes on a standard computer. Our algorithm is implemented in C++ for performance, and accessible from both Python and R for ease of use.
Models
Epidemics on static networks
We introduce non-Markovian epidemic models on static weighted networks. Network nodes represent individuals, who can be in a susceptible (S), infected (I), and possibly recovered (R) state. A link from node i to node j represents a contact along which infected individual i can spread the disease to j. We consider in general directed networks, although several of our examples will be non-directed. Links are assigned weights
. We consider the three classic models (Fig 1a):
- Susceptible-Infected (SI). Infected individuals transmit the disease to their susceptible contacts at a rate
, where τ is the time since their infection. The unweighted case is recovered by setting wij = 1 for all links (i,j). We call
the probability density of transmitting the disease at time τ along a given link. This density is related with the spreading rate by
(1)
Any distribution can be written in the form of Eq (1) by a suitable choice of, see S1 Algorithms for details. The probability that an infected node eventually transmits the disease along a link having unit weight is given by
(2)
For arbitrary weight, the probability of eventual transmission is. For quickly decaying
such that
, the distribution
is thus not normalized.
- Susceptible-Infected-Recovered (SIR). In this extension of the SI model, infected individual can recover, and recovered individuals can neither transmit the disease nor be reinfected. Recovery occurs at a time-dependent rate
, leading to a distribution of recovery times
(3)
and a probability of eventual recovery of. In well-mixed populations, the SIR model is equivalent to an SI model with modified infectiousness
where the exponential factor represents that probability that an individual has not recovered. On networks, however, this equivalence is no longer exact: recovery times are assigned to nodes rather than links, and transmission times thus become correlated once recovery is taken into account. We therefore simulate recovery as a separate event and discard transmissions which would take place after a node has recovered.
- Susceptible-Infected-Susceptible (SIS). In this variant of the SIR model, recovery makes individuals susceptible again. For simplicity of implementation, we assume that each infected individual can infect each of their neighbors at most once before recovering. This does not preclude individuals spreading the disease to their neighbors multiple times if they repeatedly contract the disease.
(a) The SI, SIR, and SIS models. Susceptible nodes are represented in yellow, infected nodes in red, and recovered (immune) nodes in blue. (b) A state of an epidemic on a Watts-Strogatz network with size n = 200 and parameters k0 = 2, p = 0.2 where k0 is the initial number of nearest neighbors and p is the probability of rewiring an edge [17]. (c) Epidemic trajectories on Watts-Strogatz networks of size n = 106 with parameters k0 = 5, p = 0.3 for infection times that are Gamma distributed with mean 5 and variance 3 and recovery times that are Gamma distributed with mean 12 and variance 5. Oscillations in the trajectories appear due to the shape of these functions. (d) Illustration of the next reaction method for the SIR model. Numbers on the nodes represent the time t at which they contracted the infection. The times t on the red arrows are the times at which nodes transmit the infection via a given link. We assign transmission times even if a link connects two infected nodes, in which case transmission has no effect. Numbers on the yellow arrows represent the recovery times.
These three models can be defined on an arbitrary static network (Fig 1b) and produce markedly different epidemic trajectories (Fig 1c).
Epidemic models on temporal networks
We now extend the SI, SIR, and SIS models to temporal networks, i.e., networks in which links are created and removed at certain moments in time. We represent a temporal network as a field of functions , whose value is one if a link between node i and j exists at time t and zero otherwise. The network evolution can be deterministic or stochastic. In particular,
may depend on the epidemic state of the nodes up to time t.
An infected individual i can infect j at time t only if a link between i and j exists at time t. This means that the effective transmission rate between i and j is now , where τ is the time since infection of j. It follows that the effective distribution of infection times from node i to node j is
where Ti is the absolute time at which individual i was infected. Equation (4) is the equivalent of Eq (1) for temporal networks. We observe that, in contrast with the static network case, now depends both on the time since infection of node i and on the absolute time t (through Ti). For SIR and SIS models, the recovery distribution
is defined as for static networks, see Eq (3).
In some real temporal networks, contacts are brief enough to be treated as instantaneous, with a non-negligible chance of infection in each contact. Mathematically, such contacts can be represented as Dirac-δ peaks in with weight wij. In this case
becomes a probability distribution over a set of discrete events. The individual transmission probability during each such instantaneous contact, assuming that no earlier transmission took place, is given by
.
Simulation algorithms
We now examine algorithms for simulating the spread of epidemics on static and temporal networks. We do not consider the Laplace-Gillespie algorithm, since it can only be used if the infection time distribution is monotonically decreasing, which is not the case for most diseases. The first three methods we present are restricted to static networks, while the fourth deals with temporal networks.
Next Reaction Method (NEXT-Net).
We here describe the implementation of the next reaction method in NEXT-Net, see Fig 1d. Every time an individual is infected, we draw the times until infection of each neighbor from the distribution . For SIR and SIS models, we also draw the random time until recovery from the distribution
. The absolute times of these events, together with their type (infection or recovery) and the participating nodes, are inserted into a global priority queue. At each step of the algorithm, we retrieve the earliest event from this queue and execute it. In the case of infections, this operation on average adds R0 further future events into the queue, where R0 is the basic reproduction number (i.e. average number of subsequent infections caused by a single infection). The resulting time complexity of a single step when I nodes are infected (and the size of the priority queue is thus at most IR0) is dominated by the complexity of maintaining the priority queue, that is at most
. The algorithm is described in detail in S1 Algorithms.
non-Markovian Gillespie (nMGA).
The non-Markovian Gillespie algorithm (nMGA) [7] extends the Gillespie algorithm to time-varying infectiousness functions by neglecting variations in
between subsequent global events. The cumulative distribution
of the time until the next event is thereby approximated by the exponential distribution
where i, j ranges over all links such that node i is infected and denotes the time since infection of node i. The algorithm tends to be exact when the number of infected individuals is very large, since the time between events tends to zero in this limit. However, since
must be evaluated for every infected individual, a single time step has time complexity
.
Rejection-based Gillespie for non-Markovian Reaction (REGIR).
The REGIR algorithm [9] is an optimized version of the nGMA algorithm for unweighted networks in which in Eq (5) is replaced by an upper bound
. The resulting under-estimation of the time until the next event is then corrected by accepting events at time τ with probability
. This modification eliminates the need to evaluate
for each infected node i and thus reduces the time complexity of a single time step down to
. However, the time required for a single step is inversely proportional to the acceptance rate. The advantage of REGIR over nGMA thus depends on the choice of
and may be small if
is characterized by narrow peaks. Despite the formally lower time complexity of
for REGIR vs.
for NEXT-Net, we shall see that NEXT-Net is considerably faster in practice.
NEXT-Net for temporal networks.
NEXT-Net extends the next reaction method to simulate epidemics on temporal networks. It is designed to only require information on the network up to the present time and is therefore apt to simulate temporal networks whose structure evolves in response to the epidemics. This feature prevents us from simply mapping the temporal case into the static case by means of Eq (4). NEXT-Net evolves the network in lock-step with the epidemic. At every time step, we query two times: (i) the tentative next time a link is added or removed, and (ii) the tentative next time when a node is infected or recovers, and execute the earlier event.
To generate transmission times distribution according to Eq (4) without knowledge of the future evolution of , we employ a rejection sampling scheme (Fig 2). When a node is infected, it is initially handled as in the static case. For each neighbor present, at the time of infection the link is “activated”, i.e. an infection time with distribution
is generated. This step tentatively assumes the link will remain present until transmission occurs. If new links are later added to already infected nodes, these are immediately activated as well, but the transmission time is generated assuming no transmission prior to the link’s appearance, see S1 Algorithms for details. If a link connected to an infected node is removed, we mark this link as “masked”, which blocks subsequent transmission events. If a masked link attempts to transmit and it is later re-added, it is treated as a new link. If a masked link is re-added before its transmission time, it is simply unmasked. To distinguish these two cases, masked links are unmasked upon an attempted transmission. This masking/unmasking scheme effectively employs a method known as “thinning” [18] to sample the first firing time of a Poisson process with intensity
from the firing times generated for a process with intensity
. See S1 Algorithms for a detailed description and pseudo-code of the algorithm.
Illustration of the temporal simulation algorithm. A link from node i to node j appears and disappears after node i has been infected at time Ti. Upon appearing at time t1, the link is “activated”, i.e., a time between infection and transmission is drawn from
. Here, the condition
ensures that the time lies in the future, i.e. after t1. However, before the simulation reaches time
, the link disappears,
, causing the algorithm to mask the link. Since the link is still masked when transmission is attempted at time
, the attempt is blocked. The link is then unmasked so that when it reappears at time t2, it is re-activated, i.e. a time until transmission
(again conditioned to lie in the future, i.e. after t2) is drawn. Further disappearances and reappearances of the link before time
then do not cause further activations but merely change the state of the link. Once the simulation reaches time
, the disease is then transmitted to node j since the link happens to be unmasked at that time. See S1 Algorithms for a detailed description of the algorithm.
NEXT-Net also allows for instantaneous contacts, that simply transmit the disease with probability .
The contribution of the temporal NEXT-Net algorithm to the time complexity of generating a single infection is , assuming K transmission attempts until an unmask link is encountered on average, where R0 is now the average number of simultaneous neighbors (see S1 Algorithms). In practice, however, the computational bottleneck is usually caused by the network evolution. The complexity of this step depends on the specific temporal network model.
Implementation
We implemented three algorithms (NEXT-Net, nMGA, REGIR) in C++ to ensure high performance and made them accessible from both Python and R for convenience.
Our implementations do not presume any specific networks or infection/recovery time distributions and support SI, SIR, and SIS models. For convenience, we provide a range of classic network models (such as Watts-Strogatz [17], Erdős-Rényi, and Barabási-Albert [19] among others) and allow arbitrary weighted networks defined in terms of an adjacency list or edge list to be used. Infection/recovery time distributions can either be specified by providing through vectors
,
, or by directly employing classic infection time distributions like exponential, Gamma, Lognormal, and Weibull, see S1 Algorithms for a detailed list of options. Users can also easily implement their own networks and time distributions through flexible interfaces. When used from Python, our code also allows seamless access to all network models available in NetworkX [20].
Results
Epidemics on static networks
We simulated SIR and SIS models on different static networks. We found that our implementation of the next reaction method in NEXT-Net consistently outperforms both nMGA [7] and REGIR [9] (Fig 3). In these comparisons, we used the paradigmatic Barabási-Albert and Watts-Strogatz models, and also real-world networks from different databases [21–23]. To ensure a fair comparison, we use a Gamma distribution for transmission and recovery times. This distribution allows for an efficient bound , which favors the REGIR algorithm. However, our benchmark indicates that REGIR still seems to spend more time on retries than NEXT-Net does to maintain its priority queue. Simulations are always initialized with a single infected node, chosen at random. For SIR epidemics, we measure the average time to simulate an SIR epidemic until no infected nodes remain. For SIS epidemics, we stop the simulation at a time
, chosen as the average time for the SIR epidemic to end on the same network.
We simulate SIR: (a), (b), (c) and SIS: (d), (e), (f) epidemic processes using various algorithms on various networks: on Watts-Strogatz networks with parameters k0 = 5, p = 0.3 (a), (d); Barabási-Albert networks with parameters m = 5 (b), (e) and on real-world networks (c), (f). A list of the real world networks, their parameters, and their mean simulation times are reported in S1 Table. The infection times are Gamma distributed with mean 5 and variance 3 while the recovery time are Gamma distributed with mean 10 and variance 12. Each dot represents an average over 100 simulations; bars represent standard deviations. Simulations were executed on a workstation with an Intel Xeon 6128 CPU @ 3.40 GHz under Ubuntu 24.04.
The runtime for both NEXT-Net and REGIR appears to scale slightly super-linearly. For NEXT-Net, a possible explanation is the logarithmic dependency of the time complexity on the size of the priority queue. However, since REGIR shows super-linear scaling as well, it is also possible that the working set starts to exceed the cache size at around 105 nodes. Despite presenting similar scaling, the NEXT-Net outperforms the REGIR algorithm by roughly a factor of 100 in speed. For example, for a SIR epidemic on a Watts-Strogatz network of size , the average time to complete an epidemic for REGIR is 24 minutes 50 seconds, while NEXT-Net only takes 4.7 seconds. We expect this performance gap to be even larger for other infection time distributions. Finally, the total runtime of nMGA appears to scale quadratically with the total number of links in the network, as we would expect from its computational complexity. This makes nMGA substantially slower than both REGIR and NEXT-Net, preventing simulations on large networks.
We also compare our implementation on static networks with a Python library implementing a next reaction method for epidemics on networks [3], see Fig 4. As expected, we obtain a similar scaling in computational complexity since both implementations use a priority queue. However, NEXT-Net is about one order of magnitude faster, likely because it is implemented in C++.
We simulate SIR epidemic processes on Watts-Strogatz networks with parameters k0 = 5, p = 0.1 (a) and Barabási-Albert networks with parameter m = 5 (b) using the Python wrapper of our C++ implementation and compare its performance with the Python library from Ref. [3] (EoN). The infection times are Gamma distributed with mean 5 and variance 3 while the recovery time are lognormal with mean 10 and variance 12. For each network we repeat the simulations 100 times. Dots represent average times and bars represent standard deviations.
Epidemics on temporal networks
We now demonstrate the use of NEXT-Net for epidemic simulations on temporal networks. In this case, we are not aware of other established algorithms to compare with. We consider epidemics spreading on three different types of temporal networks: (1) An activity-driven network in which nodes randomly activate and deactivate, affecting their connectivity. (2) A network defined by spatial proximity of diffusing particles. (3) Empirically observed networks consisting of instantaneous contacts between nodes.
Activity-driven networks.
In an activity-driven network, nodes stochastically alternate between an active and inactive state. Nodes lose all of their links when they are inactivated and form new connections upon activation. We here focus on a specific model inspired from Ref. [24]. The network model is defined as follows. Inactive nodes becomes active at a constant rate a, and active nodes deactivate at constant rate b. Upon activation, a node connects to m other nodes, selected uniformly at random and not necessarily active. Upon deactivation of a node, all its links are severed.
We run SIR and SIS models on such an activity network, see Fig 5a. The epidemic is seeded with a single infection after the activity dynamics have reached a steady state, as indicated by a constant average degree. The computational time scales approximately linearly with the network size as expected, see Fig 5b. For moderately large network size (N = 105), approximately 62% of computational time is devoted to the activity dynamics, 31% for simulating the epidemic, and the remaining 7% to notify the epidemic process of the appearance of new active links. This means that the main computational cost is due to updating the temporal network, while the epidemic algorithm is rather efficient. To confirm this, we measure the average time it takes to run an epidemic on an equivalent static network. When the activity driven network is in equilibrium, there are on average links at any given time where
[24]. For our parameters this gives
. A static network with the same average degree would on average not support an epidemic outbreak due to the lack of a giant component. We thus consider a Erdős-Rényi network with
to ensure an exponential outbreak. As expected, the computational time on these static networks is much lower than the temporal ones, see Fig 5b.
Panel (a): A SIS epidemic on an activity driven network of size N = 105 with activation rate a = 0.1, deactivation rate b = 0.1, and m = 3. The infection times are Gamma distributed with mean 3 and variance 1, the recovery times are lognormally distributed with mean 10 and variance 1. Panel (b): Runtime for a SIR epidemic on an activity driven network as a function of the network size. We average over 100 simulations. Panel (c): Phase diagram of the SIS model for a constant infection and recovery rates on an activity driven network of size 105. Each simulation is initialized with the degree distribution at equilibrium and ends at a time
when the epidemic is at steady state.
Epidemic spreading on a temporal network drastically differs from the case of static networks when the network dynamics and the epidemic operate on a comparable time scale. As an example, we simulated the SIS model on this activity network for different values of the infectiousness and recovery rates, where and
are exponential distributions with rate β and μ. On static networks, the epidemic threshold is a function of
only. In contrast, here the epidemic threshold does not only depend on their ratio, but also on the timescale of recovery, see Fig 5c, in agreement with the results of Ref. [24].
Brownian proximity networks.
We consider a spatially-structured network in which the network evolution optionally responds to the epidemic outbreak. The temporal network is defined by the distances between N diffusing Brownian particles. These particles represent individuals that move randomly and can infect each other when they are in close proximity (Fig 6). Specifically, particles diffuse in two dimensions with particle-dependent diffusivity D(i). Particles i,j are connected by a link whenever
, where R is a pre-defined contact distance and
is the position of particle i.
Network parameters are N = 103 nodes, K = 8 average neighbors. The epidemic model is SI with Gamma-distributed transmission times, mean , variance
and probability of infection
. Panel (a): Snapshot of an epidemic with infected nodes in red and susceptible nodes in yellow. Panel (b): Epidemic growth for equal and constant diffusivity
for infected and non-infected nodes. Panels (c): Epidemic growth for diffusivities D0 = D for non-infected and D1 = D/10 for infected nodes. Panel (d): Epidemic growth for state-dependent diffusivities
where
is the number of infected nodes. Panel (e): Epidemic growth for diffusivities
and
.
In the limit , the number of infected individuals grows as
due to the two-dimensional geometry (Fig 6a). In the opposite limit of large D, the population is well-mixed and epidemics initially grow exponentially with a rate Λ defined by the Euler-Lotka equation
[1]. We ran simulations for different constant diffusivities
to numerically explore the transition between these two regimes (Fig 6b and S1 Video, S2 Video, S3 Video).
In real epidemics, individual mobility usually depends on the current state of the epidemic. First, infected individuals might have a reduced mobility. Secondly, as the number of infected individuals grows, containment measures may limit the mobility of all individuals, regardless of whether they are infected. Our algorithm allows for the evolution of networks to depend on the current epidemic state, and can therefore be used to model these effects as well. We here present three examples. In the first, the diffusivity of infected nodes is reduced 10-fold (Fig 6c). In the second, the diffusivity of all nodes is scaled as where
is the number of infected individuals and N the total number of individuals (Fig 6d). In the third example, both effects take place simultaneously (Fig 6e and S4 Video, S5 Video, S6 Video).
Empirical networks of instantaneous contacts.
As an example of an empirically observed temporal network, we simulated an epidemic on a temporal network reconstructed from face-to-face contact data collected in a high school in Marseille, France [25]. The data, obtained from Ref. [26], include interactions among students from five classes over a span of 9 days. The resulting network comprises 180 nodes and 45047 temporal links, see Fig 7a and 7b.
(a) Illustration of the temporal network structure over nine days, comprising 180 nodes (students) and 45,047 temporal links (face-to-face contacts). (b) Number of contacts per hour observed throughout the recorded period. (c) Average fraction of infected individuals in simulations using different infectiousness profiles: constant infectiousness and periodic infectiousness with periods of 3 and 9 days, each peaking at maximum infectiousness w. A fraction of 10% of the nodes are initially infected at time t = 0 and the epidemics are averaged over 100 simulations.
We simulate epidemics using three different choices of : a constant infectiousness
, and two periodic infectiousness
with periods of T = 3 days and T = 9 days, respectively. We run simulations for different contact weights w (which effectively scale
) and calculate the average fraction of infected individuals in each case (Fig 7c).
Our findings confirm that the shape of infectiousness affects epidemic spreading. Specifically, a constant infectiousness (corresponding to exponentially distributed infection times on static networks) results in a larger number of infected individuals compared to cases with periodic variations in infectiousness.
Finite-duration vs. instantaneous contacts.
To compare the behavior and performance of instantaneous and finite-duration contacts, we tested both models on a range of different empirical contact networks [27–30].
We first compare the epidemic trajectories produced by instantaneous vs. finite-duration contacts for a SIR model of the spread of a computer virus on a network created by messages exchanged on a social networking platform at the University of California, Irvine [27] (Fig 8a). Finite-duration contacts had weight and lasted for a finite time interval
centered at the reported times of contact between two nodes. Instantaneous contacts had weight wij = 1; this ensures that in the limit
, both models transmit the disease with probability
during each contact between an infected and a susceptible node. For finite
, we observe the resulting trajectories to be qualitatively similar, but to show some minor differences (Fig 8a).
(a): Average trajectory over 1000 simulations of a SIR epidemic spreading along the College Messaging temporal network [27]. The dataset spans 193 days with 20296 messages, during which 1899 users have either received or sent at least one message. At the initial time, the first user is infected. For the finite-duration simulations, links with weight wij = 3 exists for days during each contact. When simulating with instantaneous contacts, contacts had weight wij = 1. In both cases, the infectiousness
was constant, and nodes recover after a lognormally distributed time with mean 14 and standard deviation 10 days. (b): Runtimes of an SI epidemic on different empirical temporal networks. Temporal networks were selected from an online database [22] and are in order of increasing size: Bitcoin web of trust network [28], an emails network [29], Mathoverflow [29], Hyperlinks between subreddits on Reddit [30], User edits network on Wikipedia [29]. Plot shows runtime averages over 1000 simulations.
We next compared the performance for finite-duration contacts compared to instantaneous contacts for 5 other empirical networks with sizes ranging from about to about
contacts (Fig 8b). We find that while simulating instantaneous contacts is more efficient as we would expect, the difference in run times is only about 1.2-fold in practice.
Discussion
In this paper, we have presented NEXT-Net, an efficient and flexible implementation of stochastic methods to simulate epidemics on networks at the individual level. NEXT-Net includes two main algorithms: one for static networks based on the next-reaction scheme, and a newly conceived algorithm for temporal networks. Both algorithms are highly versatile, fast, and exact. In particular, the distributions of transmission and recovery times can be freely chosen, and simulations can be carried out on arbitrary weighted, unweighted, and temporal networks.
Our systematic comparisons show that NEXT-Net, besides being exact, vastly outperforms alternative methods for static networks in terms of performance. The performance gap with respect to other methods increases with increasing network size.
For temporal networks, we are not aware of other algorithms with a similarly wide scope. The NEXT-Net algorithm can deal with a large variety of temporal network models, including the networks that react to epidemic states and which include instantaneous contacts. Despite being versatile, the algorithm still performs very well. In most practical cases, the majority of the computational time is devoted to evolving the network, rather than to the epidemic process itself. In our implementation of NEXT-NET for temporal networks, the algorithm builds on the next reaction scheme. However, the algorithm is not restricted to that, and could for example also be combined with the Gillespie or Laplace-Gillespie algorithm when the infectiousness function permits these choices of algorithms.
Our implementation of NEXT-Net is designed to be easily extensible. New transmission/recovery time distributions and static and temporal network models can be easily added without having to modify the existing algorithms. At the moment, NEXT-Net provides various types of synthetic static networks such as Erdős-Rényi, Barabási-Albert, Watts-Strogatz, as well as non-clustered and clustered versions of the configuration model [31–33], and allows arbitrary static networks to be defined through adjacency lists. NEXT-Net currently implements SI, SIS, and SIR, as these are the most commonly employed epidemic models. Should the need arise, the code could be adapted to models with more states [34,35]. For temporal networks, NEXT-Net comes with implementations of temporal Erdős-Rényi networks, activity-driven networks [24], a network SIRX model [36] and Brownian proximity networks, and users can add arbitrary custom models by implementing a custom time evolution procedure. In the future, we hope to further extend the range of possibilities by implementing additional models of temporal networks as they are proposed in the literature.
NEXT-Net is available at https://github.com/oist/NEXTNet under an open-source license. To make the features of NEXT-Net easily accessible, we provide wrapper libraries for R and Python, and offer a range of empirical networks from the SNAP [22], ICON [21] and KONECT [23] databases in a format compatible with NEXT-Net (see the NEXT-Net website).
Supporting information
S1 Algorithms. Detailed description of the algorithms.
https://doi.org/10.1371/journal.pcbi.1013490.s001
(PDF)
S1 Table. Performance data on empirical networks.
The table lists the empirical networks we analyzed, alongside their parameters and mean computing time for SIR and SIS models using different algorithms. We selected networks of size N > 1000, only possessing undirected links, not temporal, and not bipartite, from the online databases: SNAP [22], ICON [21] and KONECT [23].
https://doi.org/10.1371/journal.pcbi.1013490.s002
(CSV)
S1 Video. Epidemic on a Brownian proximity network with constant low diffusivity.
Network parameters are N = 1000 nodes, K = 8 neighbors on average, constant diffusivities . The epidemic model is SI with Gamma-distributed transmission times, mean
, variance
, and probability of infection
.
https://doi.org/10.1371/journal.pcbi.1013490.s003
(MP4)
S2 Video. Epidemic on a Brownian proximity network with constant medium diffusivity.
Network parameters are N = 1000 nodes, K = 8 neighbors on average, constant diffusivities . The epidemic model is SI with Gamma-distributed transmission times, mean
, variance
and probability of infection
.
https://doi.org/10.1371/journal.pcbi.1013490.s004
(MP4)
S3 Video. Epidemic on a Brownian proximity network with constant large diffusivity.
Network parameters are N = 1000 nodes, K = 8 neighbors on average, constant diffusivities . The epidemic model is SI with Gamma-distributed transmission times, mean
, variance
and probability of infection
.
https://doi.org/10.1371/journal.pcbi.1013490.s005
(MP4)
S4 Video. Epidemic on a Brownian proximity network with low state-dependent diffusivity.
Network parameters are N = 1000 nodes, K = 8 neighbors on average, diffusivities and
. The epidemic model is SI with Gamma-distributed transmission times, mean
, variance
and probability of infection
.
https://doi.org/10.1371/journal.pcbi.1013490.s006
(MP4)
S5 Video. Epidemic on a Brownian proximity network with medium state-dependent diffusivity.
Network parameters are N = 1000 nodes, K = 8 neighbors on average, diffusivities and
. The epidemic model is SI with Gamma-distributed transmission times, mean
, variance
and probability of infection
.
https://doi.org/10.1371/journal.pcbi.1013490.s007
(MP4)
S6 Video. Epidemic on a Brownian proximity network with large state-dependent diffusivity.
Network parameters are N = 1000 nodes, K = 8 neighbors on average, diffusivities and
. The epidemic model is SI with Gamma-distributed transmission times, mean
, variance
and probability of infection
.
https://doi.org/10.1371/journal.pcbi.1013490.s008
(MP4)
Acknowledgments
We are grateful for the help and support provided by the Scientific Computing and Data Analysis section of Core Facilities at OIST.
References
- 1. Grassly NC, Fraser C. Mathematical models of infectious disease transmission. Nat Rev Microbiol. 2008;6(6):477–87. pmid:18533288
- 2. Pastor-Satorras R, Castellano C, Van Mieghem P, Vespignani A. Epidemic processes in complex networks. Rev Mod Phys. 2015;87(3):925–79.
- 3.
Kiss IZ, Miller JC, Simon PL. Mathematics of epidemics on networks. Cham: Springer; 2017.
- 4. Moore S, Rogers T. Predicting the speed of epidemics spreading in networks. Phys Rev Lett. 2020;124(6):068301. pmid:32109112
- 5.
Cure S, Pflug FG, Pigolotti S. Rate of epidemic spreading on complex networks. arXiv preprint 2024. https://arxiv.org/abs/240615449
- 6. Gillespie DT. Exact stochastic simulation of coupled chemical reactions. J Phys Chem. 1977;81(25):2340–61.
- 7. Boguñá M, Lafuerza LF, Toral R, Serrano MÁ. Simulating non-Markovian stochastic processes. Phys Rev E Stat Nonlin Soft Matter Phys. 2014;90(4):042108. pmid:25375439
- 8. Masuda N, Rocha LEC. A Gillespie algorithm for non-Markovian stochastic processes. SIAM Rev. 2018;60(1):95–115.
- 9.
Pelissier A, Phan M, Beerenwinkel N, Martinez MR. Practical and scalable simulations of non-Markovian stochastic processes. arXiv preprint 2022. https://arxiv.org/abs/2212.05059
- 10. Gibson MA, Bruck J. Efficient exact stochastic simulation of chemical systems with many species and many channels. J Phys Chem A. 2000;104(9):1876–89.
- 11. Anderson DF. A modified next reaction method for simulating chemical systems with time dependent propensities and delays. J Chem Phys. 2007;127(21):214107. pmid:18067349
- 12. Feng M, Cai S-M, Tang M, Lai Y-C. Equivalence and its invalidation between non-Markovian and Markovian spreading dynamics on complex networks. Nat Commun. 2019;10(1):3748. pmid:31444336
- 13. Holme P, Saramäki J. Temporal networks. Physics Reports. 2012;519(3):97–125.
- 14. Wang B, Xie Z, Han Y. Impact of individual behavioral changes on epidemic spreading in time-varying networks. Phys Rev E. 2021;104(4–1):044307. pmid:34781523
- 15. Paarporn K, Eksin C, Weitz JS, Shamma JS. Networked SIS epidemics with awareness. IEEE Trans Comput Soc Syst. 2017;4(3):93–103.
- 16. Hu P, Ding L, An X. Epidemic spreading with awareness diffusion on activity-driven networks. Phys Rev E. 2018;98(6).
- 17. Watts DJ, Strogatz SH. Collective dynamics of “small-world” networks. Nature. 1998;393(6684):440–2. pmid:9623998
- 18. Lewis PAW, Shedler GS. Simulation of nonhomogeneous poisson processes by thinning. Naval Research Logistics. 1979;26(3):403–13.
- 19.
Newman M. Networks. Oxford University Press; 2018.
- 20.
Hagberg A, Conway D. Networkx: Network analysis with Python. 2020. https://networkx.github.io
- 21.
Clauset A, Tucker E, Sainz M. The Colorado Index of Complex Networks. 2016. https://icon.colorado.edu/
- 22.
Leskovec J, Krevl A. SNAP datasets: Stanford large network dataset collection. 2014. http://snap.stanford.edu/data
- 23.
Kunegis J. KONNECT – The Koblenz network collection. 2013. p. 1343–50. http://dl.acm.org/citation.cfm?id=2488173
- 24. Cai C-R, Nie Y-Y, Holme P. Epidemic criticality in temporal networks. Phys Rev Research. 2024;6(2).
- 25. Fournet J, Barrat A. Contact patterns among high school students. PLoS One. 2014;9(9):e107878. pmid:25226026
- 26.
SocioPatterns Website. http://www.sociopatterns.org/
- 27. Panzarasa P, Opsahl T, Carley KM. Patterns and dynamics of users’ behavior and interaction: network analysis of an online community. J Am Soc Inf Sci. 2009;60(5):911–32.
- 28.
Kumar S, Spezzano F, Subrahmanian V, Faloutsos C. Edge weight prediction in weighted signed networks. In: Data Mining (ICDM), 2016 IEEE 16th International Conference on. 2016. p. 221–30.
- 29.
Paranjape A, Benson AR, Leskovec J. Motifs in temporal networks. In: Proceedings of the Tenth ACM International Conference on Web Search and Data Mining. 2017. p. 601–10. https://doi.org/10.1145/3018661.3018731
- 30.
Kumar S, Hamilton WL, Leskovec J, Jurafsky D. Community interaction and conflict on the web. In: Proceedings of the 2018 World Wide Web Conference on World Wide Web - WWW ’18. 2018. p. 933–43. https://doi.org/10.1145/3178876.3186141
- 31. Bender EA, Canfield ER. The asymptotic number of labeled graphs with given degree sequences. Journal of Combinatorial Theory, Series A. 1978;24(3):296–307.
- 32. Molloy M, Reed B. A critical point for random graphs with a given degree sequence. Random Struct Algorithms. 1995;6(2–3):161–80.
- 33. Angeles Serrano M, Boguñá M. Tuning clustering in random networks with arbitrary degree distributions. Phys Rev E Stat Nonlin Soft Matter Phys. 2005;72(3 Pt 2):036133. pmid:16241541
- 34. Darabi Sahneh F, Scoglio C, Van Mieghem P. Generalized epidemic mean-field model for spreading processes over multilayer complex networks. IEEE/ACM Trans Networking. 2013;21(5):1609–20.
- 35.
Moshiri N. Scalable epidemic simulation using FAVITES-Lite. In: Muley VY, editor. Computational Virology. New York, NY: Springer US; 2025. p. 173–93.
- 36. Maier BF, Brockmann D. Effective containment explains subexponential growth in recent confirmed COVID-19 cases in China. Science. 2020;368(6492):742–6.