1
2
3
4
5
6
7
8
9
10 package edu.uci.ics.jung.algorithms.scoring;
11
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import junit.framework.Assert;
16 import junit.framework.Test;
17 import junit.framework.TestCase;
18 import junit.framework.TestSuite;
19
20 import com.google.common.base.Functions;
21 import com.google.common.base.Supplier;
22
23 import edu.uci.ics.jung.graph.DirectedGraph;
24 import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
25 import edu.uci.ics.jung.graph.Graph;
26
27
28
29
30 public class TestPageRank extends TestCase {
31
32 private Map<Integer,Number> edgeWeights;
33 private DirectedGraph<Integer,Integer> graph;
34 private Supplier<Integer> edgeFactory;
35
36 public static Test suite() {
37 return new TestSuite(TestPageRank.class);
38 }
39
40 @Override
41 protected void setUp() {
42 edgeWeights = new HashMap<Integer,Number>();
43 edgeFactory = new Supplier<Integer>() {
44 int i=0;
45 public Integer get() {
46 return i++;
47 }};
48 }
49
50 private void addEdge(Graph<Integer,Integer> G, Integer v1, Integer v2, double weight) {
51 Integer edge = edgeFactory.get();
52 graph.addEdge(edge, v1, v2);
53 edgeWeights.put(edge, weight);
54 }
55
56 public void testRanker() {
57 graph = new DirectedSparseMultigraph<Integer,Integer>();
58 for(int i=0; i<4; i++) {
59 graph.addVertex(i);
60 }
61 addEdge(graph,0,1,1.0);
62 addEdge(graph,1,2,1.0);
63 addEdge(graph,2,3,0.5);
64 addEdge(graph,3,1,1.0);
65 addEdge(graph,2,1,0.5);
66
67 PageRankWithPriors<Integer, Integer> pr = new PageRank<Integer, Integer>(graph, Functions.forMap(edgeWeights), 0);
68 pr.evaluate();
69
70 Assert.assertEquals(pr.getVertexScore(0), 0.0, pr.getTolerance());
71 Assert.assertEquals(pr.getVertexScore(1), 0.4, pr.getTolerance());
72 Assert.assertEquals(pr.getVertexScore(2), 0.4, pr.getTolerance());
73 Assert.assertEquals(pr.getVertexScore(3), 0.2, pr.getTolerance());
74
75
76
77
78
79 }
80 }