UNPKG

@rohitbakoliya/test-gen

Version:

Quickly generate test cases for stress testing using interactive CLI.

40 lines (39 loc) 1.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class TreeCheck { constructor(nodes, edges) { this.nodes = nodes; this.vis = new Array(nodes + 1).fill(false); this.adj = Array.from(Array(nodes + 1), () => []); edges.forEach(edge => { const [u, v] = edge; this.adj[u].push(v); this.adj[v].push(u); }); this.haveCycle = this.dfs(1); } dfs(i, parent = -1) { this.vis[i] = true; this.adj[i].forEach(x => { if (!this.vis[x]) { if (this.dfs(x, i)) return true; } // if back edge found else if (x !== parent) return true; }); return false; } isConntected() { for (let i = 1; i <= this.nodes; i++) { if (!this.vis[i]) return false; } return true; } isTree() { return this.isConntected() && !this.haveCycle; } } exports.default = TreeCheck;