MCPcopy Index your code
hub / github.com/TheAlgorithms/TypeScript / dfs

Function dfs

graph/tarjan.ts:27–61  ·  view source on GitHub ↗
(node: number)

Source from the content-addressed store, hash-verified

25 const sccs: number[][] = []
26
27 const dfs = (node: number) => {
28 discovery[node] = index
29 low[node] = index
30 ++index
31 stack.push(node)
32 stackContains[node] = true
33
34 for (const child of graph[node]) {
35 if (low[child] === undefined) {
36 dfs(child)
37 if (low[child] < low[node]) {
38 // Child node loops back to this node's ancestor. Update the low node.
39 low[node] = low[child]
40 }
41 } else if (stackContains[child] && low[node] > discovery[child]) {
42 // Found a backedge. Update the low for this node if needed.
43 low[node] = discovery[child]
44 }
45 }
46
47 if (discovery[node] == low[node]) {
48 // node is the root of a SCC. Gather the SCC's nodes from the stack.
49 const scc: number[] = []
50 let i
51 for (i = stack.length - 1; stack[i] != node; --i) {
52 scc.push(stack[i])
53 stackContains[stack[i]] = false
54 stack.pop()
55 }
56 scc.push(stack[i])
57 stackContains[stack[i]] = false
58 stack.pop()
59 sccs.push(scc)
60 }
61 }
62
63 for (let i = 0; i < graph.length; ++i) {
64 if (low[i] === undefined) {

Callers 1

tarjanFunction · 0.70

Calls 2

pushMethod · 0.65
popMethod · 0.65

Tested by

no test coverage detected