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


相关推荐

  • JMESPath_jsonpath的用法

    JMESPath_jsonpath的用法前言JMESPath是JSON的查询语言。您可以从JSON文档中提取和转换元素官方文档:https://jmespath.org/tutorial.html基本表达式JMESPath用的最多的

    2022年7月31日
    7
  • itoa函数,srpintf()函数 ,atoi函数

    itoa函数,srpintf()函数 ,atoi函数

    2021年9月27日
    85
  • pycharm调试详解

    pycharm调试详解pycharm 调试按钮的意思 1 单击打断点 2 右键选择 debug3 运行出现以下的界面 4 竖排按钮的含义 5 横排按钮的含义参考博客

    2025年11月24日
    4
  • Ubuntu18.04安装Anaconda+PyCharm+VS Code[通俗易懂]

    Ubuntu18.04安装Anaconda+PyCharm+VS Code[通俗易懂]PyCharm安装、桌面快捷方式建立及设置中文输入

    2022年8月28日
    3
  • c++中无法打开源文件_无法打开源文件iostream

    c++中无法打开源文件_无法打开源文件iostream一、无法打开文件“xxx.lib”出现这种错误一般为①未添加xxx.lib库文件②库添加后,路径不对,找不到对应的库文件路径解决方案:先查看库文件是否已经添加若未添加,右击项目->属性->链接器->输入;将库文件加入即可如果库文件已经添加,仍然报错,此时需要查看生成的库文件的路径了。先找到生成库文件的路径,右击项目->属性->常规->查看输出目录是否与生成的库文件的路径是否匹配,若不匹配,修改路径即可。二、无法打开源文件说明是库的附加包含路径有问题

    2022年10月14日
    5
  • 信贷风控模型搭建及核心风控模式分类

    信贷风控模型搭建及核心风控模式分类一、当前风控模式现状近年来,信用风险管理发展呈现出数据化、模型化、系统化、自动化和智能化的特点。传统的人工专家经验正逐步被模型与算法替代。因此,科技较为领先的金融服务公司会选择采用模型方式完成对借款人的自动评估与审批。目前,对于信贷审核来说主要基于的风控模式为IPC、信贷工厂、大数据三种,每一种都有自己不同的侧重点。二、最核心的风控模式分类1.IPC模式IPC模式起源于德国邮储银行,该模…

    2022年5月1日
    62

发表回复

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

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