예제 #1
0
 public static void AssertNoInEdge <TVertex, TEdge>(IBidirectionalIncidenceGraph <TVertex, TEdge> graph, TVertex vertex)
     where TEdge : IEdge <TVertex>
 {
     Assert.IsTrue(graph.IsInEdgesEmpty(vertex));
     Assert.AreEqual(0, graph.InDegree(vertex));
     CollectionAssert.IsEmpty(graph.InEdges(vertex));
 }
예제 #2
0
        private static int MinRoutes(IBidirectionalIncidenceGraph <Node, Edge <Node> > grpah, Node source, Node target, IEdge <Node> edge,
                                     int depth = 0)
        {
            Node other;

            if (edge.Target == source)
            {
                other = edge.Source;
            }
            else if (edge.Source == source)
            {
                other = edge.Target;
            }
            else
            {
                throw new InvalidOperationException();
            }
            if (other == target)
            {
                return(++depth);
            }
            if (other == source)
            {
                return(int.MaxValue);
            }
            var lst = new List <Edge <Node> >();

            lst.AddRange(grpah.InEdges(other));
            lst.AddRange(grpah.OutEdges(other));
            return(lst.Min(o => MinRoutes(grpah, other, target, o, ++depth)));
        }
예제 #3
0
        private int Routes(IBidirectionalIncidenceGraph <Node, Edge <Node> > grpah, Node a, IEdge <Node> be, int depth = 0)
        {
            var other = be.Target == a ? be.Source : be.Target;
            var lst   = new List <KeyValuePair <int, Edge <Node> > >();

            foreach (var c in grpah.InEdges(a))
            {
                if (c.Source == other)
                {
                    return(++depth);
                }
                lst.Add(new KeyValuePair <int, Edge <Node> >(Routes(grpah, c.Source, c, depth), c));
            }

            foreach (var c in grpah.OutEdges(a))
            {
                if (c.Target == other)
                {
                    return(++depth);
                }
                lst.Add(new KeyValuePair <int, Edge <Node> >(Routes(grpah, c.Target, c, depth), c));
            }

            return(lst.Select(o => o.Key).Min());
        }
        TEdge IBidirectionalIncidenceGraph <TVertex, TEdge> .InEdge(TVertex v, int index)
        {
            IBidirectionalIncidenceGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(v != null);
            Contract.Requires(ithis.ContainsVertex(v));
            Contract.Ensures(Contract.Result <TEdge>().Equals(Enumerable.ElementAt(ithis.InEdges(v), index)));

            return(default(TEdge));
        }
        int IBidirectionalIncidenceGraph <TVertex, TEdge> .InDegree(TVertex v)
        {
            IBidirectionalIncidenceGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(v != null);
            Contract.Requires(ithis.ContainsVertex(v));
            Contract.Ensures(Contract.Result <int>() == Enumerable.Count(ithis.InEdges(v)));

            return(default(int));
        }
예제 #6
0
        public static void AssertHasInEdges <TVertex, TEdge>(
            IBidirectionalIncidenceGraph <TVertex, TEdge> graph,
            TVertex vertex,
            IEnumerable <TEdge> edges)
            where TEdge : IEdge <TVertex>
        {
            TEdge[] edgeArray = edges.ToArray();
            CollectionAssert.IsNotEmpty(edgeArray);

            Assert.IsFalse(graph.IsInEdgesEmpty(vertex));
            Assert.AreEqual(edgeArray.Length, graph.InDegree(vertex));
            CollectionAssert.AreEquivalent(edgeArray, graph.InEdges(vertex));
        }
        protected static void InEdges_Throws_Test <TVertex, TEdge>(
            IBidirectionalIncidenceGraph <TVertex, TEdge> graph)
            where TVertex : IEquatable <TVertex>, new()
            where TEdge : IEdge <TVertex>
        {
            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            var vertex = new TVertex();

            Assert.Throws <VertexNotFoundException>(() => graph.IsInEdgesEmpty(vertex));
            Assert.Throws <VertexNotFoundException>(() => graph.InDegree(vertex));
            Assert.Throws <VertexNotFoundException>(() => graph.InEdges(vertex).ToArray());
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
 protected static void InEdges_NullThrows_Test <TVertex, TEdge>(
     IBidirectionalIncidenceGraph <TVertex, TEdge> graph)
     where TVertex : class
     where TEdge : IEdge <TVertex>
 {
     // ReSharper disable ReturnValueOfPureMethodIsNotUsed
     // ReSharper disable AssignNullToNotNullAttribute
     Assert.Throws <ArgumentNullException>(() => graph.IsInEdgesEmpty(null));
     Assert.Throws <ArgumentNullException>(() => graph.InDegree(null));
     Assert.Throws <ArgumentNullException>(() => graph.InEdges(null).ToArray());
     // ReSharper restore AssignNullToNotNullAttribute
     // ReSharper restore ReturnValueOfPureMethodIsNotUsed
 }
예제 #9
0
        public static void AssertHasReversedInEdges <TVertex, TEdge>(
            [NotNull] IBidirectionalIncidenceGraph <TVertex, SReversedEdge <TVertex, TEdge> > graph,
            [NotNull] TVertex vertex,
            [NotNull, ItemNotNull] IEnumerable <TEdge> edges)
            where TEdge : IEdge <TVertex>
        {
            TEdge[] edgeArray = edges.ToArray();
            CollectionAssert.IsNotEmpty(edgeArray);

            Assert.IsFalse(graph.IsInEdgesEmpty(vertex));
            Assert.AreEqual(edgeArray.Length, graph.InDegree(vertex));
            CollectionAssert.AreEquivalent(
                edgeArray.Select(edge => new SReversedEdge <TVertex, TEdge>(edge)),
                graph.InEdges(vertex));
        }
예제 #10
0
        protected static void InEdges_Throws_Test(
            [NotNull] GraphData <int, Edge <int> > data,
            [NotNull] IBidirectionalIncidenceGraph <int, Edge <int> > graph)
        {
            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            data.CheckCalls(0);

            data.ShouldReturnValue = false;
            Assert.Throws <VertexNotFoundException>(() => graph.IsInEdgesEmpty(1));
            data.CheckCalls(1);

            Assert.Throws <VertexNotFoundException>(() => graph.InDegree(1));
            data.CheckCalls(1);

            Assert.Throws <VertexNotFoundException>(() => graph.InEdges(1));
            data.CheckCalls(1);
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }