(node, parent)
| 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 |
no test coverage detected