View Javadoc
1   /*
2    * Created on Jul 14, 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;
13  
14  import com.google.common.base.Function;
15  
16  import edu.uci.ics.jung.graph.Hypergraph;
17  
18  /**
19   * An abstract class for iterative random-walk-based vertex scoring algorithms 
20   * that have a 
21   * fixed probability, for each vertex, of 'jumping' to that vertex at each
22   * step in the algorithm (rather than following a link out of that vertex).
23   *
24   * @param <V> the vertex type
25   * @param <E> the edge type
26   * @param <S> the score type
27   */
28  public abstract class AbstractIterativeScorerWithPriors<V,E,S> extends
29          AbstractIterativeScorer<V,E,S> implements VertexScorer<V,S>
30  {
31      /**
32       * The prior probability of each vertex being visited on a given 
33       * 'jump' (non-link-following) step.
34       */
35      protected Function<? super V,? extends S> vertex_priors;
36  
37      /**
38       * The probability of making a 'jump' at each step.
39       */
40      protected double alpha;
41  
42      /**
43       * Creates an instance for the specified graph, edge weights, vertex
44       * priors, and jump probability.
45       * @param g the graph whose vertices are to be assigned scores
46       * @param edge_weights the edge weights to use in the score assignment
47       * @param vertex_priors the prior probabilities of each vertex being 'jumped' to
48       * @param alpha the probability of making a 'jump' at each step
49       */
50      public AbstractIterativeScorerWithPriors(Hypergraph<V,E> g,
51              Function<? super E,? extends Number> edge_weights, 
52              Function<? super V,? extends S> vertex_priors, double alpha)
53      {
54          super(g, edge_weights);
55          this.vertex_priors = vertex_priors;
56          this.alpha = alpha;
57          initialize();
58      }
59  
60      /**
61       * Creates an instance for the specified graph, vertex priors, and jump
62       * probability, with edge weights specified by the subclass.
63       * @param g the graph whose vertices are to be assigned scores
64       * @param vertex_priors the prior probabilities of each vertex being 'jumped' to
65       * @param alpha the probability of making a 'jump' at each step
66       */
67      public AbstractIterativeScorerWithPriors(Hypergraph<V,E> g, 
68      		Function<V,? extends S> vertex_priors, double alpha)
69      {
70          super(g);
71          this.vertex_priors = vertex_priors;
72          this.alpha = alpha;
73          initialize();
74      }
75  
76      /**
77       * Initializes the state of this instance.
78       */
79      @Override
80      public void initialize()
81      {
82          super.initialize();
83          // initialize output values to priors
84          // (output and current are swapped before each step(), so current will
85          // have priors when update()s start happening)
86          for (V v : graph.getVertices())
87              setOutputValue(v, getVertexPrior(v));
88      }
89      
90      /**
91       * Returns the prior probability for <code>v</code>.
92       * @param v the vertex whose prior probability is being queried
93       * @return the prior probability for <code>v</code>
94       */
95      protected S getVertexPrior(V v)
96      {
97          return vertex_priors.apply(v);
98      }
99  
100     /**
101      * Returns a Function which maps each vertex to its prior probability.
102      * @return a Function which maps each vertex to its prior probability
103      */
104     public Function<? super V, ? extends S> getVertexPriors()
105     {
106         return vertex_priors;
107     }
108 
109     /**
110      * Returns the probability of making a 'jump' (non-link-following step).
111      * @return the probability of making a 'jump' (non-link-following step)
112      */
113     public double getAlpha()
114     {
115         return alpha;
116     }
117 }