diff --git a/graph-search/notes.md b/graph-search/notes.md index 7d29ac8eb026d5cb292f8a5b6e91bd39fdbc77c4..64ba34077f5074f9f1153dd6d3d36a22f72a3b50 100644 --- a/graph-search/notes.md +++ b/graph-search/notes.md @@ -134,8 +134,14 @@ In the weighted directed graph on the whiteboard, what is the shortest path from ## Recursive Depth-First Search (DFS) -* Worklist: … +* Worklist: Call Ctack (activation frames) Activation Frames Backpointers Returned ----------------------------------- ------------ ---------------- - edge = (⊥, a), incidence = … + edge = (⊥, a), incidence = (a, c) a → (⊥, a) [a, c, b, e, f, d] + edge = (a, c), incidence = (c, b) c → (a, c) [c, b, e, f, d] + edge = (c, b), incidence = (b, e) b → (c, b) [b, e, f, d] + edge = (b, a) ⊥ + edge = (b, e), incidence = (e, f) e → (b, e) [e, f, d] + edge = (e, f), incidence = (f, d) f → (e, f) [f, d] + edge = (f, d) [d] diff --git a/graph-search/src/features/search/search.js b/graph-search/src/features/search/search.js index b60729c296d395a7f362a9f73639831b5f335a26..59b954c51620013404b231e28670cd4107c882dc 100644 --- a/graph-search/src/features/search/search.js +++ b/graph-search/src/features/search/search.js @@ -131,6 +131,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 [source]; + } + 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 (