项目作者: cschen1205

项目描述 :
PageRank implemented in .NET
高级语言: C#
项目地址: git://github.com/cschen1205/cs-page-rank.git
创建时间: 2018-04-30T04:39:17Z
项目社区:https://github.com/cschen1205/cs-page-rank

开源协议:MIT License

下载


cs-page-rank

Iterative PageRank implemented in .NET 4.6.1

Install

  1. Install-Package cs-page-rank

Usage

The sample code below shows how to use the page rank to perform link analysis:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace PageRank
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. PageRankByIterativeMethod();
  10. PageRankByMatrixIterativeMethod();
  11. }
  12. private static void PageRankByIterativeMethod()
  13. {
  14. Random random = new Random();
  15. int pageCount = 1000;
  16. IterativePageRank pr = new IterativePageRank(pageCount);
  17. List<int> pages = new List<int>();
  18. for(int pageId = 0; pageId < pageCount; ++pageId)
  19. {
  20. pages.Add(pageId);
  21. }
  22. for(int fromPageId =0; fromPageId < pageCount; ++fromPageId)
  23. {
  24. int outLinkCount = random.Next(pageCount / 50);
  25. Shuffle(pages, random);
  26. for(int i=0; i < outLinkCount; ++i)
  27. {
  28. int toPageId = pages[i];
  29. pr.AddLink(fromPageId, toPageId);
  30. }
  31. }
  32. double tolerance = 0.00001;
  33. float[] ranks = pr.RankPages(tolerance);
  34. for(int pageId = 0; pageId < 50; ++pageId)
  35. {
  36. Console.WriteLine("Page: {0}, Score: {1}", pageId, ranks[pageId]);
  37. }
  38. }
  39. private static void PageRankByMatrixIterativeMethod()
  40. {
  41. Random random = new Random();
  42. int pageCount = 1000;
  43. MatrixIterativePageRank pr = new MatrixIterativePageRank(pageCount);
  44. List<int> pages = new List<int>();
  45. for (int pageId = 0; pageId < pageCount; ++pageId)
  46. {
  47. pages.Add(pageId);
  48. }
  49. for (int fromPageId = 0; fromPageId < pageCount; ++fromPageId)
  50. {
  51. int outLinkCount = random.Next(pageCount / 50);
  52. Shuffle(pages, random);
  53. for (int i = 0; i < outLinkCount; ++i)
  54. {
  55. int toPageId = pages[i];
  56. pr.AddLink(fromPageId, toPageId);
  57. }
  58. }
  59. double tolerance = 0.00001;
  60. double[] ranks = pr.RankPages(tolerance);
  61. for (int pageId = 0; pageId < 50; ++pageId)
  62. {
  63. Console.WriteLine("Page: {0}, Score: {1}", pageId, ranks[pageId]);
  64. }
  65. }
  66. private static void Shuffle<T>(List<T> a, Random random)
  67. {
  68. int i = 0;
  69. while(i < a.Count)
  70. {
  71. int j = random.Next(i + 1);
  72. Swap(a, i, j);
  73. i++;
  74. }
  75. }
  76. private static void Swap<T>(List<T> a, int i, int j)
  77. {
  78. T temp = a[i];
  79. a[i] = a[j];
  80. a[j] = temp;
  81. }
  82. }
  83. }