DBSCAN.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace CheckServer
  8. {
  9. public class DBSCAN
  10. {
  11. private double eps;
  12. private int minPts;
  13. private List<double[]> dataset;
  14. bool[] visited;
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. /// <param name="eps">邻域半径</param>
  19. /// <param name="minPts">邻域内最小点数</param>
  20. /// <param name="dataset">数据集</param>
  21. public DBSCAN(double eps, int minPts, List<double[]> dataset)
  22. {
  23. this.eps = eps;
  24. this.minPts = minPts;
  25. this.dataset = dataset;
  26. }
  27. public List<int> Run()
  28. {
  29. int n = dataset.Count;
  30. visited = new bool[n];
  31. List<int> cluster = new List<int>();
  32. for (int i = 0; i < n; i++)
  33. {
  34. if (!visited[i])
  35. {
  36. List<int> neighbors = RegionQuery(i);
  37. if (neighbors.Count < minPts)
  38. {
  39. visited[i] = true;
  40. }
  41. else
  42. {
  43. cluster.Add(ExpandCluster(i, neighbors));
  44. }
  45. }
  46. }
  47. return cluster;
  48. }
  49. //查询给定点邻域内所有点
  50. private List<int> RegionQuery(int i)
  51. {
  52. List<int> neighbors = new List<int>();
  53. for (int j = 0; j < dataset.Count; j++)
  54. {
  55. if (i != j && Distance(dataset[i], dataset[j]) <= eps)
  56. {
  57. neighbors.Add(j);
  58. }
  59. }
  60. return neighbors;
  61. }
  62. //扩展簇
  63. private int ExpandCluster(int i, List<int> neighbors)
  64. {
  65. int clusterId = i;
  66. visited[i] = true;
  67. List<int> seeds = new List<int>(neighbors);
  68. while (seeds.Count > 0)
  69. {
  70. int current = seeds[0];
  71. seeds.RemoveAt(0);
  72. List<int> currentNeighbors = RegionQuery(current);
  73. if (currentNeighbors.Count >= minPts)
  74. {
  75. foreach (var neighbor in currentNeighbors)
  76. {
  77. if (!visited[neighbor])
  78. {
  79. seeds.Add(neighbor);
  80. }
  81. }
  82. }
  83. if (!visited[current])
  84. {
  85. visited[current] = true;
  86. clusterId = current;
  87. }
  88. }
  89. return clusterId;
  90. }
  91. private double Distance(double[] a, double[] b)
  92. {
  93. double sum = 0;
  94. for (int i = 0; i < a.Length; i++)
  95. {
  96. sum += Math.Pow(a[i] - b[i], 2);
  97. }
  98. return Math.Sqrt(sum);
  99. }
  100. }
  101. }