Example #1
0
/*
 *      public void RemoveDanglingLinks()
 *      {
 *          VertexCollection danglings = new VertexCollection();
 *          do
 *          {
 *              danglings.Clear();
 *
 *              // create filtered graph
 *              IVertexListGraph fg = new FilteredVertexListGraph(
 *                  this.VisitedGraph,
 *                  new InDictionaryVertexPredicate(this.ranks)
 *                  );
 *
 *              // iterate over of the vertices in the rank map
 *              foreach (IVertex v in this.ranks.Keys)
 *              {
 *                  // if v does not have out-edge in the filtered graph, remove
 *                  if (fg.OutDegree(v) == 0)
 *                      danglings.Add(v);
 *              }
 *
 *              // remove from ranks
 *              foreach (IVertex v in danglings)
 *                  this.ranks.Remove(v);
 *              // iterate until no dangling was removed
 *          } while (danglings.Count != 0);
 *      }
 */
        protected override void InternalCompute()
        {
            var cancelManager = this.Services.CancelManager;
            IDictionary <TVertex, double> tempRanks = new Dictionary <TVertex, double>();

            // create filtered graph
            FilteredBidirectionalGraph <
                TVertex,
                TEdge,
                IBidirectionalGraph <TVertex, TEdge>
                > fg = new FilteredBidirectionalGraph <TVertex, TEdge, IBidirectionalGraph <TVertex, TEdge> >(
                this.VisitedGraph,
                new InDictionaryVertexPredicate <TVertex, double>(this.ranks).Test,
                e => true
                );

            int    iter  = 0;
            double error = 0;

            do
            {
                if (cancelManager.IsCancelling)
                {
                    return;
                }

                // compute page ranks
                error = 0;
                foreach (KeyValuePair <TVertex, double> de in this.Ranks)
                {
                    if (cancelManager.IsCancelling)
                    {
                        return;
                    }

                    TVertex v    = de.Key;
                    double  rank = de.Value;
                    // compute ARi
                    double r = 0;
                    foreach (var e in fg.InEdges(v))
                    {
                        r += this.ranks[e.Source] / fg.OutDegree(e.Source);
                    }

                    // add sourceRank and store
                    double newRank = (1 - this.damping) + this.damping * r;
                    tempRanks[v] = newRank;
                    // compute deviation
                    error += Math.Abs(rank - newRank);
                }

                // swap ranks
                var temp = ranks;
                ranks     = tempRanks;
                tempRanks = temp;

                iter++;
            } while (error > this.tolerance && iter < this.maxIterations);
            Console.WriteLine("{0}, {1}", iter, error);
        }
