Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int[] Bfs(int v)
- {
- int[] dist = new int[_maxVertexCount];
- for (int i = 0; i < _maxVertexCount; ++i)
- {
- dist[i] = -1;
- }
- if (_isDeleted[v])
- return dist;
- Queue<(int, int)> way = new Queue<(int, int)>();
- way.Enqueue((v, 0));
- while (way.Count > 0)
- {
- var (u, d) = way.Dequeue();
- if (_isDeleted[u])
- continue;
- dist[u] = d;
- for (int i = 0; i < _maxVertexCount; ++i)
- {
- if (_adjacencyMatrix[u, i])
- {
- if (dist[i] == -1)
- {
- way.Enqueue((i, d + 1));
- }
- }
- }
- }
- return dist;
- }
- public int CountDiameter()
- {
- int res = -1;
- for (int i = 0; i < _maxVertexCount; ++i)
- {
- if (_isDeleted[i])
- continue;
- int[] dist = Bfs(i);
- foreach (var d in dist)
- {
- if (d > res)
- res = d;
- }
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement