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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 伯努利分布、二项式分布与多项式分布简介「建议收藏」

    伯努利分布、二项式分布与多项式分布简介「建议收藏」一,伯努利分布(bernoulidistribution)又叫做0-1分布,指一次随机试验,结果只有两种。也就是一个随机变量的取值只有0和1。记为:0-1分布或B(1,p),其中p表示一次伯努利实验中结果为正或为1的概率。 概率计算:P(X=0)=p0P(X=1)=p1期望计算:E(X)=0∗p0+1∗p1=p最简单的例子就是,抛一次硬币,预测…

    2022年10月11日
    0
  • 测试用例设计的八大要素「建议收藏」

    测试用例设计的八大要素「建议收藏」1、测试用例的八大要素用例编号和其他编号一样,测试用例编号是用来唯一识别测试用例的编号,要求具有易识别和易维护性,用户可以很容易根据用例编号获取到相应用例的目的和作用,在系统测试用例中,编号的一般格式为A-B-C-D这几部分的作用分别如下:A:产品或项目类型,如CMS(内容管理系统)、CRM(客户关系管理系统)B:一般用来说明用例的属性,如ST(系统测试)、IT(集成测试)、UT(单元测试)C:测试需求的表示,说明该用例针对的需求点,可包括测试项和测试子项等,如文档管理、客户投诉信息管理等。

    2022年6月28日
    32
  • FastJson TypeReference 缓存「建议收藏」

    FastJson TypeReference 缓存「建议收藏」一直用FastJson做rest接口的序列化,FastJson对泛型的支持也非常好。经过一段时间使用后发现不定时的会报JsonObjectcan’tcovertto****的错误,但是重启之后就好了。排查过程不赘述,直接上代码演示StringitemJsonStr=&quot;{\&quot;models\&quot;:{\&quot;_defaultModel\&quot;:{\&quot;id\&quot;:824,\&q

    2022年6月18日
    52
  • BLSP接口_jcom接口

    BLSP接口_jcom接口http://huaqianlee.github.io/2016/04/27/Uav/Qualcomm-uav-blsp-port/概述BLSP是高通对于低速接口的一种管理方式,8074平台含有两个BLSP(BAMLow-SpeedPeripheral)块,对应于12个BLSP端口。每一个BLSP块含有最多六个QualcommUniversalPe

    2022年10月19日
    0
  • 基于PS2手柄的Arduino遥控小车

    基于PS2手柄的Arduino遥控小车前言本文利用PS2手柄和Arduino开发板制作了一个简易的遥控小车,利用蓝牙进行通信,可以实现前后左右的移动。(原理掌握之后可以自己拓展相关功能)一、零件1.ArduinoUNO开发板:ArduinoUNO是ArduinoUSB接口系列的最新版本,作为Arduino平台的参考标准模板。UNO的处理器核心是ATmega328,同时具有14路数字输入/输出口(其中6路可作为PWM输出),6路模拟输入,一个16MHz晶体振荡器,一个USB口,一个电源插座,一个ICSPheader和一个复位按钮。2

    2022年6月12日
    40
  • 如何将Java完全卸载

    之前安装的Java没有卸载干净,造成重新安装JDK能正常安装,接着安装JRE的时候总是报1603错误。虽然说JRE安装报错了没安装上,但是eclipse、IntelliJIDEA和AndroidStudio都能正常打开和使用,然而在命令行里却无法使用。虽然工具能正常打开,但是这不能忍,为此我差点就直接使用狂暴AOE秒杀大招重装系统了,还好,最后解决了。在这里,我分享一下我是如何解决的,有需要的小…

    2022年4月3日
    1.4K

发表回复

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

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