/** * Adds the specified source and target vertices to the graph, if not already included, and * creates a new edge and adds it to the specified graph similarly to the * {@link Graph#addEdge(Object, Object)} method. * * @param g the graph for which the specified edge to be added * @param sourceVertex source vertex of the edge * @param targetVertex target vertex of the edge * @param <V> the graph vertex type * @param <E> the graph edge type * * @return The newly created edge if added to the graph, otherwise <code> * null</code>. */ public static E addEdgeWithVertices <V, E>(Graph <V, E> g, V sourceVertex, V targetVertex) { g.addVertex(sourceVertex); g.addVertex(targetVertex); return(g.addEdge(sourceVertex, targetVertex)); }
/** * Adds the specified edge to the graph, including its vertices if not already included. * * @param targetGraph the graph for which the specified edge to be added * @param sourceGraph the graph in which the specified edge is already present * @param edge edge to add * @param <V> the graph vertex type * @param <E> the graph edge type * * @return <tt>true</tt> if the target graph did not already contain the specified edge. */ public static bool addEdgeWithVertices <V, E>(Graph <V, E> targetGraph, Graph <V, E> sourceGraph, E edge) { V sourceVertex = sourceGraph.getEdgeSource(edge); V targetVertex = sourceGraph.getEdgeTarget(edge); targetGraph.addVertex(sourceVertex); targetGraph.addVertex(targetVertex); return(targetGraph.addEdge(sourceVertex, targetVertex, edge)); }
/** * Creates a new edge and adds it to the specified graph similarly to the * {@link Graph#addEdge(Object, Object)} method. * * @param g the graph for which the edge to be added * @param sourceVertex source vertex of the edge * @param targetVertex target vertex of the edge * @param weight weight of the edge * @param <V> the graph vertex type * @param <E> the graph edge type * * @return The newly created edge if added to the graph, otherwise <code> * null</code>. * * @see Graph#addEdge(Object, Object) */ public static E addEdge <V, E>(Graph <V, E> g, V sourceVertex, V targetVertex, double weight) { EdgeFactory <V, E> ef = g.getEdgeFactory(); E e = ef.createEdge(sourceVertex, targetVertex); // we first create the edge and set the weight to make sure that // listeners will see the correct weight upon addEdge. g.setEdgeWeight(e, weight); return(g.addEdge(sourceVertex, targetVertex, e) ? e : default(E)); }
/** * 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 void addGraphReversed <V, E>(Graph <V, E> destination, Graph <V, E> source) { if (!source.getType().isDirected() || !destination.getType().isDirected()) { throw new ArgumentException("graph must be directed"); } addAllVertices(destination, source.vertexSet()); foreach (E edge in source.edgeSet()) { destination.addEdge(source.getEdgeTarget(edge), source.getEdgeSource(edge)); } }
/** * 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); } }
/** * 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); } }
/** * Adds a subset of the edges of the specified source graph to the specified destination graph. * The behavior of this operation is undefined if either of the graphs is modified while the * operation is in progress. {@link #addEdgeWithVertices} is used for the transfer, so source * vertexes will be added automatically to the target graph. * * @param destination the graph to which edges are to be added * @param source the graph used as a source for edges to add * @param edges the edges to be added * @param <V> the graph vertex type * @param <E> the graph edge type * * @return <tt>true</tt> if this graph changed as a result of the call */ public static bool addAllEdges <V, E>(Graph <V, E> destination, Graph <V, E> source, ICollection <E> edges) { bool modified = false; foreach (E e in edges) { V s = source.getEdgeSource(e); V t = source.getEdgeTarget(e); destination.addVertex(s); destination.addVertex(t); modified |= destination.addEdge(s, t, e); } return(modified); }