forked from mrizky-kur/Redux-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDepth-First-Search.js
32 lines (29 loc) · 1.28 KB
/
Depth-First-Search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Implementation of DFS (depth-first search) Algorithm to find the shortest path from a start to a target node.
* Given a start node, this returns the node in the tree below the start node with the target value (or null if it doesn't exist)
* Runs in O(n), where n is the number of nodes in the tree, or O(b^d), where b is the branching factor and d is the depth.
*
* @param start the node to start the search from
* @param target the value to search for
* @return The node containing the target value or null if it doesn't exist.
*/
const dfs = function (start, target) {
console.log("Visiting Node " + start.value);
if (start.value === target) {
// We have found the goal node we we're searching for
console.log("Found the node we're looking for!");
return start;
}
// Recurse with all children
for (var i = 0; i < start.children.length; i++) {
var result = dfs(start.children[i], target);
if (result != null) {
// We've found the goal node while going down that child
return result;
}
}
// We've gone through all children and not found the goal node
console.log("Went through all children of " + start.value + ", returning to it's parent.");
return null;
};
module.exports = {dfs};