Initial opendaylight infrastructure commit!!
[controller.git] / third-party / net.sf.jung2 / src / main / java / edu / uci / ics / jung / algorithms / scoring / util / ScoringUtils.java
1 /*
2  * Created on Jul 12, 2007
3  *
4  * Copyright (c) 2007, the JUNG Project and the Regents of the University 
5  * of California
6  * All rights reserved.
7  *
8  * This software is open-source under the BSD license; see either
9  * "license.txt" or
10  * http://jung.sourceforge.net/license.txt for a description.
11  */
12 package edu.uci.ics.jung.algorithms.scoring.util;
13
14 import java.util.Collection;
15
16 import org.apache.commons.collections15.Transformer;
17
18 import edu.uci.ics.jung.algorithms.scoring.HITS;
19
20 /**
21  * Methods for assigning values (to be interpreted as prior probabilities) to vertices in the context
22  * of random-walk-based scoring algorithms.
23  */
24 public class ScoringUtils
25 {
26     /**
27      * Assigns a probability of 1/<code>roots.size()</code> to each of the elements of <code>roots</code>.
28      * @param <V> the vertex type
29      * @param roots the vertices to be assigned nonzero prior probabilities
30      * @return
31      */
32     public static <V> Transformer<V, Double> getUniformRootPrior(Collection<V> roots)
33     {
34         final Collection<V> inner_roots = roots;
35         Transformer<V, Double> distribution = new Transformer<V, Double>()
36         {
37             public Double transform(V input)
38             {
39                 if (inner_roots.contains(input))
40                     return new Double(1.0 / inner_roots.size());
41                 else
42                     return 0.0;
43             }
44         };
45         
46         return distribution;
47     }
48     
49     /**
50      * Returns a Transformer that hub and authority values of 1/<code>roots.size()</code> to each 
51      * element of <code>roots</code>.
52      * @param <V> the vertex type
53      * @param roots the vertices to be assigned nonzero scores
54      * @return a Transformer that assigns uniform prior hub/authority probabilities to each root
55      */
56     public static <V> Transformer<V, HITS.Scores> getHITSUniformRootPrior(Collection<V> roots)
57     {
58         final Collection<V> inner_roots = roots;
59         Transformer<V, HITS.Scores> distribution = 
60                 new Transformer<V, HITS.Scores>()
61         {
62             public HITS.Scores transform(V input)
63             {
64                 if (inner_roots.contains(input))
65                     return new HITS.Scores(1.0 / inner_roots.size(), 1.0 / inner_roots.size());
66                 else
67                     return new HITS.Scores(0.0, 0.0);
68             }
69         };
70         return distribution;
71     }
72 }