MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / DFS

Function DFS

graphs/cyle-in-graph.js:17–33  ·  view source on GitHub ↗
(node, edges, visitedVertices, inStack)

Source from the content-addressed store, hash-verified

15}
16
17function DFS(node, edges, visitedVertices, inStack) {
18 visitedVertices[node] = true;
19 inStack[node] = true;
20
21 const neighbors = edges[node];
22
23 for (const neighbor of neighbors) {
24 if (!visitedVertices[neighbor]) {
25 const containsCycle = DFS(neighbor, edges, visitedVertices, inStack);
26 if (containsCycle) return true;
27 } else if (inStack[neighbor]) {
28 return true;
29 }
30 }
31 inStack[node] = false;
32 return false;
33}
34
35const edges = [[1, 3], [2, 3, 4], [0], [], [2, 5], []];
36

Callers 1

cycleInGraphFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected