Posterior a consultar con expertos en la materia, programadores de deferentes áreas y profesores hemos dado con la respuesta al problema y la plasmamos en esta publicación.
Ejemplo 1: dfs python
###############
#The Algorithm(In English):
# 1) Pick any node.
# 2) If it is unvisited, mark it as visited and recur on all its
#adjacentnodes.
# 3) Repeat until all the nodes are visited,or the node to be
#searchedis found.#The graph below(declared as a Python dictionary)#isfrom the linked website and is used for the sake of#testingthe algorithm. Obviously, you will have your own#graphto iterate through.
graph ='A':['B','C'],'B':['D','E'],'C':['F'],'D':[],'E':['F'],'F':[]
visited =set() # Set to keep track of visited nodes.
##################
#The Algorithm(In Code)
def dfs(visited, graph, node):if node not in visited:print(node)
visited.add(node)for neighbour in graph[node]:dfs(visited, graph, neighbour)#Driver Code to test in python yourself.#Note that when calling this, you need to#callthe starting node. In thiscase it is 'A'.dfs(visited, graph,'A')#NOTE: There are a few ways to do DFS, depending on what your#variablesare and/or what you want returned. This specific#exampleis the most fleshed-out, yet still understandable,#explanationI could find.
Ejemplo 2: primer gráfico transversal de profundidad java
publicvoiddepthFirstSearch(Node node)
node.visit();
System.out.print(node.name +" ");
LinkedList<Node> allNeighbors = adjacencyMap.get(node);if(allNeighbors == null)return;for(Node neighbor : allNeighbors)if(!neighbor.isVisited())depthFirstSearch(neighbor);
Comentarios y puntuaciones del post
Si para ti ha sido de provecho nuestro artículo, nos gustaría que lo compartas con el resto entusiastas de la programación y nos ayudes a extender nuestra información.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)