Example usage for com.google.common.graph MutableGraph addNode

List of usage examples for com.google.common.graph MutableGraph addNode

Introduction

In this page you can find the example usage for com.google.common.graph MutableGraph addNode.

Prototype

@CanIgnoreReturnValue
boolean addNode(N node);

Source Link

Document

Adds node to this graph (optional operation).

Usage

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);/* w  w w . j  a v  a 2  s.  com*/
    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;
}

From source file:org.jgrapht.graph.guava.ImmutableGraphAdapter.java

@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    ois.defaultReadObject();/*w  ww. j ava  2  s .  com*/

    GraphType type = (GraphType) ois.readObject();
    if (type.isMixed() || type.isAllowingMultipleEdges()) {
        throw new IOException("Graph type not supported");
    }

    MutableGraph<V> mutableGraph = (type.isDirected() ? GraphBuilder.directed() : GraphBuilder.undirected())
            .allowsSelfLoops(type.isAllowingSelfLoops()).build();

    // read vertices
    int n = ois.readInt();
    for (int i = 0; i < n; i++) {
        V v = (V) ois.readObject();
        mutableGraph.addNode(v);
    }

    // read edges
    int m = ois.readInt();
    for (int i = 0; i < m; i++) {
        V s = (V) ois.readObject();
        V t = (V) ois.readObject();
        mutableGraph.putEdge(s, t);
    }

    // setup the immutable copy
    this.graph = ImmutableGraph.copyOf(mutableGraph);
}