class GraphUnweightedUndirected {
constructor () {
this.connections = {}
}
addNode (node) {
this.connections[node] = new Set()
}
addEdge (node1, node2) {
if (!(node1 in this.connections)) { this.addNode(node1) }
if (!(node2 in this.connections)) { this.addNode(node2) }
this.connections[node1].add(node2)
this.connections[node2].add(node1)
}
DFSIterative (node, value) {
const stack = [node]
const visited = new Set()
while (stack.length > 0) {
const currNode = stack.pop()
if (currNode === value) { return true }
visited.add(currNode)
for (const neighbour of this.connections[currNode]) {
if (!visited.has(neighbour)) {
stack.push(neighbour)
}
}
}
return false
}
}
function main () {
const graph = new GraphUnweightedUndirected()
graph.addEdge(1, 2)
graph.addEdge(2, 3)
graph.addEdge(2, 4)
graph.addEdge(3, 5)
console.log(graph.DFSIterative(5, 1))
console.log(graph.DFSIterative(5, 100))
}
main()