View Javadoc
1   /*
2    * Created on Jul 11, 2008
3    *
4    * Copyright (c) 2008, 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 com.google.common.base.Function;
15  
16  import edu.uci.ics.jung.graph.Graph;
17  import edu.uci.ics.jung.graph.util.EdgeType;
18  
19  /**
20   * Assigns weights to directed edges (the edge of the vertex/edge pair) depending on 
21   * whether the vertex is the edge's source or its destination.
22   * If the vertex v is the edge's source, assigns 1/outdegree(v).
23   * Otherwise, assigns 1/indegree(w).
24   * Throws <code>IllegalArgumentException</code> if the edge is not directed.
25   */
26  public class UniformInOut<V,E> implements Function<VEPair<V,E>, Double>
27  {
28  	/**
29  	 * The graph for which the edge weights are defined.
30  	 */
31      protected Graph<V,E> graph;
32      
33      /**
34       * Creates an instance for the specified graph.
35       * @param graph the graph for which the edge weights will be defined
36       */
37      public UniformInOut(Graph<V,E> graph)
38      {
39          this.graph = graph;
40      }
41      
42      public Double apply(VEPair<V,E> ve_pair)
43      {
44      	V v = ve_pair.getV();
45      	E e = ve_pair.getE();
46      	if (graph.getEdgeType(e) != EdgeType.DIRECTED)
47      		throw new IllegalArgumentException("This Function only" +
48      				" operates on directed edges");
49      	return 1.0 / (graph.isSource(v, e) ? 
50      			graph.outDegree(v) : 
51      			graph.inDegree(v));
52      }
53  }