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

Commit 5672e17

Browse files
perf: O(1) neighbor index lookups in face-walk dart tracing
Pre-build HashMap<vertex, HashMap<neighbor, position>> from the planar embedding before tracing faces. Replaces List.indexOf(curr) — an O(degree) scan called once per dart (2E times total) — with O(1) HashMap.get(), reducing enumerateFacesInternal from O(E × max_degree) to O(V + E).
1 parent 4a79e21 commit 5672e17

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

Gvisual/src/gvisual/PlanarGraphAnalyzer.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,20 @@ private static List<Face> enumerateFacesInternal(Graph<String, Edge> graph) {
239239
return faces;
240240
}
241241

242+
// Pre-build index maps: for each vertex v, map neighbor name → position
243+
// in v's neighbor list. This turns the O(degree) indexOf() call inside
244+
// the dart walk into an O(1) HashMap lookup, reducing face tracing from
245+
// O(E × max_degree) to O(E).
246+
Map<String, Map<String, Integer>> neighborIndex = new HashMap<>();
247+
for (Map.Entry<String, List<String>> entry : embedding.entrySet()) {
248+
List<String> nbrs = entry.getValue();
249+
Map<String, Integer> idxMap = new HashMap<>(nbrs.size() * 2);
250+
for (int i = 0; i < nbrs.size(); i++) {
251+
idxMap.put(nbrs.get(i), i);
252+
}
253+
neighborIndex.put(entry.getKey(), idxMap);
254+
}
255+
242256
// Trace faces using the "next-Edge" walk on the combinatorial embedding
243257
Set<String> visitedDarts = new HashSet<String>();
244258
List<Face> faces = new ArrayList<Face>();
@@ -260,9 +274,9 @@ private static List<Face> enumerateFacesInternal(Graph<String, Edge> graph) {
260274
visitedDarts.add(d);
261275
faceVertices.add(curr);
262276

263-
// Find the position of curr in next's neighbor list
277+
// O(1) position lookup via pre-built index map
264278
List<String> nextNeighbors = embedding.get(next);
265-
int idx = nextNeighbors.indexOf(curr);
279+
int idx = neighborIndex.get(next).get(curr);
266280
// The next dart goes to the *previous* neighbor in cyclic order
267281
int prevIdx = (idx - 1 + nextNeighbors.size()) % nextNeighbors.size();
268282

0 commit comments

Comments
 (0)