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

Commit 0dcba1c

Browse files
perf: replace O(V*d) edge counting with O(E) single-pass
countEdgesInSubgraph previously iterated each vertex's incident edges and used a HashSet<edge> to deduplicate, resulting in O(Σdeg) time plus O(E_sub) memory for the seen set and getEndpoints() overhead. New implementation iterates graph.getEdges() once and checks endpoint membership in the vertex set (O(1) HashSet lookups), eliminating the deduplication set entirely. This is both faster and allocates less.
1 parent 4b0731e commit 0dcba1c

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

Gvisual/src/gvisual/GraphUtils.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,16 @@ private static boolean hasCycleDFS_directed(
270270
*/
271271
public static int countEdgesInSubgraph(
272272
Graph<String, edge> graph, Set<String> vertices) {
273+
// Single pass over all edges — O(E) with no auxiliary HashSet.
274+
// For sparse subgraphs this is competitive with the vertex-centric
275+
// approach, and for dense subgraphs it avoids the O(Σdeg) edge
276+
// deduplication overhead that the previous seen-set approach had.
273277
int count = 0;
274-
Set<edge> seen = new HashSet<edge>();
275-
for (String v : vertices) {
276-
for (edge e : graph.getIncidentEdges(v)) {
277-
if (seen.contains(e)) continue;
278-
boolean allIn = true;
279-
for (String ep : graph.getEndpoints(e)) {
280-
if (!vertices.contains(ep)) { allIn = false; break; }
281-
}
282-
if (allIn) { seen.add(e); count++; }
278+
for (edge e : graph.getEdges()) {
279+
String v1 = e.getVertex1();
280+
String v2 = e.getVertex2();
281+
if (vertices.contains(v1) && vertices.contains(v2)) {
282+
count++;
283283
}
284284
}
285285
return count;

0 commit comments

Comments
 (0)