Skip to content

Commit

Permalink
Merge pull request #21 from ldm2468/file-statistics
Browse files Browse the repository at this point in the history
Implement file statistics for eigencentrality and bcc
  • Loading branch information
ldm2468 authored Jul 7, 2022
2 parents f97d0a2 + 6c16a90 commit 1184289
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
10 changes: 10 additions & 0 deletions biconnected.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "biconnected.h"

#include <stack>
#include <fstream>

namespace snu {
std::string BiconnectedComponents::statName() {
Expand Down Expand Up @@ -36,6 +37,7 @@ bool BiconnectedComponents::calculateUndirectedStat(USGraph &graph, bool verify)
max_conn_bcc = 0;
for (auto &pair : graph.id_to_vertex) {
long long cbcc = getMetadata(pair.second)->num_connected_bcc;
connected_bcc[pair.first] = cbcc;
if (cbcc > 1) {
num_arp++;
if (cbcc > max_conn_bcc) {
Expand Down Expand Up @@ -71,6 +73,14 @@ void BiconnectedComponents::writeToHTMLStat(FILE *fp, bool directed) {
num_arp, num_bcc, size_lbcc, max_conn_bcc);
}

bool BiconnectedComponents::writeToFileStat(std::string graph_name, bool directed) {
std::ofstream fout(graph_name + "_Biconnected.txt");
for (auto [nodeId, val] : connected_bcc) {
fout << nodeId << ' ' << val << '\n';
}
return true;
}

// This is the algorithm presented by Hopcroft and Tarjan (1973).
// It has been modified to avoid recursion (to prevent stack overflows)
void BiconnectedComponents::countBcc(USGraph &graph) {
Expand Down
4 changes: 4 additions & 0 deletions biconnected.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef BICONNECTED_H
#define BICONNECTED_H

#include <map>
#include "graph.h"
#include "stat.h"

Expand All @@ -12,6 +13,7 @@ class BiconnectedComponents : public UndirectedStat {
protected:
virtual bool calculateUndirectedStat(USGraph &graph, bool verify) override;
virtual void writeToHTMLStat(FILE *fp, bool directed) override;
virtual bool writeToFileStat(std::string graph_name, bool directed) override;

private:
void countBcc(USGraph &graph);
Expand All @@ -22,6 +24,8 @@ class BiconnectedComponents : public UndirectedStat {
long long num_bcc; // number of biconnected components
long long max_conn_bcc; // maximum number of bcc's connected to a single arp
long long size_lbcc; // largest size of biconnected components

std::map<Graph::Vid, long long> connected_bcc;
};
} // namespace snu

Expand Down
25 changes: 25 additions & 0 deletions eigencentrality.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "eigencentrality.h"

#include <cmath>
#include <fstream>
#include <iostream>

namespace snu {
std::string EigenCentrality::statName() {
Expand All @@ -20,6 +22,7 @@ bool EigenCentrality::calculateStat(Graph &graph, bool verify) {
max_eigencentrality = probv[i];
max_eigencentrality_id = graph.vid_order[i];
}
eigencentrality[graph.vid_order[i]] = probv[i];
}
}

Expand All @@ -32,6 +35,7 @@ bool EigenCentrality::calculateStat(Graph &graph, bool verify) {
max_pagerank = probv[i];
max_pagerank_id = graph.vid_order[i];
}
pagerank[graph.vid_order[i]] = probv[i];
}
}

Expand All @@ -44,6 +48,7 @@ bool EigenCentrality::calculateStat(Graph &graph, bool verify) {
max_katz_centrality = probv[i];
max_katz_centrality_id = graph.vid_order[i];
}
katz_centrality[graph.vid_order[i]] = probv[i];
}

return eigencentrality_converged && pagerank_converged;
Expand Down Expand Up @@ -77,6 +82,26 @@ void EigenCentrality::writeToHTMLStat(FILE *fp, bool directed) {
fprintf(fp, "</h3>");
}

void EigenCentrality::writeMap(std::string fname, std::map<Graph::Vid, double> map) {
std::ofstream fout(fname.data());
for (auto [nodeId, val] : map) {
fout << nodeId << ' ' << val << '\n';
}
}

bool EigenCentrality::writeToFileStat(std::string graph_name, bool directed) {
if (eigencentrality_converged) {
writeMap(graph_name + "_EigenCentrality.txt", eigencentrality);
}
if (pagerank_converged) {
writeMap(graph_name + "_PageRank.txt", pagerank);
}
if (katz_centrality_computed) {
writeMap(graph_name + "_KatzCentrality.txt", katz_centrality);
}
return true;
}

void EigenCentrality::normalizeProb(double n, std::vector<double> &probv) {
double sum = 0;
for (double p: probv) {
Expand Down
8 changes: 8 additions & 0 deletions eigencentrality.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "graph.h"
#include "stat.h"
#include <map>

#define PAGERANK_DAMPING_FACTOR 0.85
#define CONVERGENCE_TEST 1E-5
Expand All @@ -17,13 +18,16 @@ class EigenCentrality : public CommonStat {
protected:
virtual bool calculateStat(Graph &graph, bool verify) override;
virtual void writeToHTMLStat(FILE *fp, bool directed) override;
virtual bool writeToFileStat(std::string graph_name, bool directed) override;

private:
void normalizeProb(double n, std::vector<double> &prob);
bool calcPageRank(Graph &graph, std::vector<double> &prob);
double calcEigenCentrality(Graph &graph, std::vector<double> &prob);
void calcKatzCentrality(Graph &graph, std::vector<double> &prob, double eigenvalue);

void writeMap(std::string fname, std::map<Graph::Vid, double> map);

bool pagerank_converged = false; // whether pagerank converged
double max_pagerank; // maximum pagerank value
long long max_pagerank_id; // id of max pagerank value vertex
Expand All @@ -34,6 +38,10 @@ class EigenCentrality : public CommonStat {
double max_katz_centrality; // maximum katz centrality value
long long max_katz_centrality_id; // id of max katz centrality vertex
double max_eigenvalue; // maximum eigenvalue calculated with eigencentrality

std::map<Graph::Vid, double> eigencentrality;
std::map<Graph::Vid, double> pagerank;
std::map<Graph::Vid, double> katz_centrality;
};
} // namespace snu

Expand Down
3 changes: 2 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ void usage(void) {
printf(" options:\n");
printf(" -d | --directed set graph directed\n");
printf(" -u | --undirected set graph undirected (default)\n");
printf(" -f | --file_output output centrality details as seperate files\n");
printf(" -f | --file_output output centrality details as separate files\n");
printf(" -v | --verify verify correctness of results (slower)\n");
printf(" -h | --help print this list of help\n");
printf("\n");
printf(" e.g. ./main some_path/some_graph.snap --directed\n");
Expand Down

0 comments on commit 1184289

Please sign in to comment.