forked from gmgu/graph-analysis-tool
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.cpp
193 lines (160 loc) · 5.41 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <getopt.h>
#include <stdio.h>
#include <chrono>
#include "snugal.h"
#include "statanalyzer.h"
template <typename T>
static void printElapsedTime(const char *msg, T &t) {
using namespace std::chrono;
printf("%10ldms: %s\n", (duration_cast<milliseconds>(high_resolution_clock::now() - t)).count(), msg);
t = high_resolution_clock::now();
}
void usage(void);
int main(int argc, char *argv[]) {
if (argc < 2) {
usage();
return 1;
}
bool directed = false; // suppose given graph is undirected.
bool file_output = false; // default: skip output of detailed files
bool display_time = true; // always: show time statistics
bool verify = false; // verify results (default: skip verification)
while (true) {
static struct option long_options[] = {
{"directed", 0, NULL, 'd'},
{"undirected", 0, NULL, 'u'},
{"help", 0, NULL, 'h'},
{"file_output", 0, NULL, 'f'},
{"verify", 0, NULL, 'v'},
{0, 0, 0, 0}
};
int option = getopt_long(argc, argv, "duhfv", long_options, NULL);
if (option < 0) {
break;
}
switch (option) {
case 'd':
directed = true;
break;
case 'u':
directed = false;
break;
case 'f':
file_output = true;
break;
case 'v':
verify = true;
break;
case 'h':
usage();
return 0;
default:
usage();
return 1;
}
}
// There exists no non-option argument.
// That is, input file path is not given.
if (argc <= optind) {
fprintf(stderr, "input file is not given.\n");
return 1;
}
// Suppose input file path is the first non-option argument.
std::string input_path(argv[optind]);
// Extract name of given graph from input path.
std::size_t slash = input_path.rfind("/");
if (slash == std::string::npos) {
slash = -1;
}
std::size_t dot = input_path.rfind(".");
if (dot == std::string::npos) {
dot = input_path.length();
}
std::string graph_name = input_path.substr(slash + 1, dot - (slash + 1));
// parse graph from file
std::shared_ptr<snu::Graph> graph_shptr = snu::parseFile(input_path, directed);
if (!graph_shptr)
return 1;
snu::Graph &graph = *graph_shptr.get();
std::vector<snu::Stat *> all_stats;
std::vector<snu::CommonStat *> common_stats;
std::vector<snu::DirectedStat *> directed_only_stats;
std::vector<snu::UndirectedStat *> undirected_only_stats;
/* Add Stats Here */
auto basicStat = snu::BasicStat();
auto connectStat = snu::ConnectStat();
auto closenessCentrality = snu::ClosenessCentrality();
auto betweennessCentrality = snu::BetweennessCentrality();
auto eigenCentrality = snu::EigenCentrality();
auto countStat = snu::CountStat();
auto biconnectedComponents = snu::BiconnectedComponents();
/* -- Add Common Stats To Run (support both directed & undirected) -- */
common_stats = {
&basicStat,
&connectStat,
&eigenCentrality,
&closenessCentrality,
&betweennessCentrality,
};
/* -- Add Directed Stats To Run -- */
directed_only_stats = {};
/* -- Add Undirected Stats To Run -- */
undirected_only_stats = {
&countStat,
&biconnectedComponents,
};
// run all stats using the graph
auto analyzer = snu::StatAnalyzer(graph_name, graph_shptr, file_output, display_time, verify);
analyzer.addCommonStats(common_stats);
analyzer.addDirectedStats(directed_only_stats);
analyzer.addUndirectedStats(undirected_only_stats);
analyzer.run();
// plot
auto t = std::chrono::high_resolution_clock::now();
snu::Plot plot(graph_name);
snu::makePlot(graph, plot);
printElapsedTime("makePlot()", t);
// write to HTML
FILE *fp = snu::openHtml(graph_name.c_str(), directed);
std::vector<snu::Stat *> html_order;
if (directed) {
/* -- Set Directed Html Order Here -- */
html_order = {
&basicStat,
&connectStat,
&eigenCentrality,
&closenessCentrality,
&betweennessCentrality,
};
} else {
/* -- Set Undirected Html Order Here -- */
html_order = {
&basicStat,
&connectStat,
&eigenCentrality,
&biconnectedComponents,
&closenessCentrality,
&betweennessCentrality,
&countStat,
};
}
for (auto stat : html_order) {
snu::addStatToHtml(fp, stat, directed);
}
snu::closeHtml(fp, plot);
printElapsedTime("Make HTML", t);
return 0;
}
void usage(void) {
printf(" usage: main <input file> [options]\n");
printf("\n");
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 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");
return;
}