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

Commit 14ffb13

Browse files
refactor: cache adjacency map in LinkPredictionAnalyzer via lazy init
predict(), predictEnsemble(), and evaluatePairs() each independently called GraphUtils.buildAdjacencyMap(graph), rebuilding the full O(V+E) adjacency structure on every method invocation. When a user calls multiple methods on the same analyzer instance (e.g. predict then predictEnsemble), the map was rebuilt redundantly. Introduce a lazily-initialized cachedAdjacency field that builds the map once on first access and reuses it across all method calls. Also removes the unused buildAdjacency(vertices) wrapper method that ignored its parameter.
1 parent 6645105 commit 14ffb13

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

Gvisual/src/gvisual/LinkPredictionAnalyzer.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ public class LinkPredictionAnalyzer {
3030

3131
private final Graph<String, Edge> graph;
3232

33+
/**
34+
* Lazily cached adjacency map — built once on first use and reused
35+
* across predict(), predictEnsemble(), and score() calls. Previously
36+
* each method independently called GraphUtils.buildAdjacencyMap(),
37+
* resulting in O(V+E) redundant work per call on the same analyzer
38+
* instance.
39+
*/
40+
private Map<String, Set<String>> cachedAdjacency;
41+
3342
/**
3443
* Create a new link prediction analyzer.
3544
*
@@ -43,6 +52,16 @@ public LinkPredictionAnalyzer(Graph<String, Edge> graph) {
4352
this.graph = graph;
4453
}
4554

55+
/**
56+
* Returns the cached adjacency map, building it on first access.
57+
*/
58+
private Map<String, Set<String>> adjacency() {
59+
if (cachedAdjacency == null) {
60+
cachedAdjacency = GraphUtils.buildAdjacencyMap(graph);
61+
}
62+
return cachedAdjacency;
63+
}
64+
4665
// ── Score methods ───────────────────────────────────────────
4766

4867
/**
@@ -170,7 +189,7 @@ public PredictionResult predict(Method method, int topK) {
170189
int n = vertices.size();
171190
int existingEdges = graph.getEdgeCount();
172191
long possibleEdges = (long) n * (n - 1) / 2;
173-
Map<String, Set<String>> adjacency = GraphUtils.buildAdjacencyMap(graph);
192+
Map<String, Set<String>> adjacency = adjacency();
174193

175194
List<String> vertexList = new ArrayList<String>(vertices);
176195

@@ -250,7 +269,7 @@ public PredictionResult predictEnsemble(int topK) {
250269
int n = vertices.size();
251270
int existingEdges = graph.getEdgeCount();
252271
long possibleEdges = (long) n * (n - 1) / 2;
253-
Map<String, Set<String>> adjacency = GraphUtils.buildAdjacencyMap(graph);
272+
Map<String, Set<String>> adjacency = adjacency();
254273
List<String> vertexList = new ArrayList<String>(vertices);
255274

256275
Method[] methods = {
@@ -380,7 +399,7 @@ private PairEvaluation evaluatePairs() {
380399
int n = vertices.size();
381400
int existingEdges = graph.getEdgeCount();
382401
long possibleEdges = (long) n * (n - 1) / 2;
383-
Map<String, Set<String>> adjacency = buildAdjacency(vertices);
402+
Map<String, Set<String>> adjacency = adjacency();
384403

385404
List<String> vertexList = new ArrayList<String>(vertices);
386405
List<String[]> pairs = new ArrayList<String[]>();
@@ -438,10 +457,6 @@ private double computeScore(Method method, Map<String, Set<String>> adjacency,
438457

439458
// ── Helpers ─────────────────────────────────────────────────
440459

441-
private Map<String, Set<String>> buildAdjacency(Collection<String> vertices) {
442-
return GraphUtils.buildAdjacencyMap(graph);
443-
}
444-
445460
private Set<String> getCommonNeighbors(Map<String, Set<String>> adjacency,
446461
String u, String v) {
447462
return GraphUtils.getCommonNeighbors(adjacency, u, v);

0 commit comments

Comments
 (0)