Refine search
/** * Return a Collection of successor nodes of a certain vertex. * * @param vertex the step or node its successors nodes will be returned. * @return collection of successor DAGNodes for each node. */ protected Collection<DAGNode<? extends Job>> getNodeSuccessors(Object vertex) { Collection<DAGNode<? extends Job>> nodeSuccessors = Sets.newHashSet(); List successorNodes = Graphs.successorListOf(jobsGraph, vertex); for (Object node : successorNodes) { BaseFlowStep step = (BaseFlowStep) node; String name = step.getName(); nodeSuccessors.add(dagNamesMap.get(name)); } return nodeSuccessors; } }
Set<V> negImbalancedVertices = new HashSet<>(); Set<V> postImbalancedVertices = new HashSet<>(); for (V v : graph.vertexSet()) { int imbalance = graph.outDegreeOf(v) - graph.inDegreeOf(v); V u = duplicateMap.get(i); V v = duplicateMap.get(j); Graphs.addEdge(auxGraph, i, j, shortestPaths.get(new Pair<>(u, v)).getWeight()); graph.getVertexSupplier(), graph.getEdgeSupplier(), graph.getType().isWeighted()); Graphs.addGraph(eulerGraph, graph); Map<E, GraphPath<V, E>> shortcutEdges = new HashMap<>(); for (DefaultWeightedEdge e : matching.getEdges()) {
/** * Create a copy of a graph for internal use. * * @param graph the graph to copy. * * @return A copy of the graph projected to a SimpleGraph. */ private static <V, E> Graph<V, E> copyAsSimpleGraph(Graph<V, E> graph) { Graph<V, E> copy = GraphTypeBuilder .<V, E> undirected().edgeSupplier(graph.getEdgeSupplier()) .vertexSupplier(graph.getVertexSupplier()).allowingMultipleEdges(false) .allowingSelfLoops(false).buildGraph(); if (graph.getType().isSimple()) { Graphs.addGraph(copy, graph); } else { // project graph to SimpleGraph Graphs.addAllVertices(copy, graph.vertexSet()); for (E e : graph.edgeSet()) { V v1 = graph.getEdgeSource(e); V v2 = graph.getEdgeTarget(e); if (!v1.equals(v2) && !copy.containsEdge(e)) { copy.addEdge(v1, v2); } } } return copy; }
/** * Adds all the vertices and all the edges of the specified source digraph to the specified * destination digraph, reversing all of the edges. If you want to do this as a linked view of * the source graph (rather than by copying to a destination graph), use * {@link EdgeReversedGraph} instead. * * <p> * The behavior of this operation is undefined if any of the specified graphs is modified while * operation is in progress. * * @param destination the graph to which vertices and edges are added * @param source the graph used as source for vertices and edges to add * @param <V> the graph vertex type * @param <E> the graph edge type * * @see EdgeReversedGraph */ public static <V, E> void addGraphReversed(Graph<? super V, ? super E> destination, Graph<V, E> source) { if (!source.getType().isDirected() || !destination.getType().isDirected()) { throw new IllegalArgumentException("graph must be directed"); } addAllVertices(destination, source.vertexSet()); for (E edge : source.edgeSet()) { destination.addEdge(source.getEdgeTarget(edge), source.getEdgeSource(edge)); } }
graph.vertexSet().stream().filter(v -> graph.degreeOf(v) % 2 == 1).collect( Collectors.toList()); Graphs.addAllVertices(auxGraph, oddDegreeVertices); if (u == v) continue; Graphs.addEdge( auxGraph, u, v, shortestPaths.get(new UnorderedPair<>(u, v)).getWeight()); graph.getVertexSupplier(), graph.getEdgeSupplier(), graph.getType().isWeighted()); Graphs.addGraph(eulerGraph, graph); Map<E, GraphPath<V, E>> shortcutEdges = new HashMap<>(); for (DefaultWeightedEdge e : matching.getEdges()) {
final Graph<E, EE> target, final BiFunction<E, E, Double> weightFunction) Graphs.addAllVertices(target, graph.edgeSet()); if (graph.getType().isDirected()) { for (V vertex : graph.vertexSet()) { for (E e1 : graph.incomingEdgesOf(vertex)) { for (E e2 : graph.outgoingEdgesOf(vertex)) {
/** * Adds all the vertices and all the edges of the specified source graph to the specified * destination graph. First all vertices of the source graph are added to the destination graph. * Then every edge of the source graph is added to the destination graph. This method returns * <code>true</code> if the destination graph has been modified as a result of this operation, * otherwise it returns <code>false</code>. * * <p> * The behavior of this operation is undefined if any of the specified graphs is modified while * operation is in progress. * </p> * * @param destination the graph to which vertices and edges are added * @param source the graph used as source for vertices and edges to add * @param <V> the graph vertex type * @param <E> the graph edge type * * @return <code>true</code> if and only if the destination graph has been changed as a result * of this operation. */ public static <V, E> boolean addGraph(Graph<? super V, ? super E> destination, Graph<V, E> source) { boolean modified = addAllVertices(destination, source.vertexSet()); modified |= addAllEdges(destination, source, source.edgeSet()); return modified; }
@Override public void generateGraph(Graph<V, E> target, Map<String, V> resultMap) Graphs.addAllVertices(target, graph.vertexSet()); if (graph.getType().isDirected()) { for (V u : graph.vertexSet()) for (V v : graph.vertexSet()) if (u == v)
/** * Removes the given vertex from the given graph. If the vertex to be removed has one or more * predecessors, the predecessors will be connected directly to the successors of the vertex to * be removed. * * @param graph graph to be mutated * @param vertex vertex to be removed from this graph, if present * @param <V> the graph vertex type * @param <E> the graph edge type * * @return true if the graph contained the specified vertex; false otherwise. */ public static <V, E> boolean removeVertexAndPreserveConnectivity(Graph<V, E> graph, V vertex) { if (!graph.containsVertex(vertex)) { return false; } if (vertexHasPredecessors(graph, vertex)) { List<V> predecessors = Graphs.predecessorListOf(graph, vertex); List<V> successors = Graphs.successorListOf(graph, vertex); for (V predecessor : predecessors) { addOutgoingEdges(graph, predecessor, successors); } } graph.removeVertex(vertex); return true; }
private void exportAdjacencyMatrix(Graph<V, E> g, Writer writer) { for (V from : g.vertexSet()) { // assign ids in vertex set iteration order vertexIDProvider.getName(from); } PrintWriter out = new PrintWriter(writer); if (g.getType().isDirected()) { for (V from : g.vertexSet()) { exportAdjacencyMatrixVertex(out, from, Graphs.successorListOf(g, from)); } } else { for (V from : g.vertexSet()) { exportAdjacencyMatrixVertex(out, from, Graphs.neighborListOf(g, from)); } } out.flush(); }
private void exportAsAdjacencyList(Graph<V, E> g, PrintWriter out) { boolean exportEdgeWeights = parameters.contains(CSVFormat.Parameter.EDGE_WEIGHTS); for (V v : g.vertexSet()) { exportEscapedField(out, vertexIDProvider.getName(v)); for (E e : g.outgoingEdgesOf(v)) { V w = Graphs.getOppositeVertex(g, e, v); out.print(delimiter); exportEscapedField(out, vertexIDProvider.getName(w)); if (exportEdgeWeights) { out.print(delimiter); exportEscapedField(out, String.valueOf(g.getEdgeWeight(e))); } } out.println(); } }
V v = iterator.next(); if (generators.contains(v)) { Set<V> separator = new HashSet<>(Graphs.neighborListOf(hprime, v)); tmpGraph.removeAllVertices(separator); ConnectivityInspector<V, E> con = new ConnectivityInspector<>(tmpGraph); if (con.isConnected()) { gprime.removeAllVertices(component); component.addAll(separator); atoms.add(new HashSet<>(component)); hprime.removeVertex(v);
/** * Increments the cardinalities of the neighbours of the {@code vertex} by 1. If the maximum * cardinality increases, increments {@code maxCardinality} by 1. * * @param vertex the vertex whose neighbours are to be updated. */ private void updateNeighbours(V vertex) { Set<V> processed = new HashSet<>(); for (E edge : graph.edgesOf(vertex)) { V opposite = Graphs.getOppositeVertex(graph, edge, vertex); if (cardinalityMap.containsKey(opposite) && !processed.contains(opposite)) { processed.add(opposite); addToBucket(opposite, removeFromBucket(opposite) + 1); } } if (maxCardinality < graph.vertexSet().size() && maxCardinality >= 0 && buckets.get(maxCardinality + 1) != null) { ++maxCardinality; } } }
/** * Returns a set of vertices that are neighbors of a specified vertex. * * @param g the graph to look for neighbors in * @param vertex the vertex to get the neighbors of * @param <V> the graph vertex type * @param <E> the graph edge type * @return a set of the vertices that are neighbors of the specified vertex */ public static <V, E> Set<V> neighborSetOf(Graph<V, E> g, V vertex) { Set<V> neighbors = new LinkedHashSet<>(); for (E e : g.edgesOf(vertex)) { neighbors.add(Graphs.getOppositeVertex(g, e, vertex)); } return neighbors; }
private void exportNormalizedLaplacianMatrix(Graph<V, E> g, Writer writer) { PrintWriter out = new PrintWriter(writer); ComponentNameProvider<V> nameProvider = new IntegerComponentNameProvider<>(); for (V from : g.vertexSet()) { // assign ids in vertex set iteration order nameProvider.getName(from); } for (V from : g.vertexSet()) { String fromName = nameProvider.getName(from); Set<V> neighbors = new LinkedHashSet<>(Graphs.neighborListOf(g, from)); if (neighbors.isEmpty()) { exportEntry(out, fromName, fromName, "0"); } else { exportEntry(out, fromName, fromName, "1"); for (V to : neighbors) { String toName = nameProvider.getName(to); double value = -1 / Math.sqrt(g.degreeOf(from) * g.degreeOf(to)); exportEntry(out, fromName, toName, Double.toString(value)); } } } out.flush(); }
private void exportLaplacianMatrix(Graph<V, E> g, Writer writer) { PrintWriter out = new PrintWriter(writer); ComponentNameProvider<V> nameProvider = new IntegerComponentNameProvider<>(); for (V from : g.vertexSet()) { // assign ids in vertex set iteration order nameProvider.getName(from); } for (V from : g.vertexSet()) { String fromName = nameProvider.getName(from); List<V> neighbors = Graphs.neighborListOf(g, from); exportEntry(out, fromName, fromName, Integer.toString(neighbors.size())); for (V to : neighbors) { String toName = nameProvider.getName(to); exportEntry(out, fromName, toName, "-1"); } } out.flush(); }
@Override public List<FlowElement> predecessorListOf( FlowElement flowElement ) { return Graphs.predecessorListOf( graph, flowElement ); }
/** * Constructs a Block-Cutpoint graph * * @param graph the input graph */ public BlockCutpointGraph(Graph<V, E> graph) { super(DefaultEdge.class); this.graph = graph; BiconnectivityInspector<V, E> biconnectivityInspector = new BiconnectivityInspector<>(graph); // Construct the Block-cut point graph cutpoints = biconnectivityInspector.getCutpoints(); blocks = biconnectivityInspector.getBlocks(); for (Graph<V, E> block : blocks) for (V v : block.vertexSet()) vertex2block.put(v, block); Graphs.addAllVertices(this, blocks); for (V cutpoint : this.cutpoints) { Graph<V, E> subgraph = new AsSubgraph<>(graph, Collections.singleton(cutpoint)); this.vertex2block.put(cutpoint, subgraph); this.addVertex(subgraph); for (Graph<V, E> block : biconnectivityInspector.getBlocks(cutpoint)) addEdge(subgraph, block); } }
/** * Returns the successor list of a node for directed graphs or the neighbor * list of a node for undirected graphs. Used in BFS, DFS, Strahler. * * @param node The node. * @return The outgoing edges of the node. */ public List<V> successorListOf(V node) { if (graph instanceof DirectedGraph) { return Graphs.successorListOf((DirectedGraph) graph, node); } else { return Graphs.neighborListOf(graph, node); } }
/** * Returns a list of vertices that are the direct successors of a specified vertex. If the graph * is a multigraph vertices may appear more than once in the returned list. * * <p> * The method uses {@link Graph#outgoingEdgesOf(Object)} to traverse the graph. * * @param g the graph to look for successors in * @param vertex the vertex to get the successors of * @param <V> the graph vertex type * @param <E> the graph edge type * * @return a list of the vertices that are the direct successors of the specified vertex. */ public static <V, E> List<V> successorListOf(Graph<V, E> g, V vertex) { List<V> successors = new ArrayList<>(); Set<? extends E> edges = g.outgoingEdgesOf(vertex); for (E e : edges) { successors.add(getOppositeVertex(g, e, vertex)); } return successors; }