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 java.util.HashSet;
13  import java.util.Set;
14  
15  import junit.framework.Assert;
16  import junit.framework.Test;
17  import junit.framework.TestCase;
18  import junit.framework.TestSuite;
19  import edu.uci.ics.jung.algorithms.scoring.util.ScoringUtils;
20  import edu.uci.ics.jung.graph.DirectedGraph;
21  import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
22  
23  
24  /**
25   * Tests HITSWithPriors.
26   */
27  public class TestHITSWithPriors extends TestCase {
28  
29  	DirectedGraph<Number,Number> graph;
30  	Set<Number> roots;
31  	
32      public static Test suite() {
33          return new TestSuite(TestHITSWithPriors.class);
34      }
35  
36      @Override
37      protected void setUp() {
38      	graph = new DirectedSparseMultigraph<Number,Number>();
39      	for(int i=0; i<4; i++) {
40      		graph.addVertex(i);
41      	}
42      	int j=0;
43      	graph.addEdge(j++, 0, 1);
44      	graph.addEdge(j++, 1, 2);
45      	graph.addEdge(j++, 2, 3);
46      	graph.addEdge(j++, 3, 0);
47      	graph.addEdge(j++, 2, 1);
48  
49          roots = new HashSet<Number>();
50          roots.add(2);
51      }
52  
53      public void testRankings() {
54  
55          HITSWithPriors<Number,Number> ranker = 
56              new HITSWithPriors<Number,Number>(graph, ScoringUtils.getHITSUniformRootPrior(roots), 0.3);
57          ranker.evaluate();
58          
59          double[] expected_auth = {0.0, 0.765, 0.365, 0.530};
60          double[] expected_hub = {0.398, 0.190, 0.897, 0.0};
61  
62          double hub_sum = 0;
63          double auth_sum = 0;
64          for (Number n : graph.getVertices())
65          {
66              int i = n.intValue();
67              double auth = ranker.getVertexScore(i).authority;
68              double hub = ranker.getVertexScore(i).hub;
69              Assert.assertEquals(auth, expected_auth[i], 0.001);
70              Assert.assertEquals(hub, expected_hub[i], 0.001);
71              hub_sum += hub * hub;
72              auth_sum += auth * auth;
73          }
74          Assert.assertEquals(1.0, hub_sum, 0.001);
75          Assert.assertEquals(1.0, auth_sum, 0.001);
76      }
77  }