JoeKarlsson/data-structures

View on GitHub
graph-traversing/depth-first-search-imperative.js

Summary

Maintainability
A
25 mins
Test Coverage
const DFS = (start, searchFor) => {
  const stack = [start];
  const visited = [];
  let currNode = start;

  while ( stack.length !== 0) {
    currNode = stack.pop();
    if (currNode.value === searchFor) {
      return currNode;
    }
    if (visited.indexOf(currNode) === -1) {
      visited.push(currNode);
      currNode.neighbors.forEach((w) => {
        stack.push(w);
      });
    }
  }

  if (stack.length === 0) {
    return false;
  }
};

module.exports = DFS;