PageRank algorithm C + + code implementation Standard Edition

Source: Internet
Author: User

For the PageRank algorithm, Wikipedia and many of the online blogs of Daniel have been described in detail, here is a self-written PageRank algorithm C + + implementation version:

/** 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 1000000struct edge//define edge{int u;int v;} Edge[5200000];int rednum[max]={0};//to Mark a point the if it has been visited, and record a new Numberint orinum[max]={0 };//to record the original number of the new recorded Numberint d[max]={0};//to mark the out degree of the pointdouble ra[ Max]={0};//to mark the current rank value of the pointdouble rb[max]={0};//to mark the updated rank value of the Pointint CMP (const int &AMP;A, const int &b) {if (Ra[rednum[a]]>ra[rednum[b]]) return 1;return 0;}  void Solve () {ifstream fin ("d:\\web-google.txt");//if TA want to test my code, click the text ' Web-google.txt ' to the D disk.ofstream fout ("D:\\output.txt"); memset (edge); string edge,0,sizeof (int s;for) {i=0;i<4;++i (getline) ; cout<<s<<endl;} Read the first four lines of the input fileint ncnt=0;int ecnt=0;int cnt=0;double eps=0.1;double flag;int i;for (i=0;fin& Gt;>edge[i].u>>edge[i].v;++i)//input the 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 pointorinum[ncnt++]=edge[i].u;// Record the original number of current point}if (!REDNUM[EDGE[I].V])//judge the point whether it had been visited{rednum[ed Ge[i].v]=ncnt;//redefine the number of current Pointorinum[ncnt++]=edge[i].v;//record the original number of current Poin t}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) {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 valuefor (int i=0;i<ncnt;++i) {rb[i]=rb[i]*0.8+ (0.2*1/ncnt);//add the random Jumpin G COefficientβ, and Setβ=0.8eps+=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 scorefor (int i=0;i<100;++i) Fout<<orinum[i] << ' <<ra[rednum[orinum[i]]]<<endl;} int main () {solve (); return 0;}


PageRank algorithm C + + code implementation Standard Edition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.