0% found this document useful (0 votes)
10 views3 pages

Ailab0101.ipynb - Colab

Uploaded by

K27 Sneha Bharti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Ailab0101.ipynb - Colab

Uploaded by

K27 Sneha Bharti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

from collections import deque

def BFS(maze, start, end):


q = deque([(start, [start])])
v = set([start])

while q:
current, path = q.popleft()

if current == end:
return path

for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
neighbour = (current[0] + dx, current[1] + dy)

if 0 <= neighbour[0] < len(maze) and 0 <= neighbour[1] < len(maze[0]) and maze[
v.add(neighbour)
q.append((neighbour, path + [neighbour]))

return "No path found"

def DFS(maze, start, end):


s = [(start, [start])]
v = set([start])
p = []

while s:
current, path = s.pop()

if current == end:
p.append(path)

for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
neighbour = (current[0] + dx, current[1] + dy)

if 0 <= neighbour[0] < len(maze) and 0 <= neighbour[1] < len(maze[0]) and maze[
v.add(neighbour)
s.append((neighbour, path + [neighbour]))

if not p:
return "No path found"
else:
return p[0]

def main():
maze = [
[1, 0, 1, 1, 0],
[1, 0, 1, 0, 1],
[1, 1, 1, 1, 1],
[0, 0, 0, 1, 0],
[1, 1, 1, 1, 1]
]

start = (0, 0)
end = (4, 4)

bfs_path = BFS(maze, start, end)


dfs_path = DFS(maze, start, end)
print("Sneha Bharti")
print(22051893)
from datetime import datetime
import pytz
local_timezone = pytz.timezone("Asia/Kolkata")
current_datetime = datetime.now(local_timezone)
print(current_datetime)

print("BFS Path:", bfs_path)


print("DFS Path:", dfs_path)

Sneha Bharti
22051893
2025-01-06 20:23:07.565313+05:30
BFS Path: [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (2, 3), (3, 3), (4, 3), (4, 4
DFS Path: [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (2, 3), (3, 3), (4, 3), (4, 4

from collections import heapq


def GreedyBestFirstSearch(start, goal):
pq=[]
heapq.heappush(pq, (grid[start[0]][start[1]],start))
path={start:none}
visited=set()
while pq:
current=heapq.heappop(pq)[1]
if current==goal:
return path

for next in neighbors(current):


if next not in visited:
heapq.heappush(pq, (grid[next[0]][next[1]],next))
path[next]=current
visited.add(next)
return"No Path Found"
def path(path,start,goal):
result=[]
current=goal
while current!=start:
result.append(current)
current=path[current]
result.reverse()
return result

def neighnors(cell):
row,col=cell
result=[]
for dr

You might also like