CodeGraph/docs
Section 5.4

Iterative Depth-First Search

Stack-based iterative DFS with early target termination and parent map backtracking for call chain discovery.

Source: server/src/analytics/paths/CallPathAnalyzer.ts

Memory Efficiency: Parent Map vs Path Array Passing

Passing active path arrays down recursion branches incurs an O(depth × branching) memory overhead due to repetitive array allocations.

CodeGraph records parent pointers in a flat parent: Map<string, string> that costs onlyO(V) space. When the target node is reached, the path is reconstructed backwards inO(path length) time:

Path Backtracking Implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
private buildPath(sourceNodeId: string, targetNodeId: string, parent: Map<string, string>): string[] { const path: string[] = []; let current: string | undefined = targetNodeId; while (current !== undefined) { path.push(current); if (current === sourceNodeId) break; current = parent.get(current); } path.reverse(); // [source -> ... -> target] return path; }