Easy AI Mod2
Easy AI Mod2
1)
BFS
Breadth First Search Algorithm
Here is the Breadth-First Search (BFS) algorithm in a simple, step-by-step manner:
BFS is used for traversing or searching tree or graph data structures. It explores all the
vertices at the present depth level before moving on to the next depth level.
Steps:
1. Initialize:
• Create a queue and enqueue the starting node.
• Mark the starting node as visited.
2. Process:
• While the queue is not empty:
1. Dequeue the front node from the queue and process it.
2. Get all the adjacent (neighboring) nodes of the dequeued node.
3. For each unvisited neighbor:
• Mark it as visited.
• Enqueue it.
3. Repeat:
• Repeat Step 2 until the queue becomes empty.
Pseudocode:
BFS(graph, start_node):
Create an empty queue Q
Mark start_node as visited and enqueue it into Q
# Call BFS
bfs(graph, 'A')
Output Example:
Steps:
1. Initialize:
• Create a stack and push the starting node.
• Mark the starting node as visited.
2. Process:
• While the stack is not empty:
1. Pop the top node from the stack and process it.
2. For each unvisited neighbor of the popped node:
• Mark it as visited.
• Push it onto the stack.
Pseudocode:
DFS(graph, start_node):
Create an empty stack S
Mark start_node as visited and push it into S
# Example graph
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
# Call DFS
dfs(graph, 'A')
Output Example:
DLS limits the depth of exploration to avoid infinite traversal in cyclic or infinite graphs.
Steps:
1. Initialize:
• Create a stack and push the starting node with depth 0.
• Define a maximum depth limit.
2. Process:
• While the stack is not empty:
1. Pop the top node and its depth from the stack.
2. Process the node if it is within the depth limit.
3. For each unvisited neighbor:
• Push the neighbor and its depth onto the stack if the depth is within the limit.
Pseudocode:
Steps:
1. Define a recursive function:
• If the current depth exceeds the limit, return.
• Process the node.
• Recur for each unvisited neighbor.
Pseudocode:
Process current_node
visited = set()
dls_recursive(graph, 'A', visited, 2, 0)
UCS is a search algorithm used to find the least-cost path in a weighted graph. It uses a
priority queue to explore nodes with the lowest cumulative cost first.
Steps:
1. Initialize a priority queue and add the start node with cost 0.
2. While the queue is not empty:
• Dequeue the node with the smallest cost.
• If the node is the goal, return the cost.
• Otherwise, add all neighbors with updated costs to the queue.
Pseudocode:
if current_node == goal:
return cost
# Call UCS
print(ucs(graph, 'A', 'F'))
5)
PROBLEM SOLVING AGENTS
EXAMPLE PROBLEMS (Toy problems)
A toy problem is intended to illustrate or exercise various problem-solving methods. It can be
given a concise, exact description and hence is usable by different researchers to compare
the performance of algorithms. A real-world problem is one whose solutions people actually
care about. Such problems tend not to have a single agreed-upon description, but we can
give the general flavour of their formulations.
2.1. Vacuum world
The State space for the vacuum world is shown in figure3.3. Vacuum world problem can be
formulated as
a problem as follows:
• States: The state is determined by both the agent location and the dirt locations. The agent
is in one of two locations, each of which might or might not contain dirt. Thus, there are
2 × 2^2= 8 possible world states. A larger environment with n locations has n · 2n
states.
Properties:
Whether the algorithm is complete/optimal depends on the search strategies in both
searches.
1. Time complexity: O(b d/2 ) (Assume BFS is used) Checking node for membership in the
other
search tree can be done in constant time.
2. Space complexity: O(b d/2 ) (Assume BFS is used)
At least one of the search tree must be kept in memory for membership checking.
3. Optimal: Bidirectional search is Optimal.
Advantages:
Bidirectional search is fast.
Bidirectional search requires less memory
Disadvantages:
Implementation of the bidirectional search tree is difficult.
In bidirectional search, one should know the goal state in advance.