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

Designed and implemented a recursive DFS.

parent 06d8916b
Branches
Tags
No related merge requests found
......@@ -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]
......@@ -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;
}
......@@ -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