Skip to content

Commit

Permalink
Merge pull request #352 from gilcu3/Dev
Browse files Browse the repository at this point in the history
fixing division by zero error in DynBetweenness
  • Loading branch information
avdgrinten authored Jun 27, 2019
2 parents 4a9b9e1 + c0ddf77 commit 28f34d3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
14 changes: 8 additions & 6 deletions networkit/cpp/centrality/DynBetweenness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,14 @@ void DynBetweenness::decreaseScore(std::vector<bool> & affected, node y, std::pr
}
G.forNeighborsOf(x, [&](node w, edgeweight weightxw){
if (w!= x && (w != v || x != u) && x != y && distancesOld[x][y] == distancesOld[w][y] + weightxw) {
if (affected[x]) {
// TRACE("Affected node ", x, ". Subtracting from dep of ", w, ": ", sigmaOld[w][y]/sigmaOld[x][y]*(1+dep[x]));
dep[w] += sigmaOld[w][y]/sigmaOld[x][y]*(1+dep[x]);
} else {
// TRACE("Non affected node ", x, ". Subtracting from dep of ", w, ": ", sigmaOld[w][y]/sigmaOld[x][y]*(dep[x]));
dep[w] += sigmaOld[w][y]/sigmaOld[x][y]*(dep[x]);
if(sigmaOld[x][y] > 0){
if (affected[x]) {
// TRACE("Affected node ", x, ". Subtracting from dep of ", w, ": ", sigmaOld[w][y]/sigmaOld[x][y]*(1+dep[x]));
dep[w] += sigmaOld[w][y]/sigmaOld[x][y]*(1+dep[x]);
} else {
// TRACE("Non affected node ", x, ". Subtracting from dep of ", w, ": ", sigmaOld[w][y]/sigmaOld[x][y]*(dep[x]));
dep[w] += sigmaOld[w][y]/sigmaOld[x][y]*(dep[x]);
}
}
if (! visited[w] && !affected[w] && w != y) {
TRACE("Inserting node ", w, " with old priority ", distancesOld[w][y]);
Expand Down
75 changes: 75 additions & 0 deletions networkit/cpp/centrality/test/DynBetweennessGTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <networkit/graph/Sampling.hpp>
#include <networkit/generators/DorogovtsevMendesGenerator.hpp>
#include <networkit/generators/ErdosRenyiGenerator.hpp>
#include <networkit/centrality/DynBetweenness.hpp>
#include <networkit/auxiliary/Random.hpp>

namespace NetworKit {

Expand Down Expand Up @@ -134,5 +136,78 @@ TEST_F(DynBetweennessGTest, runApproxBetweenness) {
DEBUG("Number of samples: ", bc1.numberOfSamples());
}

TEST_F(DynBetweennessGTest, runDynVsStaticCaseInsertDirected){
Aux::Random::setSeed(0, false);

auto genEdgeInsert = [](const Graph& g){
node u = g.randomNode();
node v = g.randomNode();
while (u == v || g.hasEdge(u, v)){
u = g.randomNode();
v = g.randomNode();
}
return std::make_pair(u, v);
};

for(count n = 2; n <= 25; n++)
for(count t = 0; t < 100; t++){
auto g = ErdosRenyiGenerator(n, 0.3, true).generate();

if(g.numberOfEdges() == g.numberOfNodes() * (g.numberOfNodes() - 1))continue;

auto e = genEdgeInsert(g);
auto ibet = DynBetweenness(g);

ibet.run();

auto ne = GraphEvent(GraphEvent::EDGE_ADDITION, e.first, e.second);
g.addEdge(e.first, e.second);
ibet.update(ne);

EXPECT_TRUE(g.hasEdge(e.first, e.second));
auto brandes = Betweenness(g);
brandes.run();
g.forNodes([&](node & v){
EXPECT_NEAR(brandes.score(v), ibet.score(v), 1e-8);
});
}
}

TEST_F(DynBetweennessGTest, runDynVsStaticCaseInsertUndirected){
Aux::Random::setSeed(0, false);

auto genEdgeInsert = [](const Graph& g){
node u = g.randomNode();
node v = g.randomNode();
while (u == v || g.hasEdge(u, v)){
u = g.randomNode();
v = g.randomNode();
}
return std::make_pair(u, v);
};

for(count n = 2; n <= 25; n++)
for(count t = 0; t < 100; t++){
auto g = ErdosRenyiGenerator(n, 0.3, false).generate();
if(g.numberOfEdges() == g.numberOfNodes() * (g.numberOfNodes() - 1) / 2)continue;

auto e = genEdgeInsert(g);
auto ibet = DynBetweenness(g);

ibet.run();

auto ne = GraphEvent(GraphEvent::EDGE_ADDITION, e.first, e.second);
g.addEdge(e.first, e.second);
ibet.update(ne);
EXPECT_TRUE(g.hasEdge(e.first, e.second));

auto brandes = Betweenness(g);
brandes.run();
g.forNodes([&](node & v){
EXPECT_NEAR(brandes.score(v), ibet.score(v), 1e-8);
});
}
}


} /* namespace NetworKit */

0 comments on commit 28f34d3

Please sign in to comment.