Skip to content
This repository was archived by the owner on Jun 18, 2026. It is now read-only.

Commit e49fbbc

Browse files
refactor: replace duplicated BFS/component logic in GraphStorytellerExporter with GraphUtils
- findComponents() now delegates to GraphUtils.findComponents() - estimateDiameter() uses GraphUtils.bfsDistances() instead of local bfs/bfsFarthest/bfsMaxDist - Removes ~40 lines of duplicated BFS traversal code - No behavioral changes
1 parent 8637e2a commit e49fbbc

1 file changed

Lines changed: 8 additions & 52 deletions

File tree

Gvisual/src/gvisual/GraphStorytellerExporter.java

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.nio.charset.StandardCharsets;
77
import java.util.*;
88
import java.util.stream.Collectors;
9+
// BFS and component-finding delegated to GraphUtils — see helper methods below.
910

1011
/**
1112
* Generates a natural-language narrative report describing a graph's structure,
@@ -264,29 +265,10 @@ private String buildHtml() {
264265
}
265266

266267
// ---- Helper methods ----
268+
// Component finding and BFS delegate to GraphUtils to avoid duplication.
267269

268270
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);
290272
}
291273

292274
private double computeAvgClustering() {
@@ -324,41 +306,15 @@ private long countTriangles() {
324306
}
325307

326308
private int estimateDiameter(Set<String> component) {
327-
// Double BFS heuristic
309+
// Double BFS heuristic: BFS from arbitrary start, then BFS from farthest
328310
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()
336313
.max(Map.Entry.comparingByValue())
337314
.map(Map.Entry::getKey)
338315
.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);
362318
}
363319

364320
private double computeStdDev(Collection<Integer> values) {

0 commit comments

Comments
 (0)