List of usage examples for com.google.common.graph MutableNetwork addEdge
@CanIgnoreReturnValue
boolean addEdge(E edge, N node1, N node2);
From source file:org.apache.beam.runners.dataflow.worker.graph.MapTaskToNetworkFunction.java
/** * Adds an edge to the network from a PCollection to an instruction based upon the {@link * InstructionInput}./*w w w. ja va2s. c o m*/ */ private static void attachInput(InstructionInput input, MutableNetwork<Node, Edge> network, ParallelInstructionNode node, InstructionOutputNode[][] outputNodes) { int producerInstructionIndex = Apiary.intOrZero(input.getProducerInstructionIndex()); int outputNum = Apiary.intOrZero(input.getOutputNum()); network.addEdge(outputNodes[producerInstructionIndex][outputNum], node, DefaultEdge.create()); }
From source file:edu.uci.ics.jung.samples.SpatialLensLargeGraphDemo.java
private static void createEdge(MutableNetwork<String, Number> g, String v1Label, String v2Label, int weight) { g.addEdge(v1Label, v2Label, new Double(Math.random())); }
From source file:edu.uci.ics.jung.samples.NodeCollapseDemo.java
public static Network<String, Number> getSmallGraph() { MutableNetwork g = NetworkBuilder.undirected().allowsParallelEdges(true).build(); int nodeIt;// ww w . java 2s.c o m int current; String i; String next; for (nodeIt = 1; nodeIt <= 3; ++nodeIt) { for (current = nodeIt + 1; current <= 3; ++current) { i = "" + nodeIt; next = "" + current; g.addEdge(i, next, Math.pow((double) (nodeIt + 2), (double) current)); } } for (nodeIt = 11; nodeIt <= 4; ++nodeIt) { for (current = nodeIt + 1; current <= 4; ++current) { if (Math.random() <= 0.6D) { i = "" + nodeIt; next = "" + current; g.addEdge(i, next, Math.pow((double) (nodeIt + 2), (double) current)); } } } // Iterator var5 = g.nodes().iterator(); // String var6 = (String) var5.next(); // int var7 = 0; // while(var5.hasNext()) { // next = (String)var5.next(); // g.addEdge(var6, next, new Integer(var7++)); // } return g; }
From source file:org.apache.beam.runners.core.construction.graph.Networks.java
/** * Return a set of nodes in sorted topological order. * * <p>Note that back edges within directed graphs are "broken" returning a topological order for a * directed acyclic network which approximates the original network. * * <p>Nodes will be considered in the order specified by the {@link * ElementOrder#sorted(Comparator) sorted ElementOrder} created with the provided comparator. *///from ww w. ja v a 2 s.c o m public static <NodeT, EdgeT> Iterable<NodeT> topologicalOrder(Network<NodeT, EdgeT> network, Comparator<NodeT> nodeOrder) { // Copy the characteristics of the network to ensure that the result network can represent the // original network, just with the provided suborder MutableNetwork<NodeT, EdgeT> orderedNetwork = NetworkBuilder.from(network) .nodeOrder(ElementOrder.sorted(nodeOrder)).build(); for (NodeT node : network.nodes()) { orderedNetwork.addNode(node); } for (EdgeT edge : network.edges()) { EndpointPair<NodeT> incident = network.incidentNodes(edge); orderedNetwork.addEdge(incident.source(), incident.target(), edge); } return computeTopologicalOrder(orderedNetwork); }
From source file:edu.uci.ics.jung.samples.SpatialLensLargeGraphDemo.java
public static Network<String, Number> getGraph() { MutableNetwork<String, Number> g = NetworkBuilder.undirected().allowsParallelEdges(true).build(); for (int i = 0; i < pairs.length; i++) { String[] pair = pairs[i]; createEdge(g, pair[0], pair[1], Integer.parseInt(pair[2])); }// w w w . j a v a 2 s. c o m int edge = 10; for (int i = 1; i <= 10; i++) { for (int j = i + 1; j <= 10; j++) { String i1 = "c" + i; String i2 = "c" + j; g.addEdge(i1, i2, edge++); } } for (int i = 1; i <= 10; i++) { for (int j = i + 1; j <= 10; j++) { String i1 = "d" + i; String i2 = "d" + j; g.addEdge(i1, i2, edge++); } } // and, last, a partial clique for (int i = 1; i <= 20; i++) { for (int j = i + 1; j <= 20; j++) { if (Math.random() > 0.6) { continue; } String i1 = "q" + i; String i2 = "q" + j; g.addEdge(i1, i2, edge++); } } // and, last, a partial clique for (int i = 1; i <= 20; i++) { for (int j = i + 1; j <= 20; j++) { if (Math.random() > 0.6) { continue; } String i1 = "p" + i; String i2 = "p" + j; g.addEdge(i1, i2, edge++); } } Iterator<String> nodeIt = g.nodes().iterator(); String current = nodeIt.next(); while (nodeIt.hasNext()) { String next = nodeIt.next(); g.addEdge(current, next, edge++); } return g; }
From source file:edu.uci.ics.jung.samples.DrawnIconNodeDemo.java
Network<Integer, Number> createGraph() { MutableNetwork<Integer, Number> graph = NetworkBuilder.directed().build(); graph.addEdge(0, 1, Math.random()); graph.addEdge(3, 0, Math.random()); graph.addEdge(0, 4, Math.random()); graph.addEdge(4, 5, Math.random()); graph.addEdge(5, 3, Math.random()); graph.addEdge(2, 1, Math.random()); graph.addEdge(4, 1, Math.random()); graph.addEdge(8, 2, Math.random()); graph.addEdge(3, 8, Math.random()); graph.addEdge(6, 7, Math.random()); graph.addEdge(7, 5, Math.random()); graph.addEdge(0, 9, Math.random()); graph.addEdge(9, 8, Math.random()); graph.addEdge(7, 6, Math.random()); graph.addEdge(6, 5, Math.random()); graph.addEdge(4, 2, Math.random()); graph.addEdge(5, 4, Math.random()); graph.addEdge(4, 10, Math.random()); graph.addEdge(10, 4, Math.random()); return graph; }
From source file:edu.uci.ics.jung.samples.LensNodeImageShaperDemo.java
Network<Number, Number> createGraph() { MutableNetwork<Number, Number> graph = NetworkBuilder.directed().build(); graph.addEdge(0, 1, Math.random()); graph.addEdge(3, 0, Math.random()); graph.addEdge(0, 4, Math.random()); graph.addEdge(4, 5, Math.random()); graph.addEdge(5, 3, Math.random()); graph.addEdge(2, 1, Math.random()); graph.addEdge(4, 1, Math.random()); graph.addEdge(8, 2, Math.random()); graph.addEdge(3, 8, Math.random()); graph.addEdge(6, 7, Math.random()); graph.addEdge(7, 5, Math.random()); graph.addEdge(0, 9, Math.random()); graph.addEdge(9, 8, Math.random()); graph.addEdge(7, 6, Math.random()); graph.addEdge(6, 5, Math.random()); graph.addEdge(4, 2, Math.random()); graph.addEdge(5, 4, Math.random()); graph.addEdge(4, 10, Math.random()); graph.addEdge(10, 4, Math.random()); return graph; }
From source file:dagger.internal.codegen.IncorrectlyInstalledBindsMethodsValidator.java
private ImmutableNetwork<Node, Edge> dependencyGraph(BindingGraph graph) { MutableNetwork<Node, Edge> dependencyGraph = NetworkBuilder.from(graph).build(); for (DependencyEdge dependencyEdge : graph.dependencyEdges()) { EndpointPair<Node> endpoint = graph.incidentNodes(dependencyEdge); dependencyGraph.addEdge(endpoint.source(), endpoint.target(), dependencyEdge); }/*from www . j a v a2 s . c om*/ return ImmutableNetwork.copyOf(dependencyGraph); }
From source file:org.apache.beam.runners.dataflow.worker.graph.Networks.java
/** * Applies the {@code function} to all nodes within the {@code network}. Replaces any node which * is not {@link #equals(Object)} to the original node, maintaining all existing edges between * nodes.//from w w w. j av a2 s . com */ public static <N, E> void replaceDirectedNetworkNodes(MutableNetwork<N, E> network, Function<N, N> function) { checkArgument(network.isDirected(), "Only directed networks are supported, given %s", network); checkArgument(!network.allowsSelfLoops(), "Only networks without self loops are supported, given %s", network); // A map from the existing node to the replacement node Map<N, N> oldNodesToNewNodes = new HashMap<>(network.nodes().size()); for (N currentNode : network.nodes()) { N newNode = function.apply(currentNode); // Skip updating the network if the old node is equivalent to the new node if (!currentNode.equals(newNode)) { oldNodesToNewNodes.put(currentNode, newNode); } } // For each replacement, connect up the existing predecessors and successors to the new node // and then remove the old node. for (Map.Entry<N, N> entry : oldNodesToNewNodes.entrySet()) { N oldNode = entry.getKey(); N newNode = entry.getValue(); network.addNode(newNode); for (N predecessor : ImmutableSet.copyOf(network.predecessors(oldNode))) { for (E edge : ImmutableSet.copyOf(network.edgesConnecting(predecessor, oldNode))) { network.removeEdge(edge); network.addEdge(predecessor, newNode, edge); } } for (N successor : ImmutableSet.copyOf(network.successors(oldNode))) { for (E edge : ImmutableSet.copyOf(network.edgesConnecting(oldNode, successor))) { network.removeEdge(edge); network.addEdge(newNode, successor, edge); } } network.removeNode(oldNode); } }
From source file:dagger.internal.codegen.BindingCycleValidation.java
/** Returns the subgraph containing only {@link DependencyEdge}s that would not break a cycle. */ private ImmutableNetwork<Node, DependencyEdge> nonCycleBreakingDependencyGraph(BindingGraph bindingGraph) { MutableNetwork<Node, DependencyEdge> dependencyNetwork = NetworkBuilder.from(bindingGraph) .expectedNodeCount(bindingGraph.nodes().size()) .expectedEdgeCount(bindingGraph.dependencyEdges().size()).build(); bindingGraph.dependencyEdges().stream().filter(edge -> !breaksCycle(edge, bindingGraph)).forEach(edge -> { EndpointPair<Node> endpoints = bindingGraph.incidentNodes(edge); dependencyNetwork.addEdge(endpoints.source(), endpoints.target(), edge); });//from w w w. j a v a 2s.c o m return ImmutableNetwork.copyOf(dependencyNetwork); }