コード例 #1
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));
                }
            }
        }
コード例 #2
0
        public bool TryGetEdge(
            TVertex source,
            TVertex target,
            out TEdge edge)
        {
            GraphContracts.AssumeInVertexSet(this, source, "source");
            GraphContracts.AssumeInVertexSet(this, target, "target");

            EdgeList edgeList;

            if (this.vertexOutEdges.TryGetValue(source, out edgeList) &&
                edgeList.Count > 0)
            {
                foreach (var e in edgeList)
                {
                    if (e.Target.Equals(target))
                    {
                        edge = e;
                        return(true);
                    }
                }
            }
            edge = default(TEdge);
            return(false);
        }
コード例 #3
0
        public bool TryGetEdges(
            TVertex source,
            TVertex target,
            out IEnumerable <TEdge> edges)
        {
            GraphContracts.AssumeInVertexSet(this, source, "source");
            GraphContracts.AssumeInVertexSet(this, target, "target");

            EdgeList edgeList;

            if (this.vertexOutEdges.TryGetValue(source, out edgeList))
            {
                List <TEdge> list = new List <TEdge>(edgeList.Count);
                foreach (var edge in edgeList)
                {
                    if (edge.Target.Equals(target))
                    {
                        list.Add(edge);
                    }
                }
                edges = list;
                return(true);
            }
            else
            {
                edges = null;
                return(false);
            }
        }
コード例 #4
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);
        }
コード例 #5
0
 public EdgeEdgeEventArgs(TEdge edge, TEdge targetEdge)
 {
     GraphContracts.AssumeNotNull(edge, "edge");
     GraphContracts.AssumeNotNull(targetEdge, "targetEdge");
     this.edge       = edge;
     this.targetEdge = targetEdge;
 }
コード例 #6
0
        public void ClearEdges(TVertex v)
        {
            GraphContracts.AssumeInVertexSet(this, v, "v");

            ClearOutEdges(v);
            ClearInEdges(v);
        }
コード例 #7
0
ファイル: Edge.cs プロジェクト: eosfor/quickgraph-peli
        public Edge(TVertex source, TVertex target)
        {
            GraphContracts.AssumeNotNull(source, "source");
            GraphContracts.AssumeNotNull(target, "target");

            this.source = source;
            this.target = target;
        }
コード例 #8
0
 public virtual void AddVertexRange(IEnumerable <TVertex> vertices)
 {
     GraphContracts.AssumeNotNull(vertices, "vertices");
     foreach (var v in vertices)
     {
         this.AddVertex(v);
     }
 }
コード例 #9
0
 public void AddEdgeRange(IEnumerable <TEdge> edges)
 {
     GraphContracts.AssumeNotNull(edges, "edges");
     foreach (var edge in edges)
     {
         this.AddEdge(edge);
     }
 }
コード例 #10
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);
        }
コード例 #11
0
 public bool AddEdge(TEdge edge)
 {
     GraphContracts.AssumeNotNull(edge, "edge");
     if (this.ContainsEdge(edge))
     {
         return(false);
     }
     this.edges.Add(edge, edge);
     this.OnEdgeAdded(new EdgeEventArgs <TVertex, TEdge>(edge));
     return(true);
 }
コード例 #12
0
 public bool AddEdge(TEdge edge)
 {
     GraphContracts.AssumeNotNull(edge, "edge");
     if (this.edges[edge.Source, edge.Target] != null)
     {
         throw new ParallelEdgeNotAllowedException();
     }
     this.edges[edge.Source, edge.Target] = edge;
     this.edgeCount++;
     this.OnEdgeAdded(new EdgeEventArgs <int, TEdge>(edge));
     return(true);
 }
