MCPcopy
hub / github.com/TheAlgorithms/JavaScript / dfs

Method dfs

Graphs/BinaryLifting.js:41–54  ·  view source on GitHub ↗
(node, parent)

Source from the content-addressed store, hash-verified

39 }
40
41 dfs(node, parent) {
42 // The dfs function calculates 2^i-th ancestor of all nodes for i ranging from 0 to this.log
43 // We make use of the fact the two consecutive jumps of length 2^(i-1) make the total jump length 2^i
44 this.up.set(node, new Map())
45 this.up.get(node).set(0, parent)
46 for (let i = 1; i < this.log; i++) {
47 this.up
48 .get(node)
49 .set(i, this.up.get(this.up.get(node).get(i - 1)).get(i - 1))
50 }
51 for (const child of this.connections.get(node)) {
52 if (child !== parent) this.dfs(child, node)
53 }
54 }
55
56 kthAncestor(node, k) {
57 // if value of k is more than or equal to the number of total nodes, we return the root of the graph

Callers 1

constructorMethod · 0.95

Calls 2

setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected