/// <summary>
        /// Add a new vertex to the graph and returns it.
        /// </summary>
        /// <returns>Create vertex</returns>
        internal virtual void AddVertex(Vertex v)
        {
            if (v==null)
                throw new ArgumentNullException("vertex");
            if (this.vertexOutEdges.Contains(v))
                throw new ArgumentException("vertex already in graph");

            this.version++;
            this.VertexProvider.UpdateVertex(v);
            this.vertexOutEdges.Add(v);
            this.vertexInEdges.Add(v);
        }
Example #2
0
 /// <summary>
 /// Builds an edge from source to target
 /// </summary>
 /// <param name="source">Source vertex</param>
 /// <param name="target">Target vertex</param>
 /// <exception cref="ArgumentNullException">Source or Target is null</exception>
 public Edge(Vertex source, Vertex target)
 {
     if (source == null)
         throw new ArgumentNullException("Source");
     if (target == null)
         throw new ArgumentNullException("Target");
     m_Source = source;
     m_Target = target;
 }
        /// <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(Vertex v)
        {
            if (v == null)
                throw new ArgumentNullException("v");
            if (!ContainsVertex(v))
                throw new VertexNotFoundException("v");

            this.version++;
            this.ClearVertex(v);

            // removing vertex
            this.vertexOutEdges.Remove(v);
            this.vertexInEdges.Remove(v);
        }
        /// <summary>
        /// Remove the edge (u,v) from the graph. 
        /// If the graph allows parallel edges this remove all occurrences of 
        /// (u,v).
        /// </summary>
        /// <param name="u">source vertex</param>
        /// <param name="v">target vertex</param>
        public virtual void RemoveEdge(Vertex u, Vertex v)
        {
            if (u == null)
                throw new ArgumentNullException("u");
            if (v == null)
                throw new ArgumentNullException("v");

            this.version++;
            // getting out-edges
            EdgeCollection outEdges = this.vertexOutEdges[u];

            // marking edges to remove
            EdgeCollection removedEdges = new EdgeCollection();
            foreach(Edge e in outEdges)
            {
                if (e.Target == v)
                    removedEdges.Add(e);
            }
            //removing out-edges
            foreach(Edge e in removedEdges)
                outEdges.Remove(e);

            removedEdges.Clear();
            EdgeCollection inEdges = this.vertexInEdges[v];
            foreach(Edge e in inEdges)
            {
                if (e.Source == u)
                    removedEdges.Add(e);
            }
            //removing in-edges
            foreach(Edge e in removedEdges)
                inEdges.Remove(e);
        }
        /// <summary>
        /// Remove all the out-edges of vertex u for which the predicate pred 
        /// returns true.
        /// </summary>
        /// <param name="u">vertex</param>
        /// <param name="pred">edge predicate</param>
        public virtual void RemoveOutEdgeIf(Vertex u, IEdgePredicate pred)
        {
            if (u==null)
                throw new ArgumentNullException("u");
            if (pred == null)
                throw new ArgumentNullException("pred");

            EdgeCollection edges = this.vertexOutEdges[u];
            EdgeCollection removedEdges = new EdgeCollection();
            foreach(Edge e in edges)
            {
                if (pred.Test(e))
                    removedEdges.Add(e);
            }

            foreach(Edge e in removedEdges)
                this.RemoveEdge(e);
        }
 public EdgeCollection this[Vertex v]
 {
     get
     {
         return (EdgeCollection)this.Dictionary[v];
     }
 }
 /// <summary>
 /// Gets a value indicating if the set of out-edges is empty
 /// </summary>
 /// <remarks>
 /// <para>
 /// Usually faster that calling <see cref="OutDegree"/>.
 /// </para>
 /// </remarks>
 /// <value>
 /// true if the out-edge set is empty, false otherwise.
 /// </value>
 /// <exception cref="ArgumentNullException">
 /// v is a null reference (Nothing in Visual Basic)
 /// </exception>
 /// <exception cref="VertexNotFoundException">
 /// v is not part of the graph.
 /// </exception>
 public bool OutEdgesEmpty(Vertex v)
 {
     if (v == null)
         throw new ArgumentNullException("v");
     EdgeCollection edges = this.vertexOutEdges[v];
     if (edges==null)
         throw new VertexNotFoundException(v.ToString());
     return edges.Count==0;
 }
