/// <summary> /// Calculate a PageRank object's PageRank Score using the formula provided by Google's founders Larry Page and Sergey Brin. /// This algorithm corresponds with the algorithm originally created by Massimo Marchiori and also is defined as a Markov Chain. /// </summary> /// <param name="pr">PageRank object</param> /// <returns></returns> private double CalculatePageRankScore(PageRank pr) { double left = (1 - this.m_Damper); int outgoingLinkAmount = pr.OutgoingLinkCount; if (outgoingLinkAmount > 0) { return(left + this.m_Damper * (pr.Score / outgoingLinkAmount)); } return(left); }
/// <summary> /// Do the iterative calculation of the graph's pages PageRank score. /// Hide this from the consumer of this class. /// </summary> /// <param name="_graph">A directed graph object</param> /// <param name="_damping">A value between 0 and 1</param> private void CalculatePageRank(IGraph _graph, double _damping = 0.85) { // Make sure it's within the bounds: 0 <= d <= 1 if (_damping < 0 || _damping > 1) { _damping = m_Damper; } // Create initial page rankings foreach (IEdge <WebPageInfo> edge in _graph.Edges) { // We only need to process any URL once... // Therefore if only add a URL to the PageRankList if it's not in the list! if (!this.PageRankList.Any <PageRank>((PageRank prs) => Helper.IsUriSame(prs.Url, edge.Source.Url))) { this.PageRankList.Add(edge.Source); } // Same with target URL's if (!this.PageRankList.Any <PageRank>((PageRank prt) => Helper.IsUriSame(prt.Url, edge.Target.Url))) { this.PageRankList.Add(edge.Target); } } double tempTotal = 0.0; Boolean isDone = false; // Iterate until isDone is satisfied (TotalScore >= 1.0) while (!isDone) { for (int i = 0; i < this.PageRankList.Count; i++) { PageRank pr = this.PageRankList[i]; Debug.Assert(!Double.IsNaN(pr.Score)); // Make sure it's a number pr.Score = CalculatePageRankScore(pr); // Update the PageRank Score tempTotal += pr.Score; // TotalScore; } isDone = tempTotal >= 1.0; // Done yet? if (isDone) { // If done, then save the temporary total score in the global instance variable TotalScore = tempTotal; } tempTotal = 0; // Reset temporary total score for this iteration! IterationCount++; // Increment by 1 after each iteration } }