0% found this document useful (0 votes)
3 views10 pages

Easy AI Mod2

The document outlines various graph traversal algorithms including Breadth-First Search (BFS), Depth-First Search (DFS), Iterative Depth-Limited Search (DLS), and Uniform Cost Search (UCS), detailing their steps and pseudocode. It also introduces problem-solving agents through toy problems like the vacuum world, explaining state space, actions, and goal tests. Additionally, it discusses bidirectional search, its properties, advantages, and disadvantages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Easy AI Mod2

The document outlines various graph traversal algorithms including Breadth-First Search (BFS), Depth-First Search (DFS), Iterative Depth-Limited Search (DLS), and Uniform Cost Search (UCS), detailing their steps and pseudocode. It also introduces problem-solving agents through toy problems like the vacuum world, explaining state space, actions, and goal tests. Additionally, it discusses bidirectional search, its properties, advantages, and disadvantages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Module 2:

1)
BFS
Breadth First Search Algorithm
Here is the Breadth-First Search (BFS) algorithm in a simple, step-by-step manner:

Breadth-First Search (BFS) Algorithm

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

while Q is not empty:


current_node = Dequeue(Q)
Process current_node // Example: print(current_node)

for each neighbor in graph[current_node]:


if neighbor is not visited:
Mark neighbor as visited
Enqueue neighbor into Q

# Example graph as an adjacency list


graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}

# Call BFS
bfs(graph, 'A')

Output Example:

For the graph provided:

A -> B -> C -> D -> E -> F

Let me know if you need further clarifications or modifications!

1. Depth-First Search (DFS) Algorithm


Depth First Search Algorithm
DFS is a graph traversal algorithm that explores as far as possible along a branch before
backtracking.

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

while S is not empty:


current_node = Pop(S)
Process current_node // Example: print(current_node)

for each neighbor in graph[current_node]:


if neighbor is not visited:
Mark neighbor as visited
Push neighbor into S

# Example graph
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}

# Call DFS
dfs(graph, 'A')

Output Example:

For the graph provided:

A -> C -> F -> B -> E -> D

2. Iterative Depth-Limited Search (DLS)

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:

DLS(graph, start_node, limit):


Create an empty stack S
Push (start_node, 0) into S // Node and depth

while S is not empty:


current_node, depth = Pop(S)

if depth <= limit:


Process current_node

for each neighbor in graph[current_node]:


if neighbor is not visited:
Push (neighbor, depth + 1) into S
3. Depth-Limited Search (Recursive)

This is the recursive implementation of Depth-Limited Search.

Steps:
1. Define a recursive function:
• If the current depth exceeds the limit, return.
• Process the node.
• Recur for each unvisited neighbor.

Pseudocode:

DLS_Recursive(graph, current_node, limit, depth):


if depth > limit:
return

Process current_node

for each neighbor in graph[current_node]:


if neighbor is not visited:
Mark neighbor as visited
DLS_Recursive(graph, neighbor, limit, depth + 1)

dls_recursive(graph, neighbor, visited, limit, depth + 1)

visited = set()
dls_recursive(graph, 'A', visited, 2, 0)

4. Uniform Cost Search (UCS)

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:

UCS(graph, start_node, goal):


Create a priority queue Q
Add (start_node, 0) to Q // Node and cumulative cost
while Q is not empty:
current_node, cost = Dequeue(Q)

if current_node == goal:
return cost

for neighbor, edge_cost in graph[current_node]:


if neighbor is not visited:
Add (neighbor, cost + edge_cost) to Q

# Example weighted graph


graph = {
'A': [('B', 1), ('C', 4)],
'B': [('D', 2), ('E', 5)],
'C': [('F', 3)],
'D': [],
'E': [('F', 1)],
'F': []
}

# 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.

• Initialstate: Any state can be designated as the initial state.


• Actions: In this simple environment, each state has just three actions: Left, Right, and
Suck. Larger environments might also include Up and Down.
• Transition model: The actions have their expected effects, except that moving Left in the
leftmost square, moving Right in the rightmost square, and Sucking in a clean squarehave
no effect. The complete state space is shown in Figure 3.3.
• Goal test: This checks whether all the squares are clean.
• Path cost: Each step costs 1, so the path cost is the number of steps in the path.

DIFFERENCE BETWEEN UNINFORMED AND INFORMED SEARCH ​

Bidirectional search(not so imp)


Bidirectional search algorithm runs two simultaneous searches, one form initial state called
as forward-search and other from goal node called as backward-search, to find the goal
node.
Bidirectional search replaces one single search graph with two small subgraphs in which one
starts the search from an initial vertex and other starts from goal vertex. The search stops
when these two graphs intersect each other.

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.

You might also like