|
6 | 6 | import java.nio.charset.StandardCharsets; |
7 | 7 | import java.util.*; |
8 | 8 | import java.util.stream.Collectors; |
| 9 | +// BFS and component-finding delegated to GraphUtils — see helper methods below. |
9 | 10 |
|
10 | 11 | /** |
11 | 12 | * Generates a natural-language narrative report describing a graph's structure, |
@@ -264,29 +265,10 @@ private String buildHtml() { |
264 | 265 | } |
265 | 266 |
|
266 | 267 | // ---- Helper methods ---- |
| 268 | + // Component finding and BFS delegate to GraphUtils to avoid duplication. |
267 | 269 |
|
268 | 270 | private List<Set<String>> findComponents() { |
269 | | - List<Set<String>> components = new ArrayList<>(); |
270 | | - Set<String> visited = new HashSet<>(); |
271 | | - for (String v : graph.getVertices()) { |
272 | | - if (!visited.contains(v)) { |
273 | | - Set<String> comp = new HashSet<>(); |
274 | | - Queue<String> queue = new LinkedList<>(); |
275 | | - queue.add(v); |
276 | | - visited.add(v); |
277 | | - while (!queue.isEmpty()) { |
278 | | - String cur = queue.poll(); |
279 | | - comp.add(cur); |
280 | | - for (String nb : graph.getNeighbors(cur)) { |
281 | | - if (visited.add(nb)) { |
282 | | - queue.add(nb); |
283 | | - } |
284 | | - } |
285 | | - } |
286 | | - components.add(comp); |
287 | | - } |
288 | | - } |
289 | | - return components; |
| 271 | + return GraphUtils.findComponents(graph); |
290 | 272 | } |
291 | 273 |
|
292 | 274 | private double computeAvgClustering() { |
@@ -324,41 +306,15 @@ private long countTriangles() { |
324 | 306 | } |
325 | 307 |
|
326 | 308 | private int estimateDiameter(Set<String> component) { |
327 | | - // Double BFS heuristic |
| 309 | + // Double BFS heuristic: BFS from arbitrary start, then BFS from farthest |
328 | 310 | String start = component.iterator().next(); |
329 | | - String farthest = bfsFarthest(start); |
330 | | - return bfsMaxDist(farthest); |
331 | | - } |
332 | | - |
333 | | - private String bfsFarthest(String start) { |
334 | | - Map<String, Integer> dist = bfs(start); |
335 | | - return dist.entrySet().stream() |
| 311 | + Map<String, Integer> dist1 = GraphUtils.bfsDistances(graph, start); |
| 312 | + String farthest = dist1.entrySet().stream() |
336 | 313 | .max(Map.Entry.comparingByValue()) |
337 | 314 | .map(Map.Entry::getKey) |
338 | 315 | .orElse(start); |
339 | | - } |
340 | | - |
341 | | - private int bfsMaxDist(String start) { |
342 | | - Map<String, Integer> dist = bfs(start); |
343 | | - return dist.values().stream().mapToInt(Integer::intValue).max().orElse(0); |
344 | | - } |
345 | | - |
346 | | - private Map<String, Integer> bfs(String start) { |
347 | | - Map<String, Integer> dist = new HashMap<>(); |
348 | | - Queue<String> queue = new LinkedList<>(); |
349 | | - dist.put(start, 0); |
350 | | - queue.add(start); |
351 | | - while (!queue.isEmpty()) { |
352 | | - String cur = queue.poll(); |
353 | | - int d = dist.get(cur); |
354 | | - for (String nb : graph.getNeighbors(cur)) { |
355 | | - if (!dist.containsKey(nb)) { |
356 | | - dist.put(nb, d + 1); |
357 | | - queue.add(nb); |
358 | | - } |
359 | | - } |
360 | | - } |
361 | | - return dist; |
| 316 | + Map<String, Integer> dist2 = GraphUtils.bfsDistances(graph, farthest); |
| 317 | + return dist2.values().stream().mapToInt(Integer::intValue).max().orElse(0); |
362 | 318 | } |
363 | 319 |
|
364 | 320 | private double computeStdDev(Collection<Integer> values) { |
|
0 commit comments