C# 语言Pagerank两种实现

  最近正在学习Hadoop相关的知识,参考了很多材料,如下是C#的两种实现。

  目前对这个算法只是知道了运算的过程和思路,理解的还不是太深刻,我看到论坛上很多朋友算的pr值超过1,总感觉是有问题,具体哪有问题反倒倒说不好了,请知晓的朋友帮忙分析下。
        附件是两种实现方式,感兴趣的朋友可以自行下来看看,有不对的地方,欢迎批评指正。
        这是第二种实现的代码,我是通过判断两个矩阵的相差的范围进而判断是否退出迭代的。
       C# 语言的实现,参考http://f.dataguru.cn/forum.php?mod=viewthread&tid=58606             输出结果中的S是构建的原始矩阵

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Pagerank
  6. {
  7.     public class  PagerankMapReduce
  8.     {
  9.         const double EPS = 0.000001;//精度值设置越小,迭代的次数就越多
  10.         const double A = 0.85;
  11.         const int NODE_COUNT = 5;
  12.         public void Rank()
  13.         {
  14.             double[,] temp = new double[NODE_COUNT, NODE_COUNT] { 
  15.             { 0,1 / 2.0, 1 / 2.0, 0, 1 / 2.0 }, 
  16.             { 1 / 4.0, 0, 0, 0, 0 },
  17.             { 1 / 4.0, 0, 0, 1, 1 / 2.0 }, 
  18.             {1/4.0,1/2.0,1/2.0,0,0},
  19.             { 1 / 4.0, 0, 0, 0, 0 } };
  20.             double[] last_q_vec = new double[] { 0, 0, 0, 0, 0 };
  21.             bool canBreak = false;
  22.             int interCount = 1;
  23.             while (!canBreak)
  24.             {
  25.                 double[] next_q_vec = new double[] { 0, 0, 0, 0, 0 };
  26.                 for (int row = 0; row < NODE_COUNT; row++)
  27.                 {
  28.                     for (int col = 0; col < NODE_COUNT; col++)
  29.                     {
  30.                         next_q_vec[row] += temp[row, col] * last_q_vec[col];
  31.                     }
  32.                     next_q_vec[row] = A * next_q_vec[row] + (1 - A) / NODE_COUNT;
  33.                 }
  34.                 canBreak = true;
  35.                 for (int i = 0; i < last_q_vec.Length; i++)
  36.                 {
  37.                     if (Math.Abs(last_q_vec[i] - next_q_vec[i]) > EPS)
  38.                     {
  39.                         canBreak = false;
  40.                         break;
  41.                     }
  42.                 }
  43.                 if (!canBreak)
  44.                 {
  45.                     for (int i = 0; i < next_q_vec.Length; i++)
  46.                     {
  47.                         last_q_vec[i] = next_q_vec[i];
  48.                     }
  49.                 }
  50.                 interCount++;
  51.             }
  52.             Console.WriteLine("InterCount:{0}", interCount);
  53.             PrintArray(last_q_vec);
  54.         }
  55.         public void PrintArray(double[] row)
  56.         {
  57.             int dimension = row.Length;
  58.             Console.Write("[");
  59.             for (int col = 0; col < dimension; col++)
  60.             {
  61.                 Console.Write(string.Format("{0,8}", row[col]));
  62.                 if (col == dimension - 1)
  63.                 {
  64.                     Console.Write("]");
  65.                 }
  66.                 else
  67.                 {
  68.                     Console.Write(" ");
  69.                 }
  70.             }
  71.             Console.WriteLine();
  72.         }
  73.     }
  74. }
复制代码

运算结果: 

2013-08-24_223858.png 

代码下载

原文地址:https://www.cnblogs.com/wangn/p/3281849.html