您的位置:首页 > 其它

HDU 5097 Page Rank(矩阵模拟)——2014上海全国邀请赛——题目重现(感谢上海大学提供题目)

2016-10-12 20:16 471 查看
传送门

Page Rank

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 346 Accepted Submission(s): 105


[align=left]Problem Description[/align] 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 qcur and qnext respectively. The process is to compute the iterative powering for finding the first eigenvector.



The computation ends until

for some small ε(10-10).
[align=left]Input[/align] 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 Eij (0 <= i, j < N) on the matrix represents whether the i-th page has a hyper link to the j-th page.
[align=left]Output[/align] Output one line with the eigenvector. The numbers should be separated by a space and be correctly rounded to two decimal places.
[align=left]Sample Input[/align]
4

0111

0011

0001

0100


题目大意:

说实话,这个题目最难的应该是题意了把,这个题目读了很多次都没有读懂,首先读一下题目大意,然后大体了解一下背景,主要是那个公式:然后,我说一下公式的每隔变量都代表啥: G =α∗S+(1−α)∗1n∗U,其中 α 是一个常数 等于 0.85,然后 S 是一个 n∗n 的矩阵,怎么算的呢,首先题目输入中给了我们一个 n∗n 的01矩阵,然后我们通过这个矩阵来构造出 S 来,首先将输入的矩阵中每一行的 1 的个数 cnt 求出来,然后在 S 中就是 1/cnt ,然后将行列转置,输入矩阵 1 对应的行和列,就是 S 矩阵中对应的列和行,然后值就是 1/cnt,限制再来说一下 n ,n是输入的时候给定的, U 是一个全是 1 的矩阵,当时我是怎么都没读出来这个意思 U is all one matrix 我理解的就是 这个是一个矩阵,唉,这英语水平绝了,我们知道了这个之后就是让我们求一个特征向量,然后满足 qnext=G∗qcur 最后当这个 q 收敛了就不用算了。

解题思路:

其实读懂了题目就很简单了,关键是题目的意思,就是按照题目的意思进行模拟就行了,处理到 qnext−qcur<eps就行了(其实也可以就是循环 ≥20 次就行了)。

My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
const int MAXN = 3e3+5;
const double eps = 1e-10;
double a[MAXN][MAXN];
char str[MAXN][MAXN];
int n;
bool Judge(double qcur[], double qnext[]){
double sum = 0;
for(int i=0; i<n; i++)
sum += fabs(qcur[i]-qnext[i]);
if(sum < eps)
return 0;
return 1;
}
double qcur[MAXN], qnext[MAXN];
int main()
{
while(~scanf("%d",&n)){
for(int i=0; i<n; i++)
scanf("%s",str[i]);
memset(a, 0, sizeof(a));
memset(qcur, 0, sizeof(qcur));
memset(qnext, 0, sizeof(qnext));
double tmp = 0.85;///阿尔法
for(int i=0; i<n; i++){
double cnt = 0;
for(int j=0; j<n; j++)
if(str[i][j] == '1')
cnt++;
for(int j=0; j<n; j++)
if(str[i][j] == '1')
a[j][i] = tmp/cnt;
}
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
a[i][j] += 0.15/n;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
qcur[i] += a[i][j];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
qnext[i] += qcur[j] * a[i][j];
while(Judge(qcur, qnext)){
for(int i=0; i<n; i++)
qcur[i] = qnext[i];
memset(qnext, 0, sizeof(qnext));
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
qnext[i] += qcur[j] * a[i][j];
}
for(int i=0; i<n-1; i++)
printf("%.2f ",qcur[i]);
printf("%.2f\n",qcur[n-1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: