예제 #1
0
        public bool TryGetSuccessor(IImplicitGraph <TVertex, TEdge> g, TVertex u, out TEdge successor)
        {
            Contract.Requires(g != null);
            Contract.Requires(u != null);

            int outDegree = g.OutDegree(u);

            if (outDegree > 0)
            {
                int index;
                if (!outEdgeIndices.TryGetValue(u, out index))
                {
                    index = 0;
                    outEdgeIndices.Add(u, index);
                }
                TEdge e = g.OutEdge(u, index);
                this.outEdgeIndices[u] = (++index) % outDegree;

                successor = e;
                return(true);
            }

            successor = default(TEdge);
            return(false);
        }
예제 #2
0
        public override TEdge Successor(IImplicitGraph <TVertex, TEdge> g, TVertex u)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (u == null)
            {
                throw new ArgumentNullException("u");
            }

            // get number of out-edges
            int n = g.OutDegree(u);

            if (n == 0)
            {
                return(default(TEdge));
            }
            // compute out-edge su
            double outWeight = GetOutWeight(g, u);
            // scale and get next edge
            double r = this.Rand.NextDouble() * outWeight;

            return(Successor(g, u, r));
        }
 public virtual IEdge Successor(IImplicitGraph g, IVertex u)
 {
     if (g == null)
     {
         throw new ArgumentNullException("g");
     }
     if (u == null)
     {
         throw new ArgumentNullException("u");
     }
     int num = g.OutDegree(u);
     double num2 = 0.0;
     IEdgeEnumerator enumerator = g.OutEdges(u).GetEnumerator();
     while (enumerator.MoveNext())
     {
         IEdge edge = enumerator.get_Current();
         num2 += this.weights.get_Item(edge);
     }
     double num3 = this.rnd.NextDouble() * num2;
     double num4 = 0.0;
     double num5 = 0.0;
     IEdgeEnumerator enumerator2 = g.OutEdges(u).GetEnumerator();
     while (enumerator2.MoveNext())
     {
         IEdge edge2 = enumerator2.get_Current();
         num5 = num4 + this.weights.get_Item(edge2);
         if ((num3 >= num4) && (num3 <= num5))
         {
             return edge2;
         }
         num4 = num5;
     }
     return null;
 }
예제 #4
0
 public static void AssertNoOutEdge <TVertex, TEdge>(IImplicitGraph <TVertex, TEdge> graph, TVertex vertex)
     where TEdge : IEdge <TVertex>
 {
     Assert.IsTrue(graph.IsOutEdgesEmpty(vertex));
     Assert.AreEqual(0, graph.OutDegree(vertex));
     CollectionAssert.IsEmpty(graph.OutEdges(vertex));
 }
예제 #5
0
        bool IImplicitGraph <TVertex, TEdge> .IsOutEdgesEmpty(TVertex v)
        {
            IImplicitGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(v != null);
            Contract.Requires(ithis.ContainsVertex(v));
            Contract.Ensures(Contract.Result <bool>() == (ithis.OutDegree(v) == 0));

            return(default(bool));
        }
        TEdge IImplicitGraph <TVertex, TEdge> .OutEdge(TVertex v, int index)
        {
            IImplicitGraph <TVertex, TEdge> ithis = this;

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

            return(default(TEdge));
        }
 public IEdge Successor(IImplicitGraph g, IVertex u)
 {
     if (g == null)
     {
         throw new ArgumentNullException("g");
     }
     if (u == null)
     {
         throw new ArgumentNullException("u");
     }
     return this.NextState(g.OutDegree(u), g.OutEdges(u));
 }
예제 #8
0
 protected static void OutEdges_NullThrows_Test <TVertex, TEdge>(
     IImplicitGraph <TVertex, TEdge> graph)
     where TVertex : class
     where TEdge : IEdge <TVertex>
 {
     // ReSharper disable ReturnValueOfPureMethodIsNotUsed
     // ReSharper disable AssignNullToNotNullAttribute
     Assert.Throws <ArgumentNullException>(() => graph.IsOutEdgesEmpty(null));
     Assert.Throws <ArgumentNullException>(() => graph.OutDegree(null));
     Assert.Throws <ArgumentNullException>(() => graph.OutEdges(null).ToArray());
     // ReSharper restore AssignNullToNotNullAttribute
     // ReSharper restore ReturnValueOfPureMethodIsNotUsed
 }
