• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

networkit / networkit / 22892510196

10 Mar 2026 07:52AM UTC coverage: 79.569% (+0.1%) from 79.428%
22892510196

push

github

web-flow
Merge pull request #1394 from Schwarf/coverage/add_tests_epidemic_simulation

Add tests epidemic simulation

23 of 24 new or added lines in 1 file covered. (95.83%)

1 existing line in 1 file now uncovered.

29626 of 37233 relevant lines covered (79.57%)

2265374.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
/networkit/cpp/simulation/EpidemicSimulationSEIR.cpp
1
/*
2
 * EpidemicSimulationSEIR.cpp
3
 *
4
 *  Created on: 20.11.2015
5
 *      Author: Christian Staudt
6
 */
7

8
#include <networkit/auxiliary/Log.hpp>
9
#include <networkit/auxiliary/Random.hpp>
10
#include <networkit/graph/GraphTools.hpp>
11
#include <networkit/simulation/EpidemicSimulationSEIR.hpp>
12

13
namespace NetworKit {
14

15
EpidemicSimulationSEIR::EpidemicSimulationSEIR(const Graph &G, count tMax, double transP,
10✔
16
                                               count eTime, count iTime, node start)
10✔
17
    : Algorithm(), G(&G), maxTimestamps(tMax), transmissionProbability(transP), exposedTime(eTime),
10✔
18
      infectiousTime(iTime), start(start) {}
10✔
19

20
void EpidemicSimulationSEIR::run() {
9✔
21

22
    using State = EpidemicSimulationSEIR::State;
23

24
    index t = 0;
9✔
25

26
    // initialize state and timestamp arrays
27
    state.resize(G->upperNodeIdBound(), State::Undefined);
9✔
28
    timestamp.resize(G->upperNodeIdBound(), none);
9✔
29

30
    auto setState = [&](node v, State X) {
87✔
31
        state[v] = X;
87✔
32
        timestamp[v] = t;
87✔
33
    };
96✔
34

35
    // initialize nodes to Susceptible
36
    G->parallelForNodes([&](node v) { setState(v, State::Susceptible); });
62✔
37

38
    // contact may expose susceptible node to infection
39
    auto contact = [&](node v) {
54✔
40
        if ((state[v] == State::Susceptible)
54✔
41
            && (Aux::Random::probability() <= transmissionProbability)) {
54✔
42
            setState(v, State::Exposed);
10✔
43
        }
44
    };
63✔
45

46
    // update state of nodes
47
    auto sweep = [&](node u) {
272✔
48
        if (state[u] == State::Susceptible) {
272✔
49
            // do nothing
50
        } else if (state[u] == State::Exposed) {
74✔
51
            // exposed nodes become infectious after time
52
            if ((t - timestamp[u]) >= exposedTime) {
29✔
53
                setState(u, State::Infectious);
5✔
54
            }
55
        } else if (state[u] == State::Infectious) {
45✔
56
            // contact neighbors of infectious node
57
            G->forNeighborsOf(u, [&](node v) { contact(v); });
83✔
58
            // infectious nodes become removed after time
59
            if ((t - timestamp[u]) >= infectiousTime) {
29✔
60
                setState(u, State::Removed);
10✔
61
            }
62
        } else if (state[u] == State::Removed) {
16✔
63
            // do nothing
NEW
64
        } else if (state[u] == State::Undefined) {
×
65
            throw std::runtime_error("node in undefined state encountered - should not happen");
×
66
        } else {
67
            throw std::runtime_error("else branch taken - should not happen");
×
68
        }
69
    };
272✔
70

71
    auto census = [&]() {
35✔
72
        std::vector<count> data(5);
35✔
73
        G->forNodes([&](node v) { data[(index)state[v]] += 1; });
307✔
74
        return data;
35✔
75
    };
×
76

77
    // if starting node node provided, start with random node
78
    if (start == none) {
9✔
79
        start = GraphTools::randomNode(*G);
1✔
80
    }
81
    INFO("zero node: ", start);
9✔
82
    setState(start, State::Infectious); // infect node zero
9✔
83

84
    while (t < maxTimestamps) {
44✔
85
        G->parallelForNodes(sweep);
35✔
86
        auto populations = census();
35✔
87

88
        for (int s = (int)State::Susceptible; s != (int)State::Undefined; ++s) {
175✔
89
            std::vector<count> data = {start, t, (count)s, populations[s]};
140✔
90
            stats.push_back(data);
140✔
91
        }
140✔
92

93
        t += 1;
35✔
94
    }
35✔
95

96
    hasRun = true;
9✔
97
}
9✔
98

99
const std::vector<std::vector<count>> &EpidemicSimulationSEIR::getData() const {
9✔
100
    return stats;
9✔
101
}
102

103
} /* namespace NetworKit */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc