Exemplo n.º 1
0
        public static bool VertexCountEqual <TVertex>(this IVertexSet <TVertex> left, IVertexSet <TVertex> right)
        {
            Contract.Requires(left != null);
            Contract.Requires(right != null);

            return(left.VertexCount == right.VertexCount);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the vertex identity.
        /// </summary>
        /// <remarks>
        /// Returns more efficient methods for primitive types,
        /// otherwise builds a dictionary
        /// </remarks>
        /// <typeparam name="TVertex">The type of the vertex.</typeparam>
        /// <param name="graph">The graph.</param>
        /// <returns></returns>
        public static VertexIdentity <TVertex> GetVertexIdentity <TVertex>(
#if !NET20
            this
#endif
            IVertexSet <TVertex> graph)
        {
            Contract.Requires(graph != null);

            // simpler identity for primitive types
            var type = typeof(TVertex);

            if (type == typeof(Boolean) || (type == typeof(Byte)) || (type == typeof(Char)) ||
                (type == typeof(UInt16)) || (type == typeof(UInt32)) || (type == typeof(UInt64)) ||
                (type == typeof(SByte)) || (type == typeof(Int16)) || (type == typeof(Int32)) ||
                (type == typeof(Int64)) || (type == typeof(String)) || (type == typeof(Single)) ||
                (type == typeof(Double)) || (type == typeof(DateTime)) || (type == (typeof(Decimal))))
            {
                return((v) => v.ToString());
            }

            // create dictionary
            var ids = new Dictionary <TVertex, string>(graph.VertexCount);

            return(v =>
            {
                string id;
                if (!ids.TryGetValue(v, out id))
                {
                    ids[v] = id = ids.Count.ToString();
                }
                return id;
            });
        }
Exemplo n.º 3
0
 public static void AssertVertexCountEqual <TVertex>(
     this IVertexSet <TVertex> left,
     IVertexSet <TVertex> right)
 {
     Assert.IsNotNull(left);
     Assert.IsNotNull(right);
     Assert.AreEqual(left.VertexCount, right.VertexCount);
 }
Exemplo n.º 4
0
 public static bool InVertexSet <TVertex>(
     this IVertexSet <TVertex> g,
     TVertex v)
 {
     Contract.Requires(g != null);
     Contract.Requires(v != null);
     // todo make requires
     return(g.ContainsVertex(v));
 }
Exemplo n.º 5
0
 private static void CheckComponentCount<TVertex, TEdge>(
     [NotNull] IVertexListGraph<TVertex, TEdge> graph,
     [NotNull] IVertexSet<AdjacencyGraph<TVertex, TEdge>> condensedGraph)
     where TEdge : IEdge<TVertex>
 {
     // Check number of vertices = number of strongly connected components
     int components = graph.WeaklyConnectedComponents(new Dictionary<TVertex, int>());
     Assert.AreEqual(components, condensedGraph.VertexCount, "Component count does not match.");
 }
        protected static void AddVertex_ImmutableGraph_WithUpdate(
            IMutableVertexSet <int> wrappedGraph,
            Func <IVertexSet <int> > createGraph)
        {
            IVertexSet <int> graph = createGraph();

            wrappedGraph.AddVertex(1);
            AssertHasVertices(graph, new[] { 1 });  // Graph is updated
        }
        protected static void AddVertex_ImmutableGraph_NoUpdate(
            IMutableVertexSet <int> wrappedGraph,
            Func <IVertexSet <int> > createGraph)
        {
            IVertexSet <int> graph = createGraph();

            wrappedGraph.AddVertex(1);
            AssertNoVertex(graph);  // Graph is not updated
        }
Exemplo n.º 8
0
 public static TVertex GetVertex <TVertex>(
     [NotNull] IVertexSet <TVertex> graph,
     [NotNull] Random rng)
 {
     if (graph is null)
     {
         throw new ArgumentNullException(nameof(graph));
     }
     return(GetVertex(graph.Vertices, graph.VertexCount, rng));
 }
Exemplo n.º 9
0
        protected static void CheckComponentCount <TVertex, TEdge>(
            IVertexListGraph <TVertex, TEdge> graph,
            IVertexSet <AdjacencyGraph <TVertex, TEdge> > condensedGraph)
            where TEdge : IEdge <TVertex>
        {
            // Check number of vertices = number of strongly connected components
            var components     = new Dictionary <TVertex, int>();
            int componentCount = graph.StronglyConnectedComponents(components);

            Assert.AreEqual(componentCount, condensedGraph.VertexCount, "Component count does not match.");
        }
 private static void VerifyVertexCount <TVertex, TEdge>(
     [NotNull] IVertexSet <TVertex> graph,
     // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
     [NotNull] MultiSourceSinkGraphAugmentorAlgorithm <TVertex, TEdge> augmentor,
     int vertexCount)
     where TEdge : IEdge <TVertex>
 {
     Assert.AreEqual(vertexCount + 2 /* Source + Sink */, graph.VertexCount);
     Assert.IsTrue(graph.ContainsVertex(augmentor.SuperSource));
     Assert.IsTrue(graph.ContainsVertex(augmentor.SuperSink));
 }
Exemplo n.º 11
0
        public static void AssertHasVertices <TVertex>(
            IVertexSet <TVertex> graph,
            IEnumerable <TVertex> vertices)
        {
            TVertex[] vertexArray = vertices.ToArray();
            CollectionAssert.IsNotEmpty(vertexArray);

            Assert.IsFalse(graph.IsVerticesEmpty);
            Assert.AreEqual(vertexArray.Length, graph.VertexCount);
            CollectionAssert.AreEquivalent(vertexArray, graph.Vertices);
        }
Exemplo n.º 12
0
 public static void AssumeInVertexSet <TVertex>(
     IVertexSet <TVertex> g,
     TVertex v,
     string parameterName)
 {
     AssumeNotNull(g, "g");
     AssumeNotNull(v, parameterName);
     if (!g.ContainsVertex(v))
     {
         throw new VertexNotFoundException(parameterName);
     }
 }
Exemplo n.º 13
0
 public static void AssumeNotInVertexSet <TVertex>(
     IVertexSet <TVertex> g,
     TVertex v,
     string parameterName)
 {
     AssumeNotNull(g, "g");
     AssumeNotNull(v, parameterName);
     if (g.ContainsVertex(v))
     {
         throw new ArgumentException("vertex already in set", parameterName);
     }
 }
Exemplo n.º 14
0
        public static bool VertexCountEqual <TVertex>(
#if !NET20
            this
#endif
            IVertexSet <TVertex> left,
            IVertexSet <TVertex> right)
        {
            //Contract.Requires(left != null);
            //Contract.Requires(right != null);

            return(left.VertexCount == right.VertexCount);
        }
        protected static void CheckVertexCount <TVertex, TEdge>(
            IVertexSet <TVertex> graph,
            IVertexSet <AdjacencyGraph <TVertex, TEdge> > condensedGraph)
            where TEdge : IEdge <TVertex>
        {
            int count = 0;

            foreach (AdjacencyGraph <TVertex, TEdge> vertices in condensedGraph.Vertices)
            {
                count += vertices.VertexCount;
            }
            Assert.AreEqual(graph.VertexCount, count, $"{nameof(graph.VertexCount)} does not match.");
        }
Exemplo n.º 16
0
 private void Check(IVertexSet <IGeometry> graph, IDictionary <IEdge <IGeometry>, double> consts,
                    VertexPredecessorRecorderObserver <IGeometry, IEdge <IGeometry> > predecessorObserver)
 {
     foreach (IGeometry v in graph.Vertices)
     {
         double            distance = 0;
         IGeometry         vertex   = v;
         IEdge <IGeometry> predecessor;
         while (predecessorObserver.VertexPredecessors.TryGetValue(vertex, out predecessor))
         {
             distance += consts[predecessor];
             vertex    = predecessor.Source;
         }
         Console.WriteLine("A -> {0}: {1}", v, distance);
     }
 }
        /// <summary>
        /// Gets the vertex identity.
        /// </summary>
        /// <remarks>
        /// Returns more efficient methods for primitive types,
        /// otherwise builds a dictionary
        /// </remarks>
        /// <typeparam name="TVertex">The type of the vertex.</typeparam>
        /// <param name="graph">The graph.</param>
        /// <returns></returns>
        public static VertexIdentity <TVertex> GetVertexIdentity <TVertex>(
#if !NET20
            this
#endif
            IVertexSet <TVertex> graph)
        {
            Contract.Requires(graph != null);

            // simpler identity for primitive types
            switch (Type.GetTypeCode(typeof(TVertex)))
            {
            case TypeCode.String:
            case TypeCode.Boolean:
            case TypeCode.Byte:
            case TypeCode.Char:
            case TypeCode.Decimal:
            case TypeCode.Double:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.SByte:
            case TypeCode.Single:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                return((v) => v.ToString());
            }

            // create dictionary
            var ids = new Dictionary <TVertex, string>(graph.VertexCount);

            return(v =>
            {
                string id;
                if (!ids.TryGetValue(v, out id))
                {
                    ids[v] = id = ids.Count.ToString();
                }
                return id;
            });
        }
Exemplo n.º 18
0
        /// <summary>
        /// Gets the vertex identity.
        /// </summary>
        /// <remarks>
        /// Returns more efficient methods for primitive types,
        /// otherwise builds a dictionary
        /// </remarks>
        /// <typeparam name="TVertex">The type of the vertex.</typeparam>
        /// <param name="graph">The graph.</param>
        /// <returns></returns>
        public static VertexIdentity <TVertex> GetVertexIdentity <TVertex>(
            this IVertexSet <TVertex> graph)
        {
            Contract.Requires(graph != null);

            // simpler identity for primitive types
            if (typeof(TVertex) == typeof(string) ||
                typeof(TVertex) == typeof(bool) ||
                typeof(TVertex) == typeof(byte) ||
                typeof(TVertex) == typeof(char) ||
                typeof(TVertex) == typeof(decimal) ||
                typeof(TVertex) == typeof(double) ||
                typeof(TVertex) == typeof(short) ||
                typeof(TVertex) == typeof(int) ||
                typeof(TVertex) == typeof(long) ||
                typeof(TVertex) == typeof(sbyte) ||
                typeof(TVertex) == typeof(float) ||
                typeof(TVertex) == typeof(ushort) ||
                typeof(TVertex) == typeof(uint) ||
                typeof(TVertex) == typeof(ulong))
            {
                return((v) => v.ToString());
            }

            // create dictionary
            var ids = new Dictionary <TVertex, string>(graph.VertexCount);

            return(v =>
            {
                string id;
                if (!ids.TryGetValue(v, out id))
                {
                    ids[v] = id = ids.Count.ToString();
                }
                return id;
            });
        }
        private static Size EvaluateCanvasSize(
            [NotNull] IVertexSet <object> compoundGraph,
            [NotNull] ILayoutAlgorithm <object, IEdge <object>, CompoundGraph <object, IEdge <object> > > algorithm,
            [NotNull] IReadOnlyDictionary <object, Size> verticesSizes)
        {
            var topLeft     = new Point(double.PositiveInfinity, double.PositiveInfinity);
            var bottomRight = new Point(double.NegativeInfinity, double.NegativeInfinity);

            foreach (object v in compoundGraph.Vertices)
            {
                Point pos  = algorithm.VerticesPositions[v];
                Size  size = verticesSizes[v];

                topLeft.X = Math.Min(topLeft.X, pos.X - size.Width / 2.0);
                topLeft.Y = Math.Min(topLeft.Y, pos.Y - size.Height / 2.0);

                bottomRight.X = Math.Max(bottomRight.X, pos.X + size.Width / 2.0);
                bottomRight.Y = Math.Max(bottomRight.Y, pos.Y + size.Height / 2.0);
            }

            Vector sizeVector = bottomRight - topLeft;

            return(new Size(sizeVector.X, sizeVector.Y));
        }
Exemplo n.º 20
0
 public static void AssertNoVertex <TVertex>(IVertexSet <TVertex> graph)
 {
     Assert.IsTrue(graph.IsVerticesEmpty);
     Assert.AreEqual(0, graph.VertexCount);
     CollectionAssert.IsEmpty(graph.Vertices);
 }
Exemplo n.º 21
0
 private void Check(IVertexSet<IGeometry> graph, IDictionary<IEdge<IGeometry>, double> consts, 
     VertexPredecessorRecorderObserver<IGeometry, IEdge<IGeometry>> predecessorObserver)
 {
     foreach (IGeometry v in graph.Vertices)
     {
         double distance = 0;
         IGeometry vertex = v;
         IEdge<IGeometry> predecessor;
         while (predecessorObserver.VertexPredecessors.TryGetValue(vertex, out predecessor))
         {
             distance += consts[predecessor];
             vertex = predecessor.Source;
         }
         Console.WriteLine("A -> {0}: {1}", v, distance);
     }
 }
Exemplo n.º 22
0
 public static bool InVertexSet <TVertex>(
     [NotNull] IVertexSet <TVertex> graph,
     [NotNull] TVertex vertex)
 {
     return(graph.ContainsVertex(vertex));
 }
Exemplo n.º 23
0
 public static bool InVertexSet <TVertex>(
     IVertexSet <TVertex> graph,
     TVertex vertex)
 {
     return(graph.ContainsVertex(vertex));
 }