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

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

Introduction

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

Prototype

Set<N> predecessors(Object node);

Source Link

Document

Returns all nodes in this graph adjacent to node which can be reached by traversing node 's incoming edges against the direction (if any) of the edge.

Usage

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

protected static <N, E> boolean link(Graph<N> g, N a, N b) {
    return g.predecessors(b).contains(a);
}

From source file:edu.uci.ics.jung.graph.util.TreeUtils.java

public static <N> ImmutableSet<N> roots(Graph<N> graph) {
    checkNotNull(graph, "graph");
    return graph.nodes().stream().filter(node -> graph.predecessors(node).isEmpty()).collect(toImmutableSet());
}

From source file:edu.uci.ics.jung.graph.util.TreeUtils.java

/**
 * A graph is "forest-shaped" if it is directed, acyclic, and each node has at most one
 * predecessor.//from  w w  w  .j  a va 2  s  .com
 */
public static <N> boolean isForestShaped(Graph<N> graph) {
    checkNotNull(graph, "graph");
    return graph.isDirected() && !Graphs.hasCycle(graph)
            && graph.nodes().stream().allMatch(node -> graph.predecessors(node).size() <= 1);
}

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

/**
 * @param node the node whose center is to be returned
 * @return the coordinates of {@code node}'s parent, or the center of this layout's area if it's a
 *     root./*from  w w w  .j ava  2 s.co m*/
 */
public Point getCenter(LayoutModel<N> layoutModel, N node) {
    Graph<N> graph = layoutModel.getGraph();
    N parent = Iterables.getOnlyElement(graph.predecessors(node), null);
    if (parent == null) {
        return getCenter(layoutModel);
    }
    return layoutModel.get(parent);
}

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

/**
 * A recursive method for allocating the level for each node. Ensures that all predecessors of v
 * have a level which is at least one greater than the level of v.
 *
 * @param node the node whose minimum level is to be calculated
 *//* w w w.j  av  a2  s.  co  m*/
public void propagateMinimumLevel(N node) {
    Graph<N> graph = layoutModel.getGraph();
    int level = minLevels.get(node).intValue();
    for (N child : graph.predecessors(node)) {
        int oldLevel, newLevel;
        Number o = minLevels.get(child);
        if (o != null) {
            oldLevel = o.intValue();
        } else {
            oldLevel = 0;
        }
        newLevel = Math.max(oldLevel, level + 1);
        minLevels.put(child, new Integer(newLevel));

        if (newLevel > graphHeight) {
            graphHeight = newLevel;
        }
        propagateMinimumLevel(child);
    }
}

From source file:edu.uci.ics.jung.algorithms.filters.KNeighborhoodFilter.java

public static <N> MutableGraph<N> filterGraph(Graph<N> graph, Set<N> rootNodes, int radius) {
    checkNotNull(graph);//from w  w w  .  j  a v  a 2 s. c om
    checkNotNull(rootNodes);
    checkArgument(graph.nodes().containsAll(rootNodes), "graph must contain all of rootNodes");
    checkArgument(radius > 0, "radius must be > 0");

    MutableGraph<N> filtered = GraphBuilder.from(graph).build();
    for (N root : rootNodes) {
        filtered.addNode(root);
    }
    Queue<N> currentNodes = new ArrayDeque<>(rootNodes);
    Queue<N> nextNodes = new ArrayDeque<>();

    for (int depth = 1; depth <= radius && !currentNodes.isEmpty(); depth++) {
        while (!currentNodes.isEmpty()) {
            N currentNode = currentNodes.remove();
            for (N nextNode : graph.successors(currentNode)) {
                // the addNode needs to happen before putEdge() because we need to know whether
                // the node was present in the graph
                // (and putEdge() will always add the node if not present)
                if (filtered.addNode(nextNode)) {
                    nextNodes.add(nextNode);
                }
                filtered.putEdge(currentNode, nextNode);
            }
        }
        Queue<N> emptyQueue = currentNodes;
        currentNodes = nextNodes;
        nextNodes = emptyQueue;
    }

    // put in in-edges from nodes in the filtered graph
    for (N node : filtered.nodes()) {
        for (N predecessor : graph.predecessors(node)) {
            if (filtered.nodes().contains(predecessor)) {
                filtered.putEdge(predecessor, node);
            }
        }
    }

    return filtered;
}