/// <inheritdoc />
        public bool RemoveVertex(TVertex vertex)
        {
            ClearAdjacentEdges(vertex);

            bool removed = _adjacentEdges.Remove(vertex);

            if (removed)
            {
                OnVertexRemoved(vertex);
            }

            return(removed);
        }
        /// <inheritdoc />
        public virtual bool RemoveVertex(TVertex vertex)
        {
            if (!ContainsVertex(vertex))
            {
                return(false);
            }

            // Remove out edges
            IEdgeList <TVertex, TEdge> edgesToRemove = _vertexEdges[vertex];

            _vertexEdges.Remove(vertex);
            EdgeCount -= edgesToRemove.Count;
            Debug.Assert(EdgeCount >= 0);

            // Remove in edges (Run over edges and remove each edge touching the vertex)
            RemoveInEdges(v => EqualityComparer <TVertex> .Default.Equals(v, vertex));

            NotifyEdgesRemoved(edgesToRemove);
            OnVertexRemoved(vertex);

            return(true);
        }
        /// <inheritdoc />
        public virtual bool RemoveVertex(TVertex vertex)
        {
            if (!ContainsVertex(vertex))
            {
                return(false);
            }

            // Remove out edges
            IEdgeList <TVertex, TEdge> edges = _vertexEdges[vertex];

            if (EdgeRemoved != null) // Lazily notify
            {
                foreach (TEdge edge in edges)
                {
                    OnEdgeRemoved(edge);
                }
            }

            EdgeCount -= edges.Count;
            edges.Clear();


            // Run over edges and remove each edge touching the vertex
            foreach (var pair in _vertexEdges)
            {
                if (pair.Key.Equals(vertex))
                {
                    continue; // We've already
                }
                // Collect edges to remove
                foreach (var edge in pair.Value.Clone())
                {
                    if (edge.Target.Equals(vertex))
                    {
                        pair.Value.Remove(edge);
                        OnEdgeRemoved(edge);
                        --EdgeCount;
                    }
                }
            }

            Debug.Assert(EdgeCount >= 0);

            _vertexEdges.Remove(vertex);
            OnVertexRemoved(vertex);

            return(true);
        }
        /// <inheritdoc />
        public virtual bool RemoveVertex(TVertex vertex)
        {
            if (!ContainsVertex(vertex))
            {
                return(false);
            }

            // Collect edges to remove
            var edgesToRemove = new EdgeList <TVertex, TEdge>();

            foreach (TEdge outEdge in OutEdges(vertex))
            {
                _vertexInEdges[outEdge.Target].Remove(outEdge);
                edgesToRemove.Add(outEdge);
            }

            foreach (TEdge inEdge in InEdges(vertex))
            {
                // Might already have been removed
                if (_vertexOutEdges[inEdge.Source].Remove(inEdge))
                {
                    edgesToRemove.Add(inEdge);
                }
            }

            // Notify users
            if (EdgeRemoved != null)
            {
                foreach (TEdge edge in edgesToRemove)
                {
                    OnEdgeRemoved(edge);
                }
            }

            _vertexOutEdges.Remove(vertex);
            _vertexInEdges.Remove(vertex);
            EdgeCount -= edgesToRemove.Count;
            OnVertexRemoved(vertex);

            return(true);
        }
        private IEnumerable<TEdge> RemoveInOutEdges([NotNull] TVertex vertex)
        {
            IEdgeList<TVertex, TEdge> outEdges = _vertexOutEdges[vertex];
            _vertexOutEdges.Remove(vertex);
            foreach (TEdge outEdge in outEdges)
            {
                _vertexInEdges[outEdge.Target].Remove(outEdge);
            }

            IEdgeList<TVertex, TEdge> inEdges = _vertexInEdges[vertex];
            _vertexInEdges.Remove(vertex);
            foreach (TEdge inEdge in inEdges)
            {
                _vertexOutEdges[inEdge.Source].Remove(inEdge);
            }

            EdgeCount -= outEdges.Count + inEdges.Count;
            Debug.Assert(EdgeCount >= 0);

            return outEdges.Concat(inEdges);
        }
        public virtual bool RemoveVertex(TVertex v)
        {
            if (!ContainsVertex(v))
            {
                return(false);
            }

            // collect edges to remove
            var edgesToRemove = new EdgeList <TVertex, TEdge>();

            foreach (var outEdge in OutEdges(v))
            {
                vertexInEdges[outEdge.Target].Remove(outEdge);
                edgesToRemove.Add(outEdge);
            }
            foreach (var inEdge in InEdges(v))
            {
                if (vertexOutEdges[inEdge.Source].Remove(inEdge))
                {
                    edgesToRemove.Add(inEdge);
                }
            }

            // notify users
            if (EdgeRemoved != null)
            {
                foreach (var edge in edgesToRemove)
                {
                    OnEdgeRemoved(edge);
                }
            }

            vertexOutEdges.Remove(v);
            vertexInEdges.Remove(v);
            EdgeCount -= edgesToRemove.Count;
            OnVertexRemoved(v);

            return(true);
        }