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

Commit 3ad3b07

Browse files
refactor: cache adjacency map in IndependentSetAnalyzer — eliminated 7+ redundant O(V+E) rebuilds
IndependentSetAnalyzer called GraphUtils.buildAdjacencyMap() independently in greedyIndependentSet, greedyMaxDegreeIndependentSet, exactMaximumIndependentSet, allMaximalIndependentSets, kernelReduction, and independencePolynomial. When fullReport() chains through most of these, the same adjacency structure was rebuilt 7+ times. Added a lazy-cached adjacency() accessor that builds the map once on first call. All six methods now use the shared cache instead of constructing their own copy.
1 parent b6ee196 commit 3ad3b07

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

Gvisual/src/gvisual/IndependentSetAnalyzer.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ public class IndependentSetAnalyzer {
3737

3838
private final Graph<String, Edge> graph;
3939

40+
/**
41+
* Cached full-vertex adjacency map, built lazily on first use.
42+
* Avoids rebuilding the same O(V+E) structure in every method call;
43+
* before this change, {@link #fullReport()} triggered 7+ redundant
44+
* rebuilds via greedyIndependentSet (×2), exactMaximumIndependentSet,
45+
* allMaximalIndependentSets, kernelReduction, and independencePolynomial.
46+
*/
47+
private Map<String, Set<String>> cachedAdj;
48+
49+
/**
50+
* Returns the full-vertex adjacency map, building it once and caching.
51+
*/
52+
private Map<String, Set<String>> adjacency() {
53+
if (cachedAdj == null) {
54+
cachedAdj = GraphUtils.buildAdjacencyMap(graph);
55+
}
56+
return cachedAdj;
57+
}
58+
4059
/**
4160
* Constructs an analyzer for the given undirected graph.
4261
*
@@ -105,7 +124,7 @@ public boolean isMaximalIndependentSet(Set<String> vertices) {
105124
public Set<String> greedyIndependentSet() {
106125
Set<String> result = new LinkedHashSet<>();
107126
Set<String> remaining = new HashSet<>(graph.getVertices());
108-
Map<String, Set<String>> adjMap = GraphUtils.buildAdjacencyMap(graph, remaining);
127+
Map<String, Set<String>> adjMap = adjacency();
109128

110129
while (!remaining.isEmpty()) {
111130
// Pick vertex with minimum degree among remaining
@@ -142,7 +161,7 @@ public Set<String> greedyIndependentSet() {
142161
public Set<String> greedyMaxDegreeIndependentSet() {
143162
Set<String> result = new LinkedHashSet<>();
144163
Set<String> remaining = new HashSet<>(graph.getVertices());
145-
Map<String, Set<String>> adjMap = GraphUtils.buildAdjacencyMap(graph, remaining);
164+
Map<String, Set<String>> adjMap = adjacency();
146165

147166
while (!remaining.isEmpty()) {
148167
String maxVertex = null;
@@ -220,7 +239,7 @@ public Set<String> exactMaximumIndependentSet(int maxVertices) {
220239

221240
List<String> vertices = new ArrayList<>(graph.getVertices());
222241
Collections.sort(vertices);
223-
Map<String, Set<String>> adjMap = GraphUtils.buildAdjacencyMap(graph, new HashSet<>(vertices));
242+
Map<String, Set<String>> adjMap = adjacency();
224243

225244
int[] bestSize = {0};
226245
Set<String> bestSet = new LinkedHashSet<>();
@@ -288,7 +307,7 @@ public List<Set<String>> allMaximalIndependentSets() {
288307
public List<Set<String>> allMaximalIndependentSets(int maxCount) {
289308
List<String> vertices = new ArrayList<>(graph.getVertices());
290309
Collections.sort(vertices);
291-
Map<String, Set<String>> adjMap = GraphUtils.buildAdjacencyMap(graph, new HashSet<>(vertices));
310+
Map<String, Set<String>> adjMap = adjacency();
292311

293312
// Build complement adjacency
294313
Map<String, Set<String>> compAdj = new HashMap<>();
@@ -388,7 +407,7 @@ public KernelResult(Set<String> forced, Set<String> kernel,
388407
public KernelResult kernelReduction() {
389408
Set<String> forced = new LinkedHashSet<>();
390409
Set<String> remaining = new LinkedHashSet<>(graph.getVertices());
391-
Map<String, Set<String>> adj = GraphUtils.buildAdjacencyMap(graph, remaining);
410+
Map<String, Set<String>> adj = adjacency();
392411
List<String> log = new ArrayList<>();
393412
int rules = 0;
394413

@@ -572,7 +591,7 @@ public int[] independencePolynomial() {
572591

573592
List<String> vertices = new ArrayList<>(graph.getVertices());
574593
Collections.sort(vertices);
575-
Map<String, Set<String>> adj = GraphUtils.buildAdjacencyMap(graph, new HashSet<>(vertices));
594+
Map<String, Set<String>> adj = adjacency();
576595

577596
int[] counts = new int[n + 1];
578597
counts[0] = 1; // empty set

0 commit comments

Comments
 (0)