使用C#实现PageRank算法
using System;
using System.Collections.Generic;
using System.IO;
namespace EXP2_C
{
class PageRank
{
public int N;
private double AFA;
private double DELTA;
private double MAX_TIMES;
List<List<int>> graph;
public PageRank(int N, double AFA, double DELTA, double MAX_TIMES)
{
this.N = N;
this.AFA = AFA;
this.DELTA = DELTA;
this.MAX_TIMES= MAX_TIMES;
}
public void pagerank()
{
double[] Prnew = new double[N];
for (int i = 0; i < N; i++)
{
Prnew[i] = 1.0 / N;
}
double[] Pr;
//迭代至|Pn+1−Pn|<ϵ
for (int i = 1; i < MAX_TIMES; i++)
{
//保留迭代前的Pr
Pr = Prnew;
//迭代后
Prnew = get_Prnew(graph, Pr);
double delta = get_DELTA(Prnew, Pr);
Console.WriteLine("第" + i + "次迭代完成,DELTA = " + delta);
if (delta < DELTA)
break;
}
showResult(Prnew);
}
private void showResult(double[] Prnew)
{
//打印前十个pr节点
Console.WriteLine("开始计算前五节点...");
double[][] big = getBiggestPr(Prnew);
for (int j = 0; j < 5; j++)
{
Console.WriteLine("第" + (j + 1) + "大节点, node: " +( Convert.ToInt32(big[