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

Commit 6f18d8e

Browse files
refactor: extract newEmptyShell + addEdgeCopy in GraphSparsificationAnalyzer
Deduplicated 5 identical patterns across sparsification methods: - UndirectedSparseGraph creation + vertex copy → newEmptyShell() - Edge copy with type/weight/label + null guard → addEdgeCopy() Eliminates ~30 lines of repeated boilerplate from spanningTreeSparsify, randomSparsify, thresholdSparsify, localSparsify, and importanceSparsify. Each method now delegates edge duplication to the shared static helper, ensuring consistent null-endpoint handling and copy semantics.
1 parent 584f8fb commit 6f18d8e

1 file changed

Lines changed: 36 additions & 35 deletions

File tree

Gvisual/src/gvisual/GraphSparsificationAnalyzer.java

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ public GraphSparsificationAnalyzer(Graph<String, Edge> graph) {
3838
this.graph = graph;
3939
}
4040

41+
/**
42+
* Creates a new empty graph containing all vertices from the original
43+
* but no edges. Every sparsification method needs this scaffold.
44+
*/
45+
private Graph<String, Edge> newEmptyShell() {
46+
Graph<String, Edge> shell = new UndirectedSparseGraph<String, Edge>();
47+
for (String vtx : graph.getVertices()) shell.addVertex(vtx);
48+
return shell;
49+
}
50+
51+
/**
52+
* Copies a single edge into a target graph, duplicating type, weight,
53+
* and label. Silently skips edges with null endpoints.
54+
*
55+
* @return true if the edge was added, false if skipped
56+
*/
57+
private static boolean addEdgeCopy(Graph<String, Edge> target, Edge e) {
58+
String u = e.getVertex1(), v = e.getVertex2();
59+
if (u == null || v == null) return false;
60+
Edge copy = new Edge(e.getType(), u, v);
61+
copy.setWeight(e.getWeight());
62+
copy.setLabel(e.getLabel());
63+
target.addEdge(copy, u, v);
64+
return true;
65+
}
66+
4167
/** Scores each Edge by importance (0.0-1.0). Higher = more important. */
4268
public Map<Edge, Double> scoreEdgeImportance() {
4369
Map<Edge, Double> scores = new LinkedHashMap<Edge, Double>();
@@ -102,8 +128,7 @@ public Set<Edge> findBridges() {
102128

103129
/** Spanning tree (Kruskal's MST). */
104130
public Graph<String, Edge> spanningTreeSparsify() {
105-
Graph<String, Edge> sparse = new UndirectedSparseGraph<String, Edge>();
106-
for (String vtx : graph.getVertices()) sparse.addVertex(vtx);
131+
Graph<String, Edge> sparse = newEmptyShell();
107132
if (graph.getVertexCount() <= 1) return sparse;
108133

109134
List<Edge> edges = new ArrayList<Edge>(graph.getEdges());
@@ -117,9 +142,7 @@ public Graph<String, Edge> spanningTreeSparsify() {
117142
if (u == null || vt == null) continue;
118143
String ru = ufFind(par, u), rv = ufFind(par, vt);
119144
if (!ru.equals(rv)) {
120-
Edge ne = new Edge(e.getType(), u, vt);
121-
ne.setWeight(e.getWeight()); ne.setLabel(e.getLabel());
122-
sparse.addEdge(ne, u, vt);
145+
addEdgeCopy(sparse, e);
123146
ufUnion(par, rnk, ru, rv);
124147
}
125148
}
@@ -139,34 +162,22 @@ private void ufUnion(Map<String, String> p, Map<String, Integer> r, String a, St
139162
/** Random sparsification — keeps each Edge with given probability. */
140163
public Graph<String, Edge> randomSparsify(double keepProb, long seed) {
141164
if (keepProb < 0 || keepProb > 1) throw new IllegalArgumentException("keepProbability must be between 0 and 1");
142-
Graph<String, Edge> sparse = new UndirectedSparseGraph<String, Edge>();
143-
for (String vtx : graph.getVertices()) sparse.addVertex(vtx);
165+
Graph<String, Edge> sparse = newEmptyShell();
144166
Random rng = new Random(seed);
145167
for (Edge e : graph.getEdges()) {
146168
if (rng.nextDouble() < keepProb) {
147-
String u = e.getVertex1(), vt = e.getVertex2();
148-
if (u != null && vt != null) {
149-
Edge ne = new Edge(e.getType(), u, vt);
150-
ne.setWeight(e.getWeight()); ne.setLabel(e.getLabel());
151-
sparse.addEdge(ne, u, vt);
152-
}
169+
addEdgeCopy(sparse, e);
153170
}
154171
}
155172
return sparse;
156173
}
157174

158175
/** Threshold sparsification — keeps edges with weight >= threshold. */
159176
public Graph<String, Edge> thresholdSparsify(float threshold) {
160-
Graph<String, Edge> sparse = new UndirectedSparseGraph<String, Edge>();
161-
for (String vtx : graph.getVertices()) sparse.addVertex(vtx);
177+
Graph<String, Edge> sparse = newEmptyShell();
162178
for (Edge e : graph.getEdges()) {
163179
if (e.getWeight() >= threshold) {
164-
String u = e.getVertex1(), vt = e.getVertex2();
165-
if (u != null && vt != null) {
166-
Edge ne = new Edge(e.getType(), u, vt);
167-
ne.setWeight(e.getWeight()); ne.setLabel(e.getLabel());
168-
sparse.addEdge(ne, u, vt);
169-
}
180+
addEdgeCopy(sparse, e);
170181
}
171182
}
172183
return sparse;
@@ -175,8 +186,7 @@ public Graph<String, Edge> thresholdSparsify(float threshold) {
175186
/** Local sparsification — keep top-k edges per vertex by weight. */
176187
public Graph<String, Edge> localSparsify(int k) {
177188
if (k < 1) throw new IllegalArgumentException("k must be at least 1");
178-
Graph<String, Edge> sparse = new UndirectedSparseGraph<String, Edge>();
179-
for (String vtx : graph.getVertices()) sparse.addVertex(vtx);
189+
Graph<String, Edge> sparse = newEmptyShell();
180190
Set<String> added = new HashSet<String>();
181191

182192
for (String vertex : graph.getVertices()) {
@@ -189,9 +199,7 @@ public Graph<String, Edge> localSparsify(int k) {
189199
if (u == null || vt == null) continue;
190200
String key = u.compareTo(vt) < 0 ? u + "|" + vt : vt + "|" + u;
191201
if (!added.contains(key)) {
192-
Edge ne = new Edge(e.getType(), u, vt);
193-
ne.setWeight(e.getWeight()); ne.setLabel(e.getLabel());
194-
sparse.addEdge(ne, u, vt);
202+
addEdgeCopy(sparse, e);
195203
added.add(key);
196204
}
197205
cnt++;
@@ -203,8 +211,7 @@ public Graph<String, Edge> localSparsify(int k) {
203211
/** Importance-based sparsification — keeps the most important edges. */
204212
public Graph<String, Edge> importanceSparsify(double keepRatio) {
205213
if (keepRatio < 0 || keepRatio > 1) throw new IllegalArgumentException("keepRatio must be between 0 and 1");
206-
Graph<String, Edge> sparse = new UndirectedSparseGraph<String, Edge>();
207-
for (String vtx : graph.getVertices()) sparse.addVertex(vtx);
214+
Graph<String, Edge> sparse = newEmptyShell();
208215

209216
Map<Edge, Double> scores = scoreEdgeImportance();
210217
List<Map.Entry<Edge, Double>> sorted = new ArrayList<Map.Entry<Edge, Double>>(scores.entrySet());
@@ -215,13 +222,7 @@ public Graph<String, Edge> importanceSparsify(double keepRatio) {
215222
int cnt = 0;
216223
for (Map.Entry<Edge, Double> entry : sorted) {
217224
if (cnt >= keep) break;
218-
Edge e = entry.getKey();
219-
String u = e.getVertex1(), vt = e.getVertex2();
220-
if (u != null && vt != null) {
221-
Edge ne = new Edge(e.getType(), u, vt);
222-
ne.setWeight(e.getWeight()); ne.setLabel(e.getLabel());
223-
sparse.addEdge(ne, u, vt);
224-
}
225+
addEdgeCopy(sparse, entry.getKey());
225226
cnt++;
226227
}
227228
return sparse;

0 commit comments

Comments
 (0)