예제 #1
0
        public static void Clone <TVertex, TEdge>(
            IVertexAndEdgeListGraph <TVertex, TEdge> g,
            IMutableVertexAndEdgeListGraph <TVertex, TEdge> clone)
            where TVertex : ICloneable
            where TEdge : ICloneableEdge <TVertex>
        {
            GraphContracts.AssumeNotNull(g, "g");
            GraphContracts.AssumeNotNull(clone, "clone");

            var vertexClones = new Dictionary <TVertex, TVertex>();

            foreach (var v in g.Vertices)
            {
                var vc = (TVertex)v.Clone();
                clone.AddVertex(vc);
                vertexClones.Add(v, vc);
            }

            foreach (var edge in g.Edges)
            {
                var ec = (TEdge)edge.Clone(
                    vertexClones[edge.Source],
                    vertexClones[edge.Target]);
                clone.AddEdge(ec);
            }
        }
예제 #2
0
        public static bool IsDirectedAcyclicGraph <TVertex, TEdge>(IVertexListGraph <TVertex, TEdge> g)
            where TEdge : IEdge <TVertex>
        {
            GraphContracts.AssumeNotNull(g, "g");

            return(new DagTester().IsDag(g));
        }
예제 #3
0
파일: BinaryHeap.cs 프로젝트: ssaha6/Precis
        private bool Less(int i, int j)
        {
            GraphContracts.Assert(i >= 0 & i < this.count &
                                  j >= 0 & j < this.count &
                                  i != j, String.Format("i: {0}, j: {1}", i, j));

            return(this.priorityComparison(this.items[i].Key, this.items[j].Key) <= 0);
        }
예제 #4
0
 public void AddEdgeRange(IEnumerable <TEdge> edges)//yes
 {
     GraphContracts.AssumeNotNull(edges, "edges");
     foreach (var edge in edges)
     {
         this.AddEdge(edge);
     }
 }
예제 #5
0
        public void Compute(TVertex rootVertex)
        {
            GraphContracts.AssumeNotNull(rootVertex, "rootVertex");
            // GraphContracts.AssumeInVertexSet(this.VisitedGraph, rootVertex, "rootVertex");

            this.SetRootVertex(rootVertex);
            this.Compute();
        }
예제 #6
0
파일: BinaryHeap.cs 프로젝트: ssaha6/Precis
        public void Add(TPriority priority, TValue value)
        {
            GraphContracts.Assert(count <= this.items.Length);

            this.version++;
            this.ResizeArray();
            this.items[this.count++] = new KeyValuePair <TPriority, TValue>(priority, value);
            this.MinHeapifyDown(this.count - 1);
        }
예제 #7
0
        public void Serialize(TextWriter writer, IVertexAndEdgeSet <TVertex, TEdge> visitedGraph)
        {
            GraphContracts.AssumeNotNull(writer, "writer");
            GraphContracts.AssumeNotNull(visitedGraph, "visitedGraph");

            using (var xwriter = new XmlTextWriter(writer))
            {
                xwriter.Formatting = Formatting.Indented;
                Serialize(xwriter, visitedGraph);
            }
        }
예제 #8
0
        public static ICollection <TVertex> SourceFirstTopologicalSort <TVertex, TEdge>(
            IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph)
            where TEdge : IEdge <TVertex>
        {
            GraphContracts.AssumeNotNull(visitedGraph, "visitedGraph");

            var vertices = new List <TVertex>(visitedGraph.VertexCount);

            SourceFirstTopologicalSort(visitedGraph, vertices);
            return(vertices);
        }
예제 #9
0
파일: BinaryHeap.cs 프로젝트: ssaha6/Precis
        private void Swap(int i, int j)
        {
            GraphContracts.Assert(i >= 0 & i < this.count &
                                  j >= 0 & j < this.count &
                                  i != j);

            var kv = this.items[i];

            this.items[i] = this.items[j];
            this.items[j] = kv;
        }
예제 #10
0
        public static void SourceFirstTopologicalSort <TVertex, TEdge>(
            IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph,
            IList <TVertex> vertices)
            where TEdge : IEdge <TVertex>
        {
            GraphContracts.AssumeNotNull(visitedGraph, "visitedGraph");
            GraphContracts.AssumeNotNull(vertices, "vertices");

            var topo = new SourceFirstTopologicalSortAlgorithm <TVertex, TEdge>(visitedGraph);

            topo.Compute(vertices);
        }
예제 #11
0
 public bool ContainsEdge(TEdge edge) //yes
 {
     GraphContracts.AssumeInVertexSet(this, edge, "edge");
     foreach (var e in this.Edges)
     {
         if (e.Equals(edge))
         {
             return(true);
         }
     }
     return(false);
 }