コード例 #13
0
 public bool ContainsEdge(TEdge edge)
 {
     GraphContracts.AssumeInVertexSet(this, edge, "edge");
     foreach (var e in this.Edges)
     {
         if (e.Equals(edge))
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #14
0
 public bool ContainsEdge(TVertex source, TVertex target)
 {
     GraphContracts.AssumeInVertexSet(this, source, "source");
     GraphContracts.AssumeInVertexSet(this, target, "target");
     foreach (var outEdge in this.OutEdges(source))
     {
         if (outEdge.Target.Equals(target))
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #15
0
 public virtual void AddVertex(TVertex v)
 {
     GraphContracts.AssumeNotInVertexSet(this, v, "v");
     if (this.EdgeCapacity > 0)
     {
         this.vertexEdges.Add(v, new EdgeList(this.EdgeCapacity));
     }
     else
     {
         this.vertexEdges.Add(v, new EdgeList());
     }
     this.OnVertexAdded(new VertexEventArgs <TVertex>(v));
 }
コード例 #16
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));
        }
コード例 #17
0
        public int RemoveEdges(IEnumerable <TEdge> edges)
        {
            GraphContracts.AssumeNotNull(edges, "edges");

            int count = 0;

            foreach (var edge in edges)
            {
                if (RemoveEdge(edge))
                {
                    count++;
                }
            }
            return(count);
        }
コード例 #18
0
        public int RemoveEdgeIf(EdgePredicate <TVertex, TEdge> predicate)
        {
            GraphContracts.AssumeNotNull(predicate, "predicate");

            List <TEdge> edges = new List <TEdge>();

            foreach (var edge in this.Edges)
            {
                if (predicate(edge))
                {
                    edges.Add(edge);
                }
            }
            return(this.RemoveEdges(edges));
        }
コード例 #19
0
 public virtual bool RemoveEdge(TEdge e)
 {
     GraphContracts.AssumeInVertexSet(this, e, "e");
     if (this.vertexEdges[e.Source].Remove(e))
     {
         this.edgeCount--;
         System.Diagnostics.Debug.Assert(this.edgeCount >= 0);
         this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(e));
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #20
0
        public void ClearInEdges(TVertex v)
        {
            GraphContracts.AssumeInVertexSet(this, v, "v");

            EdgeList inEdges = this.vertexInEdges[v];

            foreach (var edge in inEdges)
            {
                this.vertexOutEdges[edge.Source].Remove(edge);
                this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge));
            }

            this.edgeCount -= inEdges.Count;
            inEdges.Clear();
            GraphContracts.Assert(this.edgeCount >= 0);
        }
コード例 #21
0
        public virtual bool AddEdge(TEdge e)
        {
            GraphContracts.AssumeInVertexSet <TVertex, TEdge>(this, e, "e");
            if (!this.AllowParallelEdges)
            {
                if (this.ContainsEdge(e.Source, e.Target))
                {
                    return(false);
                }
            }
            this.vertexEdges[e.Source].Add(e);
            this.edgeCount++;

            this.OnEdgeAdded(new EdgeEventArgs <TVertex, TEdge>(e));

            return(true);
        }
コード例 #22
0
        public void ClearOutEdges(TVertex v)
        {
            GraphContracts.AssumeInVertexSet(this, v, "v");

            var edges = this.vertexEdges[v];
            int count = edges.Count;

            if (this.EdgeRemoved != null) // call only if someone is listening
            {
                foreach (var edge in edges)
                {
                    this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge));
                }
            }
            edges.Clear();
            this.edgeCount -= count;
            GraphContracts.Assert(this.edgeCount >= 0);
        }
コード例 #23
0
        public void ClearAdjacentEdges(TVertex v)
        {
            GraphContracts.AssumeInVertexSet(this, v, "v");
            IList <TEdge> edges = this.adjacentEdges[v];

            this.edgeCount -= edges.Count;
            foreach (var edge in edges)
            {
                if (edge.Source.Equals(v))
                {
                    this.adjacentEdges[edge.Target].Remove(edge);
                }
                else
                {
                    this.adjacentEdges[edge.Source].Remove(edge);
                }
            }
            System.Diagnostics.Debug.Assert(this.edgeCount >= 0);
        }
コード例 #24
0
        public int RemoveAdjacentEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate)
        {
            GraphContracts.AssumeInVertexSet(this, v, "v");
            GraphContracts.AssumeNotNull(predicate, "predicate");

            IList <TEdge> outEdges = this.adjacentEdges[v];
            List <TEdge>  edges    = new List <TEdge>(outEdges.Count);

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

            this.RemoveEdges(edges);
            return(edges.Count);
        }
コード例 #25
0
        public int RemoveVertexIf(VertexPredicate <TVertex> pred)
        {
            GraphContracts.AssumeNotNull(pred, "pred");
            List <TVertex> vertices = new List <TVertex>();

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

            foreach (var v in vertices)
            {
                RemoveVertex(v);
            }
            return(vertices.Count);
        }
コード例 #26
0
        public bool AddEdge(TEdge edge)
        {
            GraphContracts.AssumeInVertexSet(this, edge, "edge");

            if (!this.AllowParallelEdges)
            {
                if (this.adjacentEdges[edge.Source].Contains(edge))
                {
                    return(false);
                }
            }
            this.adjacentEdges[edge.Source].Add(edge);
            this.adjacentEdges[edge.Target].Add(edge);
            this.edgeCount++;

            this.OnEdgeAdded(new EdgeEventArgs <TVertex, TEdge>(edge));

            return(true);
        }
コード例 #27
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);
        }
コード例 #28
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);
        }
コード例 #29
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);
        }
コード例 #30
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);
            }
        }