Page RankTime Limit: 3000/1500 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 284 Accepted Submission(s): 79
Problem Description
Evaluation and rank of web pages is a hot topic for many internet companies and researchers. PageRank is a link analysis tool and it assigns a numerical weighting to each element of a hyperlinked set of documents, such as the World Wide Web, with the purpose of "measuring" its relative importance within the set. The algorithm may be applied to any collection of entities with reciprocal quotations and references. The numerical weight that it assigns to any given element E is referred to as the PageRank of E and denoted by . Other factors like Author Rank can contribute to the importance of an entity.
For simplicity, in this problem PageRank vector q is defined as q = Gq, Where , S is the destination-by-source stochastic matrix, U is all one matrix, n is the number of nodes and α is the weight between 0 and 1 (here we use 0.85). For the example on the right, we have: Denote the current PageRank vector and the next PageRank vector by q cur and q next respectively. The process is to compute the iterative powering for finding the first eigenvector. The computation ends until for some small ε(10 -10).
Input
The input contains many test cases.
For each case, there are multiple lines. The first line contains an integer N(N<=3000), which represents the number of pages. Then a N*N zero-one matrix follows. The element E ij (0 <= i, j < N) on the matrix represents whether the i-th page has a hyper link to the j-th page.
Output
Output one line with the eigenvector. The numbers should be separated by a space and be correctly rounded to two decimal places.
Sample Input
Sample Output
|
为了方便, 我用【S】表示S是一个矩阵,q[]表示列向量。
题意:给定公式【G】 = 0.85 * 【S】 + 0.15 * 【U】 / N 和 。其中【U】矩阵所有元素均是1。
给你一个只含0和1的矩阵【T】,【T】可以转化成【S】。
转化遵循
一、若矩阵【T】的第i行有num个1,那么若T[i][j] = 1,则有S[j][i] = 1 / num。
二、若T[i][j] = 0,则有S[j][i] = 0。
现在让你利用公式q[next] = 【G】 * q[now]一直求解新的列向量q,直到|q[next] q[now]| <= 1e-10为止。输出这时的q[next]。
初始的q[]列向量我选取的是全是1,应该有一个定理可以证明q向量的选取在某个范围里面都会收敛到同一个列向量。
思路:先求出矩阵S,再求出矩阵G。最后利用滚动数组思维模拟矩阵乘即可。
AC代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define MAXN 3001
#define eps 1e-10
using namespace std;
int N;
double G[MAXN][MAXN];
double q[2][MAXN];
void init()
{
char s[MAXN];
for(int i = 0; i < N; i++)
{
scanf("%s", s);
int num = 0;
for(int j = 0; j < N; j++)
if(s[j] == '1')
num++;
for(int j = 0; j < N; j++)
{
if(s[j] == '1')
G[j][i] = 0.85 / num;
else
G[j][i] = 0.0;
G[j][i] += 0.15 / N;
}
}
}
bool judge()
{
double sum = 0.0;
for(int i = 0; i < N; i++)
sum += (q[0][i] - q[1][i]) * (q[0][i] - q[1][i]);
return sqrt(sum) > eps;
}
void solve()
{
for(int i = 0; i < N; i++)
{
q[0][i] = 1.0;
q[1][i] = 0.0;
}
int k = 0;
while(judge())
{
for(int i = 0; i < N; i++)
{
q[k^1][i] = 0.0;
for(int j = 0; j < N; j++)
q[k^1][i] += G[i][j] * q[k][j];
}
k ^= 1;
}
for(int i = 0; i < N; i++)
{
if(i) printf(" ");
printf("%.2lf", q[k][i]);
}
printf("\n");
}
int main()
{
while(scanf("%d", &N) != EOF)
{
init();//构造G矩阵
solve();
}
return 0;
}