feat(curriculum): add depth-first search lab (#56378)

Co-authored-by: Zaira <33151350+zairahira@users.noreply.github.com>
This commit is contained in:
Dario-DC
2024-10-01 18:22:14 +02:00
committed by GitHub
parent 1b06afbcf9
commit da5e0177c4
5 changed files with 239 additions and 5 deletions
+6 -1
View File
@@ -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",
@@ -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.
@@ -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"
}
@@ -0,0 +1,212 @@
---
id: 587d825d367417b2b2512c96
title: Implement the Depth-First Search Algorithm
challengeType: 14
dashedName: implement-the-depth-first-search-algorithm
---
# --description--
<dfn>Depth-first search</dfn> (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 <dfn>depth-first search</dfn> 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));
```
+1 -4
View File
@@ -177,11 +177,8 @@ const duplicatedProjectIds = [
// Stacks
'587d8250367417b2b2512c5f',
// Linked Lists
// Hash Tables
// Depth-first Search
'587d825d367417b2b2512c96',
// Nth Fibonacci Number