Skip to content

Commit 947b2b9

Browse files
authored
Update DepthFirstSearchIterative.js
1 parent b02a5af commit 947b2b9

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

Graphs/DepthFirstSearchIterative.js

+30-31
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,61 @@
11
class GraphUnweightedUndirected {
22
// Unweighted Undirected Graph class
33
constructor() {
4-
this.connections = {}
4+
this.connections = {};
55
}
66

77
addNode(node) {
88
// Function to add a node to the graph (connection represented by set)
9-
this.connections[node] = new Set()
9+
this.connections[node] = new Set();
1010
}
1111

1212
addEdge(node1, node2) {
1313
// Function to add an edge (adds the node too if they are not present in the graph)
1414
if (!(node1 in this.connections)) {
15-
this.addNode(node1)
15+
this.addNode(node1);
1616
}
1717
if (!(node2 in this.connections)) {
18-
this.addNode(node2)
18+
this.addNode(node2);
1919
}
20-
this.connections[node1].add(node2)
21-
this.connections[node2].add(node1)
20+
this.connections[node1].add(node2);
21+
this.connections[node2].add(node1);
2222
}
2323

2424
DFSIterative(node, value) {
2525
// DFS Function to search if a node with the given value is present in the graph
26-
if (!(node in this.connections)) { // Changed 'startNode' to 'node'
27-
console.log(`Start node ${node} does not exist in the graph.`); // Updated the log message
28-
return false; // Early return if the node doesn't exist
29-
}
30-
31-
const stack = [node]
32-
const visited = new Set()
26+
if (!(node in this.connections)) { // Changed 'startNode' to 'node'
27+
console.log(`Start node ${node} does not exist in the graph.`); // Updated the log message
28+
return false; // Early return if the node doesn't exist
29+
}
30+
31+
const stack = [node];
32+
const visited = new Set();
3333
while (stack.length > 0) {
34-
const currNode = stack.pop()
35-
// if the current node contains the value being searched for, true is returned
34+
const currNode = stack.pop();
35+
// If the current node contains the value being searched for, true is returned
3636
if (currNode === value) {
37-
return true
37+
return true;
3838
}
39-
// adding the current node to the visited set
40-
visited.add(currNode)
41-
// adding neighbours in the stack
39+
// Adding the current node to the visited set
40+
visited.add(currNode);
41+
// Adding neighbors in the stack
4242
for (const neighbour of this.connections[currNode]) {
4343
if (!visited.has(neighbour)) {
44-
stack.push(neighbour)
44+
stack.push(neighbour);
4545
}
4646
}
4747
}
48-
return false
48+
return false;
4949
}
5050
}
5151

52-
export { GraphUnweightedUndirected }
53-
54-
// Example
52+
export { GraphUnweightedUndirected };
5553

56-
// const graph = new GraphUnweightedUndirected()
57-
// graph.addEdge(1, 2)
58-
// graph.addEdge(2, 3)
59-
// graph.addEdge(2, 4)
60-
// graph.addEdge(3, 5)
61-
// graph.DFSIterative(5, 1)
62-
// graph.DFSIterative(5, 100)
54+
// Example usage
55+
// const graph = new GraphUnweightedUndirected();
56+
// graph.addEdge(1, 2);
57+
// graph.addEdge(2, 3);
58+
// graph.addEdge(2, 4);
59+
// graph.addEdge(3, 5);
60+
// console.log(graph.DFSIterative(5, 1)); // Should print true or false
61+
// console.log(graph.DFSIterative(5, 100)); // Should print false

0 commit comments

Comments
 (0)