forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPageRankTest.java
More file actions
319 lines (254 loc) · 8.78 KB
/
PageRankTest.java
File metadata and controls
319 lines (254 loc) · 8.78 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package com.thealgorithms.others;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/**
* Test class for PageRank algorithm implementation
*
* @author Hardvan
*/
class PageRankTest {
private static final double EPSILON = 0.0001; // Tolerance for floating point comparisons
/**
* Test basic PageRank calculation with a simple 3-node graph
* Graph: 1 -> 2, 2 -> 3, 3 -> 1
*/
@Test
void testSimpleThreeNodeGraph() {
PageRank pageRank = new PageRank(3);
// Create a simple circular graph: 1 -> 2 -> 3 -> 1
int[][] adjacencyMatrix = new int[10][10];
adjacencyMatrix[1][2] = 1; // Node 1 links to Node 2
adjacencyMatrix[2][3] = 1; // Node 2 links to Node 3
adjacencyMatrix[3][1] = 1; // Node 3 links to Node 1
pageRank.setAdjacencyMatrix(adjacencyMatrix);
double[] result = pageRank.calculatePageRank(3);
// All nodes should have equal PageRank in a circular graph
assertNotNull(result);
assertEquals(result[1], result[2], EPSILON);
assertEquals(result[2], result[3], EPSILON);
}
/**
* Test PageRank with a two-node graph where one node points to another
*/
@Test
void testTwoNodeGraph() {
PageRank pageRank = new PageRank(2);
// Node 1 links to Node 2
pageRank.setEdge(1, 2, 1);
double[] result = pageRank.calculatePageRank(2);
// Node 2 should have higher PageRank than Node 1 (after 2 iterations)
assertNotNull(result);
assertEquals(0.2775, result[2], EPSILON);
assertEquals(0.15, result[1], EPSILON);
}
/**
* Test PageRank with a single node (no links)
*/
@Test
void testSingleNode() {
PageRank pageRank = new PageRank(1);
double[] result = pageRank.calculatePageRank(1);
// Single node should have (1-d) = 0.15 after applying damping
assertNotNull(result);
assertEquals(0.15, result[1], EPSILON);
}
/**
* Test PageRank with a hub-and-spoke configuration
* Node 1 is the hub, pointing to nodes 2, 3, and 4
*/
@Test
void testHubAndSpokeGraph() {
PageRank pageRank = new PageRank(4);
// Hub node (1) links to all other nodes
pageRank.setEdge(1, 2, 1);
pageRank.setEdge(1, 3, 1);
pageRank.setEdge(1, 4, 1);
// All spokes link back to hub
pageRank.setEdge(2, 1, 1);
pageRank.setEdge(3, 1, 1);
pageRank.setEdge(4, 1, 1);
double[] result = pageRank.calculatePageRank(4);
assertNotNull(result);
// Hub should have higher PageRank
assertEquals(result[2], result[3], EPSILON);
assertEquals(result[3], result[4], EPSILON);
}
/**
* Test PageRank with multiple iterations
*/
@Test
void testMultipleIterations() {
PageRank pageRank = new PageRank(3);
pageRank.setEdge(1, 2, 1);
pageRank.setEdge(2, 3, 1);
pageRank.setEdge(3, 1, 1);
double[] result2Iterations = pageRank.calculatePageRank(3, 0.85, 2, false);
double[] result5Iterations = pageRank.calculatePageRank(3, 0.85, 5, false);
assertNotNull(result2Iterations);
assertNotNull(result5Iterations);
}
/**
* Test getPageRank method for individual node
*/
@Test
void testGetPageRank() {
PageRank pageRank = new PageRank(2);
pageRank.setEdge(1, 2, 1);
pageRank.calculatePageRank(2);
double node1PageRank = pageRank.getPageRank(1);
double node2PageRank = pageRank.getPageRank(2);
assertEquals(0.15, node1PageRank, EPSILON);
assertEquals(0.2775, node2PageRank, EPSILON);
}
/**
* Test getAllPageRanks method
*/
@Test
void testGetAllPageRanks() {
PageRank pageRank = new PageRank(2);
pageRank.setEdge(1, 2, 1);
pageRank.calculatePageRank(2);
double[] allPageRanks = pageRank.getAllPageRanks();
assertNotNull(allPageRanks);
assertEquals(0.15, allPageRanks[1], EPSILON);
assertEquals(0.2775, allPageRanks[2], EPSILON);
}
/**
* Test that self-loops are not allowed
*/
@Test
void testNoSelfLoops() {
PageRank pageRank = new PageRank(2);
// Try to set a self-loop
pageRank.setEdge(1, 1, 1);
pageRank.setEdge(1, 2, 1);
double[] result = pageRank.calculatePageRank(2);
assertNotNull(result);
// Self-loop should be ignored
}
/**
* Test exception when node count is too small
*/
@Test
void testInvalidNodeCountTooSmall() {
assertThrows(IllegalArgumentException.class, () -> new PageRank(0));
}
/**
* Test exception when node count is too large
*/
@Test
void testInvalidNodeCountTooLarge() {
assertThrows(IllegalArgumentException.class, () -> new PageRank(11));
}
/**
* Test exception for invalid damping factor (negative)
*/
@Test
void testInvalidDampingFactorNegative() {
PageRank pageRank = new PageRank(2);
pageRank.setEdge(1, 2, 1);
assertThrows(IllegalArgumentException.class, () -> pageRank.calculatePageRank(2, -0.1, 2, false));
}
/**
* Test exception for invalid damping factor (greater than 1)
*/
@Test
void testInvalidDampingFactorTooLarge() {
PageRank pageRank = new PageRank(2);
pageRank.setEdge(1, 2, 1);
assertThrows(IllegalArgumentException.class, () -> pageRank.calculatePageRank(2, 1.5, 2, false));
}
/**
* Test exception for invalid iterations (less than 1)
*/
@Test
void testInvalidIterations() {
PageRank pageRank = new PageRank(2);
pageRank.setEdge(1, 2, 1);
assertThrows(IllegalArgumentException.class, () -> pageRank.calculatePageRank(2, 0.85, 0, false));
}
/**
* Test exception when getting PageRank for invalid node
*/
@Test
void testGetPageRankInvalidNode() {
PageRank pageRank = new PageRank(2);
pageRank.calculatePageRank(2);
assertThrows(IllegalArgumentException.class, () -> pageRank.getPageRank(3));
}
/**
* Test exception when getting PageRank for node less than 1
*/
@Test
void testGetPageRankNodeLessThanOne() {
PageRank pageRank = new PageRank(2);
pageRank.calculatePageRank(2);
assertThrows(IllegalArgumentException.class, () -> pageRank.getPageRank(0));
}
/**
* Test complex graph with multiple incoming and outgoing links
*/
@Test
void testComplexGraph() {
PageRank pageRank = new PageRank(4);
// Create a more complex graph
pageRank.setEdge(1, 2, 1);
pageRank.setEdge(1, 3, 1);
pageRank.setEdge(2, 3, 1);
pageRank.setEdge(3, 4, 1);
pageRank.setEdge(4, 1, 1);
double[] result = pageRank.calculatePageRank(4);
assertNotNull(result);
// Node 3 should have high PageRank (receives links from nodes 1 and 2)
// After 2 iterations, the sum will not equal total nodes
double sum = result[1] + result[2] + result[3] + result[4];
assertEquals(1.8325, sum, EPSILON);
}
/**
* Test that PageRank values sum after 2 iterations
*/
@Test
void testPageRankSum() {
PageRank pageRank = new PageRank(5);
// Create arbitrary graph
pageRank.setEdge(1, 2, 1);
pageRank.setEdge(2, 3, 1);
pageRank.setEdge(3, 4, 1);
pageRank.setEdge(4, 5, 1);
pageRank.setEdge(5, 1, 1);
double[] result = pageRank.calculatePageRank(5);
double sum = 0;
for (int i = 1; i <= 5; i++) {
sum += result[i];
}
// Sum after 2 iterations with default damping factor
assertEquals(2.11, sum, EPSILON);
}
/**
* Test graph with isolated node (no incoming or outgoing links)
*/
@Test
void testGraphWithIsolatedNode() {
PageRank pageRank = new PageRank(3);
// Node 1 and 2 are connected, Node 3 is isolated
pageRank.setEdge(1, 2, 1);
pageRank.setEdge(2, 1, 1);
double[] result = pageRank.calculatePageRank(3);
assertNotNull(result);
// Isolated node should have some PageRank due to damping factor
assertEquals(0.15, result[3], EPSILON);
}
/**
* Test verbose mode (should not throw exception)
*/
@Test
void testVerboseMode() {
PageRank pageRank = new PageRank(2);
pageRank.setEdge(1, 2, 1);
// This should execute without throwing an exception
double[] result = pageRank.calculatePageRank(2, 0.85, 2, true);
assertNotNull(result);
}
}