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; }
public override TEdge Successor(IImplicitGraph <TVertex, TEdge> g, TVertex u) { if (g.IsOutEdgesEmpty(u)) { return(default(TEdge)); } // get outweight double outWeight = GetOutWeight(g, u); // get succesor TEdge s = Successor(g, u, this.Rand.NextDouble() * outWeight); // update probabilities this.Weights[s] *= this.Factor; // normalize foreach (TEdge e in g.OutEdges(u)) { checked { this.Weights[e] /= outWeight; } } return(s); }
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)); }
public override bool TryGetSuccessor(IImplicitGraph <TVertex, TEdge> g, TVertex u, out TEdge successor) { if (!g.IsOutEdgesEmpty(u)) { // get outweight double outWeight = GetOutWeight(g, u); // get succesor TEdge s; if (this.TryGetSuccessor(g, u, this.Rand.NextDouble() * outWeight, out s)) { // update probabilities this.Weights[s] *= this.Factor; // normalize foreach (TEdge e in g.OutEdges(u)) { checked { this.Weights[e] /= outWeight; } } successor = s; return(true); } } successor = default(TEdge); return(false); }
/// <inheritdoc /> public override bool TryGetSuccessor(IImplicitGraph <TVertex, TEdge> graph, TVertex vertex, out TEdge successor) { if (!graph.IsOutEdgesEmpty(vertex)) { // Get out weight double outWeight = GetOutWeight(graph, vertex); // Get successor if (TryGetSuccessor(graph, vertex, Rand.NextDouble() * outWeight, out successor)) { // Update probabilities Weights[successor] *= Factor; // Normalize foreach (TEdge edge in graph.OutEdges(vertex)) { Weights[edge] /= outWeight; } return(true); } } successor = default(TEdge); return(false); }
protected bool TryGetSuccessor(IImplicitGraph <TVertex, TEdge> g, TVertex u, double position, out TEdge successor) { Contract.Requires(g != null); Contract.Requires(u != null); var edges = g.OutEdges(u); return(TryGetSuccessor(edges, position, out successor)); }
protected double GetOutWeight(IImplicitGraph <TVertex, TEdge> g, TVertex u) { Contract.Requires(g != null); Contract.Requires(u != null); var edges = g.OutEdges(u); return(GetWeights(edges)); }
/// <summary> /// Tries to get the successor of the given <paramref name="vertex"/> in the given <paramref name="graph"/>. /// </summary> /// <param name="graph">The graph to search in.</param> /// <param name="vertex">The vertex.</param> /// <param name="position">The position.</param> /// <param name="successor">Found successor, otherwise null.</param> /// <returns>True if a successor was found, false otherwise.</returns> protected bool TryGetSuccessor([NotNull] IImplicitGraph <TVertex, TEdge> graph, [NotNull] TVertex vertex, double position, out TEdge successor) { Debug.Assert(graph != null); Debug.Assert(vertex != null); IEnumerable <TEdge> edges = graph.OutEdges(vertex); return(TryGetSuccessor(edges, position, out successor)); }
protected double GetOutWeight(IImplicitGraph <TVertex, TEdge> g, TVertex u) { double outWeight = 0; foreach (TEdge e in g.OutEdges(u)) { outWeight += this.weights[e]; } return(outWeight); }
TEdge IImplicitGraph <TVertex, TEdge> .OutEdge(TVertex v, int index) { IImplicitGraph <TVertex, TEdge> ithis = this; Contract.Requires(v != null); Contract.Requires(ithis.ContainsVertex(v)); Contract.Ensures(Enumerable.Any(ithis.OutEdges(v), e => e.Equals(Contract.Result <TEdge>()))); return(default(TEdge)); }
int IImplicitGraph <TVertex, TEdge> .OutDegree(TVertex v) { IImplicitGraph <TVertex, TEdge> ithis = this; Contract.Requires(v != null); Contract.Requires(ithis.ContainsVertex(v)); Contract.Ensures(Contract.Result <int>() == Enumerable.Count <TEdge>(ithis.OutEdges(v))); return(default(int)); }
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)); }
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 }
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)); }
/// <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))); }
/// <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> /// Tries to get the successor of the given <paramref name="vertex"/> in the given <paramref name="graph"/>. /// </summary> /// <param name="graph">The graph to search in.</param> /// <param name="vertex">The vertex.</param> /// <param name="position">The position.</param> /// <param name="successor">Found successor, otherwise null.</param> /// <returns>True if a successor was found, false otherwise.</returns> protected bool TryGetSuccessor([NotNull] IImplicitGraph <TVertex, TEdge> graph, [NotNull] TVertex vertex, double position, out TEdge successor) { if (graph is null) { throw new ArgumentNullException(nameof(graph)); } if (vertex == null) { throw new ArgumentNullException(nameof(vertex)); } IEnumerable <TEdge> edges = graph.OutEdges(vertex); return(TryGetSuccessor(edges, position, out successor)); }
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)); }
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 }
protected TEdge Successor(IImplicitGraph <TVertex, TEdge> g, TVertex u, double position) { double pos = 0; double nextPos = 0; foreach (TEdge e in g.OutEdges(u)) { nextPos = pos + this.weights[e]; if (position >= pos && position <= nextPos) { return(e); } pos = nextPos; } return(default(TEdge)); }
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 }
public IEnumerable <Edge> OutEdges(Vertex v) { return(_impl.OutEdges(v)); }
protected double GetOutWeight(IImplicitGraph <TVertex, TEdge> g, TVertex u) { var edges = g.OutEdges(u); return(GetWeights(edges)); }
/// <summary> /// Gets the weight of the given <paramref name="vertex"/> out edges. /// </summary> /// <param name="graph">Graph to consider.</param> /// <param name="vertex">Vertex to get out weight.</param> /// <returns>Out weight.</returns> protected double GetOutWeight([NotNull] IImplicitGraph <TVertex, TEdge> graph, [NotNull] TVertex vertex) { IEnumerable <TEdge> edges = graph.OutEdges(vertex); return(GetWeights(edges)); }
/// <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; }