View Javadoc
1   /**
2    * Copyright (c) 2008, 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    * Created on Jul 14, 2008
10   * 
11   */
12  package edu.uci.ics.jung.algorithms.scoring.util;
13  
14  import com.google.common.base.Function;
15  
16  import edu.uci.ics.jung.graph.Hypergraph;
17  import edu.uci.ics.jung.graph.util.EdgeType;
18  
19  /**
20   * An edge weight function that assigns weights as uniform
21   * transition probabilities.
22   * For undirected edges, returns 1/degree(v) (where 'v' is the
23   * vertex in the VEPair.
24   * For directed edges, returns 1/outdegree(source(e)) (where 'e'
25   * is the edge in the VEPair).
26   * Throws an <code>IllegalArgumentException</code> if the input 
27   * edge is neither EdgeType.UNDIRECTED nor EdgeType.DIRECTED.
28   *
29   */
30  public class UniformDegreeWeight<V, E> implements
31  		Function<VEPair<V, E>, Double> 
32  {
33      private Hypergraph<V, E> graph;
34      
35      /**
36       * @param graph the graph for which an instance is being created
37       */
38      public UniformDegreeWeight(Hypergraph<V, E> graph)
39      {
40          this.graph = graph;
41      }
42  
43  	public Double apply(VEPair<V, E> ve_pair) 
44  	{
45  		E e = ve_pair.getE();
46  		V v = ve_pair.getV();
47  		EdgeType edge_type = graph.getEdgeType(e);
48  		if (edge_type == EdgeType.UNDIRECTED)
49  			return 1.0 / graph.degree(v);
50  		if (edge_type == EdgeType.DIRECTED)
51  			return 1.0 / graph.outDegree(graph.getSource(e));
52  		throw new IllegalArgumentException("can't handle edge type: " + edge_type);
53  	}
54  
55  }