--- id: 587d825d367417b2b2512c96 title: Depth-First Search challengeType: 1 videoUrl: '' localeTitle: Pesquisa de Profundidade-Primeiro --- ## Description
Semelhante à pesquisa em largura , aqui aprenderemos sobre outro algoritmo de travessia de gráfico chamado de busca em profundidade . Enquanto a pesquisa por largura inicial pesquisa comprimentos de aresta incrementais longe do nó de origem, a pesquisa em profundidade primeiro desce um caminho de arestas o máximo possível. Uma vez que atinja uma extremidade de um caminho, a pesquisa retrocederá até o último nó com um caminho de borda não visitado e continuará pesquisando. Visualmente, isso é o que o algoritmo está fazendo, onde o nó superior é o ponto inicial da pesquisa. Uma saída simples desse algoritmo é uma lista de nós que podem ser alcançados de um determinado nó. Portanto, ao implementar esse algoritmo, você precisará acompanhar os nós que você visita.
## Instructions
Escreva uma função dfs() que tenha um graph matriz de adjacência não graph e uma root rótulo de nó como parâmetros. O rótulo do nó será apenas o valor numérico do nó entre 0 e n - 1 , onde n é o número total de nós no gráfico. Sua função deve produzir uma matriz de todos os nós acessíveis pela root .
## Tests
```yml tests: - text: 'O gráfico de entrada [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]] com um nó inicial de 1 deve retornar uma matriz com 0 , 1 , 2 e 3 .' testString: '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 1 should return an array with 0, 1, 2, and 3.");' - text: 'O gráfico de entrada [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0]] com um nó inicial de 1 deve retornar um array com quatro elementos.' testString: '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, 1], [0, 0, 1, 0]] with a start node of 1 should return an array with four elements.");' - text: 'O gráfico de entrada [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]] com um nó inicial de 3 deve retornar um array com 3 .' testString: '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 3.");' - text: 'O gráfico de entrada [[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]] com um nó inicial de 3 deve retornar um array com um elemento.' testString: '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, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]] with a start node of 3 should return an array with one element.");' - text: 'O gráfico de entrada [[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]] com um nó inicial de 3 deve retornar um array com 2 e 3 .' testString: '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 2 and 3.");' - text: 'O gráfico de entrada [[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]] com um nó inicial de 3 deve retornar um array com dois elementos.' testString: '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 3 should return an array with two elements.");' - text: 'O gráfico de entrada [[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]] com um nó inicial de 0 deve retornar uma matriz com 0 e 1 .' testString: '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 0 and 1.");' - text: 'O gráfico de entrada [[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]] com um nó inicial de 0 deve retornar um array com dois elementos.' testString: '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, "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.");' ```
## Challenge Seed
```js function dfs(graph, root) { } var exDFSGraph = [ [0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0] ]; console.log(dfs(exDFSGraph, 3)); ```
## Solution
```js // solution required ```