예제 #9
0
        /// <summary>
        /// Selects the next out-<see cref="IEdge"/> in the Markov Chain.
        /// </summary>
        /// <param name="g">visted graph</param>
        /// <param name="u">source vertex</param>
        /// <returns>Random next out-edge</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="g"/> or <paramref name="u"/> is a null reference
        /// </exception>
        public IEdge Successor(IImplicitGraph g, IVertex u)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (u == null)
            {
                throw new ArgumentNullException("u");
            }

            return(NextState(g.OutDegree(u), g.OutEdges(u)));
        }
        public override TEdge Successor(IImplicitGraph <TVertex, TEdge> g, TVertex u)
        {
            int outDegree = g.OutDegree(u);

            if (outDegree == 0)
            {
                return(default(TEdge));
            }

            int index = this.Rand.Next(0, outDegree);

            return(g.OutEdge(u, index));
        }
예제 #11
0
        public static void AssertHasOutEdges <TVertex, TEdge>(
            IImplicitGraph <TVertex, TEdge> graph,
            TVertex vertex,
            IEnumerable <TEdge> edges)
            where TEdge : IEdge <TVertex>
        {
            TEdge[] edgeArray = edges.ToArray();
            CollectionAssert.IsNotEmpty(edgeArray);

            Assert.IsFalse(graph.IsOutEdgesEmpty(vertex));
            Assert.AreEqual(edgeArray.Length, graph.OutDegree(vertex));
            CollectionAssert.AreEquivalent(edgeArray, graph.OutEdges(vertex));
        }
예제 #12
0
        /// <inheritdoc />
        public override bool TryGetSuccessor(IImplicitGraph<TVertex, TEdge> graph, TVertex vertex, out TEdge successor)
        {
            int outDegree = graph.OutDegree(vertex);
            if (outDegree > 0)
            {
                int index = Rand.Next(0, outDegree);
                successor = graph.OutEdge(vertex, index);
                return true;
            }

            successor = default(TEdge);
            return false;
        }
        public override bool TryGetSuccessor(IImplicitGraph <TVertex, TEdge> g, TVertex u, out TEdge successor)
        {
            int outDegree = g.OutDegree(u);

            if (outDegree > 0)
            {
                int index = this.Rand.Next(0, outDegree);
                successor = g.OutEdge(u, index);
                return(true);
            }

            successor = default(TEdge);
            return(false);
        }
예제 #14
0
        public static void AssertHasReversedOutEdges <TVertex, TEdge>(
            [NotNull] IImplicitGraph <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.IsOutEdgesEmpty(vertex));
            Assert.AreEqual(edgeArray.Length, graph.OutDegree(vertex));
            CollectionAssert.AreEquivalent(
                edgeArray.Select(edge => new SReversedEdge <TVertex, TEdge>(edge)),
                graph.OutEdges(vertex));
        }
