root / cpp / pagerank.cpp @ 22:645db3196f17
History | View | Annotate | Download (5 kB)
1 |
/* Copyright (c) 2010, Panos Louridas, GRNET S.A.
|
---|---|
2 |
|
3 |
All rights reserved.
|
4 |
|
5 |
Redistribution and use in source and binary forms, with or without
|
6 |
modification, are permitted provided that the following conditions
|
7 |
are met:
|
8 |
|
9 |
* Redistributions of source code must retain the above copyright
|
10 |
notice, this list of conditions and the following disclaimer.
|
11 |
|
12 |
* Redistributions in binary form must reproduce the above copyright
|
13 |
notice, this list of conditions and the following disclaimer in the
|
14 |
documentation and/or other materials provided with the
|
15 |
distribution.
|
16 |
|
17 |
* Neither the name of GRNET S.A, nor the names of its contributors
|
18 |
may be used to endorse or promote products derived from this
|
19 |
software without specific prior written permission.
|
20 |
|
21 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
22 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
23 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
24 |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
25 |
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
26 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
27 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
28 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
29 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
30 |
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
32 |
OF THE POSSIBILITY OF SUCH DAMAGE.
|
33 |
*/
|
34 |
|
35 |
#include <iostream> |
36 |
#include <vector> |
37 |
#include <cstring> |
38 |
#include <cstdlib> |
39 |
|
40 |
using namespace std; |
41 |
|
42 |
#include "table.h" |
43 |
|
44 |
const char *TRACE_ARG = "-t"; |
45 |
const char *NUMERIC_ARG = "-n"; |
46 |
const char *ALPHA_ARG = "-a"; |
47 |
const char *CONVERGENCE_ARG = "-c"; |
48 |
const char *SIZE_ARG = "-s"; |
49 |
const char *DELIM_ARG = "-d"; |
50 |
const char *ITER_ARG = "-m"; |
51 |
|
52 |
void usage() {
|
53 |
cerr << "pagerank [-tn] [-a alpha ] [-s size] [-d delim] "
|
54 |
<< "[-m max_iterations] <graph_file>" << endl
|
55 |
<< " -t enable tracing " << endl
|
56 |
<< " -n treat graph file as numeric; i.e. input comprises "
|
57 |
<< "integer vertex names" << endl
|
58 |
<< " -a alpha" << endl
|
59 |
<< " the dumping factor " << endl
|
60 |
<< " -c convergence" << endl
|
61 |
<< " the convergence criterion " << endl
|
62 |
<< " -s size" << endl
|
63 |
<< " hint for internal tables " << endl
|
64 |
<< " -d delim" << endl
|
65 |
<< " delimiter for separating vertex names in each input"
|
66 |
<< "line " << endl
|
67 |
<< " -m max_iterations" << endl
|
68 |
<< " maximum number of iterations to perform" << endl;
|
69 |
} |
70 |
|
71 |
int check_arg(int i, int max) { |
72 |
if (i == max) {
|
73 |
usage(); |
74 |
exit(1);
|
75 |
} |
76 |
return i + 1; |
77 |
} |
78 |
|
79 |
int main(int argc, char **argv) { |
80 |
|
81 |
Table t; |
82 |
char *pos;
|
83 |
char *endptr;
|
84 |
string input = "stdin"; |
85 |
|
86 |
int i = 1; |
87 |
while (i < argc) {
|
88 |
if (!strcmp(argv[i], TRACE_ARG)) {
|
89 |
t.set_trace(true);
|
90 |
} else if (!strcmp(argv[i], NUMERIC_ARG)) { |
91 |
t.set_numeric(true);
|
92 |
} else if ((pos = strstr(argv[i], ALPHA_ARG))) { |
93 |
i = check_arg(i, argc); |
94 |
double alpha = strtod(argv[i], &endptr);
|
95 |
if ((alpha == 0 || alpha > 1) && endptr) { |
96 |
cerr << "Invalid alpha argument" << endl;
|
97 |
exit(1);
|
98 |
} |
99 |
t.set_alpha(alpha); |
100 |
} else if ((pos = strstr(argv[i], CONVERGENCE_ARG))) { |
101 |
i = check_arg(i, argc); |
102 |
double convergence = strtod(argv[i], &endptr);
|
103 |
if (convergence == 0 && endptr) { |
104 |
cerr << "Invalid convergence argument" << endl;
|
105 |
exit(1);
|
106 |
} |
107 |
t.set_convergence(convergence); |
108 |
} else if ((pos = strstr(argv[i], SIZE_ARG))) { |
109 |
i = check_arg(i, argc); |
110 |
size_t size = strtol(argv[i], &endptr, 10);
|
111 |
if (size == 0 && endptr) { |
112 |
cerr << "Invalid size argument" << endl;
|
113 |
exit(1);
|
114 |
} |
115 |
t.set_num_rows(size); |
116 |
} else if ((pos = strstr(argv[i], ITER_ARG))) { |
117 |
i = check_arg(i, argc); |
118 |
size_t iterations = strtol(argv[i], &endptr, 10);
|
119 |
if (iterations == 0 && endptr) { |
120 |
cerr << "Invalid iterations argument" << endl;
|
121 |
exit(1);
|
122 |
} |
123 |
t.set_max_iterations(iterations); |
124 |
} else if ((pos = strstr(argv[i], DELIM_ARG))) { |
125 |
i = check_arg(i, argc); |
126 |
t.set_delim(argv[i]); |
127 |
} else if (i == argc-1) { |
128 |
input = argv[i]; |
129 |
} else {
|
130 |
usage(); |
131 |
exit(1);
|
132 |
} |
133 |
i++; |
134 |
} |
135 |
|
136 |
t.print_params(cerr); |
137 |
cerr << "Reading input from " << input << "..." << endl; |
138 |
if (!strcmp(input.c_str(), "stdin")) { |
139 |
t.read_file("");
|
140 |
} else {
|
141 |
t.read_file(input); |
142 |
} |
143 |
t.print_table(); |
144 |
exit(1);
|
145 |
cerr << "Calculating pagerank..." << endl;
|
146 |
t.pagerank(); |
147 |
cerr << "Done calculating!" << endl;
|
148 |
t.print_pagerank_v(); |
149 |
} |