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

networkit / networkit / 20781110983

07 Jan 2026 12:13PM UTC coverage: 79.413% (+0.007%) from 79.406%
20781110983

push

github

web-flow
Merge pull request #1375 from fabratu/compact_scores

Add the ability to return scores as numpy array and compacted (centrality)

21 of 24 new or added lines in 2 files covered. (87.5%)

8 existing lines in 2 files now uncovered.

29478 of 37120 relevant lines covered (79.41%)

2279850.99 hits per line

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

86.89
/networkit/cpp/centrality/Centrality.cpp
1
/*
2
 * Centrality.cpp
3
 *
4
 *  Created on: 19.02.2014
5
 *      Author: cls
6
 */
7

8
#include <networkit/auxiliary/Log.hpp>
9
#include <networkit/auxiliary/Parallel.hpp>
10
#include <networkit/centrality/Centrality.hpp>
11

12
namespace NetworKit {
13

14
Centrality::Centrality(const Graph &G, bool normalized, bool computeEdgeCentrality)
323✔
15
    : Algorithm(), G(G), normalized(normalized), computeEdgeCentrality(computeEdgeCentrality) {
323✔
16
    if (computeEdgeCentrality && !G.hasEdgeIds()) {
323✔
17
        throw std::runtime_error("For edge centralities to be computed, edges must "
×
18
                                 "be indexed first: call G.indexEdges()");
×
19
    }
20
}
323✔
21

22
double Centrality::score(node v) {
770,040✔
23
    assureFinished();
770,040✔
24
    return scoreData.at(v);
770,040✔
25
}
26

27
std::vector<std::pair<node, double>> Centrality::ranking() {
62✔
28
    assureFinished();
62✔
29
    std::vector<std::pair<node, double>> ranking;
62✔
30
    G.forNodes([&](node v) { ranking.emplace_back(v, scoreData[v]); });
486,117✔
31
    Aux::Parallel::sort(ranking.begin(), ranking.end(),
62✔
32
                        [](std::pair<node, double> x, std::pair<node, double> y) {
9,634,091✔
33
                            if (x.second == y.second) {
9,634,091✔
34
                                return x.first < y.first;
731,296✔
35
                            }
36
                            return x.second > y.second;
8,902,795✔
37
                        });
38
    return ranking;
62✔
39
}
×
40

41
const std::vector<double> &Centrality::scores() const {
142✔
42
    assureFinished();
142✔
43
    return scoreData;
142✔
44
}
45

46
std::vector<double> Centrality::compactScores() const {
1✔
47
    assureFinished();
1✔
48
    std::vector<double> compactScoreData;
1✔
49
    compactScoreData.reserve(G.numberOfNodes());
1✔
50
    G.forNodes([&](node v) { compactScoreData.push_back(scoreData[v]); });
6✔
51
    return compactScoreData;
1✔
NEW
52
}
×
53

54
std::vector<double> Centrality::edgeScores() {
2✔
55
    assureFinished();
2✔
56
    return edgeScoreData;
2✔
57
}
58

59
std::vector<double> Centrality::compactEdgeScores() {
1✔
60
    assureFinished();
1✔
61
    std::vector<double> compactEdgeScoreData;
1✔
62
    compactEdgeScoreData.reserve(G.numberOfEdges());
1✔
63
    G.forEdges([&](node /*u*/, node /*v*/, edgeid eid) {
1✔
64
        compactEdgeScoreData.push_back(edgeScoreData[eid]);
5✔
65
    });
5✔
66
    return compactEdgeScoreData;
1✔
NEW
67
}
×
68

69
double Centrality::maximum() {
×
70
    throw std::runtime_error("Not implemented: Compute the maximum centrality "
×
71
                             "score in the respective centrality subclass.");
×
72
}
73

74
double Centrality::centralization() {
3✔
75
    assureFinished();
3✔
76
    double centerScore = 0.0;
3✔
77
    G.forNodes([&](node v) {
3✔
78
        if (scoreData[v] > centerScore) {
18✔
79
            centerScore = scoreData[v];
4✔
80
        }
81
    });
18✔
82
    INFO("center score: ", centerScore);
3✔
83
    double maxScore = maximum();
3✔
84
    double diff1 = 0.0;
3✔
85
    double diff2 = 0.0;
3✔
86
    G.forNodes([&](node v) {
3✔
87
        diff1 += (centerScore - scoreData[v]);
18✔
88
        diff2 += (maxScore - scoreData[v]);
18✔
89
    });
18✔
90
    return diff1 / diff2;
3✔
91
}
92

93
} /* 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