From 447e4a3bd9c0c08bb09dc32d1bd5a87bc6e00b6e Mon Sep 17 00:00:00 2001
From: "Brady J. Garvin" <bgarvin@cse.unl.edu>
Date: Wed, 12 Oct 2022 14:51:18 -0500
Subject: [PATCH] Designed and implemented a recursive DFS.

---
 graph-search/notes.md                        | 10 ++++++++--
 graph-search/src/features/search/search.js   | 17 +++++++++++++++--
 graph-search/src/features/search/solution.js |  2 +-
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/graph-search/notes.md b/graph-search/notes.md
index 7d29ac8..64ba340 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 b60729c..59b954c 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 da0ee23..94ab48c 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 (
-- 
GitLab