Optimize DFS while marking connected components

This commit is contained in:
Viswanath Kuchibhotla 2024-11-27 22:35:50 +05:30
parent d9aa525c9e
commit 1d0539decb
1 changed files with 8 additions and 1 deletions

View File

@ -163,6 +163,10 @@ public class HnswUtil {
throws IOException {
// Start at entry point and search all nodes on this level
// System.out.println("markRooted level=" + level + " entryPoint=" + entryPoint);
if (connectedNodes.get(entryPoint)) {
return new Component(entryPoint, 0);
}
FixedBitSet nodesInStack = new FixedBitSet(hnswGraph.size());
Deque<Integer> stack = new ArrayDeque<>();
stack.push(entryPoint);
int count = 0;
@ -178,7 +182,10 @@ public class HnswUtil {
int friendCount = 0;
while ((friendOrd = hnswGraph.nextNeighbor()) != NO_MORE_DOCS) {
++friendCount;
stack.push(friendOrd);
if (connectedNodes.get(friendOrd) == false && nodesInStack.get(friendOrd) == false) {
stack.push(friendOrd);
nodesInStack.set(friendOrd);
}
}
if (friendCount < maxConn && notFullyConnected != null) {
notFullyConnected.set(node);