/// <summary> /// Add a new vertex from source to target /// /// Complexity: 2 search + 1 insertion /// </summary> /// <param name="source">Source vertex</param> /// <param name="target">Target vertex</param> /// <returns>Created Edge</returns> /// <exception cref="ArgumentNullException">source or target is null</exception> /// <exception cref="Exception">source or target are not part of the graph</exception> public virtual IEdge AddEdge( IVertex source, IVertex target ) { // look for the vertex in the list if (!VertexOutEdges.ContainsKey(source)) { throw new VertexNotFoundException("Could not find source vertex"); } if (!VertexOutEdges.ContainsKey(target)) { throw new VertexNotFoundException("Could not find target vertex"); } // if parralel edges are not allowed check if already in the graph if (!this.AllowParallelEdges) { if (ContainsEdge(source, target)) { return(null); } } // create edge IEdge e = Provider.ProvideEdge(source, target); VertexOutEdges[source].Add(e); m_Edges.Add(e); return(e); }
/// <summary> /// Used for serialization. Not for private use. /// </summary> /// <param name="e">edge to add.</param> public virtual void AddEdge(IEdge e) { if (e == null) { throw new ArgumentNullException("vertex"); } if (!VertexOutEdges.ContainsKey(e.Source)) { throw new VertexNotFoundException("Could not find source vertex"); } if (!VertexOutEdges.ContainsKey(e.Target)) { throw new VertexNotFoundException("Could not find target vertex"); } // if parralel edges are not allowed check if already in the graph if (!this.AllowParallelEdges) { if (ContainsEdge(e.Source, e.Target)) { throw new ArgumentException("graph does not allow duplicate edges"); } } // create edge EdgeProvider.UpdateEdge(e); VertexOutEdges[e.Source].Add(e); }
/// <summary> /// Add a new vertex to the graph and returns it. /// /// Complexity: 1 insertion. /// </summary> /// <returns>Create vertex</returns> public virtual IVertex AddVertex() { IVertex v = VertexProvider.ProvideVertex(); VertexOutEdges.Add(v, new EdgeCollection()); return(v); }
/// <summary> /// Add a new vertex to the graph and returns it. /// /// Complexity: 1 insertion. /// </summary> /// <returns>Create vertex</returns> public virtual void AddVertex(IVertex v) { if (v == null) { throw new ArgumentNullException("vertex"); } if (VertexOutEdges.Contains(v)) { throw new ArgumentException("vertex already in graph"); } VertexProvider.UpdateVertex(v); VertexOutEdges.Add(v, new EdgeCollection()); }
/// <summary> /// Removes the vertex from the graph. /// </summary> /// <param name="v">vertex to remove</param> /// <exception cref="ArgumentNullException">v is null</exception> public virtual void RemoveVertex(IVertex v) { if (v == null) { throw new ArgumentNullException("vertex"); } if (!ContainsVertex(v)) { throw new VertexNotFoundException("v"); } ClearVertex(v); // removing vertex VertexOutEdges.Remove(v); }
/// <summary> /// Tests if a vertex is part of the graph /// </summary> /// <param name="v">Vertex to test</param> /// <returns>true if is part of the graph, false otherwize</returns> public bool ContainsVertex(IVertex v) { return(VertexOutEdges.Contains(v)); }
/// <summary> /// Remove all of the edges and vertices from the graph. /// </summary> public virtual void Clear() { VertexOutEdges.Clear(); m_Edges.Clear(); }