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


相关推荐

  • TypeScript高级类型-Partial

    TypeScript高级类型-PartialTypeScript高级类型-Partial预备知识:TypeScript类型系统接口泛型先来看一下Partial类型的定义/***MakeallpropertiesinToptional*/typePartial<T>={[PinkeyofT]?:T[P];};假设我们有一个定义user的接口,如下int…

    2025年7月10日
    2
  • 解决项目中java heap space的问题[通俗易懂]

    解决项目中java heap space的问题[通俗易懂]起因 17年的一个项目出了OOM(javaheapspace)问题,眼下有个问题:法院项目,不能外网,一连接外网高院会直接定位到计算机,发出警报(档案的机密性啊)不能远程,那只能视频教他们怎么做了,全程和一个文员说代码,真的很累==! 过程 这个过程对一个不太了解内存的问题的开发无疑是艰难的,搜了一下,知道了是内存溢出导致的,于是着手解决 网上大多数都说调整运行…

    2022年7月8日
    23
  • Error 1962:No operating system found. Boot sequence will automatically repeat.–解决办法

    Error 1962:No operating system found. Boot sequence will automatically repeat.–解决办法此问题的解决办法为 在这里插入图片描述

    2025年6月12日
    0
  • python面试题目及答案(数据库常见面试题及答案)

    Python是目前编程领域最受欢迎的语言。在本文中,我将总结Python面试中最常见的50个问题。每道题都提供参考答案,希望能够帮助你在2019年求职面试中脱颖而出,找到一份高薪工作。这些面试题涉及Python基础知识、Python编程、数据分析以及Python函数库等多个方面。Q1、Python中的列表和元组有什么区别?Q2、Python的主要功能是什么?Python是一种解释型…

    2022年4月17日
    70
  • SecureCRT显示乱码的解决办法

    SecureCRT显示乱码的解决办法SecureCRT是一款支持SSH的终端仿真程序,用于连接运行包括Windows、UNIX和VMS的工具。对于学ARM的人来说,这个软件也是十分的好用!下面来看看SecureCRT的显示问题,如果没有设置好,那么就会出现乱码这种情况。比如:我发现在连接Linux系统之后,因为我装的是中文版的Linux系统,所以在显示中文的时候,SecureCRT显示出乱码。原因在于我们的Linux

    2022年7月17日
    45
  • 三角函数公式和图像大全[通俗易懂]

    三角函数公式和图像大全[通俗易懂]初等函数的图形幂函数的图形指数函数的图形对数函数的图形三角函数的图形反三角函数的图形各三角函数值在各象限的符号三角函数的性质反三角函数的性质三角函数公式两角和公式倍角公式三倍角公式半角公式和差化积积化和差诱导公式万能公式其它公式其他非重点三角函数双曲函数公式一…

    2022年9月14日
    0

发表回复

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

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