View Javadoc
1   /*
2   * Copyright (c) 2003, The JUNG Authors
3   *
4   * All rights reserved.
5   *
6   * This software is open-source under the BSD license; see either
7   * "license.txt" or
8   * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9   */
10  package edu.uci.ics.jung.algorithms.scoring;
11  
12  import junit.framework.Assert;
13  import junit.framework.Test;
14  import junit.framework.TestCase;
15  import junit.framework.TestSuite;
16  import edu.uci.ics.jung.graph.DirectedGraph;
17  import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
18  
19  
20  /**
21   * @author Scott White
22   * @author Tom Nelson - adapted to jung2
23   */
24  public class TestHITS extends TestCase {
25  
26  	DirectedGraph<Number,Number> graph;
27  	
28      public static Test suite() {
29          return new TestSuite(TestHITS.class);
30      }
31  
32      @Override
33      protected void setUp() {
34          graph = new DirectedSparseMultigraph<Number,Number>();
35          for(int i=0; i<5; i++) {
36          	graph.addVertex(i);
37          }
38  
39          int j=0;
40          graph.addEdge(j++, 0, 1);
41          graph.addEdge(j++, 1, 2);
42          graph.addEdge(j++, 2, 3);
43          graph.addEdge(j++, 3, 0);
44          graph.addEdge(j++, 2, 1);
45      }
46  
47      public void testRanker() {
48  
49          HITS<Number,Number> ranker = new HITS<Number,Number>(graph);
50          for (int i = 0; i < 10; i++)
51          {
52              ranker.step();
53  //            // check hub scores in terms of previous authority scores
54  //            Assert.assertEquals(t.transform(0).hub, 
55  //            		0.5*ranker.getAuthScore(1) + 0.2*ranker.getAuthScore(4));
56  //            Assert.assertEquals(t.transform(1).hub, 
57  //            		ranker.getAuthScore(2) + 0.2*ranker.getAuthScore(4));
58  //            Assert.assertEquals(t.transform(2).hub, 
59  //            		0.5*ranker.getAuthScore(1) + ranker.getAuthScore(3) + 0.2*ranker.getAuthScore(4));
60  //            Assert.assertEquals(t.transform(3).hub, 
61  //            		ranker.getAuthScore(0) + 0.2*ranker.getAuthScore(4));
62  //            Assert.assertEquals(t.transform(4).hub, 
63  //            		0.2*ranker.getAuthScore(4));
64  //            
65  //            // check authority scores in terms of previous hub scores
66  //            Assert.assertEquals(t.transform(0).authority, 
67  //            		ranker.getVertexScore(3) + 0.2*ranker.getVertexScore(4));
68  //            Assert.assertEquals(t.transform(1).authority, 
69  //            		ranker.getVertexScore(0) + 0.5 * ranker.getVertexScore(2) + 0.2*ranker.getVertexScore(4));
70  //            Assert.assertEquals(t.transform(2).authority, 
71  //            		ranker.getVertexScore(1) + 0.2*ranker.getVertexScore(4));
72  //            Assert.assertEquals(t.transform(3).authority, 
73  //            		0.5*ranker.getVertexScore(2) + 0.2*ranker.getVertexScore(4));
74  //            Assert.assertEquals(t.transform(4).authority, 
75  //            		0.2*ranker.getVertexScore(4));
76  //            
77              // verify that sums of each scores are 1.0
78              double auth_sum = 0;
79              double hub_sum = 0;
80              for (int j = 0; j < 5; j++)
81              {
82  //                auth_sum += ranker.getAuthScore(j);
83  //                hub_sum += ranker.getVertexScore(j);
84  //            	auth_sum += (ranker.getAuthScore(j) * ranker.getAuthScore(j));
85  //            	hub_sum += (ranker.getVertexScore(j) * ranker.getVertexScore(j));
86              	HITS.Scores score = ranker.getVertexScore(j);
87              	auth_sum += score.authority * score.authority;
88              	hub_sum += score.hub * score.hub;
89              }
90              Assert.assertEquals(auth_sum, 1.0, .0001);
91              Assert.assertEquals(hub_sum, 1.0, 0.0001);
92          }
93          
94          ranker.evaluate();
95  
96          Assert.assertEquals(ranker.getVertexScore(0).authority, 0, .0001);  
97          Assert.assertEquals(ranker.getVertexScore(1).authority, 0.8507, .001);
98          Assert.assertEquals(ranker.getVertexScore(2).authority, 0.0, .0001);
99          Assert.assertEquals(ranker.getVertexScore(3).authority, 0.5257, .001);
100 
101         Assert.assertEquals(ranker.getVertexScore(0).hub, 0.5257, .001);
102         Assert.assertEquals(ranker.getVertexScore(1).hub, 0.0, .0001);
103         Assert.assertEquals(ranker.getVertexScore(2).hub, 0.8507, .0001);
104         Assert.assertEquals(ranker.getVertexScore(3).hub, 0.0, .0001);
105 
106         // the values below assume scores sum to 1 
107         // (rather than that sum of squares of scores sum to 1)
108 //        Assert.assertEquals(ranker.getVertexScore(0).authority, 0, .0001);  
109 //        Assert.assertEquals(ranker.getVertexScore(1).authority, 0.618, .001);
110 //        Assert.assertEquals(ranker.getVertexScore(2).authority, 0.0, .0001);
111 //        Assert.assertEquals(ranker.getVertexScore(3).authority, 0.3819, .001);
112 //
113 //        Assert.assertEquals(ranker.getVertexScore(0).hub, 0.38196, .001);
114 //        Assert.assertEquals(ranker.getVertexScore(1).hub, 0.0, .0001);
115 //        Assert.assertEquals(ranker.getVertexScore(2).hub, 0.618, .0001);
116 //        Assert.assertEquals(ranker.getVertexScore(3).hub, 0.0, .0001);
117     }
118 
119 }