From a8dca39ddabbdbfd79691a8f2071ce183cdefe76 Mon Sep 17 00:00:00 2001 From: Zaira <33151350+zairahira@users.noreply.github.com> Date: Fri, 13 Mar 2026 12:59:53 +0500 Subject: [PATCH] feat(curriculum): add JS DFS lab (#65778) Co-authored-by: majestic-owl448 <26656284+majestic-owl448@users.noreply.github.com> --- client/i18n/locales/english/intro.json | 6 + .../69899123889dcf5b19be1847.md | 103 ++++++++++++++++++ .../blocks/lab-depth-first-search-js.json | 15 +++ .../structure/superblocks/javascript-v9.json | 1 + 4 files changed, 125 insertions(+) create mode 100644 curriculum/challenges/english/blocks/lab-depth-first-search-js/69899123889dcf5b19be1847.md create mode 100644 curriculum/structure/blocks/lab-depth-first-search-js.json diff --git a/client/i18n/locales/english/intro.json b/client/i18n/locales/english/intro.json index e878834c508..0d0767c3e86 100644 --- a/client/i18n/locales/english/intro.json +++ b/client/i18n/locales/english/intro.json @@ -3682,6 +3682,12 @@ "In this lab, you will implement a function that converts an adjacency list representation of a graph into an adjacency matrix representation." ] }, + "lab-depth-first-search-js": { + "title": "Implement the Depth-First Search Algorithm", + "intro": [ + "In this lab, you will implement a solution for the depth-first search algorithm." + ] + }, "lab-n-queens-problem-js": { "title": "Implement the N-Queens Problem", "intro": [ diff --git a/curriculum/challenges/english/blocks/lab-depth-first-search-js/69899123889dcf5b19be1847.md b/curriculum/challenges/english/blocks/lab-depth-first-search-js/69899123889dcf5b19be1847.md new file mode 100644 index 00000000000..350aba3cb58 --- /dev/null +++ b/curriculum/challenges/english/blocks/lab-depth-first-search-js/69899123889dcf5b19be1847.md @@ -0,0 +1,103 @@ +--- +id: 69899123889dcf5b19be1847 +title: Implement the Depth-First Search Algorithm +challengeType: 26 +dashedName: implement-depth-first-search-algorithm +--- + +# --description-- + +In this lab, you will implement a graph traversal algorithm called depth-first search (DFS). + +While a breadth-first search (BFS) explores neighbors level by level, depth-first search dives as deep as possible down a single path of edges before backtracking. + +Once the algorithm reaches the end of a path (a node with no unvisited neighbors), it backtracks to the most recent node that still has unexplored edges and continues the search from there. + +To implement this algorithm iteratively, you'll want to use a **stack**. In JavaScript, a standard `Array` serves perfectly as a stack using the `.push()` and `.pop()` methods, following the **Last-In-First-Out (LIFO)** principle. This ensures that the neighbor most recently added to the stack is the next one to be explored. + +A simple output of this algorithm is a list of nodes which are reachable from a given node. Therefore, you'll also want to keep track of the nodes you visit. + +**Objective**: Fulfill the user stories below and get all the tests to pass to complete the lab. + +**User Stories:** + +1. You should define a function named `dfs`. +1. The `dfs` function should take two arguments: + + - `graph`: An adjacency matrix (an array of arrays). + - `root`: A numeric node label (the starting index) which is the numeric value of the node between `0` and `n - 1`, where `n` is the total number of nodes in the graph. + +1. The function should return an array containing all node labels reachable from the starting node. + +# --hints-- + +You should have a function named `dfs` that takes two arguments. + +```js +assert.isFunction(dfs); +assert.lengthOf(dfs, 2); +``` + +`dfs([[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]], 1)` should return a list with `1`, `2`, `3`, and `0`. + +```js +assert.sameMembers(dfs([[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]], 1), [1, 2, 3, 0]); +``` + +`dfs([[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]], 3)` should return a list with `1`, `2`, `3`, and `0`. + +```js +assert.sameMembers(dfs([[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]], 3), [1, 2, 3, 0]); +``` + +`dfs([[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]], 3)` should return `[3]`. + +```js +assert.sameMembers(dfs([[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]], 3), [3]); +``` + +`dfs([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]], 3)` should return a list with `3` and `2`. + +```js +assert.sameMembers(dfs([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]], 3), [3, 2]); + +``` + +`dfs([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]], 0)` should return a list with `0` and `1`. + +```js +assert.sameMembers(dfs([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]], 0), [0, 1]); +``` + +# --seed-- + +## --seed-contents-- + +```js + +``` + +# --solutions-- + +```js +function dfs(graph, root) { + let stack = [root]; + let visited = []; + + while (stack.length > 0) { + let current = stack.pop(); + + if (!visited.includes(current)) { + visited.push(current); + let neighbors = graph[current]; + + for (let i = 0; i < neighbors.length; i++) { + if (neighbors[i] === 1) { + stack.push(i); + } + } + } + } + return visited; +} +``` diff --git a/curriculum/structure/blocks/lab-depth-first-search-js.json b/curriculum/structure/blocks/lab-depth-first-search-js.json new file mode 100644 index 00000000000..9f67a959da8 --- /dev/null +++ b/curriculum/structure/blocks/lab-depth-first-search-js.json @@ -0,0 +1,15 @@ +{ + "name": "Implement the Depth-First Search Algorithm", + "isUpcomingChange": true, + "dashedName": "lab-depth-first-search-js", + "helpCategory": "JavaScript", + "blockLayout": "link", + "challengeOrder": [ + { + "id": "69899123889dcf5b19be1847", + "title": "Implement the Depth-First Search Algorithm" + } + ], + "blockLabel": "lab", + "usesMultifileEditor": true +} diff --git a/curriculum/structure/superblocks/javascript-v9.json b/curriculum/structure/superblocks/javascript-v9.json index 108fd804008..e4feec086e4 100644 --- a/curriculum/structure/superblocks/javascript-v9.json +++ b/curriculum/structure/superblocks/javascript-v9.json @@ -323,6 +323,7 @@ "blocks": [ "lecture-understanding-graphs-and-trees-js", "lab-adjacency-list-to-matrix-converter-js", + "lab-depth-first-search-js", "lab-n-queens-problem-js" ] },