Skip to content

Commit

Permalink
Swap inbound and outbound closeness centrality to be more intuitive
Browse files Browse the repository at this point in the history
  • Loading branch information
ldm2468 committed Jul 7, 2022
1 parent 4491e77 commit c22df3a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
20 changes: 11 additions & 9 deletions closenesscentrality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ std::string ClosenessCentrality::statName() {
return "ClosenessCentrality";
}

bool ClosenessCentrality::calculate(Graph &graph, bool inbound,
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;
Expand All @@ -27,7 +27,7 @@ bool ClosenessCentrality::calculate(Graph &graph, bool inbound,

std::map<Graph::Vid, int64_t> dist_sum;
for (auto vid : samples) {
auto dist = dijkstra(graph, vid.second, inbound);
auto dist = dijkstra(graph, vid.second, outbound);
// if ((int)dist.size() != V) return false; // disconnected graph

for (auto p : dist) {
Expand Down Expand Up @@ -62,12 +62,12 @@ bool ClosenessCentrality::calculate(Graph &graph, bool inbound,

bool ClosenessCentrality::calculateStat(Graph &graph, bool verify) {
return calculate(graph, false, closeness_centrality, max_closeness_centrality, max_closeness_centrality_id) &&
calculate(graph, true, inbound_closeness_centrality, max_inbound_closeness_centrality,
max_inbound_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.
std::map<Graph::Vid, int64_t> ClosenessCentrality::dijkstra(const Graph &graph, Graph::Vertex *start, bool inbound) {
std::map<Graph::Vid, int64_t> ClosenessCentrality::dijkstra(const Graph &graph, Graph::Vertex *start, bool outbound) {
std::map<Graph::Vid, int64_t> dist;

typedef std::pair<int64_t, Graph::Vertex *> elem;
Expand All @@ -82,7 +82,9 @@ std::map<Graph::Vid, int64_t> ClosenessCentrality::dijkstra(const Graph &graph,
auto V = cur.second;
if (!dist.count(V->id)) {
dist[V->id] = cur_dist;
for (const auto &edge : (inbound ? V->inbound_edges : V->edges)) {
// this may be confusing, but note that the sampling is done in *reverse*
// (from the viewpoint of the destination, not the source)
for (const auto &edge : (outbound ? V->inbound_edges : V->edges)) {
auto next_dist = cur_dist + edge->weight;
auto to = edge->to == V ? edge->from : edge->to;
pq.emplace(next_dist, to);
Expand All @@ -102,7 +104,7 @@ void ClosenessCentrality::writeMap(std::string fname, std::map<Graph::Vid, doubl
bool ClosenessCentrality::writeToFileStat(std::string graph_name, bool directed) {
writeMap(graph_name + "_Closeness.txt", closeness_centrality);
if (directed) {
writeMap(graph_name + "_InboundCloseness.txt", inbound_closeness_centrality);
writeMap(graph_name + "_OutboundCloseness.txt", outbound_closeness_centrality);
}
return true;
}
Expand All @@ -118,8 +120,8 @@ void ClosenessCentrality::writeToHTMLStat(FILE *fp, bool directed) {
max_closeness_centrality, max_closeness_centrality_id);
if (directed) {
fprintf(fp,
"<p> max inbound closeness centrality value = %lf at ID = %lld </p>",
max_inbound_closeness_centrality, max_inbound_closeness_centrality_id);
"<p> max outbound closeness centrality value = %lf at ID = %lld </p>",
max_outbound_closeness_centrality, max_outbound_closeness_centrality_id);
}
fprintf(fp, "</h3>");
}
Expand Down
10 changes: 5 additions & 5 deletions closenesscentrality.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ class ClosenessCentrality : public CommonStat {
virtual bool writeToFileStat(std::string graph_name, bool directed) override;

private:
std::map<Graph::Vid, int64_t> dijkstra(const Graph &graph, Graph::Vertex *start, bool inbound);
bool calculate(Graph &graph, bool inbound, std::map<Graph::Vid, double> &cc, double &max, long long &max_id);
std::map<Graph::Vid, int64_t> dijkstra(const Graph &graph, Graph::Vertex *start, bool outbound);
bool calculate(Graph &graph, bool outbound, std::map<Graph::Vid, double> &cc, double &max, long long &max_id);
void writeMap(std::string fname, std::map<Graph::Vid, double> map);

double max_closeness_centrality;
double max_inbound_closeness_centrality;
double max_outbound_closeness_centrality;
long long max_closeness_centrality_id; // id of max closeness centrality vertex
long long max_inbound_closeness_centrality_id;
long long max_outbound_closeness_centrality_id;
std::map<Graph::Vid, double> closeness_centrality;
std::map<Graph::Vid, double> inbound_closeness_centrality;
std::map<Graph::Vid, double> outbound_closeness_centrality;
};
} // namespace snu

Expand Down

0 comments on commit c22df3a

Please sign in to comment.