diff --git a/guide/portuguese/algorithms/graph-algorithms/depth-first-search/index.md b/guide/portuguese/algorithms/graph-algorithms/depth-first-search/index.md index a687f2f7c0..b6aac5fb52 100644 --- a/guide/portuguese/algorithms/graph-algorithms/depth-first-search/index.md +++ b/guide/portuguese/algorithms/graph-algorithms/depth-first-search/index.md @@ -10,91 +10,141 @@ Profundidade A primeira pesquisa é um dos algoritmos gráficos mais simples. El ![](https://upload.wikimedia.org/wikipedia/commons/7/7f/Depth-First-Search.gif) -### Implementação (C ++ 14) +### Implementation (C++14) -\`\` \`c ++ - -# incluir - -# incluir - -# incluir - -# incluir - -usando namespace std; - -class Graph { na TV; // número de vértices -``` -// pointer to a vector containing adjacency lists - vector < int > *adj; -``` - -público: Gráfico (int v); // Construtor -``` -// function to add an edge to graph - void add_edge(int v, int w); - - // prints dfs traversal from a given source `s` - void dfs(); - void dfs_util(int s, vector < bool> &visited); -``` - -}; - -Gráfico :: Gráfico (int v) { isso -> v = v; adj = novo vetor \[v\]; } - -void Graph :: add _edge (int u, int v) { adj \[u\] .push de_ volta (v); // adiciona v à lista de u adj \[v\] .push de _volta (v); // adicione u à lista de v (remova essa declaração se o gráfico for direcionado!) } void Graph :: dfs () { // visited vector - para acompanhar os nós visitados durante o DFS vetor visitado (v, falso); // marcando todos os nós / vértices como não visitados para (int i = 0; i e visitado) { // marcar o nó / vértice atual como visitado visitou \[s\] = verdadeiro; // saída para a saída padrão (tela) cout << s << ""; -``` -// traverse its adjacency list and recursively call dfs_util for all of its neighbours! - // (only if the neighbour has not been visited yet!) - for(vector < int > :: iterator itr = adj[s].begin(); itr != adj[s].end(); itr++) - if(!visited[*itr]) - dfs_util(*itr, visited); -``` - -} - -int main () { // cria um gráfico usando a classe Graph que definimos acima Gráfico g (4); g.add _edge (0, 1); g.add_ edge (0, 2); g.add _edge (1, 2); g.add_ edge (2, 0); g.add _edge (2, 3); g.add_ edge (3, 3); -``` -cout << "Following is the Depth First Traversal of the provided graph" - << "(starting from vertex 0): "; - g.dfs(); - // output would be: 0 1 2 3 - return 0; -``` - -} + ```c++ +#include +#include +#include +#include + using namespace std; + +class Graph{ + int v; // number of vertices + + // pointer to a vector containing adjacency lists + vector < int > *adj; +public: + Graph(int v); // Constructor + + // function to add an edge to graph + void add_edge(int v, int w); + + // prints dfs traversal from a given source `s` + void dfs(); + void dfs_util(int s, vector < bool> &visited); +}; + +Graph::Graph(int v){ + this -> v = v; + adj = new vector < int >[v]; +} + +void Graph::add_edge(int u, int v){ + adj[u].push_back(v); // add v to u’s list + adj[v].push_back(v); // add u to v's list (remove this statement if the graph is directed!) +} + void Graph::dfs(){ + // visited vector - to keep track of nodes visited during DFS + vector < bool > visited(v, false); // marking all nodes/vertices as not visited + for(int i = 0; i < v; i++) + if(!visited[i]) + dfs_util(i, visited); +} + // notice the usage of call-by-reference here! +void Graph::dfs_util(int s, vector < bool > &visited){ + // mark the current node/vertex as visited + visited[s] = true; + // output it to the standard output (screen) + cout << s << " "; + + // traverse its adjacency list and recursively call dfs_util for all of its neighbours! + // (only if the neighbour has not been visited yet!) + for(vector < int > :: iterator itr = adj[s].begin(); itr != adj[s].end(); itr++) + if(!visited[*itr]) + dfs_util(*itr, visited); +} + +int main() +{ + // create a graph using the Graph class we defined above + Graph g(4); + g.add_edge(0, 1); + g.add_edge(0, 2); + g.add_edge(1, 2); + g.add_edge(2, 0); + g.add_edge(2, 3); + g.add_edge(3, 3); + + cout << "Following is the Depth First Traversal of the provided graph" + << "(starting from vertex 0): "; + g.dfs(); + // output would be: 0 1 2 3 + return 0; +} ``` ### Evaluation - Space Complexity: O(n) + Complexidade Espacial: O(n) - Worse Case Time Complexity: O(n) - Depth First Search is complete on a finite set of nodes. I works better on shallow trees. + Complexidade Temporal no pior caso: O(n) + A DFS é completa num numero finito de nós. Trabalhando melhor em arvores esparssas. ### Implementation of DFS in C++ +```c++ +#include +#include +#include + +using namespace std; + +struct Graph{ + int v; + bool **adj; + public: + Graph(int vcount); + void addEdge(int u,int v); + void deleteEdge(int u,int v); + vector DFS(int s); + void DFSUtil(int s,vector &dfs,vector &visited); +}; +Graph::Graph(int vcount){ + this->v = vcount; + this->adj=new bool*[vcount]; + for(int i=0;iadj[i]=new bool[vcount]; + for(int i=0;iadj[u][w]=true; + this->adj[w][u]=true; +} + +void Graph::deleteEdge(int u,int w){ + this->adj[u][w]=false; + this->adj[w][u]=false; +} + +void Graph::DFSUtil(int s, vector &dfs, vector &visited){ + visited[s]=true; + dfs.push_back(s); + for(int i=0;iv;i++){ + if(this->adj[s][i]==true && visited[i]==false) + DFSUtil(i,dfs,visited); + } +} + +vector Graph::DFS(int s){ + vector visited(this->v); + vector dfs; + DFSUtil(s,dfs,visited); + return dfs; +} ``` -c ++ - -# incluir - -# incluir - -# incluir - -usando namespace std; - -struct graph { na TV; bool \* _adj; público: Gráfico (int vcount); void addEdge (int u, int v); void deleteEdge (int u, int v); vetor_ _DFS (int s); void DFSUtil (int s, vetor_ _& dfs, vetor_ _&visitou); }; Gráfico :: Graph (int vcount) { isto-> v = vcount; this-> adj = novo bool_ \[vcount\]; para (int i = 0; i - -void Graph :: addEdge (int u, int w) { this-> adj \[u\] \[w\] = verdadeiro; isto-> adj \[w\] \[u\] = verdadeiro; } - -void Graph :: deleteEdge (int u, int w) { isto-> adj \[u\] \[w\] = falso; isto-> adj \[w\] \[u\] = falso; } - -void Graph :: DFSUtil (int s, vetor & dfs, vetor &visitou){ visitou \[s\] = verdadeiro; dfs.push\_back (s); para (int i = 0; i v; i ++) { if (this-> adj \[s\] \[i\] == verdadeiro && visitado \[i\] == falso) DFSUtil (i, dfs, visitado); } } - -vetor Gráfico :: DFS (int s) { vetor visitado (this-> v); vetor dfs; DFSUtil (s, dfs, visitado); return dfs; } \`\` \` #### Mais Informações: @@ -102,4 +152,4 @@ vetor Gráfico :: DFS (int s) { vetor visitado (this-> v); vetor dfs; DFSUtil (s [Largura da Primeira Pesquisa (BFS)](https://github.com/freecodecamp/guides/tree/master/src/pages/algorithms/graph-algorithms/breadth-first-search/index.md) -[Primeira pesquisa de profundidade (DFS) - Wikipedia](https://en.wikipedia.org/wiki/Depth-first_search) \ No newline at end of file +[Primeira pesquisa de profundidade (DFS) - Wikipedia](https://en.wikipedia.org/wiki/Depth-first_search)