Skip to content

Commit b02a5af

Browse files
authored
Update DepthFirstSearchIterative.js
Added a check at the start of DFSIterative to handle the case where the starting node doesn't exist. This prevents runtime errors and informs the user.
1 parent 18da83a commit b02a5af

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

Graphs/DepthFirstSearchIterative.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class GraphUnweightedUndirected {
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+
2631
const stack = [node]
2732
const visited = new Set()
2833
while (stack.length > 0) {

0 commit comments

Comments
 (0)