Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. public int[] Bfs(int v)
  2. {
  3. int[] dist = new int[_maxVertexCount];
  4. for (int i = 0; i < _maxVertexCount; ++i)
  5. {
  6. dist[i] = -1;
  7. }
  8.  
  9. if (_isDeleted[v])
  10. return dist;
  11.  
  12. Queue<(int, int)> way = new Queue<(int, int)>();
  13. way.Enqueue((v, 0));
  14. while (way.Count > 0)
  15. {
  16. var (u, d) = way.Dequeue();
  17. if (_isDeleted[u])
  18. continue;
  19. dist[u] = d;
  20. for (int i = 0; i < _maxVertexCount; ++i)
  21. {
  22. if (_adjacencyMatrix[u, i])
  23. {
  24. if (dist[i] == -1)
  25. {
  26. way.Enqueue((i, d + 1));
  27. }
  28. }
  29. }
  30. }
  31.  
  32. return dist;
  33. }
  34.  
  35. public int CountDiameter()
  36. {
  37. int res = -1;
  38. for (int i = 0; i < _maxVertexCount; ++i)
  39. {
  40. if (_isDeleted[i])
  41. continue;
  42. int[] dist = Bfs(i);
  43. foreach (var d in dist)
  44. {
  45. if (d > res)
  46. res = d;
  47. }
  48. }
  49.  
  50. return res;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement