Question: import matplotlib.pyplot as plt import random from collections import deque # Maze Generator def generate _ maze ( width , height ) : maze =

import matplotlib.pyplot as plt
import random
from collections import deque
# Maze Generator
def generate_maze(width, height):
maze =[['#' for _ in range(width)] for _ in range(height)]
def carve_path(x, y):
directions =[(0,1),(1,0),(0,-1),(-1,0)] # Right, Down, Left, Up
random.shuffle(directions)
for dx, dy in directions:
nx, ny = x + dx *2, y + dy *2
if 0< nx < width -1 and 0< ny < height -1 and maze[ny][nx]=='#':
maze[y + dy][x + dx]='' # Carve path
maze[ny][nx]='' # Move to the next cell
carve_path(nx, ny)
if width %2==0: width +=1
if height %2==0: height +=1
maze[1][1]='' # Start point
carve_path(1,1)
return maze
# BFS Search (used to find the optimal path)
def bfs_search(maze, start, end):
queue = deque([start])
visited = set()
path ={}
while queue:
current = queue.popleft()
if current == end:
break
if current not in visited:
visited.add(current)
for dx, dy in [(0,1),(1,0),(0,-1),(-1,0)]:
nx, ny = current[0]+ dx, current[1]+ dy
if 0<= nx < len(maze) and 0<= ny < len(maze[0]) and maze[nx][ny]=='':
if (nx, ny) not in visited:
queue.append((nx, ny))
path[(nx, ny)]= current
return reconstruct_path(path, start, end)
# DFS Search
def dfs_search(maze, start, end):
stack =[start]
visited = set()
path ={}
while stack:
current = stack.pop()
if current == end:
break
if current not in visited:
visited.add(current)
for dx, dy in [(0,1),(1,0),(0,-1),(-1,0)]:
nx, ny = current[0]+ dx, current[1]+ dy
if 0<= nx < len(maze) and 0<= ny < len(maze[0]) and maze[nx][ny]=='':
if (nx, ny) not in visited:
stack.append((nx, ny))
path[(nx, ny)]= current
return reconstruct_path(path, start, end)
# Path Reconstruction
def reconstruct_path(path, start, end):
reverse_path =[]
current = end
while current != start:
reverse_path.append(current)
current = path.get(current, start)
reverse_path.append(start)
reverse_path.reverse()
return reverse_path
# Accuracy Metric
def calculate_accuracy(dfs_path, bfs_path):
# Length of DFS path and BFS path
dfs_len = len(dfs_path)
bfs_len = len(bfs_path)
# Calculate accuracy as the ratio of DFS path length to BFS path length
accuracy = dfs_len / bfs_len
print(f"DFS Path Length: {dfs_len}")
print(f"BFS Optimal Path Length: {bfs_len}")
print(f"Accuracy (DFS Path / Optimal Path): {accuracy:.2f}")
return accuracy
# 2D Maze Visualization
def plot_maze(maze, path=None, title="DFS Maze Solution"):
height = len(maze)
width = len(maze[0])
fig, ax = plt.subplots(figsize=(6,6))
for y in range(height):
for x in range(width):
if maze[y][x]=='#': # Wall
ax.add_patch(plt.Rectangle((x, height - y -1),1,1, color="black"))
else: # Path
ax.add_patch(plt.Rectangle((x, height - y -1),1,1, color="white"))
# Plot the solution path (if provided)
if path:
for px, py in path:
ax.add_patch(plt.Circle((py +0.5, height - px -0.5),0.3, color="blue"))
# Add start and end markers
ax.add_patch(plt.Circle((1.5, height -1.5),0.3, color="green", label="Start"))
ax.add_patch(plt.Circle((width -2.5,0.5),0.3, color="red", label="End"))
ax.set_xlim(0, width)
ax.set_ylim(0, height)
ax.set_aspect('equal')
ax.axis('off')
plt.title(title)
plt.legend()
plt.show()
# Main Function
if _name_=="_main_":
width, height =11,11
maze = generate_maze(width, height)
start =(1,1)
end =(height -2, width -2)
# BFS to get the optimal path
bfs_path = bfs_search(maze, start, end)
# DFS to get the DFS path
dfs_path = dfs_search(maze, start, end)
# Calculate and print the accuracy of the DFS algorithm
accuracy = calculate_accuracy(dfs_path, bfs_path)
# Plot the DFS path
plot_maze(maze, dfs_path, title="DFS Solution") Please explain the results of the code in boring detail in an academic way because it will be put in a report. Please explain what is the best treatment to work on the Inverse or Forward code. Please write the equation and explain it in detail.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!