Example usage for com.google.common.graph Graph degree

List of usage examples for com.google.common.graph Graph degree

Introduction

In this page you can find the example usage for com.google.common.graph Graph degree.

Prototype

int degree(Object node);

Source Link

Document

Returns the number of edges incident in this graph to node .

Usage

From source file:edu.uci.ics.jung.algorithms.metrics.Metrics.java

/**
 * Returns a <code>Map</code> of nodes to their clustering coefficients. The clustering
 * coefficient cc(v) of a node v is defined as follows:
 *
 * <ul>/*from   ww w. ja  va 2s  .c om*/
 *   <li><code>degree(v) == {0,1}</code>: 0
 *   <li><code>degree(v) == n, n &gt;= 2</code>: given S, the set of neighbors of <code>v</code>:
 *       cc(v) = (the sum over all w in S of the number of other elements of w that are neighbors
 *       of w) / ((|S| * (|S| - 1) / 2). Less formally, the fraction of <code>v</code>'s neighbors
 *       that are also neighbors of each other.
 * </ul>
 *
 * <p><b>Note</b>: This algorithm treats its argument as an undirected graph; edge direction is
 * ignored.
 *
 * @param graph the graph whose clustering coefficients are to be calculated
 * @param <N> the node type
 * @param <E> the edge type
 * @return the clustering coefficient for each node
 * @see "The structure and function of complex networks, M.E.J. Newman,
 *     aps.arxiv.org/abs/cond-mat/0303516"
 */
public static <N> ImmutableMap<N, Double> clusteringCoefficients(Graph<N> graph) {
    ImmutableMap.Builder<N, Double> coefficients = ImmutableMap.builder();

    for (N v : graph.nodes()) {
        int n = graph.degree(v);
        if (n < 2) {
            coefficients.put(v, new Double(0));
        } else {
            int edgeCount = 0;
            for (N w : graph.adjacentNodes(v)) {
                if (!w.equals(v)) {
                    for (N x : graph.adjacentNodes(v)) {
                        // TODO: replace with hasEdge() once it's ready
                        if (!w.equals(x) && graph.adjacentNodes(w).contains(x)) {
                            edgeCount++;
                        }
                    }
                }
            }
            double possible_edges = (n * (n - 1)) / 2.0;
            coefficients.put(v, new Double(edgeCount / possible_edges));
        }
    }

    return coefficients.build();
}

From source file:edu.uci.ics.jung.layout.algorithms.SpringLayoutAlgorithm.java

protected void relaxEdges() {
    Graph<N> graph = layoutModel.getGraph();
    try {/* w w w . j  a v  a 2  s .c  o m*/
        for (EndpointPair<N> endpoints : layoutModel.getGraph().edges()) {
            N node1 = endpoints.nodeU();
            N node2 = endpoints.nodeV();

            Point p1 = this.layoutModel.get(node1);
            Point p2 = this.layoutModel.get(node2);
            if (p1 == null || p2 == null) {
                continue;
            }
            double vx = p1.x - p2.x;
            double vy = p1.y - p2.y;
            double len = Math.sqrt(vx * vx + vy * vy);

            double desiredLen = lengthFunction.apply(endpoints);

            // round from zero, if needed [zero would be Bad.].
            len = (len == 0) ? .0001 : len;

            double f = force_multiplier * (desiredLen - len) / len;
            f = f * Math.pow(stretch, (graph.degree(node1) + graph.degree(node2) - 2));

            // the actual movement distance 'dx' is the force multiplied by the
            // distance to go.
            double dx = f * vx;
            double dy = f * vy;
            SpringNodeData v1D, v2D;
            v1D = springNodeData.getUnchecked(node1);
            v2D = springNodeData.getUnchecked(node2);
            v1D.edgedx += dx;
            v1D.edgedy += dy;
            v2D.edgedx += -dx;
            v2D.edgedy += -dy;
        }
    } catch (ConcurrentModificationException cme) {
        relaxEdges();
    }
}

From source file:edu.uci.ics.jung.layout.algorithms.DAGLayoutAlgorithm.java

/**
 * Overridden relaxEdges. This one reduces the effect of edges between greatly different levels.
 *//*from ww  w  . jav  a  2  s . c o  m*/
@Override
protected void relaxEdges() {
    Graph<N> graph = layoutModel.getGraph();
    for (EndpointPair<N> endpoints : graph.edges()) {
        N node1 = endpoints.nodeU();
        N node2 = endpoints.nodeV();

        Point p1 = layoutModel.apply(node1);
        Point p2 = layoutModel.apply(node2);
        double vx = p1.x - p2.x;
        double vy = p1.y - p2.y;
        double len = Math.sqrt(vx * vx + vy * vy);

        // JY addition.
        int level1 = minLevels.get(node1).intValue();
        int level2 = minLevels.get(node2).intValue();

        double desiredLen = lengthFunction.apply(endpoints);

        // round from zero, if needed [zero would be Bad.].
        len = (len == 0) ? .0001 : len;

        // force factor: optimal length minus actual length,
        // is made smaller as the current actual length gets larger.
        // why?

        double f = force_multiplier * (desiredLen - len) / len;

        f = f * Math.pow(stretch / 100.0, (graph.degree(node1) + graph.degree(node2) - 2));

        // JY addition. If this is an edge which stretches a long way,
        // don't be so concerned about it.
        if (level1 != level2) {
            f = f / Math.pow(Math.abs(level2 - level1), 1.5);
        }

        // the actual movement distance 'dx' is the force multiplied by the
        // distance to go.
        double dx = f * vx;
        double dy = f * vy;
        SpringNodeData v1D, v2D;
        v1D = springNodeData.getUnchecked(node1);
        v2D = springNodeData.getUnchecked(node2);

        v1D.edgedx += dx;
        v1D.edgedy += dy;
        v2D.edgedx += -dx;
        v2D.edgedy += -dy;
    }
}