(nodeId: string)
| 104 | }) |
| 105 | |
| 106 | function dfs(nodeId: string): boolean { |
| 107 | colors[nodeId] = 1 // Mark as in progress |
| 108 | |
| 109 | for (const neighbor of graph[nodeId] || []) { |
| 110 | if (colors[neighbor] === 1) { |
| 111 | // Back edge found - cycle detected |
| 112 | return true |
| 113 | } |
| 114 | if (colors[neighbor] === 0 && dfs(neighbor)) { |
| 115 | return true |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | colors[nodeId] = 2 // Mark as done |
| 120 | return false |
| 121 | } |
| 122 | |
| 123 | // Run DFS from all unvisited nodes |
| 124 | for (const node of nodes) { |