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
No related branches found
No related tags found
No related merge requests found
...@@ -134,8 +134,14 @@ In the weighted directed graph on the whiteboard, what is the shortest path from ...@@ -134,8 +134,14 @@ In the weighted directed graph on the whiteboard, what is the shortest path from
## Recursive Depth-First Search (DFS) ## Recursive Depth-First Search (DFS)
* Worklist: * Worklist: Call Ctack (activation frames)
Activation Frames Backpointers Returned 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) { ...@@ -131,6 +131,19 @@ export function bestFirst(graph, source, destination) {
return undefined; return undefined;
} }
export function recursiveDFS(graph, source, destination) { export function recursiveDFS(graph, source, destination, visited = new Set()) {
return undefined; // TODO: stub 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) { ...@@ -29,7 +29,7 @@ function formatSolution(solution) {
} }
export function Solution(props) { export function Solution(props) {
const search = bestFirst; const search = recursiveDFS;
const firstSolution = search(firstExample, 'a', 'd'); const firstSolution = search(firstExample, 'a', 'd');
const secondSolution = search(secondExample, '123', '321'); const secondSolution = search(secondExample, '123', '321');
return ( return (
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment