











































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
AI journal with practicals in it
Typology: Lab Reports
1 / 51
This page cannot be seen from the preview
Don't miss anything!
graph = { 'A': ['B', 'C'], 'B': ['D', 'E'], 'C': ['F', 'G'], 'D': [], 'E': [], 'F': [], 'G': [] }
queue = [] def dfs(graph, node, goal): queue.append(node) while queue: s = queue.pop(0) print(s, "\n") if s == goal: print("The goal state", goal, "has been reached") return True for neighbor in graph[s]: if neighbor not in queue: # Prevent revisiting nodes queue.append(neighbor) for i in graph[neighbor]: if i not in queue: queue.append(i) goal = input("Enter the goal state you want to reach: ") dfs(graph, 'A', goal)
stack.extend(neighbor for neighbor in reversed(graph[s]) if neighbor not in visited) print(f"The goal state {goal} was not found") return False graph = { 'A': ['B', 'C'], 'B': ['D', 'E'], 'C': ['F', 'G'], 'D': ['H', 'I'], 'E': ['J', 'K'], 'F': ['L'], 'G': ['M', 'N'], 'H': [], 'I': [], 'J': [], 'K': [], 'L': [], 'M': [] } goal = input("Enter the goal state you want to reach: ") dfs(graph, 'A', goal) Output:-