Lab-j-6 Data Sci
Lab-j-6 Data Sci
1
Lab # 6: Uninformed Search
Objectives:
To learn about uniformed searching, recall your previous lab on Graphs in Python where you implemented a
basic path finding algorithm for Directed, Weighted graphs.
Tools Used:
2
Lab Tasks:
while stack:
node = stack.pop()
if node not in visited:
visited.add(node)
path.append(node)
stack.extend(reversed(graph[node]))
return path
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F', 'G'],
'D': [],
'E': ['H'],
'F': ['I'],
3
'G': [],
'H': [],
'I': []
}
if __name__ == "__main__":
start_node = 'A'
dfs_path = dfs_iterative(graph, start_node)
print("DFS Traversal Path:", dfs_path)
Task # 2: Consider the graph given below and traverse the graph through DFS using the network library.
import networkx as nx
def create_graph():
G = nx.DiGraph()
edges = [
G.add_edges_from(edges)
return G
if __name__ == "__main__":
graph = create_graph()
Consider a room service robot that has three rooms (A, B, C) to serve. There is a service room as well.
The robot can serve one room at a time and must go back to the service room before serving the next
room.
Each room will get exactly one service.
Initial state: all rooms need service; robot is in the service room.
Actions: Left move + serve, right move+ serve, up move+ serve, back
5
Make a state space tree. Apply Depth first and Iterative Deepening search to find the goal. Display the path and
path cost. Consider Left move + serve, right move+ serve, have cost 5, whereas up move+ serve has unit cost
Your functions of DFS and IDDFS should each return the first path they find from the start node to the goal node,
as well as the total cost of the path undertaken. Note that it is possible to return more than one item/variable in
python using the syntax:
DFS
class RoomServiceRobot:
def __init__(self):
self.graph = {
'C': {'S': 0}
while stack:
if node == goal:
6
return path, cost
if __name__ == "__main__":
robot = RoomServiceRobot()
IDDFS
class RoomServiceRobotIDDFS:
def __init__(self):
self.graph = {
'C': {'S': 0}
}
7
def iddfs(self, start, goal, max_depth=5):
if node == goal:
if depth == 0:
if result:
if result:
if __name__ == "__main__":
robot = RoomServiceRobotIDDFS()
8
9