Working with Graphs & Trees
What graph algorithm pattern is implemented in this code?
function findPath(graph, start, end) {
const visited = new Set();
const path = [];
function dfs(vertex) {
if (vertex === end) return true;
visited.add(vertex);
path.push(vertex);
for (let neighbor of graph[vertex]) {
if (!visited.has(neighbor)) {
if (dfs(neighbor)) return true;
}
}
path.pop();
return false;
}
dfs(start);
return path;
}