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

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

Introduction

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

Prototype

Set<N> adjacentNodes(Object node);

Source Link

Document

Returns the nodes which have an incident edge in common with node in this graph.

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  .j a v a  2s.c  o  m*/
 *   <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.algorithms.metrics.TriadicCensus.java

/**
 * Return true iff this ordering is canonical and therefore we should build statistics for it.
 *
 * @param g the graph whose properties are being examined
 * @param id a list of the nodes in g; used to assign an index to each
 * @param u a node in g/*from  ww  w.  j  a  va  2  s.  c o m*/
 * @param v a node in g
 * @param w a node in g
 * @param <N> the node type
 * @param <E> the edge type
 * @return true if index(u) &lt; index(w), or if index(v) &lt; index(w) &lt; index(u) and v
 *     doesn't link to w; false otherwise
 */
protected static <N, E> boolean shouldCount(Graph<N> g, List<N> id, N u, N v, N w) {
    int i_u = id.indexOf(u);
    int i_w = id.indexOf(w);
    if (i_u < i_w) {
        return true;
    }
    int i_v = id.indexOf(v);
    if ((i_v < i_w) && (i_w < i_u) && (!g.adjacentNodes(w).contains(v))) {
        return true;
    }
    return false;
}

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

/**
 * Returns an array whose ith element (for i in [1,16]) is the number of occurrences of the
 * corresponding triad type in <code>g</code>. (The 0th element is not meaningful; this array is
 * effectively 1-based.)//from w  w w. ja  v  a 2 s.c  o m
 *
 * @param g the graph whose properties are being measured
 * @param <N> the node type
 * @return an array encoding the number of occurrences of each triad type
 */
public static <N> long[] getCounts(Graph<N> g) {
    Preconditions.checkArgument(g.isDirected(), "input graph must be directed");
    long[] count = new long[MAX_TRIADS];

    // TODO: can we make this more efficient and not require the extra list?
    List<N> id = new ArrayList<N>(g.nodes());

    // apply algorithm to each edge, one at at time
    for (int i_v = 0; i_v < id.size(); i_v++) {
        N v = id.get(i_v);
        for (N u : g.adjacentNodes(v)) {
            int triType = -1;
            if (id.indexOf(u) <= i_v) {
                continue;
            }
            Set<N> neighbors = new HashSet<N>(g.adjacentNodes(u));
            neighbors.addAll(g.adjacentNodes(v));
            neighbors.remove(u);
            neighbors.remove(v);
            // TODO: use hasEdge() when available
            if (g.successors(v).contains(u) && g.successors(u).contains(v)) {
                triType = 3;
            } else {
                triType = 2;
            }
            count[triType] += id.size() - neighbors.size() - 2;
            for (N w : neighbors) {
                if (shouldCount(g, id, u, v, w)) {
                    count[triType(triCode(g, u, v, w))]++;
                }
            }
        }
    }
    long sum = 0;
    for (int i = 2; i <= 16; i++) {
        sum += count[i];
    }
    int n = id.size();
    count[1] = n * (n - 1) * (n - 2) / 6 - sum;
    return count;
}

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

private synchronized void adjustNode(N node, Point tempXYD) {
    Graph<N> graph = layoutModel.getGraph();
    queue.clear();//from ww w  .  j  av a  2s  . c  o  m
    ISOMNodeData ivd = getISOMNodeData(node);
    ivd.distance = 0;
    ivd.visited = true;
    queue.add(node);
    N current;

    while (!queue.isEmpty()) {
        current = queue.remove(0);
        ISOMNodeData currData = getISOMNodeData(current);
        Point currXYData = layoutModel.apply(current);

        double dx = tempXYD.x - currXYData.x;
        double dy = tempXYD.y - currXYData.y;
        double factor = adaption / Math.pow(2, currData.distance);

        layoutModel.set(current, currXYData.x + (factor * dx), currXYData.y + (factor * dy));

        if (currData.distance < radius) {
            Collection<N> s = graph.adjacentNodes(current);
            while (true) {
                try {
                    for (N child : s) {
                        ISOMNodeData childData = getISOMNodeData(child);
                        if (childData != null && !childData.visited) {
                            childData.visited = true;
                            childData.distance = currData.distance + 1;
                            queue.add(child);
                        }
                    }
                    break;
                } catch (ConcurrentModificationException cme) {
                }
            }
        }
    }
}