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


相关推荐

  • 联想st510开卡软件_无力吐槽的一单联想ST510固态硬盘数据恢复

    联想st510开卡软件_无力吐槽的一单联想ST510固态硬盘数据恢复接苏州IT服务商客户送修一块联想ST510固态硬盘需要恢复数据,故障现象为SSD可以正常识别,而且识别的速度也是很快的!,我们接上PC3000访问第一扇区显示代码是错误的,然后读取其它扇区就BSY状态了,必须从新断电加电才可以读取!(从经验判断这块SSD的主控应该是SM2258XT或SM2256K,PC3000SSD加载恢复的速度正常是8M每秒左右)由于这块硬盘转手次数太多(起码转了4手)也没…

    2022年9月2日
    5
  • Java对象转换Map(工具类)[通俗易懂]

    Java对象转换Map(工具类)[通俗易懂]/***@Description//TODOMap工具类*@Date2020/5/79:54*@Authorhuangwb**/publicclassMapUtils{/***@returnvoid*@Authorhuangwb*@Description//TODO对象转换成map*…

    2022年6月8日
    41
  • IDEA中,java项目无法使用Test测试的解决办法

    IDEA中,java项目无法使用Test测试的解决办法一、IDEA使用junit的@Test注解报错1、File–ProjectStructure–Modules2、点击加号3、选择JARsordirectories…4、在idea的安装路径下的lib文件夹,选中两个jar包5、然后勾选上,点击Apply–ok二、使用junit无法在控制台进行输入1、Help–EditCustomVMOptions..2、添加代码-Deditable.java….

    2022年10月17日
    3
  • 华为交换机关闭网口_华为交换机如何关闭端口号

    华为交换机关闭网口_华为交换机如何关闭端口号华为交换机怎样把端口从vlan中删除??答:通过displayvlan查看当前vlan列表通过displayvlanvlan-id比如displayvlan100,查看vlan100的状态,里面列出vlan100里有哪些端口,有哪些端口为untagged或者tagged也可以通过displaycur查看配置来得出还有查看端口状态displayinterface。通过display…

    2022年7月20日
    160
  • jq中ajax的dataType:”json”是指什么?

    jq中ajax的dataType:”json”是指什么?dataType String预期服务器返回的数据类型。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息来智能判断,比如XML MIME类型就被识别为XML。在1.4中,JSON就会生成一个JavaScript对象,而script则会执行这个脚本。随后服务器端返回的数据会根据这个值解析后,传递给回调函数。可用值:"xml": 返回 XML 文档,可用 jQuery 处理。"…

    2022年6月13日
    100
  • awk命令详解+示例

    awk命令详解+示例WK数据过滤工具(类似于grep,比grep强大)Awk编程语言/数据处理引擎创造者:Aho、Weinberger、Kernighan基于模式匹配检查输入文本,逐行处理并输出通常用在Shell脚本中,获取指定的数据,单独使用时,可对文本数据做统计#whichawk#rpm-qf/bin/awk语法格式:格式1:前置命令|awk[选项]‘条…

    2022年7月11日
    23

发表回复

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

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