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

相关推荐

  • Jlink或者stlink用于SWD接口下载程序

    Jlink或者stlink用于SWD接口下载程序最近要使用stm32f103c8t6最小系统板,直接ISP串口下载程序太麻烦,就想着使用swd接口来调试。结果:通过SWD接口下载程序成功,但调试失败,还不知原因,会的的人麻烦交流一下。SWD接口:3.3VDIO(数据)CLK(时钟)GND1.首先声明jlink和stlink都有jtag和swd调试功能。jlink接口如下:如图,我使用的就是VCC…

    2022年4月25日
  • Thread 设置 IsBackground true false 的 运行差别

    Thread 设置 IsBackground true false 的 运行差别1.这是一个后台线程,IsBackground=true,主线程完成后,后台子线程也停止了,即使子线程还有没运行完,也要停止classProgram{staticvoidMain(string[]args){Threadt=newThread(()=>{…

    2022年10月16日
  • Javascript对象归纳

    Javascript对象归纳

    2021年10月2日
  • inputstreamreader和inputstream_FileInputStream

    inputstreamreader和inputstream_FileInputStream我们的机器只会读字节码,而我们人却很难读懂字节码,所以人与机器交流过程中需要编码解码。InputStreamReader及其子类FileReader:(从字节到字符)是个解码过程;OutputStreamWrite及其子类FileWriter:(从字符到字节)是个编码过程。InputStreamReader这个解码过程中,最主要的就是StreamDecoder类 

    2022年9月26日
  • 无法解析的外部符号问题小结

    无法解析的外部符号问题小结问题1:在编写通信相关程序中,引用了一个静态库(该静态库编译没有问题,并被其他项目引用),该库是对SOCKET的一个封装。基本结构如下: 在属性中添加了该库的引用后,编译仍然报错,错误如下:safmq2005.lib(tcpsocket.obj):errorLNK2019:无法解析的外部符号_gethostbyname@4,该符号在函数”publ

    2022年6月28日
  • 微信聊天内容制作生成器微信小程序源码/支持多种制作生成[通俗易懂]

    ☑️编号:ym205☑️品牌:小程序☑️语言:wx☑️大小:345KB☑️类型:聊天内容制作☑️支持:小程序????欢迎免费领取(注明编号)????✨源码介绍这是一款微信聊天内容制作生成小程序源码,该小程序支持制作多种内容。支持单人聊天模式制作,支持群聊模式制作生成;每一种模式都支持我们微信需要的功能都有,视频,语音,时间,内容等等,大家可以最后看演示图!!另外还支持微信零钱,也就是我的界面制作生成DIY金额(具体大家看演示图);另外也支持微信红包制作DIY金额,发

    2022年4月9日

发表回复

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

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