The document presents implementations of three search algorithms: Breadth-First Search (BFS), Depth-First Search (DFS), and Uniform-Cost Search (UCS) for navigating a weighted graph. It includes a user interface for selecting start and goal nodes, as well as the desired algorithm, and outputs the complete path, total nodes on the frontier, total nodes generated, and total path cost. An example execution shows the results of using UCS to find a path from node S to G.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
12 views5 pages
Ai Ass 2
The document presents implementations of three search algorithms: Breadth-First Search (BFS), Depth-First Search (DFS), and Uniform-Cost Search (UCS) for navigating a weighted graph. It includes a user interface for selecting start and goal nodes, as well as the desired algorithm, and outputs the complete path, total nodes on the frontier, total nodes generated, and total path cost. An example execution shows the results of using UCS to find a path from node S to G.
frontier = deque([start]) # Queue for BFS visited = set() parent = {start: None} nodes_generated = 1 # Start node is generated while frontier: current = frontier.popleft() if current == goal: break if current not in visited: visited.add(current) # Sort neighbors alphabetically for tie-breaking neighbors = sorted(graph[current].keys()) for next_node in neighbors: if next_node not in visited and next_node not in frontier: frontier.append(next_node) parent[next_node] = current nodes_generated += 1
# Reconstruct path path = []
current = goal while current is not None: path.append(current) current = parent.get(current) path.reverse()
goal): frontier = [start] # Stack for DFS visited = set() parent = {start: None} nodes_generated = 1 while frontier: current = frontier.pop() if current == goal: break if current not in visited: visited.add(current) # Sort neighbors alphabetically and reverse for stack (LIFO) neighbors = sorted(graph[current].keys(), reverse=True) for next_node in neighbors: if next_node not in visited and next_node not in set(frontier): frontier.append(next_node) parent[next_node] = current nodes_generated += 1
# Reconstruct path path = []
current = goal while current is not None: path.append(current) current = parent.get(current) path.reverse()
start)] # Priority queue with (cost, node) visited = set() parent = {start: None} cost_so_far = {start: 0} nodes_generated = 1 while frontier: current_cost, current = heapq.heappop(frontier) if current == goal: break if current not in visited: visited.add(current) neighbors = sorted(graph[current].keys()) # Alphabetical tie-breaking for next_node in neighbors: new_cost = current_cost + graph[current][next_node] if next_node not in cost_so_far or new_cost < cost_so_far[next_node]: cost_so_far[next_node] = new_cost heapq.heappush(frontier, (new_cost, next_node)) parent[next_node] = current if next_node not in visited: nodes_generated += 1
# Reconstruct path path = []
current = goal while current is not None: path.append(current) current = parent.get(current) path.reverse()