/** * 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)); }
/** * 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)); } }
/** * 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); }
/** * Gets the vertex opposite another vertex across an edge. * * @param g graph containing e and v * @param e edge in g * @param v vertex in g * @param <V> the graph vertex type * @param <E> the graph edge type * * @return vertex opposite to v across e */ public static V getOppositeVertex <V, E>(Graph <V, E> g, E e, V v) { V source = g.getEdgeSource(e); V target = g.getEdgeTarget(e); if (v.Equals(source)) { return(target); } else if (v.Equals(target)) { return(source); } else { throw new ArgumentException("no such vertex: " + v.ToString()); } }
/** * Tests whether an edge is incident to a vertex. * * @param g graph containing e and v * @param e edge in g * @param v vertex in g * @param <V> the graph vertex type * @param <E> the graph edge type * * @return true iff e is incident on v */ public static bool testIncidence <V, E>(Graph <V, E> g, E e, V v) { return((g.getEdgeSource(e).Equals(v)) || (g.getEdgeTarget(e).Equals(v))); }