예제 #12
0
        public static int StronglyConnectedComponents <TVertex, TEdge>(
            IVertexListGraph <TVertex, TEdge> g,
            IDictionary <TVertex, int> components)
            where TEdge : IEdge <TVertex>
        {
            GraphContracts.AssumeNotNull(g, "g");
            GraphContracts.AssumeNotNull(components, "components");

            var conn = new StronglyConnectedComponentsAlgorithm <TVertex, TEdge>(g, components);

            conn.Compute();
            return(conn.ComponentCount);
        }
예제 #13
0
        public static IEnumerable <TVertex> Roots <TVertex, TEdge>(
            IBidirectionalGraph <TVertex, TEdge> visitedGraph)
            where TEdge : IEdge <TVertex>
        {
            GraphContracts.AssumeNotNull(visitedGraph, "visitedGraph");

            foreach (var v in visitedGraph.Vertices)
            {
                if (visitedGraph.IsInEdgesEmpty(v))
                {
                    yield return(v);
                }
            }
        }
예제 #14
0
        public void SetRootVertex(TVertex rootVertex)
        {
            GraphContracts.AssumeNotNull(rootVertex, "rootVertex");
            // GraphContracts.AssumeInVertexSet(this.VisitedGraph, rootVertex, "rootVertex");

            bool changed = !Comparison <TVertex> .Equals(this.rootVertex, rootVertex);

            this.rootVertex = rootVertex;
            if (changed)
            {
                this.OnRooVertexChanged(EventArgs.Empty);
            }
            this.hasRootVertex = true;
        }
예제 #15
0
        public static IEnumerable <TVertex> IsolatedVertices <TVertex, TEdge>(
            IBidirectionalGraph <TVertex, TEdge> visitedGraph)
            where TEdge : IEdge <TVertex>
        {
            GraphContracts.AssumeNotNull(visitedGraph, "visitedGraph");

            foreach (var v in visitedGraph.Vertices)
            {
                if (visitedGraph.Degree(v) == 0)
                {
                    yield return(v);
                }
            }
        }
예제 #16
0
파일: BinaryHeap.cs 프로젝트: ssaha6/Precis
 public void ObjectInvariant()
 {
     GraphContracts.Assert(this.items != null);
     GraphContracts.Assert(
         this.count > -1 &
         this.count <= this.items.Length);
     for (int index = 0; index < this.count; ++index)
     {
         var left = 2 * index + 1;
         GraphContracts.Assert(left >= count || this.Less(index, left));
         var right = 2 * index + 2;
         GraphContracts.Assert(right >= count || this.Less(index, right));
     }
 }
예제 #17
0
        public static IDictionary <TEdge, double> ConstantCapacities <TVertex, TEdge>(
            IEdgeSet <TVertex, TEdge> g, double value)
            where TEdge : IEdge <TVertex>
        {
            GraphContracts.AssumeNotNull(g, "g");

            var capacities = new Dictionary <TEdge, double>(g.EdgeCount);

            foreach (var e in g.Edges)
            {
                capacities.Add(e, value);
            }
            return(capacities);
        }
예제 #18
0
        public static IEnumerable <TVertex> Sinks <TVertex, TEdge>(
            IVertexListGraph <TVertex, TEdge> visitedGraph)
            where TEdge : IEdge <TVertex>
        {
            GraphContracts.AssumeNotNull(visitedGraph, "visitedGraph");

            foreach (var v in visitedGraph.Vertices)
            {
                if (visitedGraph.IsOutEdgesEmpty(v))
                {
                    yield return(v);
                }
            }
        }
예제 #19
0
        public static int ConnectedComponents <TVertex, TEdge>(
            IUndirectedGraph <TVertex, TEdge> g,
            TVertex startVertex,
            IDictionary <TVertex, int> components)
            where TEdge : IEdge <TVertex>
        {
            GraphContracts.AssumeNotNull(g, "g");
            GraphContracts.Assume(g.ContainsVertex(startVertex), "g.ContainsVertex(startVertex)");
            GraphContracts.AssumeNotNull(components, "components");

            var conn = new ConnectedComponentsAlgorithm <TVertex, TEdge>(g, components);

            conn.Compute(startVertex);
            return(conn.ComponentCount);
        }
예제 #20
0
        public int RemoveEdges(IEnumerable <TEdge> edges) //yes
        {
            GraphContracts.AssumeNotNull(edges, "edges");

            int count = 0;

            foreach (var edge in edges)
            {
                if (RemoveEdge(edge))
                {
                    count++;
                }
            }
            return(count);
        }
예제 #21
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));
        }
