Skip to content
Snippets Groups Projects
Commit d71074c0 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Implemented a recursive DFS.

parent 8bd2a1ec
No related branches found
No related tags found
No related merge requests found
......@@ -129,6 +129,19 @@ export function bestFirst(graph, source, destination) {
return undefined;
}
export function recursiveDFS(graph, source, destination) {
return undefined; // TODO: stub
export function recursiveDFS(graph, source, destination, visited = new Set()) {
if (visited.has(source)) {
return undefined;
}
visited.add(source);
if (source === destination) {
return [destination];
}
for (const incidence of graph.getIncidences(source)) {
const suffix = recursiveDFS(graph, incidence.destination, destination, visited);
if (suffix !== undefined) {
return [source, ...suffix];
}
}
return undefined;
}
......@@ -29,7 +29,7 @@ function formatSolution(solution) {
}
export function Solution(props) {
const search = bestFirst;
const search = recursiveDFS;
const firstSolution = search(firstExample, 'a', 'd');
const secondSolution = search(secondExample, '123', '321');
return (
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment