diff --git a/graph-search/src/features/search/search.js b/graph-search/src/features/search/search.js index 5918ee4e30741a905a548bd90125a6d1ad43beee..f931d50768cdc65e9ec7321b67ae7f28a7a63286 100644 --- a/graph-search/src/features/search/search.js +++ b/graph-search/src/features/search/search.js @@ -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; } diff --git a/graph-search/src/features/search/solution.js b/graph-search/src/features/search/solution.js index da0ee237318884f3c7338c8127591d408bf84d54..94ab48c52e1bb295c71130825092d5b6c81942c1 100644 --- a/graph-search/src/features/search/solution.js +++ b/graph-search/src/features/search/solution.js @@ -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 (