예제 #22
0
        /// <summary>
        /// Does a depth first search on the vertex u
        /// </summary>
        /// <param name="se">edge to explore</param>
        /// <param name="depth">current exploration depth</param>
        /// <exception cref="ArgumentNullException">se cannot be null</exception>
        private void Visit(TEdge se, int depth)
        {
            GraphContracts.AssumeNotNull(se, "se");
            if (depth > this.maxDepth)
            {
                return;
            }

            // mark edge as gray
            this.EdgeColors[se] = GraphColor.Gray;
            // add edge to the search tree
            OnTreeEdge(se);

            var cancelManager = this.Services.CancelManager;

            // iterate over out-edges
            foreach (var e in this.VisitedGraph.OutEdges(se.Target))
            {
                if (cancelManager.IsCancelling)
                {
                    return;
                }

                // check edge is not explored yet,
                // if not, explore it.
                if (!this.EdgeColors.ContainsKey(e))
                {
                    OnDiscoverTreeEdge(se, e);
                    Visit(e, depth + 1);
                }
                else
                {
                    GraphColor c = this.EdgeColors[e];
                    if (EdgeColors[e] == GraphColor.Gray)
                    {
                        OnBackEdge(e);
                    }
                    else
                    {
                        OnForwardOrCrossEdge(e);
                    }
                }
            }

            // all out-edges have been explored
            this.EdgeColors[se] = GraphColor.Black;
            OnFinishEdge(se);
        }
예제 #23
0
 public bool RemoveEdge(TEdge edge)//yes
 {
     GraphContracts.AssumeInVertexSet(this, edge, "edge");
     /* NotpAssume.IsTrue(this.adjacentEdges.ContainsKey(edge.Source)); */
     this.adjacentEdges[edge.Source].Remove(edge);
     /* NotpAssume.IsTrue(this.adjacentEdges.ContainsKey(edge.Target)); */
     if (this.adjacentEdges[edge.Target].Remove(edge))
     {
         this.edgeCount--;
         System.Diagnostics.Debug.Assert(this.edgeCount >= 0);
         this.OnEdgeRemoved(new EdgeEventArgs <TVertex, TEdge>(edge));
         return(true);
     }
     else
     {
         return(false);
     }
 }
예제 #24
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);
        }
예제 #25
0
        public bool AddEdge(TEdge edge)//yes
        {
            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);
        }
예제 #26
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);
        }
예제 #27
0
        public static IEnumerable <TVertex> Roots <TVertex, TEdge>(
            IVertexListGraph <TVertex, TEdge> visitedGraph)
            where TEdge : IEdge <TVertex>
        {
            GraphContracts.AssumeNotNull(visitedGraph, "visitedGraph");

            var dfs = new DepthFirstSearchAlgorithm <TVertex, TEdge>(visitedGraph);
            var vis = new VertexPredecessorRecorderObserver <TVertex, TEdge>();

            vis.Attach(dfs);

            foreach (var predecessor in vis.VertexPredecessors)
            {
                if (predecessor.Value.Equals(default(TEdge)))
                {
                    yield return(predecessor.Key);
                }
            }
        }
예제 #28
0
        public void ClearAdjacentEdges(TVertex v)//yes
        {
            GraphContracts.AssumeInVertexSet(this, v, "v");
            List <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);
        }
예제 #29
0
        public void Visit(TVertex u, int depth)
        {
            GraphContracts.AssumeNotNull(u, "u");
            if (depth > this.maxDepth)
            {
                return;
            }

            VertexColors[u] = GraphColor.Gray;
            OnDiscoverVertex(u);

            var     cancelManager = this.Services.CancelManager;
            TVertex v             = default(TVertex);

            foreach (var e in VisitedGraph.OutEdges(u))
            {
                if (cancelManager.IsCancelling)
                {
                    return;
                }

                OnExamineEdge(e);
                v = e.Target;
                ProcessEdge(depth, v, e);
            }

            foreach (var e in VisitedGraph.InEdges(u))
            {
                if (cancelManager.IsCancelling)
                {
                    return;
                }

                OnExamineEdge(e);
                v = e.Source;
                ProcessEdge(depth, v, e);
            }

            VertexColors[u] = GraphColor.Black;
            OnFinishVertex(u);
        }
예제 #30
0
        public static double ComputePredecessorCost <TVertex, TEdge>(
            IDictionary <TVertex, TEdge> predecessors,
            IDictionary <TEdge, double> edgeCosts,
            TVertex target
            )
            where TEdge : IEdge <TVertex>
        {
            GraphContracts.AssumeNotNull(predecessors, "predecessors");
            GraphContracts.AssumeNotNull(edgeCosts, "edgeCosts");

            double  cost    = 0;
            TVertex current = target;
            TEdge   edge;

            while (predecessors.TryGetValue(current, out edge))
            {
                cost   += edgeCosts[edge];
                current = edge.Source;
            }

            return(cost);
        }