AI Lab Report 2
AI Lab Report 2
Lab Report NO 02
Name ID
1. Tahsin Ahmmed 212002063
BFS:
Define a function create_node(x, y, level) to create a node with coordinates (x, y) and a
given level.
Implement a function is_valid(graph, x, y) to check if the given coordinates (x, y) are
valid within the graph boundaries and represent an accessible cell.
Implement the Breadth-First Search (BFS) algorithm bfs(graph, source, goal) to find the
shortest path from a given source node to a goal node within the graph. It utilizes a queue
to explore neighboring nodes level by level until reaching the goal.
In the main block, define the graph and source and goal states. Then, execute the BFS
algorithm to find the shortest path. Print the number of moves required if the goal is
found; otherwise, indicate that the goal cannot be reached from the starting block.
DFS:
Define a function create_node(x, y, level) to create a node with coordinates (x, y) and a
given level.
Implement a function is_valid(graph, x, y) to check if the given coordinates (x, y) are
valid within the graph boundaries and represent an accessible cell.
Implement the Depth-First Search (DFS) algorithm dfs(graph, source, goal) to find a path
from a given source node to a goal node within the graph. It utilizes a stack to explore
neighboring nodes in a depth-first manner until reaching the goal or exhausting all
possible paths.
In the main block, define the graph and source and goal states. Then, execute the DFS
algorithm to find a path to the goal. If the goal is reached, print the number of moves
required; otherwise, indicate that the goal cannot be reached.
4. IMPLEMENTATION
BFS:
1. def create_node(x, y, level):
2. # print((x,y,level))
3.
5.
6.
12.
13.
17.
22.
28.
32.
36. """
37. v_x = u[0] + dx: This line calculates
38.
41.
43.
44. """
45.
46. # print(u[0])
47. # print(dx)
48. # print(v_x)
49. v_y = u[1] + dy
50. # print(v_y)
51. # print(v_x, v_y)
52.
57.
59.
68.
71.
72. level = bfs(graph, source, goal)
73. #print(level)
74.
DFS:
1. def create_node(x, y, level):
2. return (x, y, level)
3.
4. def is_valid(graph, x, y):
5. N = len(graph)
6. return 0 <= x < N and 0 <= y < N and graph[x][y] == 1
7.
8. def dfs(graph, source, goal):
9. directions = [(1, 0), (-1, 0), (0, 1), (0, -1)] # Up, down, left, right
10. N = len(graph)
11.
12. stack = [source]
13. visited = set([source[:2]])
14.
15. while stack:
16. x, y, level = stack.pop(len(stack)-1)
17.
18. if (x, y) == goal:
19. print("Goal achieved!")
20. print("Number of moves required:", level)
21. return
22.
23. for dx, dy in directions:
24. v_x = x + dx
25. v_y = y + dy
26.
27. if is_valid(graph, v_x, v_y) and (v_x, v_y) not in visited:
28. visited.add((v_x, v_y)) # Mark as visited
29. new_node = create_node(v_x, v_y, level + 1) # Increment level for th
30. stack.append(new_node) # Add neighboring node with incremented level
31.
32. print("Goal not reachable")
33.
34. if __name__ == "__main__":
35. graph = [
36. [0, 0, 1, 0, 1],
37. [0, 1, 1, 1, 1],
38. [0, 1, 0, 0, 1],
39. [1, 1, 0, 1, 1],
40. [1, 0, 0, 0, 1]
41. ]
42.
43. source = (0, 2, 0) # source state (x, y, level)
44. goal = (4, 4) # goal state (x, y)
45.
46. dfs(graph, source, goal)
BFS:
DFS:
In the discussion section, we compare and contrast Depth-First Search (DFS) and Breadth-First
Search (BFS) algorithms implemented in Python. We note that DFS explores as far as possible
along each branch before backtracking, resulting in a more memory-efficient approach but
potentially leading to longer paths. On the other hand, BFS explores all neighbors of a node
before moving on to the next level, ensuring the shortest path is found but requiring more
memory. We discuss the implications of these differences in various applications, such as
puzzle-solving or network routing, where one algorithm may be preferred over the other based
on memory constraints or the need for optimal solutions. Additionally, we emphasize the
importance of understanding the characteristics and trade-offs of each algorithm in selecting the
appropriate one for a given problem domain.