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 generatemazewidth height:
maze # for in rangewidth for in rangeheight
def carvepathx y:
directions # Right, Down, Left, Up
random.shuffledirections
for dx dy in directions:
nx ny x dx y dy
if nx width and ny height and mazenynx#:
mazey dyx dx # Carve path
mazenynx # Move to the next cell
carvepathnx ny
if width : width
if height : height
maze # Start point
carvepath
return maze
# BFS Search used to find the optimal path
def bfssearchmaze start, end:
queue dequestart
visited set
path
while queue:
current queue.popleft
if current end:
break
if current not in visited:
visited.addcurrent
for dx dy in :
nx ny current dx current dy
if nx lenmaze and ny lenmaze and mazenxny:
if nx ny not in visited:
queue.appendnx ny
pathnx ny current
return reconstructpathpath start, end
# DFS Search
def dfssearchmaze start, end:
stack start
visited set
path
while stack:
current stack.pop
if current end:
break
if current not in visited:
visited.addcurrent
for dx dy in :
nx ny current dx current dy
if nx lenmaze and ny lenmaze and mazenxny:
if nx ny not in visited:
stack.appendnx ny
pathnx ny current
return reconstructpathpath start, end
# Path Reconstruction
def reconstructpathpath start, end:
reversepath
current end
while current start:
reversepath.appendcurrent
current path.getcurrent start
reversepath.appendstart
reversepath.reverse
return reversepath
# Accuracy Metric
def calculateaccuracydfspath, bfspath:
# Length of DFS path and BFS path
dfslen lendfspath
bfslen lenbfspath
# Calculate accuracy as the ratio of DFS path length to BFS path length
accuracy dfslen bfslen
printfDFS Path Length: dfslen
printfBFS Optimal Path Length: bfslen
printfAccuracy DFS Path Optimal Path: accuracy:f
return accuracy
# D Maze Visualization
def plotmazemaze pathNone, titleDFS Maze Solution":
height lenmaze
width lenmaze
fig, ax pltsubplotsfigsize
for y in rangeheight:
for x in rangewidth:
if mazeyx#: # Wall
axaddpatchpltRectanglex height y color"black"
else: # Path
axaddpatchpltRectanglex height y color"white"
# Plot the solution path if provided
if path:
for px py in path:
axaddpatchpltCirclepy height px color"blue"
# Add start and end markers
axaddpatchpltCircle height color"green", label"Start"
axaddpatchpltCirclewidth color"red", label"End"
axsetxlim width
axsetylim height
axsetaspectequal
axaxisoff
plttitletitle
pltlegend
pltshow
# Main Function
if namemain:
width, height
maze generatemazewidth height
start
end height width
# BFS to get the optimal path
bfspath bfssearchmaze start, end
# DFS to get the DFS path
dfspath dfssearchmaze start, end
# Calculate and print the accuracy of the DFS algorithm
accuracy calculateaccuracydfspath, bfspath
# Plot the DFS path
plotmazemaze dfspath, titleDFS 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