Example #8
0
 /// <summary>
 /// Compares two vertices
 /// </summary>
 /// <param name="obj">vertex to compare</param>
 /// <returns></returns>
 /// <exception cref="ArgumentException">obj is not of type Vertex</exception>
 public int CompareTo(Vertex obj)
 {
     if(obj==null)
         return -1;
     return ID.CompareTo(obj.ID);
 }
 public bool Contains(Vertex key)
 {
     return this.Dictionary.Contains(key);
 }
 /// <summary>
 /// Test is an edge (u,v) is part of the graph
 /// </summary>
 /// <param name="u">source vertex</param>
 /// <param name="v">target vertex</param>
 /// <returns>true if part of the graph</returns>
 public bool ContainsEdge(Vertex u,Vertex v)
 {
     // try to find the edge
     foreach(Edge e in this.OutEdges(u))
     {
         if (e.Target == v)
             return true;
     }
     return false;
 }
 /// <summary>
 /// Tests if a <see cref="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(Vertex v)
 {
     return this.vertexOutEdges.Contains(v);
 }
 /// <summary>
 /// Gets an enumerable collection of adjacent vertices
 /// </summary>
 /// <param name="v"></param>
 /// <returns>Enumerable collection of adjacent vertices</returns>
 public IVertexEnumerable AdjacentVertices(Vertex v)
 {
     return new TargetVertexEnumerable(this.OutEdges(v));
 }
        /// <summary>
        /// Remove all edges to and from vertex u from the graph.
        /// </summary>
        /// <param name="v"></param>
        public virtual void ClearVertex(Vertex v)
        {
            if (v == null)
                throw new ArgumentNullException("vertex");

            this.version++;
            // removing edges touching v
            this.RemoveEdgeIf(new IsAdjacentEdgePredicate(v));

            // removing edges
            this.vertexOutEdges[v].Clear();
            this.vertexInEdges[v].Clear();
        }
 /// <summary>
 /// Gets a value indicating if the set of edges connected to v is empty
 /// </summary>
 /// <remarks>
 /// <para>
 /// Usually faster that calling <see cref="Degree"/>.
 /// </para>
 /// </remarks>
 /// <value>
 /// true if the adjacent edge set is empty, false otherwise.
 /// </value>
 /// <exception cref="ArgumentNullException">v is a null reference</exception>
 public bool AdjacentEdgesEmpty(Vertex v)
 {
     if (v==null)
         throw new ArgumentNullException("v");
     return this.OutEdgesEmpty(v) && this.InEdgesEmpty(v);
 }
        /// <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 a null reference
        /// </exception>
        /// <exception cref="Exception">source or target are not part of the graph</exception>
        public virtual Edge AddEdge(
            Vertex source,
            Vertex target
            )
        {
            // look for the vertex in the list
            if (!this.vertexOutEdges.Contains(source))
                throw new VertexNotFoundException("Could not find source vertex");
            if (!this.vertexOutEdges.Contains(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))
                    throw new Exception("Parallel edge not allowed");
            }

            this.version++;
            // create edge
            Edge e = EdgeProvider.ProvideEdge(source,target);
            this.vertexOutEdges[source].Add(e);
            this.vertexInEdges[target].Add(e);

            return e;
        }
 public void Remove(Vertex key)
 {
     this.Dictionary.Remove(key);
 }
        /// <summary>
        /// Returns the collection of out-edges that matches the predicate
        /// </summary>
        /// <param name="v"></param>
        /// <param name="ep">Edge predicate</param>
        /// <returns>enumerable colleciton of vertices that matches the 
        /// criteron</returns>
        /// <exception cref="ArgumentNullException">v or ep is null</exception>
        public IEdgeEnumerable SelectOutEdges(Vertex v, IEdgePredicate ep)
        {
            if (v==null)
                throw new ArgumentNullException("v");
            if (ep==null)
                throw new ArgumentNullException("ep");

            return new FilteredEdgeEnumerable(this.OutEdges(v),ep);
        }
 public void ConstructorWithSourceNotPartOfTheGraph()
 {
     g = GraphFactory.UnBalancedFlow();
     Vertex v = new Vertex();
     new GraphBalancerAlgorithm(g, v, Traversal.FirstVertex(g));
 }
        /// <summary>
        /// Returns the first out-edge that matches the predicate
        /// </summary>
        /// <param name="v"></param>
        /// <param name="ep">Edge predicate</param>
        /// <returns>null if not found, otherwize the first Edge that
        /// matches the predicate.</returns>
        /// <exception cref="ArgumentNullException">v or ep is null</exception>
        public Edge SelectSingleOutEdge(Vertex v, IEdgePredicate ep)
        {
            if (ep==null)
                throw new ArgumentNullException("ep");

            foreach(Edge e in this.SelectOutEdges(v,ep))
                return e;

            return null;
        }
 public void ConstructorWithNullSink()
 {
     g = GraphFactory.UnBalancedFlow();
     Vertex v = new Vertex();
     new GraphBalancerAlgorithm(g, Traversal.FirstVertex(g),null);
 }
 /// <summary>
 /// Returns the number of in-edges plus out-edges.
 /// </summary>
 /// <param name="v"></param>
 /// <returns></returns>
 public int Degree(Vertex v)
 {
     if (v == null)
         throw new ArgumentNullException("v");
     EdgeCollection outEdges = this.vertexOutEdges[v];
     if (outEdges==null)
         throw new VertexNotFoundException("v");
     EdgeCollection inEdges = this.vertexInEdges[v];
     Debug.Assert(inEdges!=null);
     return outEdges.Count + inEdges.Count;
 }
 public void Add(Vertex u)
 {
     Debug.Assert(u!=null);
     this.Dictionary.Add(u, new EdgeCollection() );
 }
 public void ConstructorWithNullGraph()
 {
     Vertex v = new Vertex();
     new GraphBalancerAlgorithm(null, v, v);
 }
 /// <summary>
 /// Returns the number of in-degree edges of v
 /// </summary>
 /// <param name="v"></param>
 /// <returns>number of in-edges of the vertex v</returns>
 /// <exception cref="ArgumentNullException">
 /// v is a null reference (Nothing in Visual Basic)
 /// </exception>
 /// <exception cref="VertexNotFoundException">
 /// <paramref name="v"/> is not part of the graph.
 /// </exception>		
 public int InDegree(Vertex v)
 {
     if (v == null)
         throw new ArgumentNullException("v");
     EdgeCollection edges = this.vertexInEdges[v];
     if (edges==null)
         throw new VertexNotFoundException("v");
     return edges.Count;
 }
 public void ConstructorWithNullSource()
 {
     Vertex v = new Vertex();
     new GraphBalancerAlgorithm(GraphFactory.EmptyParallelEdgesAllowed(), null,v);
 }
 /// <summary>
 /// Returns an iterable collection over the in-edge connected to v
 /// </summary>
 /// <param name="v"></param>
 /// <returns>in-edges of v</returns>
 /// <exception cref="ArgumentNullException">
 /// v is a null reference (Nothing in Visual Basic)
 /// </exception>
 /// <exception cref="VertexNotFoundException">
 /// <paramref name="v"/> is not part of the graph.
 /// </exception>		
 public IEdgeCollection InEdges(Vertex v)
 {
     if (v == null)
         throw new ArgumentNullException("v");
     EdgeCollection edges = this.vertexInEdges[v];
     if (edges==null)
         throw new VertexNotFoundException(v.ToString());
     return edges;
 }