Exemplo n.º 1
0
        public int RemoveOutEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate)
        {
            GraphContracts.AssumeInVertexSet(this, v, "v");
            GraphContracts.AssumeNotNull(predicate, "predicate");

            var edges        = this.vertexEdges[v];
            var edgeToRemove = new EdgeList(edges.Count);

            foreach (var edge in edges)
            {
                if (predicate(edge))
                {
                    edgeToRemove.Add(edge);
                }
            }

            foreach (var edge in edgeToRemove)
            {
                edges.Remove(edge);
                this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge));
            }
            this.edgeCount -= edgeToRemove.Count;
            GraphContracts.Assert(this.edgeCount >= 0);

            return(edgeToRemove.Count);
        }
Exemplo n.º 2
0
        public void MergeVertex(TVertex v, IEdgeFactory <TVertex, TEdge> edgeFactory)
        {
            GraphContracts.AssumeInVertexSet(this, v, "v");
            GraphContracts.AssumeNotNull(edgeFactory, "edgeFactory");

            // storing edges in local array
            EdgeList inedges  = this.vertexInEdges[v];
            EdgeList outedges = this.vertexOutEdges[v];

            // remove vertex
            this.RemoveVertex(v);

            // add edges from each source to each target
            foreach (var source in inedges)
            {
                //is it a self edge
                if (source.Source.Equals(v))
                {
                    continue;
                }
                foreach (var target in outedges)
                {
                    if (v.Equals(target.Target))
                    {
                        continue;
                    }
                    // we add an new edge
                    this.AddEdge(edgeFactory.CreateEdge(source.Source, target.Target));
                }
            }
        }
Exemplo n.º 3
0
 public virtual void AddVertexRange(IEnumerable <TVertex> vertices)
 {
     GraphContracts.AssumeNotNull(vertices, "vertices");
     foreach (var v in vertices)
     {
         this.AddVertex(v);
     }
 }
Exemplo n.º 4
0
        public virtual bool RemoveVertex(TVertex v)
        {
            GraphContracts.AssumeNotNull(v, "v");
            if (!this.ContainsVertex(v))
            {
                return(false);
            }
            // remove outedges
            {
                EdgeList edges = this.vertexEdges[v];
                if (this.EdgeRemoved != null) // lazily notify
                {
                    foreach (var edge in edges)
                    {
                        this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge));
                    }
                }
                this.edgeCount -= edges.Count;
                edges.Clear();
            }

            // iterage over edges and remove each edge touching the vertex
            EdgeList edgeToRemove = new EdgeList();

            foreach (var kv in this.vertexEdges)
            {
                if (kv.Key.Equals(v))
                {
                    continue;                   // we've already
                }
                // collect edge to remove
                foreach (var edge in kv.Value)
                {
                    if (edge.Target.Equals(v))
                    {
                        edgeToRemove.Add(edge);
                    }
                }

                // remove edges
                foreach (var edge in edgeToRemove)
                {
                    kv.Value.Remove(edge);
                    this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge));
                }
                // update count
                this.edgeCount -= edgeToRemove.Count;
                edgeToRemove.Clear();
            }

            System.Diagnostics.Debug.Assert(this.edgeCount >= 0);
            this.vertexEdges.Remove(v);
            this.OnVertexRemoved(new VertexEventArgs <TVertex>(v));

            return(true);
        }
Exemplo n.º 5
0
        public virtual bool AddVerticesAndEdge(TEdge e)
        {
            GraphContracts.AssumeNotNull(e, "e");
            if (!this.ContainsVertex(e.Source))
            {
                this.AddVertex(e.Source);
            }
            if (!this.ContainsVertex(e.Target))
            {
                this.AddVertex(e.Target);
            }

            return(this.AddEdge(e));
        }
Exemplo n.º 6
0
        public int RemoveInEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate)
        {
            GraphContracts.AssumeInVertexSet(this, v, "v");
            GraphContracts.AssumeNotNull(predicate, "predicate");
            EdgeList edges = new EdgeList();

            foreach (var edge in this.InEdges(v))
            {
                if (predicate(edge))
                {
                    edges.Add(edge);
                }
            }
            foreach (var edge in edges)
            {
                this.RemoveEdge(edge);
            }
            return(edges.Count);
        }
Exemplo n.º 7
0
        public virtual bool RemoveVertex(TVertex v)
        {
            GraphContracts.AssumeNotNull(v, "v");
            if (!this.ContainsVertex(v))
            {
                return(false);
            }

            // collect edges to remove
            EdgeList edgesToRemove = new EdgeList();

            foreach (var outEdge in this.OutEdges(v))
            {
                this.vertexInEdges[outEdge.Target].Remove(outEdge);
                edgesToRemove.Add(outEdge);
            }
            foreach (var inEdge in this.InEdges(v))
            {
                // might already have been removed
                if (this.vertexOutEdges[inEdge.Source].Remove(inEdge))
                {
                    edgesToRemove.Add(inEdge);
                }
            }

            // notify users
            if (this.EdgeRemoved != null)
            {
                foreach (TEdge edge in edgesToRemove)
                {
                    this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge));
                }
            }

            this.vertexOutEdges.Remove(v);
            this.vertexInEdges.Remove(v);
            this.edgeCount -= edgesToRemove.Count;
            this.OnVertexRemoved(new VertexEventArgs <TVertex>(v));

            GraphContracts.Assert(this.edgeCount >= 0);
            return(true);
        }
Exemplo n.º 8
0
        public int RemoveVertexIf(VertexPredicate <TVertex> predicate)
        {
            GraphContracts.AssumeNotNull(predicate, "predicate");

            VertexList vertices = new VertexList();

            foreach (var v in this.Vertices)
            {
                if (predicate(v))
                {
                    vertices.Add(v);
                }
            }

            foreach (var v in vertices)
            {
                this.RemoveVertex(v);
            }
            return(vertices.Count);
        }
Exemplo n.º 9
0
        public void MergeVertexIf(VertexPredicate <TVertex> vertexPredicate, IEdgeFactory <TVertex, TEdge> edgeFactory)
        {
            GraphContracts.AssumeNotNull(vertexPredicate, "vertexPredicate");
            GraphContracts.AssumeNotNull(edgeFactory, "edgeFactory");

            // storing vertices to merge
            VertexList mergeVertices = new VertexList(this.VertexCount / 4);

            foreach (var v in this.Vertices)
            {
                if (vertexPredicate(v))
                {
                    mergeVertices.Add(v);
                }
            }

            // applying merge recursively
            foreach (var v in mergeVertices)
            {
                MergeVertex(v, edgeFactory);
            }
        }
Exemplo n.º 10
0
        public int RemoveEdgeIf(EdgePredicate <TVertex, TEdge> predicate)
        {
            GraphContracts.AssumeNotNull(predicate, "predicate");

            var edges = new EdgeList();

            foreach (var edge in this.Edges)
            {
                if (predicate(edge))
                {
                    edges.Add(edge);
                }
            }

            foreach (var edge in edges)
            {
                this.RemoveEdge(edge);
            }

            GraphContracts.Assert(this.edgeCount >= 0);
            return(edges.Count);
        }
Exemplo n.º 11
0
 public bool ContainsVertex(TVertex v)
 {
     GraphContracts.AssumeNotNull(v, "v");
     return(this.vertexOutEdges.ContainsKey(v));
 }