2 * Copyright (c) 2008, the JUNG Project and the Regents of the University
6 * This software is open-source under the BSD license; see either
8 * http://jung.sourceforge.net/license.txt for a description.
9 * Created on Aug 22, 2008
12 package edu.uci.ics.jung.algorithms.scoring;
14 import org.apache.commons.collections15.Transformer;
16 import edu.uci.ics.jung.algorithms.scoring.util.ScoringUtils;
17 import edu.uci.ics.jung.graph.Hypergraph;
20 * A special case of {@code PageRankWithPriors} in which the final scores
21 * represent a probability distribution over position assuming a random (Markovian)
22 * walk of exactly k steps, based on the initial distribution specified by the priors.
24 * <p><b>NOTE</b>: The version of {@code KStepMarkov} in {@code algorithms.importance}
25 * (and in JUNG 1.x) is believed to be incorrect: rather than returning
26 * a score which represents a probability distribution over position assuming
27 * a k-step random walk, it returns a score which represents the sum over all steps
28 * of the probability for each step. If you want that behavior, set the
29 * 'cumulative' flag as follows <i>before calling {@code evaluate()}</i>:
31 * KStepMarkov ksm = new KStepMarkov(...);
32 * ksm.setCumulative(true);
36 * By default, the 'cumulative' flag is set to false.
38 * NOTE: THIS CLASS IS NOT YET COMPLETE. USE AT YOUR OWN RISK. (The original behavior
39 * is captured by the version still available in {@code algorithms.importance}.)
41 * @see "Algorithms for Estimating Relative Importance in Graphs by Scott White and Padhraic Smyth, 2003"
43 * @see PageRankWithPriors
45 public class KStepMarkov<V,E> extends PageRankWithPriors<V,E>
47 private boolean cumulative;
50 * Creates an instance based on the specified graph, edge weights, vertex
51 * priors (initial scores), and number of steps to take.
52 * @param graph the input graph
53 * @param edge_weights the edge weights (transition probabilities)
54 * @param vertex_priors the initial probability distribution (score assignment)
55 * @param steps the number of times that {@code step()} will be called by {@code evaluate}
57 public KStepMarkov(Hypergraph<V,E> graph, Transformer<E, ? extends Number> edge_weights,
58 Transformer<V, Double> vertex_priors, int steps)
60 super(graph, edge_weights, vertex_priors, 0);
65 * Creates an instance based on the specified graph, vertex
66 * priors (initial scores), and number of steps to take. The edge
67 * weights (transition probabilities) are set to default values (a uniform
68 * distribution over all outgoing edges).
69 * @param graph the input graph
70 * @param vertex_priors the initial probability distribution (score assignment)
71 * @param steps the number of times that {@code step()} will be called by {@code evaluate}
73 public KStepMarkov(Hypergraph<V,E> graph, Transformer<V, Double> vertex_priors, int steps)
75 super(graph, vertex_priors, 0);
80 * Creates an instance based on the specified graph and number of steps to
81 * take. The edge weights (transition probabilities) and vertex initial scores
82 * (prior probabilities) are set to default values (a uniform
83 * distribution over all outgoing edges, and a uniform distribution over
84 * all vertices, respectively).
85 * @param graph the input graph
86 * @param steps the number of times that {@code step()} will be called by {@code evaluate}
88 public KStepMarkov(Hypergraph<V,E> graph, int steps)
90 super(graph, ScoringUtils.getUniformRootPrior(graph.getVertices()), 0);
94 private void initialize(int steps)
96 this.acceptDisconnectedGraph(false);
99 throw new IllegalArgumentException("Number of steps must be > 0");
101 this.max_iterations = steps;
102 this.tolerance = -1.0;
104 this.cumulative = false;
108 * Specifies whether this instance should assign a score to each vertex
112 public void setCumulative(boolean cumulative)
114 this.cumulative = cumulative;
118 * Updates the value for this vertex. Called by <code>step()</code>.
121 public double update(V v)
124 return super.update(v);
126 collectDisappearingPotential(v);
129 for (E e : graph.getInEdges(v))
131 // For graphs, the code below is equivalent to
132 // V w = graph.getOpposite(v, e);
133 // total_input += (getCurrentValue(w) * getEdgeWeight(w,e).doubleValue());
134 // For hypergraphs, this divides the potential coming from w
135 // by the number of vertices in the connecting edge e.
136 int incident_count = getAdjustedIncidentCount(e);
137 for (V w : graph.getIncidentVertices(e))
139 if (!w.equals(v) || hyperedges_are_self_loops)
140 v_input += (getCurrentValue(w) *
141 getEdgeWeight(w,e).doubleValue() / incident_count);
145 // modify total_input according to alpha
146 double new_value = alpha > 0 ?
147 v_input * (1 - alpha) + getVertexPrior(v) * alpha :
149 setOutputValue(v, new_value + getCurrentValue(v));
151 // FIXME: DO WE NEED TO CHANGE HOW DISAPPEARING IS COUNTED? NORMALIZE?
153 return Math.abs(getCurrentValue(v) - new_value);