Example #2
0
        public void InEdges_Throws()
        {
            var graph1         = new BidirectionalGraph <EquatableTestVertex, Edge <EquatableTestVertex> >();
            var filteredGraph1 = new FilteredBidirectionalGraph
                                 <
                EquatableTestVertex,
                Edge <EquatableTestVertex>,
                BidirectionalGraph <EquatableTestVertex, Edge <EquatableTestVertex> >
                                 >(
                graph1,
                _ => true,
                _ => true);

            InEdges_NullThrows_Test(filteredGraph1);
            InEdges_Throws_Test(filteredGraph1);

            var graph2         = new BidirectionalGraph <int, Edge <int> >();
            var filteredGraph2 = new FilteredBidirectionalGraph <int, Edge <int>, BidirectionalGraph <int, Edge <int> > >(
                graph2,
                vertex => vertex < 4,
                _ => true);

            graph2.AddVertexRange(new[] { 1, 2, 3, 4, 5 });
            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            Assert.Throws <VertexNotFoundException>(() => filteredGraph2.InEdges(4));
            Assert.Throws <VertexNotFoundException>(() => filteredGraph2.InEdges(5));
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
Example #3
0
        public void TryGetEdges_Throws()
        {
            var filteredGraph = new FilteredBidirectionalGraph <TestVertex, Edge <TestVertex>, BidirectionalGraph <TestVertex, Edge <TestVertex> > >(
                new BidirectionalGraph <TestVertex, Edge <TestVertex> >(),
                _ => true,
                _ => true);

            TryGetEdges_Throws_Test(filteredGraph);
        }
Example #4
0
        public void ContainsVertex_Throws()
        {
            var filteredGraph = new FilteredBidirectionalGraph <TestVertex, Edge <TestVertex>, BidirectionalGraph <TestVertex, Edge <TestVertex> > >(
                new BidirectionalGraph <TestVertex, Edge <TestVertex> >(),
                vertex => true,
                edge => true);

            ContainsVertex_Throws_Test(filteredGraph);
        }
Example #5
0
        /// <inheritdoc />
        protected override void InternalCompute()
        {
            ICancelManager cancelManager            = Services.CancelManager;
            IDictionary <TVertex, double> tempRanks = new Dictionary <TVertex, double>();

            // Create filtered graph
            var filterGraph = new FilteredBidirectionalGraph <TVertex, TEdge, IBidirectionalGraph <TVertex, TEdge> >(
                VisitedGraph,
                new InDictionaryVertexPredicate <TVertex, double>(Ranks).Test,
                edge => true);

            int    iteration = 0;
            double error;

            do
            {
                if (cancelManager.IsCancelling)
                {
                    return;
                }

                // Compute page ranks
                error = 0;
                foreach (KeyValuePair <TVertex, double> pair in Ranks)
                {
                    if (cancelManager.IsCancelling)
                    {
                        return;
                    }

                    TVertex vertex = pair.Key;
                    double  rank   = pair.Value;

                    // Compute ARi
                    double r = 0;
                    foreach (TEdge edge in filterGraph.InEdges(vertex))
                    {
                        r += Ranks[edge.Source] / filterGraph.OutDegree(edge.Source);
                    }

                    // Add sourceRank and store it
                    double newRank = (1 - Damping) + Damping * r;
                    tempRanks[vertex] = newRank;

                    // Compute deviation
                    error += Math.Abs(rank - newRank);
                }

                // Swap ranks
                IDictionary <TVertex, double> temp = Ranks;
                Ranks     = tempRanks;
                tempRanks = temp;

                ++iteration;
            } while (error > Tolerance && iteration < MaxIterations);
        }
Example #6
0
        public void ContainsEdge_Throws()
        {
            var filteredGraph = new FilteredBidirectionalGraph <TestVertex, Edge <TestVertex>, BidirectionalGraph <TestVertex, Edge <TestVertex> > >(
                new BidirectionalGraph <TestVertex, Edge <TestVertex> >(),
                _ => true,
                _ => true);

            ContainsEdge_NullThrows_Test(filteredGraph);
            ContainsEdge_SourceTarget_Throws_Test(filteredGraph);
        }
Example #7
0
        public void Degree_Throws()
        {
            var graph         = new BidirectionalGraph <EquatableTestVertex, Edge <EquatableTestVertex> >();
            var filteredGraph = new FilteredBidirectionalGraph
                                <
                EquatableTestVertex,
                Edge <EquatableTestVertex>,
                BidirectionalGraph <EquatableTestVertex, Edge <EquatableTestVertex> >
                                >(
                graph,
                _ => true,
                _ => true);

            Degree_Throws_Test(filteredGraph);
        }
Example #8
0
        /*
         *      public void RemoveDanglingLinks()
         *      {
         *          VertexCollection danglings = new VertexCollection();
         *          do
         *          {
         *              danglings.Clear();
         *
         *              // create filtered graph
         *              IVertexListGraph fg = new FilteredVertexListGraph(
         *                  this.VisitedGraph,
         *                  new InDictionaryVertexPredicate(this.ranks)
         *                  );
         *
         *              // iterate over of the vertices in the rank map
         *              foreach (IVertex v in this.ranks.Keys)
         *              {
         *                  // if v does not have out-edge in the filtered graph, remove
         *                  if (fg.OutDegree(v) == 0)
         *                      danglings.Add(v);
         *              }
         *
         *              // remove from ranks
         *              foreach (IVertex v in danglings)
         *                  this.ranks.Remove(v);
         *              // iterate until no dangling was removed
         *          } while (danglings.Count != 0);
         *      }
         */
        protected override void InternalCompute()
        {
            var cancelManager = this.Services.CancelManager;
            IDictionary <TVertex, double> tempRanks = new Dictionary <TVertex, double>(ranks);

            // create filtered graph
            var fg = new FilteredBidirectionalGraph <TVertex, TEdge, IBidirectionalGraph <TVertex, TEdge> >(
                this.VisitedGraph,
                new InDictionaryVertexPredicate <TVertex, double>(ranks).Test,
                e => true
                );
            var inEdges    = fg.Vertices.ToDictionary(v => v, v => fg.InEdges(v).ToArray());
            var outDegrees = fg.Vertices.ToDictionary(v => v, v => fg.OutDegree(v));

            int    iter  = 0;
            double error = 0;

            do
            {
                // compute page ranks
                error = 0;
                Parallel.ForEach(tempRanks.ToArray(), de =>
                {
                    // compute ARi
                    double r     = 0;
                    var edges    = inEdges[de.Key];
                    int edgesInx = edges.Count() - 1;
                    while (edgesInx > -1)
                    {
                        var e = edges[edgesInx--];
                        r    += tempRanks[e.Source] / outDegrees[e.Source];
                    }

                    // add sourceRank and store
                    double newRank    = (1 - this.damping) + this.damping * r;
                    tempRanks[de.Key] = newRank;

                    // compute deviation
                    InterlockedAdd(ref error, Math.Abs(de.Value - newRank));
                });

                iter++;
            }while (error > this.tolerance && iter < this.maxIterations && !cancelManager.IsCancelling);

            ranks = tempRanks;
        }
Example #9
0
        /// <summary>
        /// Computes the PageRank over the <see cref="VisitedGraph"/>.
        /// </summary>
        public void Compute()
        {
            VertexDoubleDictionary tempRanks = new VertexDoubleDictionary();
            // create filtered graph
            FilteredBidirectionalGraph fg = new FilteredBidirectionalGraph(
                this.VisitedGraph,
                Preds.KeepAllEdges(),
                new InDictionaryVertexPredicate(this.ranks)
                );

            int    iter  = 0;
            double error = 0;

            do
            {
                // compute page ranks
                error = 0;
                foreach (DictionaryEntry de in this.Ranks)
                {
                    IVertex v    = (IVertex)de.Key;
                    double  rank = (double)de.Value;
                    // compute ARi
                    double r = 0;
                    foreach (IEdge e in fg.InEdges(v))
                    {
                        r += this.ranks[e.Source] / fg.OutDegree(e.Source);
                    }

                    // add sourceRank and store
                    double newRank = (1 - this.damping) + this.damping * r;
                    tempRanks[v] = newRank;
                    // compute deviation
                    error += Math.Abs(rank - newRank);
                }

                // swap ranks
                VertexDoubleDictionary temp = ranks;
                ranks     = tempRanks;
                tempRanks = temp;

                iter++;
            }while(error > this.tolerance && iter < this.maxIterations);
            Console.WriteLine("{0}, {1}", iter, error);
        }
Example #10
0
        public void InEdge_Throws()
        {
            var graph1 = new BidirectionalGraph <int, Edge <int> >();

            InEdge_Throws_Test(
                graph1,
                (vertexPredicate, edgePredicate) =>
                new FilteredBidirectionalGraph <int, Edge <int>, BidirectionalGraph <int, Edge <int> > >(
                    graph1,
                    vertexPredicate,
                    edgePredicate));

            var graph2         = new BidirectionalGraph <TestVertex, Edge <TestVertex> >();
            var filteredGraph2 = new FilteredBidirectionalGraph <TestVertex, Edge <TestVertex>, BidirectionalGraph <TestVertex, Edge <TestVertex> > >(
                graph2,
                _ => true,
                _ => true);

            InEdge_NullThrows_Test(filteredGraph2);
        }
Example #11
0
        public void Construction()
        {
            VertexPredicate <int>            vertexPredicate = _ => true;
            EdgePredicate <int, Edge <int> > edgePredicate   = _ => true;

            var graph         = new BidirectionalGraph <int, Edge <int> >();
            var filteredGraph = new FilteredBidirectionalGraph <int, Edge <int>, BidirectionalGraph <int, Edge <int> > >(
                graph,
                vertexPredicate,
                edgePredicate);

            AssertGraphProperties(filteredGraph, graph);

            graph         = new BidirectionalGraph <int, Edge <int> >(false);
            filteredGraph = new FilteredBidirectionalGraph <int, Edge <int>, BidirectionalGraph <int, Edge <int> > >(
                graph,
                vertexPredicate,
                edgePredicate);
            AssertGraphProperties(filteredGraph, graph, false);

            #region Local function

            void AssertGraphProperties <TVertex, TEdge, TGraph>(
                FilteredBidirectionalGraph <TVertex, TEdge, TGraph> g,
                TGraph expectedGraph,
                bool parallelEdges = true)
                where TEdge : IEdge <TVertex>
                where TGraph : IBidirectionalGraph <TVertex, TEdge>
            {
                Assert.AreSame(expectedGraph, g.BaseGraph);
                Assert.IsTrue(g.IsDirected);
                Assert.AreEqual(parallelEdges, g.AllowParallelEdges);
                Assert.AreSame(vertexPredicate, g.VertexPredicate);
                Assert.AreSame(edgePredicate, g.EdgePredicate);
                AssertEmptyGraph(g);
            }

            #endregion
        }