首先用邻接矩阵A表示从页面j到页面i的概率,然后根据公式生成转移概率矩阵
M=(1-d)*Q+d*A 常量矩阵Q=(qi,j),qi,j=1/n
给定点击概率d,等级值初始向量R0,迭代终止条件e;
计算Ri+1=M*Ri;
ei=|Ri+1-Ri|,当ei<=e时输出Ri+1作为最终等级值向量。
#include "fstream"
#include "sstream"
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int getNum()
{
set<int> N;
ifstream is("a.txt");
int u, v;
while (is >> u >> v)
{
N.insert(u);
N.insert(v);
}
return N.size();
}
void getPage(vector<vector<int>>& page) //&
{
ifstream is("a.txt");
int u, v;
while(is>>u>>v)
{
page[u].push_back(v);
}
}
void PageRank(vector<vector<int>>&page,int N,double d,double e,int maxIterations)
{
//得到M
vector<vector<double>> M(N,vector<double>(N,0.0));
for (int i = 0; i < N; i++)
{
if (page[i].size() > 0)
{
for (int j : page[i])
{
M[j][i]++; //0/1
}
for (int j=0;j<N;j++)
{
M[j][i] /= page[i].size();
}
}
else
{
for (int j : page[i])
{
M[j][i] = 0;
}
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
//cout << M[i][j]<<" "; //正确
M[i][j] = d * M[i][j] + (1 - d) * (1.0 / N);
//cout << M[i][j] << " ";
}
}
vector<double> pagerank(N, 1.0);
while (maxIterations)
{
vector<double> newpagerank(N, 0.0);
double dif=0.0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
newpagerank[i] += M[i][j] * pagerank[j];
}
dif+= abs(pagerank[i]-newpagerank[i]);
}
//cout << dif << endl;
pagerank = newpagerank;
if (dif < e)
{
//cout << maxIterations<<endl;
break;
}
maxIterations--;
}
cout << "等级值分别为:" << endl;
for (int i = 0; i < N; i++)
{
cout << pagerank[i]<<endl;
}
}
int main()
{
int num = getNum();
vector<vector<int>> page(num);
getPage(page);
double d = 0.85;
double e = 0.1;
int maxIterations = 100;
PageRank(page, num, d, e, maxIterations);
}
数据
0 1
0 2
0 3
1 0
1 2
2 3
3 0
3 1
结果