Pouvez-vous s'il vous plaît laissez-moi savoir ce qui est incorrect dans le code DFS ci-dessous. Il donne un résultat correct AFAIK, mais je ne sais pas quand il échouera.
graph1 = {
'A' : ['B','S'],
'B' : ['A'],
'C' : ['D','E','F','S'],
'D' : ['C'],
'E' : ['C','H'],
'F' : ['C','G'],
'G' : ['F','S'],
'H' : ['E','G'],
'S' : ['A','C','G']
}
visited = []
def dfs(graph,node):
global visited
if node not in visited:
visited.append(node)
for n in graph[node]:
dfs(graph,n)
dfs(graph1,'A')
print(visited)
Production:
['A', 'B', 'S', 'C', 'D', 'E', 'H', 'G', 'F']
Je pense que vous avez là un problème d'indentation. En supposant que votre code ressemble à ceci:
graph1 = {
'A' : ['B','S'],
'B' : ['A'],
'C' : ['D','E','F','S'],
'D' : ['C'],
'E' : ['C','H'],
'F' : ['C','G'],
'G' : ['F','S'],
'H' : ['E','G'],
'S' : ['A','C','G']
}
visited = []
def dfs(graph,node):
global visited
if node not in visited:
visited.append(node)
for n in graph[node]:
dfs(graph,n)
dfs(graph1,'A')
print(visited)
Je dirais deux ou trois choses:
plus:
Code mis à jour:
graph1 = {
'A' : ['B','S'],
'B' : ['A'],
'C' : ['D','E','F','S'],
'D' : ['C'],
'E' : ['C','H'],
'F' : ['C','G'],
'G' : ['F','S'],
'H' : ['E','G'],
'S' : ['A','C','G']
}
def dfs(graph, node, visited):
if node not in visited:
visited.append(node)
for n in graph[node]:
dfs(graph,n, visited)
return visited
visited = dfs(graph1,'A', [])
print(visited)
Sans récursivité:
def dfs(graph, node):
visited = [node]
stack = [node]
while stack:
node = stack[-1]
if node not in visited:
visited.extend(node)
remove_from_stack = True
for next in graph[node]:
if next not in visited:
stack.extend(next)
remove_from_stack = False
break
if remove_from_stack:
stack.pop()
return visited
print (dfs(graph1, 'A'))
Production:
['A', 'B', 'S', 'C', 'D', 'E', 'H', 'G', 'F']
Voici une implémentation itérative (non récursive) d'un DFS:
def dfs_iterative(graph, start_vertex):
visited = set()
traversal = []
stack = [start_vertex]
while stack:
vertex = stack.pop()
if vertex not in visited:
visited.add(vertex)
traversal.append(vertex)
stack.extend(reversed(graph[vertex])) # add vertex in the same order as visited
return traversal
test_graph = {
'A' : ['B','S'],
'B' : ['A'],
'C' : ['D','E','F','S'],
'D' : ['C'],
'E' : ['C','H'],
'F' : ['C','G'],
'G' : ['F','S'],
'H' : ['E','G'],
'S' : ['A','C','G']
}
print(dfs_iterative(test_graph, 'A'))
Production:
['A', 'B', 'S', 'C', 'D', 'E', 'H', 'G', 'F']
graph = {'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']}
def dfs(s,d):
def dfs_helper(s,d):
if s == d:
return True
if s in visited :
return False
visited.add(s)
for c in graph[s]:
dfs_helper(c,d)
return False
visited = set()
return dfs_helper(s,d)
dfs('A','E') ---- True
dfs('A','M') ---- False
from collections import defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
def DFS(self,v,vertex):
visited = [False]*vertex
print(self. graph)
# print(len(self.graph),"+++")
self.DFSUtil(v,visited)
def DFSUtil(self,v,visited):
visited[v]=True
print(v)
for i in self.graph[v]:
if visited[i] == False:
# print(visited)
self.DFSUtil(i,visited)
g= Graph()
vertex=7
g.addEdge(0,1)
g.addEdge(0,2)
g.addEdge(0,6)
g.addEdge(0,5)
g.addEdge(5,3)
g.addEdge(5,4)
g.addEdge(4,3)
g.addEdge(6,4)
g.DFS(0,vertex)
Il s'agit de la modification du code ci-dessus car cela ne fonctionne pas dans tous les cas.
Nous devons spécifier le nombre de vecteurs et ensuite donner des arêtes manuellement.
Implémentation DFS en Python
from collections import defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def addEdge(self, u, v):
self.graph[u].append(v)
def DFSUtil(self, v, visited):
visited[v]=True
print(v)
for i in self.graph[v]:
if visited[i] == False:
self.DFSUtil(i, visited)
def DFS(self):
V = len(self.graph)
visited = [False]*(V)
for i in range(V):
if visited[i] == False:
self.DFSUtil(i, visited)
# Driver code
# Create a graph given in the above diagram
g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
print("Following is Depth First Traversal")
g.DFS()
Source :: ceci