PageRank算法C++代码实现标准版

对于PageRank算法,维基百科和网上很多大牛的博客已经讲得很详细了,这里附上一个自己写的PageRank算法C++实现版本

大家好,又见面了,我是你们的朋友全栈君。

对于PageRank算法,维基百科和网上很多大牛的博客已经讲得很详细了,这里附上一个自己写的PageRank算法C++实现版本:

/*
* Author: YANG Xiangyu
* The Chinese university of Hong Kong
*/
#include<cstdio>
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
#define MAX 1000000
struct edge	//define edge
{
	int u;
	int v;
}edge[5200000];

int rednum[MAX]={0};	//to mark a point that if it has been visited, and record a new number
int orinum[MAX]={0};	//to record the original number of the new recorded number
int d[MAX]={0};		//to mark the out degree of the point
double ra[MAX]={0};		//to mark the current rank value of the point
double rb[MAX]={0};		//to mark the updated rank value of the point

int cmp(const int &a, const int &b)
{
	if(ra[rednum[a]]>ra[rednum[b]])return 1;
	return 0;
}
void pagerank()
{
	ifstream fin("D:\\web-Google.txt");//If TA want to test my code, please take the text 'web-google.txt' to the D disk.
	ofstream fout("D:\\output.txt");
	memset(edge,0,sizeof(edge));
	string s;
	for(int i=0;i<4;++i)
	{getline(fin,s);cout<<s<<endl;}//Read the first four lines of the input file
	int ncnt=0;
	int ecnt=0;
	int cnt=0;
	double eps=0.1;
	double flag;
	int i;
	for(i=0;fin>>edge[i].u>>edge[i].v;++i)//input the two point of each edge
	{	
		if(!rednum[edge[i].u]) //judge the point whether it has been visited 
		{
			rednum[edge[i].u]=ncnt;//redefine the number of current point
			orinum[ncnt++]=edge[i].u;//record the original number of current point
		}
		if(!rednum[edge[i].v]) //judge the point whether it has been visited
		{
			rednum[edge[i].v]=ncnt;//redefine the number of current point
			orinum[ncnt++]=edge[i].v;//record the original number of current point
		}
		d[rednum[edge[i].u]]++;
	}
	ecnt=i;
	printf("%d %d\n",ncnt,ecnt);
	for(i=0;i<ncnt;++i)
		ra[i]=(double)1/ncnt;
	while(eps>0.0000001)//set ε=10^(-7), control the number of iterations
	{
		printf("%d %.7lf\n",cnt,eps);
		eps=0;
		cnt++;
		for(int i=0;i<ecnt;++i)
			rb[rednum[edge[i].v]]+=ra[rednum[edge[i].u]]/d[rednum[edge[i].u]]; //first step to initialize the rank value
		for(int i=0;i<ncnt;++i)
		{
			rb[i]=rb[i]*0.8+(0.2*1/ncnt); //add the random jumping coefficient β, and set β=0.8
			eps+=ra[i]>rb[i]?(ra[i]-rb[i]):(rb[i]-ra[i]);//compute the Difference between the old rank value and new rank value, and update the ε
			ra[i]=rb[i];
			rb[i]=0;
		}
	}
	sort(orinum,orinum+ncnt,cmp);//sort the array according to the score
	for(int i=0;i<100;++i)
		fout<<orinum[i]<<' '<<ra[rednum[orinum[i]]]<<endl;
}
int main()
{
	pagerank();
	return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/126142.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 推荐好用的Linux远程连接工具

    推荐好用的Linux远程连接工具在连接 linux 的时候用了很多工具 XshellSecure 还有很多其他的其中 XshellSecure 都不是免费的 当然有办法激活成功教程 这个在网上一大堆 就不再说了 putty 非常轻量级也很好用 也是最常用的 但是我在此推荐两款更好用的 国产的 FinalShell 和 MobaXterm 一 FinalShell 网址 http www hostbuf com 这个软件很强大可以实时查看 c

    2025年9月1日
    4
  • LOTO课5:三极管音频放大电路实践[通俗易懂]

    LOTO课5:三极管音频放大电路实践[通俗易懂]我们在项目中经常会遇到音频信号的采集处理,我们今天做一个最简单的音频采集模块。它的电路其实就是在我们上节课的三极管的放大电路上的一个改进,在上一节课三极管放大电路的基础之上,将输出信号换成驻极体话筒,输出端加上截止频率在20KHZ左右的RC低通滤波电路,通过滤波电路来滤除频率在20KHZ以上的噪声信号。上一节课关于三极管放大的文章链接如下:添加链接描述设计的电路原理图如图所示,通过传感器获取一个交流的小信号,经过三级管放大电路放大信号之后再进行输出:等不及打板,手工焊接了一个样品进行试验:通过

    2022年5月3日
    74
  • Myeclipse7.5 下载 安装 注冊 注冊码 100%成功

    Myeclipse7.5 下载 安装 注冊 注冊码 100%成功

    2021年12月1日
    46
  • MyEclipse10安装SVN插件的几种方法

    MyEclipse10安装SVN插件的几种方法方法一:直接解压下载SVN插件:site-1.6.18.ziphttps://github.com/subclipse/subclipse解压后将features文件和plugins文件拷贝至:D:\MyEclipse\MyEclipse10\dropins(MyEclipse的安装目录)重启MyEclipse即可出现SVN!方法二:在线安装打开HELP->MyEclipseConfi

    2022年7月21日
    12
  • idea 2021.03.02 激活码(最新序列号破解)

    idea 2021.03.02 激活码(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    443
  • PCI和PCIE插槽有什么区别?[通俗易懂]

    PCI和PCIE插槽有什么区别?[通俗易懂]PCI是PeripheralComponentInterconnect(外设部件互连标准)的缩写,它是目前个人电脑中使用最为广泛的接口,几乎所有的主板产品上都带有这种插槽。PCI插槽也是主板带有最多数量的插槽类型,在目前流行的台式机主板上,ATX结构的主板一般带有5~6个PCI插槽,而小一点的MATX主板也都带有2~3个PCI插槽,可见其应用的广泛性。PCI是由Intel公司1991年推出的一

    2022年6月29日
    64

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号