1 /*
2 * Created on Jul 12, 2007
3 *
4 * Copyright (c) 2007, The JUNG Authors
5 *
6 * All rights reserved.
7 *
8 * This software is open-source under the BSD license; see either
9 * "license.txt" or
10 * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
11 */
12 package edu.uci.ics.jung.algorithms.scoring.util;
13
14 import java.util.Collection;
15
16 import com.google.common.base.Function;
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 a Function assigning a uniform prior to each element in {@code roots}
31 */
32 public static <V> Function<V, Double> getUniformRootPrior(Collection<V> roots)
33 {
34 final Collection<V> inner_roots = roots;
35 Function<V, Double> distribution = new Function<V, Double>()
36 {
37 public Double apply(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 Function 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 Function that assigns uniform prior hub/authority probabilities to each root
55 */
56 public static <V> Function<V, HITS.Scores> getHITSUniformRootPrior(Collection<V> roots)
57 {
58 final Collection<V> inner_roots = roots;
59 Function<V, HITS.Scores> distribution =
60 new Function<V, HITS.Scores>()
61 {
62 public HITS.Scores apply(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 }