• 大小: 8.1MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-10-10
  • 语言: C/C++
  • 标签: pageRank  

资源简介

c++ 实现PageRank算法。效率较高。 需要进行的遍历次数 (TN+1)(N+L) 需要存储的内容:结果向量PR,临时变量I,规模各自为N;临时变量S,规模为TN 参数取值:通常取α=0.15,TN=20~30 约5分钟可出结果。

资源截图

代码片段和文件信息

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
set pages;
set noOutDegree;
map outDegree;
map inDegree;
map pageRank;
map I;
vector text;
double s = 0;
const int TN = 30;
int n;
double alpha = 0.15;
bool cmp(const pair& xconst pair& y)  
{  
return x.second>y.second;  
}  
void sortMapByValue(map& tMapvector>& tVector)  
{  
for(map::iterator curr=tMap.begin();curr!=tMap.end();curr++)  
{  
tVector.push_back(make_pair(curr->firstcurr->second));  
}  
sort(tVector.begin()tVector.end()cmp);  
}  
int main()
{
ifstream fin(“wt2g_inlinks.source“);
ofstream fout(“result.out“);
if (!fin)
{
cout << “文件不存在!“ << endl;
exit(-1);
}
string tmpString = ““;
string srcdes;
while(getline(fintmpString))
{
text.push_back(tmpString);
istringstream isstream(tmpString);
isstream >> src >> des;
//cout << tmpString << endl;
//cout << src << “ “ << des << endl;
pages.insert(src);
pages.insert(des);
outDegree[src] ++;
inDegree[des] ++;
}

n = pages.size();
// initialize the variables
set::iterator it;
for (it = pages.begin();it != pages.end();it ++)
{
pageRank[*it] = 1.0/n;
I[*it] = alpha/n;
//fout << iter->first << “  “ << iter->second << endl;
if (outDegree[*it] == 0)
{
noOutDegree.insert(*it);
}
}
for (int i = 0;i < TN;i ++)
{
cout << i << endl;
s = 0;
for (int j = 0;j < text.size();j ++)
{
istringstream iss(text[j]);
iss >>  src >> des;
I[des] += (1-alpha)*pageRank[src]/outDegree[src];
}
set::iterator iter;
for (iter = noOutDegree.begin (); iter != noOutDegree.end(); iter ++)
{
s += pageRank[*iter];
}
for (it = pages.begin();it != pages.end();it ++)
{
pageRank[*it] = I[*it] + (1-alpha)*s/n;
I[*it] = alpha/n;
}
}
cout << “begin sort“ << endl;
vector> tVector;
sortMapByValue(pageRanktVector);
fout << setw(16) << “网页“ << setw(16) << “PageRank“ << setw(8) << “入链接数“ << setw(8) << “出链接数“ << endl;
double ans = 0;
for (int i = 0;i < tVector.size();i ++)
{
ans += tVector[i].second ;
fout << setw(16) << tVector[i].first << setw(16) << tVector[i].second << setw(8) << inDegree[tVector[i].first] << setw(8) << outDegree[tVector[i].first] << endl;
}
fout << “sum of PageRank: “ << ans << endl;
fin.close();
fout.close();
cout << “finished“ << endl;
return 0;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件   28508160  2012-04-23 14:20  PageRank\ipch\pagerank-f98ca78f\pagerank-e0f8872f.ipch

     文件       2725  2012-04-23 14:18  PageRank\PageRank\PageRank.cpp

     文件       3922  2012-04-22 16:54  PageRank\PageRank\PageRank.vcxproj

     文件        946  2012-04-22 16:54  PageRank\PageRank\PageRank.vcxproj.filters

     文件        143  2012-04-22 16:52  PageRank\PageRank\PageRank.vcxproj.user

     文件          0  2012-04-23 14:19  PageRank\PageRank\result.out

     文件   30545013  2012-04-08 22:09  PageRank\PageRank\wt2g_inlinks.source

     文件    9719808  2012-04-23 14:20  PageRank\PageRank.sdf

     文件        891  2012-04-22 16:52  PageRank\PageRank.sln

    ..A..H.     10240  2012-04-23 14:20  PageRank\PageRank.suo

     文件         34  2012-04-23 14:18  PageRank\readme.txt

     文件      57344  2012-04-23 14:19  PageRank\Release\PageRank.exe

     文件     863232  2012-04-23 14:19  PageRank\Release\PageRank.pdb

     目录          0  2012-04-23 14:20  PageRank\ipch\pagerank-f98ca78f

     目录          0  2012-04-23 14:20  PageRank\ipch

     目录          0  2012-04-23 14:20  PageRank\PageRank

     目录          0  2012-04-23 14:19  PageRank\Release

     目录          0  2012-04-23 14:20  PageRank

----------- ---------  ---------- -----  ----

             69712458                    18


评论

共有 条评论