Skip to content

Commit

Permalink
betweenness & closeness bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Min GyeongHyo committed Jul 8, 2022
1 parent c22df3a commit 41d9161
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
12 changes: 7 additions & 5 deletions betweennesscentrality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bool BetweennessCentrality::calculateStat(Graph &graph, bool verify) {
bool suc = applyPartialValue(graph, p.second, betweennessValue, reversed);
reversed = !reversed;
if (!suc) {
std::cout << "disconnected bet\n";
std::cout << "-- skipping betweenness centrality for disconnected graph\n";
return false;
}
}
Expand Down Expand Up @@ -100,6 +100,10 @@ void BetweennessCentrality::augmented_dijkstra(const Graph &graph, Graph::Vertex
auto next_dist = cur_dist + edge->weight;
bool nextIsTo = edge->to != cur;
auto nex = nextIsTo ? edge->to : edge->from;

// prevent going to parent
if (dist.count(nex->id) > 0 && dist[nex->id] < cur_dist)
continue;
pq.emplace(next_dist, std::make_pair(cur, nex));
}
}
Expand All @@ -124,18 +128,16 @@ bool BetweennessCentrality::applyPartialValue(const Graph &graph, Graph::Vertex
typedef std::pair<int64_t, Graph::Vid> dist_info;
std::vector<dist_info> dist_decreasing;
dist_decreasing.reserve(V);
for (auto p : pathcount) {
for (auto p : dist) {
dist_decreasing.emplace_back(p.second, p.first);
}
std::sort(dist_decreasing.begin(), dist_decreasing.end(), std::greater<dist_info>());
// for (auto p : pathcount_decreasing) {
// std::cout << p.first << " " << p.second << '\n';
// }

std::unordered_map<Graph::Vid, float> P;
for (auto p : dist_decreasing) {
// auto d = p.first;
auto v = p.second;

P[v] += 1;
for (auto w : predecessors[v]) {
if (dist[v] == 0 || pathcount[v] == 0)
Expand Down
2 changes: 1 addition & 1 deletion betweennesscentrality.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "graph.h"
#include "stat.h"

#define MAX_BETWEENNESS_SAMPLE_SZ 100
#define MAX_BETWEENNESS_SAMPLE_SZ 50

namespace snu {
class BetweennessCentrality : public CommonStat {
Expand Down
7 changes: 3 additions & 4 deletions closenesscentrality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ std::string ClosenessCentrality::statName() {

bool ClosenessCentrality::calculate(Graph &graph, bool outbound,
std::map<Graph::Vid, double> &cc, double &max, long long &max_id) {

const auto &vertices = graph.id_to_vertex;
int V = vertices.size();
int sample_sz = std::min(V, MAX_CLOSENESS_SAMPLE_SZ);
Expand All @@ -42,7 +41,7 @@ bool ClosenessCentrality::calculate(Graph &graph, bool outbound,
cc[vid] = 1.0;

for (auto p : dist_sum)
cc[p.first] = (p.second != 0) ? (100.0 / p.second) : 1.0;
cc[p.first] = (p.second != 0) ? ((double)(std::min(sample_sz - 1, (int)dist_sum.size() - 1)) / p.second) : 1.0;
double total_inv_sum = 0;
for (auto p : cc)
total_inv_sum += p.second;
Expand All @@ -62,8 +61,8 @@ bool ClosenessCentrality::calculate(Graph &graph, bool outbound,

bool ClosenessCentrality::calculateStat(Graph &graph, bool verify) {
return calculate(graph, false, closeness_centrality, max_closeness_centrality, max_closeness_centrality_id) &&
calculate(graph, true, outbound_closeness_centrality, max_outbound_closeness_centrality,
max_outbound_closeness_centrality_id);
calculate(graph, true, outbound_closeness_centrality, max_outbound_closeness_centrality,
max_outbound_closeness_centrality_id);
}

// calculates single-source shortest-path for all nodes connected to start.
Expand Down
2 changes: 1 addition & 1 deletion closenesscentrality.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "graph.h"
#include "stat.h"

#define MAX_CLOSENESS_SAMPLE_SZ 100
#define MAX_CLOSENESS_SAMPLE_SZ 50

namespace snu {
class ClosenessCentrality : public CommonStat {
Expand Down

0 comments on commit 41d9161

Please sign in to comment.