Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <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);
        }