예제 #15
0
        protected static void OutEdges_Throws_Test <TVertex, TEdge>(
            IImplicitGraph <TVertex, TEdge> graph)
            where TVertex : IEquatable <TVertex>, new()
            where TEdge : IEdge <TVertex>
        {
            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            // ReSharper disable AssignNullToNotNullAttribute
            var vertex = new TVertex();

            Assert.Throws <VertexNotFoundException>(() => graph.IsOutEdgesEmpty(vertex));
            Assert.Throws <VertexNotFoundException>(() => graph.OutDegree(vertex));
            Assert.Throws <VertexNotFoundException>(() => graph.OutEdges(vertex).ToArray());
            // ReSharper restore AssignNullToNotNullAttribute
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
예제 #16
0
        public override bool TryGetSuccessor(IImplicitGraph <TVertex, TEdge> g, TVertex u, out TEdge successor)
        {
            // get number of out-edges
            int n = g.OutDegree(u);

            if (n > 0)
            {
                // compute out-edge su
                double outWeight = GetOutWeight(g, u);
                // scale and get next edge
                double r = this.Rand.NextDouble() * outWeight;
                return(TryGetSuccessor(g, u, r, out successor));
            }

            successor = default(TEdge);
            return(false);
        }
        /// <inheritdoc />
        public override bool TryGetSuccessor(IImplicitGraph <TVertex, TEdge> graph, TVertex vertex, out TEdge successor)
        {
            // Get the number of out-edges
            int n = graph.OutDegree(vertex);

            if (n > 0)
            {
                // Compute out-edge su
                double outWeight = GetOutWeight(graph, vertex);
                // Scale and get next edge
                double random = Rand.NextDouble() * outWeight;
                return(TryGetSuccessor(graph, vertex, random, out successor));
            }

            successor = default(TEdge);
            return(false);
        }
예제 #18
0
        protected static void OutEdges_Throws_Test(
            [NotNull] GraphData <int, Edge <int> > data,
            [NotNull] IImplicitGraph <int, Edge <int> > graph)
        {
            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            data.CheckCalls(0);

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

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

            Assert.Throws <VertexNotFoundException>(() => graph.OutEdges(1));
            data.CheckCalls(1);
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
예제 #19
0
        public TEdge Successor(IImplicitGraph <TVertex, TEdge> g, TVertex u)
        {
            int outDegree = g.OutDegree(u);

            if (outDegree == 0)
            {
                return(default(TEdge));
            }

            int index;

            if (!outEdgeIndices.TryGetValue(u, out index))
            {
                index = 0;
                outEdgeIndices.Add(u, index);
            }
            TEdge e = g.OutEdge(u, index);

            this.outEdgeIndices[u] = (++index) % outDegree;

            return(e);
        }
예제 #20
0
        /// <inheritdoc />
        public bool TryGetSuccessor(IImplicitGraph <TVertex, TEdge> graph, TVertex vertex, out TEdge successor)
        {
            int outDegree = graph.OutDegree(vertex);

            if (outDegree > 0)
            {
                if (!_outEdgeIndices.TryGetValue(vertex, out int index))
                {
                    index = 0;
                    _outEdgeIndices.Add(vertex, index);
                }

                TEdge edge = graph.OutEdge(vertex, index);
                _outEdgeIndices[vertex] = ++index % outDegree;

                successor = edge;
                return(true);
            }

            successor = default(TEdge);
            return(false);
        }
예제 #21
0
        /// <summary>
        /// Selects the next out-<see cref="IEdge"/> in the Markov Chain.
        /// </summary>
        /// <param name="g">visted graph</param>
        /// <param name="u">source vertex</param>
        /// <returns>Random next out-edge</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="g"/> or <paramref name="u"/> is a null reference
        /// </exception>
        public virtual IEdge Successor(IImplicitGraph g, IVertex u)
        {
            if (g == null)
            {
                throw new ArgumentNullException("g");
            }
            if (u == null)
            {
                throw new ArgumentNullException("u");
            }

            // get number of out-edges
            int n = g.OutDegree(u);
            // compute out-edge su
            double outWeight = 0;

            foreach (IEdge e in g.OutEdges(u))
            {
                outWeight += this.weights[e];
            }

            // scale and get next edge
            double r = rnd.NextDouble() * outWeight;

            double pos     = 0;
            double nextPos = 0;

            foreach (IEdge e in g.OutEdges(u))
            {
                nextPos = pos + this.weights[e];
                if (r >= pos && r <= nextPos)
                {
                    return(e);
                }
                pos = nextPos;
            }
            return(null);
        }
        /// <summary>
        /// Selects the next out-<see cref="IEdge"/> in the Markov Chain.
        /// </summary>
        /// <param name="g">visted graph</param>
        /// <param name="u">source vertex</param>
        /// <returns>Random next out-edge</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="g"/> or <paramref name="u"/> is a null reference
        /// </exception>
        public virtual IEdge Successor(IImplicitGraph g, IVertex u)
        {
            if (g==null)
                throw new ArgumentNullException("g");
            if (u==null)
                throw new ArgumentNullException("u");

            // get number of out-edges
            int n = g.OutDegree(u);
            // compute out-edge su
            double outWeight = 0;
            foreach(IEdge e in g.OutEdges(u))
            {
                outWeight+=this.weights[e];
            }

            // scale and get next edge
            double r = rnd.NextDouble() * outWeight;

            double pos = 0;
            double nextPos = 0;
            foreach(IEdge e in g.OutEdges(u))
            {
                nextPos = pos+this.weights[e];
                if (r>=pos && r<=nextPos)
                    return e;
                pos = nextPos;
            }
            return null;
        }
예제 #23
0
 public int OutDegree(Vertex v)
 {
     return(_impl.OutDegree(v));
 }