List of usage examples for com.google.common.graph MutableGraph addNode
@CanIgnoreReturnValue
boolean addNode(N node);
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); }