hdoj 5097 Page Rank 【矩阵 模拟】



Page Rank

Time 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
      
      
4 0111 0011 0001 0100
 

Sample Output
      
      
0.15 1.49 0.83 1.53
 



为了方便, 我用【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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值