List of usage examples for com.google.common.graph Graph successors
Set<N> successors(Object 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);/*from w w w. j a v a2s .c o m*/ 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: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 ww w. j av a2s . c om*/ * * @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.algorithms.generators.random.KleinbergSmallWorld.java
private WeightedChoice<N> getWeightedChoiceForDistance(N source, Graph<N> graph, Distance<N> distance) { Map<N, Double> nodeWeights = new HashMap<>(); Set<N> successors = graph.successors(source); for (N node : graph.nodes()) { // don't include the source or its successors if (!node.equals(source) && !successors.contains(node)) { nodeWeights.put(node,/*from w w w . j a v a 2 s. co m*/ Math.pow(distance.getDistance(source, node).doubleValue(), -clusteringExponent)); } } Preconditions.checkState(nodeWeights.size() >= connectionCount, "number of possible targets (%s) must be greater than connection count (%s)", nodeWeights.size(), connectionCount); WeightedChoice<N> weightedChoice = new WeightedChoice<>(nodeWeights, random); return weightedChoice; }
From source file:no.ssb.vtl.script.operations.hierarchy.HierarchyOperation.java
/** * Try to find a path in the graph between from and to. * * @return a list (possibly empty) of paths *//* ww w .j av a 2 s . c o m*/ @VisibleForTesting static <T> List<List<T>> findPaths(Graph<T> graph, T from, T to) { List<List<T>> paths = Lists.newArrayList(); if (graph.nodes().contains(from) && graph.nodes().contains(to)) { // DAG means no loop. // Each time we add nodes, check if there is a path // already. We don't check if we never saw one of them. //LinkedHashSet<String> path = Sets.newLinkedHashSet(); Deque<T> stack = Lists.newLinkedList(); stack.push(from); while (!stack.isEmpty()) { T current = stack.pop(); if (current.equals(to)) { // return false; ArrayList<T> path = Lists.newArrayList(); path.add(from); path.addAll(stack); path.add(to); paths.add(path); continue; } for (T t : graph.successors(current)) { if (!stack.contains(t)) { stack.push(t); } } } } return paths; }
From source file:edu.uci.ics.jung.layout.algorithms.DAGLayoutAlgorithm.java
/** * Calculates the level of each node in the graph. Level 0 is allocated to each node with no * successors. Level n+1 is allocated to any node whose successors' maximum level is n. *//*from ww w . j av a2 s .co m*/ public void setRoot() { Graph<N> graph = layoutModel.getGraph(); numRoots = 0; // Network<N, E> g = network; for (N node : graph.nodes()) { if (graph.successors(node).isEmpty()) { setRoot(node); numRoots++; } } }
From source file:edu.uci.ics.jung.layout.algorithms.BalloonLayoutAlgorithm.java
protected void setRootPolars(LayoutModel<N> layoutModel) { Graph<N> graph = layoutModel.getGraph(); Set<N> roots = TreeUtils.roots(graph); int width = layoutModel.getWidth(); if (roots.size() == 1) { // its a Tree N root = Iterables.getOnlyElement(roots); setRootPolar(layoutModel, root); setPolars(layoutModel, graph.successors(root), getCenter(layoutModel), width / 2); } else if (roots.size() > 1) { // its a Network setPolars(layoutModel, roots, getCenter(layoutModel), width / 2); }//w w w.java 2 s . c o m }
From source file:edu.uci.ics.jung.algorithms.shortestpath.BFSDistanceLabeler.java
/** * Computes the distances of all the node from the starting root nodes. If there is more than one * root node the minimum distance from each root node is used as the designated distance to a * given node. Also keeps track of the predecessors of each node traversed as well as the order of * nodes traversed./*from www . j a v a 2s . c o m*/ * * @param graph the graph to label * @param rootSet the set of starting nodes to traverse from */ public void labelDistances(Graph<N> graph, Set<N> rootSet) { initialize(graph, rootSet); int distance = 1; while (true) { List<N> newList = new ArrayList<N>(); for (N currentNode : mCurrentList) { if (graph.nodes().contains(currentNode)) { for (N next : graph.successors(currentNode)) { visitNewNode(currentNode, next, distance, newList); } } } if (newList.size() == 0) { break; } mCurrentList = newList; distance++; } for (N v : mUnvisitedNodes) { distanceDecorator.put(v, -1); } }