From da5e0177c4476bd0768282e86ad48e77a4487739 Mon Sep 17 00:00:00 2001 From: Dario-DC <105294544+Dario-DC@users.noreply.github.com> Date: Tue, 1 Oct 2024 18:22:14 +0200 Subject: [PATCH] feat(curriculum): add depth-first search lab (#56378) Co-authored-by: Zaira <33151350+zairahira@users.noreply.github.com> --- client/i18n/locales/english/intro.json | 7 +- .../lab-depth-first-search/index.md | 9 + .../_meta/lab-depth-first-search/meta.json | 11 + .../587d825d367417b2b2512c96.md | 212 ++++++++++++++++++ curriculum/test/utils/mongo-ids.js | 5 +- 5 files changed, 239 insertions(+), 5 deletions(-) create mode 100644 client/src/pages/learn/front-end-development/lab-depth-first-search/index.md create mode 100644 curriculum/challenges/_meta/lab-depth-first-search/meta.json create mode 100644 curriculum/challenges/english/25-front-end-development/lab-depth-first-search/587d825d367417b2b2512c96.md diff --git a/client/i18n/locales/english/intro.json b/client/i18n/locales/english/intro.json index 55028a8cfcc..9a4d7a34ccb 100644 --- a/client/i18n/locales/english/intro.json +++ b/client/i18n/locales/english/intro.json @@ -2448,7 +2448,12 @@ ] }, "rmpy": { "title": "268", "intro": [] }, - "plfo": { "title": "269", "intro": [] }, + "lab-depth-first-search": { + "title": "Implement the Depth-First Search Algorithm", + "intro": [ + "For this lab, you will use JavaScript to implement the Depth-First Search algorithm." + ] + }, "xdyh": { "title": "270", "intro": [] }, "quiz-graphs-and-trees": { "title": "Graphs and Trees Quiz", diff --git a/client/src/pages/learn/front-end-development/lab-depth-first-search/index.md b/client/src/pages/learn/front-end-development/lab-depth-first-search/index.md new file mode 100644 index 00000000000..af878009b1f --- /dev/null +++ b/client/src/pages/learn/front-end-development/lab-depth-first-search/index.md @@ -0,0 +1,9 @@ +--- +title: Introduction to the Implement the Depth-First Search Algorithm +block: lab-depth-first-search +superBlock: front-end-development +--- + +## Introduction to the Implement the Depth-First Search Algorithm + +For this lab, you will use JavaScript to implement the Depth-First Search algorithm. diff --git a/curriculum/challenges/_meta/lab-depth-first-search/meta.json b/curriculum/challenges/_meta/lab-depth-first-search/meta.json new file mode 100644 index 00000000000..95ab7bd6833 --- /dev/null +++ b/curriculum/challenges/_meta/lab-depth-first-search/meta.json @@ -0,0 +1,11 @@ +{ + "name": "Implement the Depth-First Search Algorithm", + "isUpcomingChange": true, + "usesMultifileEditor": true, + "blockType": "lab", + "dashedName": "lab-depth-first-search", + "order": 269, + "superBlock": "front-end-development", + "challengeOrder": [{ "id": "587d825d367417b2b2512c96", "title": "Implement the Depth-First Search Algorithm" }], + "helpCategory": "JavaScript" +} diff --git a/curriculum/challenges/english/25-front-end-development/lab-depth-first-search/587d825d367417b2b2512c96.md b/curriculum/challenges/english/25-front-end-development/lab-depth-first-search/587d825d367417b2b2512c96.md new file mode 100644 index 00000000000..3da2ba18111 --- /dev/null +++ b/curriculum/challenges/english/25-front-end-development/lab-depth-first-search/587d825d367417b2b2512c96.md @@ -0,0 +1,212 @@ +--- +id: 587d825d367417b2b2512c96 +title: Implement the Depth-First Search Algorithm +challengeType: 14 +dashedName: implement-the-depth-first-search-algorithm +--- + +# --description-- + +Depth-first search (DFS) is an algorithm used to search through data structures such as trees or graphs. Starting from a root node, the search first goes down a path of edges as far as it can. Once it reaches one end of a path, the search will backtrack to the last node with an unvisited edge path and continue searching down the unvisited edge path. + +**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab. + +**User Stories:** + +1. You should write a function `dfs()` that implements the depth-first search algorithm on a graph. +1. The function should keep track of the visited nodes. +1. The function should use a stack, an array where the last element added is the first to be removed, to make sure to visit the neighbors of the most recently added node. +1. The function should take an undirected, adjacency matrix `graph` and a node label `root` as parameters. The node label 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 output an array of all nodes reachable from `root`. + +# --hints-- + +The input graph `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]` with a start node of `1` should return an array with `0`, `1`, `2`, and `3`. + +```js +assert.sameMembers( + (function () { + var graph = [ + [0, 1, 0, 0], + [1, 0, 1, 0], + [0, 1, 0, 1], + [0, 0, 1, 0] + ]; + return dfs(graph, 1); + })(), + [0, 1, 2, 3] +); +``` + +The input graph `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]` with a start node of `3` should return an array with `3`, `2`, `1`, and `0`. + +```js +assert.sameMembers( + (function () { + var graph = [ + [0, 1, 0, 0], + [1, 0, 1, 0], + [0, 1, 0, 1], + [0, 0, 1, 0] + ]; + return dfs(graph, 3); + })(), + [3, 2, 1, 0] +); +``` + +The input graph `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]]` with a start node of `1` should return an array with four elements. + +```js +assert( + (function () { + var graph = [ + [0, 1, 0, 0], + [1, 0, 1, 0], + [0, 1, 0, 1], + [0, 0, 1, 0] + ]; + return dfs(graph, 1); + })().length === 4 +); +``` + +The input graph `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]` with a start node of `3` should return an array with `3`. + +```js +assert.sameMembers( + (function () { + var graph = [ + [0, 1, 0, 0], + [1, 0, 1, 0], + [0, 1, 0, 0], + [0, 0, 0, 0] + ]; + return dfs(graph, 3); + })(), + [3] +); +``` + +The input graph `[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]]` with a start node of `3` should return an array with one element. + +```js +assert( + (function () { + var graph = [ + [0, 1, 0, 0], + [1, 0, 1, 0], + [0, 1, 0, 0], + [0, 0, 0, 0] + ]; + return dfs(graph, 3); + })().length === 1 +); +``` + +The input graph `[[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]` with a start node of `3` should return an array with `2` and `3`. + +```js +assert.sameMembers( + (function () { + var graph = [ + [0, 1, 0, 0], + [1, 0, 0, 0], + [0, 0, 0, 1], + [0, 0, 1, 0] + ]; + return dfs(graph, 3); + })(), + [2, 3] +); +``` + +The input graph `[[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]` with a start node of `3` should return an array with two elements. + +```js +assert( + (function () { + var graph = [ + [0, 1, 0, 0], + [1, 0, 0, 0], + [0, 0, 0, 1], + [0, 0, 1, 0] + ]; + return dfs(graph, 3); + })().length === 2 +); +``` + +The input graph `[[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]` with a start node of `0` should return an array with `0` and `1`. + +```js +assert.sameMembers( + (function () { + var graph = [ + [0, 1, 0, 0], + [1, 0, 0, 0], + [0, 0, 0, 1], + [0, 0, 1, 0] + ]; + return dfs(graph, 0); + })(), + [0, 1] +); +``` + +The input graph `[[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]` with a start node of `0` should return an array with two elements. + +```js +assert( + (function () { + var graph = [ + [0, 1, 0, 0], + [1, 0, 0, 0], + [0, 0, 0, 1], + [0, 0, 1, 0] + ]; + return dfs(graph, 0); + })().length === 2 +); +``` + +# --seed-- + +## --seed-contents-- + +```js + +``` + +# --solutions-- + +```js +function dfs(graph, root) { + const stack = []; + let tempV; + const visited = []; + let tempVNeighbors = []; + stack.push(root); + while (stack.length > 0) { + tempV = stack.pop(); + if (visited.indexOf(tempV) == -1) { + visited.push(tempV); + tempVNeighbors = graph[tempV]; + for (let i = 0; i < tempVNeighbors.length; i++) { + if (tempVNeighbors[i] == 1) { + stack.push(i); + } + } + } + } + return visited; +} + +const exDFSGraph = [ + [0, 1, 0, 0], + [1, 0, 1, 0], + [0, 1, 0, 1], + [0, 0, 1, 0] +]; +console.log(dfs(exDFSGraph, 0)); +``` diff --git a/curriculum/test/utils/mongo-ids.js b/curriculum/test/utils/mongo-ids.js index 2dc624c9e38..ddd008fcee9 100644 --- a/curriculum/test/utils/mongo-ids.js +++ b/curriculum/test/utils/mongo-ids.js @@ -177,11 +177,8 @@ const duplicatedProjectIds = [ // Stacks '587d8250367417b2b2512c5f', - // Linked Lists - - // Hash Tables - // Depth-first Search + '587d825d367417b2b2512c96', // Nth Fibonacci Number