-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgrk
237 lines (213 loc) · 6.58 KB
/
pgrk
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import java.util.*;
import java.io.*;
import java.lang.*;
import static java.lang.Math.*;
public class pgrk{
int iter;
int initval;
String filename;
int n; // number of vertices in the graph
int m; // number of edges in the graph
int[][] L; // adjacency matrix
final double d = 0.85;
final double errorrate = 0.00001;
double[] Src;
int[] C;
pgrk() {} //default constructor
pgrk(int iter, int initval, String filename) // 3 argument constructor to initialize class variables with provided command line arguments
{
this.iter = iter;
this.initval = initval;
this.filename = filename;
int p = 0;
int q = 0;
try {
Scanner scanner = new Scanner(new File(filename));
n = scanner.nextInt();
m = scanner.nextInt();
//Adjacency matrix representation of graph
L = new int[n][n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
L[i][j] = 0;
while(scanner.hasNextInt())
{
p = scanner.nextInt();
q = scanner.nextInt();
L[p][q] = 1;
}
C = new int[n];
for(int i = 0; i < n; i++) {
C[i] = 0;
for(int j = 0; j < n; j++) {
C[i] += L[i][j];
}
}
Src = new double[n];
switch(initval) {
case 0:
for(int i = 0; i < n; i++) {
Src[i] = 0;
}
break;
case 1:
for(int i = 0; i < n; i++) {
Src[i] = 1;
}
break;
case -1:
for(int i =0; i < n; i++) {
Src[i] = 1.0/n;
}
break;
case -2:
for(int i =0; i < n; i++) {
Src[i] = 1.0/Math.sqrt(n);
}
break;
}
}
catch(FileNotFoundException fnfe)
{
}
}
public static void main(String[] args)
{
if(args.length != 3) {
System.out.println("Usage: pgrk iterations initialvalue filename");
return;
}
//command line arguments
int iterations = Integer.parseInt(args[0]);
int initialvalue = Integer.parseInt(args[1]);
String filename = args[2];
if( !(initialvalue >= -2 && initialvalue <= 1) ) {
System.out.println("Enter -2, -1, 0 or 1 for initialvalue");
return;
}
pgrk pr = new pgrk(iterations, initialvalue, filename);
pr.pgrkAlgo();
}
boolean isConverged(double[] src, double[] target)
{
for(int i = 0; i < n; i++)
{
if ( abs(src[i] - target[i]) > errorrate )
return false;
}
return true;
}
void pgrkAlgo() {
double[] D = new double[n];
boolean flag = true;
if(n > 10) {
iter = 0;
for(int i =0; i < n; i++) {
Src[i] = 1.0/n;
}
int i = 0;
do {
if(flag == true)
{
flag = false;
}
else
{
for(int l = 0; l < n; l++) {
Src[l] = D[l];
}
}
for(int l = 0; l < n; l++) {
D[l] = 0.0;
}
for(int j = 0; j < n; j++) {
for(int k = 0; k < n; k++)
{
if(L[k][j] == 1) {
D[j] += Src[k]/C[k];
}
}
}
//Compute and print pagerank of all pages
for(int l = 0; l < n; l++) {
D[l] = d*D[l] + (1-d)/n;
}
i++;
} while (isConverged(Src, D) != true);
// print pageranks at the stopping iteration
System.out.println("Iter: " + i);
for(int l = 0 ; l < n; l++) {
System.out.printf("P[" + l + "] = %.6f\n",Math.round(D[l]*1000000.0)/1000000.0);
}
return;
}
//Base Case
System.out.print("Base : 0");
for(int j = 0; j < n; j++) {
System.out.printf(" :P[" + j + "]=%.6f",Math.round(Src[j]*1000000.0)/1000000.0);
}
if (iter != 0) {
for(int i = 0; i < iter; i++)
{
for(int l = 0; l < n; l++) {
D[l] = 0.0;
}
for(int j = 0; j < n; j++) {
for(int k = 0; k < n; k++)
{
if(L[k][j] == 1) {
D[j] += Src[k]/C[k];
}
}
}
//Compute and print pagerank of all pages
System.out.println();
System.out.print("Iter : " + (i+1));
for(int l = 0; l < n; l++) {
D[l] = d*D[l] + (1-d)/n;
System.out.printf(" :P[" + l + "]=%.6f",Math.round(D[l]*1000000.0)/1000000.0);
}
for(int l = 0; l < n; l++) {
Src[l] = D[l];
}
}
System.out.println();
}
else
{ //number of iterations is 0, try convergence
int i = 0;
do {
if(flag == true)
{
flag = false;
}
else
{
for(int l = 0; l < n; l++) {
Src[l] = D[l];
}
}
for(int l = 0; l < n; l++) {
D[l] = 0.0;
}
for(int j = 0; j < n; j++) {
for(int k = 0; k < n; k++)
{
if(L[k][j] == 1) {
D[j] += Src[k]/C[k];
}
}
}
//Compute and print pagerank of all pages
System.out.println();
System.out.print("Iter : " + (i+1));
for(int l = 0; l < n; l++) {
D[l] = d*D[l] + (1-d)/n;
System.out.printf(" :P[" + l + "]=%.6f",Math.round(D[l]*1000000.0)/1000000.0);
}
i++;
} while (isConverged(Src, D) != true);
System.out.println();
}
}
}