-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.py
More file actions
31 lines (28 loc) · 740 Bytes
/
DFS.py
File metadata and controls
31 lines (28 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def dfs_path_planning(graph, start, end):
stack = [[start]]
visited = set([start])
while stack:
path = stack.pop()
current=path[-1]
if current == end:
return path
for neighbor in (graph[current]):
if neighbor not in visited:
visited.add(neighbor)
stack.append(path +[neighbor])
return "No path found"
graph = {
'A': ['B', 'C'],
'B': ['A', 'C', 'D'],
'C': ['A', 'B', 'D'],
'D': ['B', 'C', 'E'],
'E': ['D', 'F'],
'F': ['E']
}
start = 'A'
end = 'E'
path = dfs_path_planning(graph, start, end)
if path == "No path found":
print("No path Found")
else:
print(f"Path found from{start} to {end}:{path}")