-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs_algorithm.py
More file actions
69 lines (48 loc) · 1.97 KB
/
dfs_algorithm.py
File metadata and controls
69 lines (48 loc) · 1.97 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from collections import deque
import heapq
import numpy as np
from visualization import visualize_grid
# Function for DFS Graph Search
def dfs_graph(grid, start, goal):
rows, cols = grid.shape
stack = [(start, [start])]
visited = set()
while stack:
current, path = stack.pop()
# Skip if the node is already visited
if current in visited:
continue
visited.add(current)
visualize_grid(grid, start, goal, [node[0] for node in stack], path)
# Check if the goal is reached
if current == goal:
visualize_grid(grid, start, goal, [], path)
return path
# Explore neighbors
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
neighbor = (current[0] + dx, current[1] + dy)
if (0 <= neighbor[0] < rows and 0 <= neighbor[1] < cols and
grid[neighbor] == 0 and neighbor not in visited):
stack.append((neighbor, path + [neighbor]))
return None
# Function for DFS Tree Search
def dfs_tree(grid, start, goal):
rows, cols = grid.shape
stack = [(start, [start])]
visited = set()
while stack:
current, path = stack.pop()
visualize_grid(grid, start, goal, [node[0] for node in stack], path)
# Goal reached, return the path
if current == goal:
visualize_grid(grid, start, goal, [], path)
return path
# Mark the current node as visited globally
visited.add(current)
# Explore neighbors (avoid nodes already in the path or visited)
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
neighbor = (current[0] + dx, current[1] + dy)
if (0 <= neighbor[0] < rows and 0 <= neighbor[1] < cols and
grid[neighbor] == 0 and neighbor not in path):
stack.append((neighbor, path + [neighbor]))
return None