1 typedef unsigned char BYTE; 2 class CPageRank 3 { 4 public: 5 CPageRank(int nWebNum = 5, bool bLoadFromFile = false); 6 ~CPageRank(); 7 8 int Process(); 9 float *GetWeight(); 10 11 private: 12 void InitGraph(bool bLoadFromFile = false); 13 void GenerateP(); 14 BYTE *m_pu8Relation; //节点关系 i行j列表示j是否指向i 15 float *m_pf32P; //转移矩阵 16 17 //缓存 18 float *m_pf32Weight0; 19 float *m_pf32Weight1; 20 int *m_pl32Out; 21 int m_nNum; 22 float m_f32DampFactor; //阻尼系数 23 24 int m_nMaxIterTime; 25 float m_f32IterErr; 26 float *m_pf32OutWeight;//输出的最终权重 27 };
下面就是具体各个函数的实现了。
首先构造函数主要是初始化一些迭代相关变量,分配空间等,这里把生成节点指向图的工作也放在这里,支持直接随机生成和读取二进制文件两种方式。
1 CPageRank::CPageRank(int nWebNum, bool bLoadFromFile) 2 { 3 m_f32DampFactor = 0.85; 4 m_nMaxIterTime = 1000; 5 m_f32IterErr = 1e-6; 6 7 m_nNum = nWebNum; 8 m_pu8Relation = new BYTE[m_nNum * m_nNum]; 9 m_pf32P = new float[m_nNum * m_nNum]; 10 m_pf32Weight0 = new float[m_nNum]; 11 m_pf32Weight1 = new float[m_nNum]; 12 m_pl32Out = new int[m_nNum];//每个节点指向的节点数 13 14 InitGraph(bLoadFromFile); 15 }
析构函数自然就是释放内存了:
1 CPageRank::~CPageRank() 2 { 3 delete []m_pl32Out; 4 delete []m_pf32P; 5 delete []m_pf32Weight0; 6 delete []m_pf32Weight1; 7 delete []m_pu8Relation; 8 }
下面就是随机生成或读取文件产生节点指向关系,如果随机生成的话,会自动保存当前生成的图,便于遇到问题时可复现调试:
1 void CPageRank::InitGraph(bool bLoadFromFile) 2 { 3 FILE *pf = NULL; 4 if(bLoadFromFile) 5 { 6 pf = fopen("map.dat", "rb"); 7 if(pf) 8 { 9 fread(m_pu8Relation, sizeof(BYTE), m_nNum * m_nNum, pf); 10 fclose(pf); 11 return; 12 } 13 } 14 15 //建立随机的节点指向图 16 int i, j; 17 srand((unsigned)time(NULL)); 18 for(i = 0; i < m_nNum; i++) 19 { 20 //指向第i个的节点 21 for(j = 0; j < m_nNum; j++) 22 { 23 m_pu8Relation[i * m_nNum + j] = rand() & 1; 24 } 25 //自己不指向自己 26 m_pu8Relation[i * m_nNum + i] = 0; 27 } 28 29 pf = fopen("map.dat", "wb"); 30 if(pf) 31 { 32 fwrite(m_pu8Relation, sizeof(BYTE), m_nNum * m_nNum, pf); 33 fclose(pf); 34 } 35 }
既然已经产生了各个节点的关系了,那PageRank的核心思想就是根据关系,生成出上面的转移矩阵P:
1 void CPageRank::GenerateP() 2 { 3 int i,j; 4 float *pf32P = NULL; 5 BYTE *pu8Relation = NULL; 6 7 //统计流入流出每个节点数 8 memset(m_pl32Out, 0, m_nNum * sizeof(int)); 9 pu8Relation = m_pu8Relation; 10 for(i = 0; i < m_nNum; i++) 11 { 12 for(j = 0; j < m_nNum; j++) 13 { 14 m_pl32Out[j] += *pu8Relation; 15 pu8Relation++; 16 } 17 } 18 19 //生成转移矩阵,每个节点的权重平均流出 20 pu8Relation = m_pu8Relation; 21 pf32P = m_pf32P; 22 for(i = 0; i < m_nNum; i++) 23 { 24 for(j = 0; j < m_nNum; j++) 25 { 26 if(m_pl32Out[j] > 0) 27 { 28 *pf32P = *pu8Relation * 1.0f / m_pl32Out[j]; 29 } 30 else 31 { 32 *pf32P = 0.0f; 33 } 34 pu8Relation++; 35 pf32P++; 36 } 37 } 38 39 //考虑阻尼系数,修正转移矩阵 40 pf32P = m_pf32P; 41 for(i = 0; i < m_nNum; i++) 42 { 43 for(j = 0; j < m_nNum; j++) 44 { 45 *pf32P = *pf32P * m_f32DampFactor; 46 pf32P++; 47 } 48 } 49 }
接下来就需要求解出各个节点的权重,process函数里先调用GenerateP生成出P矩阵,然后采用迭代法求解,当时为了测试收敛速度,直接返回了迭代次数:
1 int CPageRank::Process() 2 { 3 int i,j,k,t; 4 float f32MaxErr = 0.0f; 5 float *pf32Org = m_pf32Weight0; 6 float *pf32New = m_pf32Weight1; 7 float f32MinWeight = (1 - m_f32DampFactor) / m_nNum; 8 9 //设置初始值,全1 10 for(i = 0; i < m_nNum; i++) 11 { 12 pf32Org[i] = 1.0f / m_nNum;//rand() * 2.0f / RAND_MAX; 13 } 14 15 //生成P矩阵 16 GenerateP(); 17 18 //迭代 19 for(t = 0; t < m_nMaxIterTime; t++) 20 { 21 //开始迭代 22 f32MaxErr = 0.0f; 23 for(i = 0; i < m_nNum; i++) 24 { 25 pf32New[i] = f32MinWeight; 26 int l32Off = i * m_nNum; 27 for(j = 0; j < m_nNum; j++) 28 { 29 pf32New[i] += m_pf32P[l32Off + j] * pf32Org[j]; 30 } 31 32 float f32Err = fabs(pf32New[i] - pf32Org[i]); 33 if(f32Err > f32MaxErr) 34 { 35 f32MaxErr = f32Err; 36 } 37 } 38 39 //迭代误差足够小,停止 40 if(f32MaxErr < m_f32IterErr) 41 { 42 break; 43 } 44 45 //交换2次迭代结果 46 float *pf32Temp = pf32Org; 47 pf32Org = pf32New; 48 pf32New = pf32Temp; 49 } 50 51 //迭代结果存在pf32New中 52 m_pf32OutWeight = pf32New; 53 return t; 54 }
最后的结果已经存在了m_pf32OutWeight中了,下面函数直接传出结果:
1 float * CPageRank::GetWeight() 2 { 3 return m_pf32OutWeight; 4 }
这样,整个算法就算完成了,考虑到篇幅,贴上来的代码把opencv显示相关的代码去掉了,完整代码见https://bitbucket.org/jcchen1987/mltest。
原文:http://www.cnblogs.com/jcchen1987/p/4271234.html