List of usage examples for com.google.common.graph Graph isDirected
boolean isDirected();
From source file:edu.uci.ics.jung.algorithms.transformation.NodePartitionCollapser.java
/** * Creates a new graph whose nodes correspond to the partitions of the supplied graph. Two nodes u * and v in the collapsed graph will be connected if there is an edge between any of the nodes in * u and any of the nodes in v, and u and v are distinct. The value of the edge represents the * number of such edges./*from w ww.j a v a 2 s . co m*/ * * @param partitioning a node partition of a graph * @return the collapsed graph */ public static <N> ValueGraph<Set<N>, Integer> collapseNodePartitions(NodePartition<N> partitioning) { Graph<N> original = partitioning.getGraph(); ValueGraphBuilder<Object, Object> builder = original.isDirected() ? ValueGraphBuilder.directed() : ValueGraphBuilder.undirected(); MutableValueGraph<Set<N>, Integer> collapsed = builder.build(); // create nodes in new graph corresponding to equivalence sets in the original graph for (Set<N> set : partitioning.getNodePartitions()) { collapsed.addNode(set); } // for each pair of endpoints in the original graph, connect the corresponding nodes // (representing partitions) in the collapsed graph if the partitions are different Map<N, Set<N>> nodeToPartition = partitioning.getNodeToPartitionMap(); for (EndpointPair<N> endpoints : original.edges()) { N nodeU = endpoints.nodeU(); N nodeV = endpoints.nodeV(); Set<N> partitionU = nodeToPartition.get(nodeU); Set<N> partitionV = nodeToPartition.get(nodeV); if (nodeU.equals(nodeV) || partitionU.equals(partitionV)) { // we only connect partitions if the partitions are different; // check the nodes first as an optimization continue; } int edgeCount = collapsed.edgeValueOrDefault(partitionU, partitionV, 0); collapsed.putEdgeValue(partitionU, partitionV, edgeCount + 1); } return collapsed; }
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 ww . j av a 2s . c om */ 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.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.)/*w w w .j ava 2s. 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; }