/** * Add edges from one source vertex to multiple target vertices. Whether duplicates are created * depends on the underlying {@link DirectedGraph} implementation. * * @param graph graph to be mutated * @param source source vertex of the new edges * @param targets target vertices for the new edges * @param <V> the graph vertex type * @param <E> the graph edge type */ public static void addOutgoingEdges <V, E>(Graph <V, E> graph, V source, ICollection <V> targets) { if (!graph.containsVertex(source)) { graph.addVertex(source); } foreach (V target in targets) { if (!graph.containsVertex(target)) { graph.addVertex(target); } graph.addEdge(source, target); } }
/** * Add edges from multiple source vertices to one target vertex. Whether duplicates are created * depends on the underlying {@link Graph} implementation. * * @param graph graph to be mutated * @param target target vertex for the new edges * @param sources source vertices for the new edges * @param <V> the graph vertex type * @param <E> the graph edge type */ public static void addIncomingEdges <V, E>(Graph <V, E> graph, V target, ICollection <V> sources) { if (!graph.containsVertex(target)) { graph.addVertex(target); } foreach (V source in sources) { if (!graph.containsVertex(source)) { graph.addVertex(source); } graph.addEdge(source, target); } }
/** * 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 bool removeVertexAndPreserveConnectivity <V, E>(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); foreach (V predecessor in predecessors) { addOutgoingEdges(graph, predecessor, successors); } } graph.removeVertex(vertex); return(true); }