Problem-Solving Agents
Problem-Solving Agents
• When using a travel website to book a flight, the system solves a problem with these parts:
✅ States – Your current location, time, and details about past flights (e.g., economy/business
class, domestic/international).
✅ Initial State – Your starting airport and time (given by the user).
✅ Transition Model – After taking a flight, you land in a new location at a new time.
✅ Path Cost – Factors like ticket price, travel time, layovers, comfort, and frequent flyer miles
• loop do
→ This means the algorithm will keep running until it finds a solution or fails.
• if the node contains a goal state then return the corresponding solution
→ If this node is the goal (the solution to the problem), return it as the answer.
• expand the chosen node, adding the resulting nodes to the frontier
→ If the node is not the goal, generate its child nodes and add them to the frontier to be
explored later.
• GRAPH-SEARCH Algorithm (Avoids exploring the same state multiple times)
• loop do
→ The algorithm keeps running until it finds a solution or fails.
• expand the chosen node, adding the resulting nodes to the frontier
→ Generate the child nodes of this node.
• Key Concepts:
• State Space: Represents all possible states and actions.
• Search Tree: Represents the paths we explore from an initial state to
a goal state.
• The arrows in the search tree show how nodes are linked: each
child node points back to its parent, forming a structure that
allows path tracking.
Take input → A problem, a parent node, and an action.
Find the new state → Apply the action to the parent’s
state.
Set the parent → Store the given parent node. Record
the action → Store the action used to reach this
node.
Calculate the path cost → Add the step cost to the
parent's path cost.
Return the new node → The child node is created with
the updated values
This function explains how new nodes are created during a search.
Steps of the Function:
The STATE of the child is determined by applying an action to the parent’s
state.
Example: If the parent is at a tile arrangement and the action is move right, the
new state reflects that move.
g(n)=g(parent)+step_cost
The step cost is the cost of moving from the parent state to the new state.
This function ensures that each new node contains all necessary
information for tracking the search process.
3.2. Measuring problem-solving performance
–
– This means that if the search tree grows too deep, the time
required becomes very large.
• Space Complexity
– BFS stores all nodes at the current level before moving deeper.
– This requires a lot of memory, making BFS memory-intensive.
• Is BFS Always the Best?
• ✔ Advantages:
• BFS always finds a solution if one exists.
• If multiple solutions exist, BFS finds the shortest
solution (the one with the fewest steps).
• ✖ Disadvantages:
• Requires a lot of memory because it keeps track of all
nodes at a level before moving deeper.
• If the solution is far from the root, BFS takes a long
time to reach it.
Figure 3.12 (Binary Tree) shows BFS
working level-by-level.
What is Happening in the Image?
The diagram shows a binary tree, where BFS explores level by level.
The dark-colored nodes are those that have already been expanded.
The triangular marker (▶) indicates the next node to be expanded.
Step-by-Step Explanation:
Start at the Root (A): BFS begins from node A and marks it as expanded.
Expand Level 1: Nodes B and C (children of A) are added to the queue. B is chosen first
for expansion.
Expand Level 2: The algorithm moves to B, expanding its children D and E.
Move to C: Now, C is expanded, and its children F and G are added.
Continue Level by Level: The process continues until all nodes are expanded or the
goal is found.
Key Takeaway:
BFS explores all nodes at a given depth before moving to the next level.
It uses a FIFO queue, ensuring shallow nodes are expanded first.
Figure 3.11 (Algorithm) explains how BFS systematically expands
nodes using a queue.
Step-by-Step Explanation
• Start with the initial node
• Create a node with the initial state.
• If this node is already the goal, return the solution
immediately.
• Set up the search structure
• Use a FIFO (First In, First Out) queue (frontier) to store nodes
that need to be explored.
• Use an explored set to keep track of visited nodes.
• Loop until a solution is found or all possibilities are checked
• If the queue (frontier) is empty, return failure (no solution
found).
• Remove a node from the queue (this is the shallowest node).
• Add this node to the explored set (to avoid visiting it again)..
• Expand the current node
• Get all possible actions for the current state.
• Generate a child node for each possible action.
• If the child node is not already explored or in the queue,
check:
– If it is the goal, return the solution.
– Otherwise, add it to the queue to explore later.
• Repeat until a solution is found or the queue is empty
4.2 Depth-first search:
• How DFS Works
• Start from the root node.
• Move to the deepest possible node (keep
expanding the last visited node).
• If a node has no children (successors), backtrack to
the previous node and explore other unexplored
paths.
• Continue this process until the goal node (M in the
figure) is found or all possibilities are exhausted.
• Key Features
• Memory Efficient: DFS only stores nodes along the
current path, unlike BFS (which stores all nodes at a
level).
• Uses a Stack (LIFO): The last explored node is
processed first, ensuring deep exploration before
backtracking.
• Time Complexity: O(bm)O(b^m)O(bm), where b is
the branching factor and m is the maximum depth.
Advantages
• Requires less memory since it doesn’t store all
nodes.
• Can be faster than BFS if the goal node is on a
deep path.
Disadvantages
• May get stuck in an infinite loop if cycles exist.
• Does not guarantee the shortest path to the
goal.
4.3. Iterative deepening depth-first search:
• Iterative Deepening Search (IDS) is a combination of Depth-
First Search (DFS) and Breadth-First Search (BFS). It starts with
a depth limit of 0 and gradually increases the limit (1, 2, 3, etc.)
until it finds the goal node.