Unit II Problem solving
Unit II Problem solving
Unit II Problem-solving
P a g e 1 | 25
AI NOTES
Example Problems:
Basically, there are two types of problem approaches:
Toy Problem: It is a concise and exact description of the problem which is used by the
researchers to compare the performance of algorithms.
Real-world Problem: It is real-world based problems which require solutions. Unlike a toy
problem, it does not depend on descriptions, but we can have a general formulation of the
problem.
Some Toy Problems
8 Puzzle Problem:
Here, we have a 3×3 matrix with movable tiles numbered from 1 to 8 with a blank space.
The tile adjacent to the blank space can slide into that space. The objective is to reach a
specified goal state similar to the goal state, as shown in the below figure. In the figure,
our task is to convert the current state into goal state by sliding digits into the blank space.
In the above figure, our task is to convert the current(Start) state into goal state by sliding
digits into the blank space.
The problem formulation is as follows:
P a g e 2 | 25
AI NOTES
States: It describes the location of each numbered tiles and the blank tile.
Initial State: We can start from any state as the initial state.
Actions: Here, actions of the blank space is defined, i.e., either left, right, up or down
Transition Model: It returns the resulting state as per the given state and actions.
Path cost: The path cost is the number of steps in the path where the cost of each
step is 1.
2 Problem Statement:
We have two jugs of capacity 5l and 3l (liter), and a tap with an endless supply of water. The
objective is to obtain 4 liters exactly in the 5-liter jug with the minimum steps possible.
Production System:
1. Fill the 5 liter jug from tap
2. Empty the 5 liter jug
3. Fill the 3 liter jug from tap
4. Empty the 3 liter jug
5. Then, empty the 3 liter jug to 5 liter
6. Empty the 5 liter jug to 3 liter
7. Pour water from 3 liters to 5 liter
8. Pour water from 5 liters to 3 liters but do not empty
Solution: 1,8,4,6,1,8 or 3,5,3,7,2,5,3,5;
Example: Missionaries and Cannibals
The Missionaries and Cannibals problem illustrates the use of state space search for planning
under constraints: Three missionaries and three cannibals wish to cross a river using a two-
person boat. If at any time the cannibals outnumber the missionaries on either side of the
river, they will eat the missionaries. How can a sequence of boat trips be performed that will
get everyone to the other side of the river without any missionaries being eaten?
Search Algorithm Terminologies:
o Search: Searching is a step-by-step procedure to solve a search-problem in a given
search space. A search problem can have three main factors:
P a g e 3 | 25
AI NOTES
o Search tree: A tree representation of search problem is called Search tree. The root
of the search tree is the root node which is corresponding to the initial state.
o Actions: It gives the description of all the available actions to the agent.
o Transition model: A description of what each action do, can be represented as a
transition model.
o Path Cost: It is a function which assigns a numeric cost to each path.
o Solution: It is an action sequence which leads from the start node to the goal node.
o Optimal Solution: If a solution has the lowest cost among all solutions.
o Properties of Search Algorithms:
o Following are the four essential properties of search algorithms to compare the
efficiency of these algorithms:
o Completeness: A search algorithm is said to be complete if it guarantees to return a
solution if at least any solution exists for any random input.
o Optimality: If a solution found for an algorithm is guaranteed to be the best solution
(lowest path cost) among all other solutions, then such a solution for is said to be an
optimal solution.
o Time Complexity: Time complexity is a measure of time for an algorithm to complete
its task.
o Space Complexity: It is the maximum storage space required at any point during the
search, as the complexity of the problem.
Based on the search problems we can classify the search algorithms into uninformed (Blind
search) search and informed search (Heuristic search) algorithms.
P a g e 4 | 25
AI NOTES
1. No heuristic function
2. Exploration based on node structure
3. No guidance towards goal
4. Complete or incomplete search
Advantages:
1. Simplicity 2. Easy implementation 3. No reliance on heuristics
P a g e 5 | 25
AI NOTES
1. Breadth-first Search:
o Breadth-first search is the most common search strategy for traversing a tree or
graph. This algorithm searches breadthwise in a tree or graph, so it is called breadth-
first search.
o BFS algorithm starts searching from the root node of the tree and expands all
successor node at the current level before moving to nodes of next level.
o The breadth-first search algorithm is an example of a general-graph search algorithm.
o Breadth-first search implemented using FIFO queue data structure.
Advantages:
o BFS will provide a solution if any solution exists.
o If there are more than one solutions for a given problem, then BFS will provide the
minimal solution which requires the least number of steps.
o It also helps in finding the shortest path in goal state, since it needs all nodes at the
same hierarchical level before making a move to nodes at lower levels.
o It is also very easy to comprehend with the help of this we can assign the higher rank
among path types.
Disadvantages:
o It requires lots of memory since each level of the tree must be saved into memory to
expand the next level.
o BFS needs lots of time if the solution is far away from the root node.
o It can be very inefficient approach for searching through deeply layered spaces, as it
needs to thoroughly explore all nodes at each level before moving on to the next.
Example:
P a g e 6 | 25
AI NOTES
In the below tree structure, we have shown the traversing of the tree using BFS
algorithm from the root node S to goal node K. BFS search algorithm traverse in
layers, so it will follow the path which is shown by the dotted arrow, and the
traversed path will be:
S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
2. Depth-first Search
o Depth-first search isa recursive algorithm for traversing a tree or graph data
structure.
o It is called the depth-first search because it starts from the root node and follows
each path to its greatest depth node before moving to the next path.
o DFS uses a stack data structure for its implementation.
o The process of the DFS algorithm is similar to the BFS algorithm.
Advantage:
o DFS requires very less memory as it only needs to store a stack of the nodes on the
path from root node to the current node.
o It takes less time to reach to the goal node than BFS algorithm (if it traverses in the
right path).
o With the help of this we can stores the route which is being tracked in memory to
save time as it only needs to keep one at a particular time.
Disadvantage:
P a g e 7 | 25
AI NOTES
o There is the possibility that many states keep re-occurring, and there is no guarantee
of finding the solution.
o DFS algorithm goes for deep down searching and sometime it may go to the infinite
loop.
o The de�pth-first search (DFS) algorithm does not always find the shorte�st path to
a solution.
Example:
In the below search tree, we have shown the flow of depth-first search, and it will
follow the order as:
Root node--->Left node ----> right node.
It will start searching from root node S, and traverse A, then B, then D and E, after
traversing E, it will backtrack the tree as E has no other successor and still goal node
is not found. After backtracking it will traverse node C and then G, and here it will
terminate as it found goal node.
o Standard failure value: It indicates that problem does not have any solution.
o Cutoff failure value: It defines no solution for the problem within a given depth limit.
P a g e 8 | 25
AI NOTES
Advantages:
Depth-Limited Search will restrict the search depth of the tree, thus, the algorithm
will require fewer memory resources than the straight BFS (Breadth-First Search) and
IDDFS (Iterative Deepening Depth-First Search). After all, this implies automatic
selection of more segments of the search space and the consequent why
consumption of the resources. Due to the depth restriction, DLS omits a predicament
of holding the entire search tree within memory which contemplatively leaves room
for a more memory-efficient vice for solving a particular kind of problems.
o When there is a leaf node depth which is as large as the highest level allowed, do not
describe its children, and then discard it from the stack.
o Depth-Limited Search does not explain the infinite loops which can arise in classical
when there are cycles in graph of cities.
Disadvantages:
o Depth-limited search also has a disadvantage of incompleteness.
o It may not be optimal if the problem has more than one solution.
o The effectiveness of the Depth-Limited Search (DLS) algorithm is largely dependent
on the depth limit specified. If the depth limit is set too low, the algorithm may fail to
find the solution altogether.
Informed Search
Informed search algorithms utilize additional information or heuristics to guide the
search process, making them more efficient and effective.
Types of Informed Search:
P a g e 9 | 25
AI NOTES
1. Best-First Search (BFS): Uses a heuristic function to estimate the distance from
each node to the goal.
2. A*: Combines the cost to reach each node with the heuristic function.
3. Iterative Deepening Depth-First Search (IDDFS): Combines depth-first search with
iterative deepening.
4. Greedy Search: Chooses the node that appears closest to the goal.
5. Hill Climbing: Explores neighbors and chooses the best one based on the heuristic.
Characteristics:
1. Heuristic function: Estimates the distance from each node to the goal.
2. Guided search: Uses heuristics to focus on promising areas.
3. Efficient exploration: Avoids exploring unnecessary nodes.
Disadvantages:
1. Heuristic quality: Poor heuristics can lead to suboptimal solutions.
2. Complexity: More complex algorithms require more computational resources.
3. Overreliance on heuristics: May not explore alternative solutions.
Real-World Applications:
P a g e 10 | 25
AI NOTES
--------------------------------------------------------------------------------------------------------------
----------
1) The A* algorithm
A* is a popular pathfinding algorithm used to find the shortest path between two
points in a weighted graph or network. It's widely used in various fields, including:
1. Video games (e.g., navigation, pathfinding)
2. Robotics (e.g., motion planning)
3. Geographic information systems (GIS)
4. Traffic routing
P a g e 11 | 25
AI NOTES
5. Network optimization
How A * works:
1. Initialization: Define the start node (S) and goal node (G).
2. Open list: Create a priority queue containing nodes to be evaluated, starting with
the start node.
3. Closed list: Create a set to store nodes that have already been evaluated.
4. Evaluation: For each node in the open list:
Calculate the estimated total cost (f) = cost to reach node (g) + heuristic cost to reach
goal (h)
- Compare f values to determine the node with the lowest estimated total cost
5. Node selection: Choose the node with the lowest f value and move it to the closed
list.
Key components:
1. Heuristic function (h): Estimates the cost to reach the goal from a given node.
2. Cost function (g): Represents the cost to reach a node from the start.
3. Priority queue: Efficiently manages nodes based on their estimated total cost (f).
Advantages:
1. Efficient: A* is relatively fast and scalable.
P a g e 12 | 25
AI NOTES
2. Optimal: Guarantees the shortest path if the heuristic function is admissible (never
overestimates) and consistent (estimates the same cost for identical nodes).
Disadvantages:
1. High computational cost: A* can be slow for large graphs or complex searches due
to the overhead of maintaining the priority queue and calculating heuristic values.
2. Memory usage: A* requires significant memory to store the open and closed lists,
especially for large graphs.
3. Non-optimality: A* may not always find the optimal solution if the heuristic
function is not admissible (never overestimates) or consistent (estimates the same
cost for identical nodes).
4. Incompleteness: A* may not find a solution if:
2. Bidirectional A*: Searches from both start and goal nodes simultaneously.
3. Iterative deepening A: Combines A with depth-first search to improve
performance.
2) Best-First Search (BFS) algorithm
Overview
Best-First Search is a pathfinding algorithm that explores nodes based on their
estimated distance from the goal (heuristic value). It's similar to A* but doesn't
consider the cost to reach each node.
Key Components
1. Heuristic function (h): Estimates the distance from each node to the goal.
2. Priority queue: Nodes are ordered based on their heuristic value (h).
3. Node selection: Choose the node with the lowest h value.
Algorithm Steps
1. Initialize the start node (S) and goal node (G).
2. Create a priority queue containing the start node.
3. While the queue is not empty:
P a g e 13 | 25
AI NOTES
Disadvantages
1. Non-optimality: May not find the shortest path.
P a g e 14 | 25
AI NOTES
Key Components:
1. Variables (X): Represent the unknowns.
2. Domains (D): Define possible values for each variable.
3. Constraints (C): Specify relationships between variables.
4. Assignment: A mapping of values to variables.
Types of Constraints:
1. Unary constraints: Involve one variable (e.g., x1 ≥ 5).
4. Engineering 5. Finance
Real-World Examples:
1. Air traffic control 2. Supply chain management 3. Resource optimization
4. Automated planning 5. Game playing
Challenges:
Techniques:
1. Domain Pruning: Remove inconsistent values from variable domains.
2. Constraint Elimination: Remove redundant constraints.
3. Constraint Strengthening: Strengthen constraints to reduce search space.
4. Value Ordering: Order values to reduce search space.
Benefits:
1. Reduced Search Space: Fewer variables and values to explore.
2. Improved Search Efficiency: Faster solution finding.
3. Increased Solution Quality: Better solutions through reduced search space.
4. Simplified Constraint Modelling: Easier constraint specification.
Applications:
1. Scheduling: Resource allocation, timetabling.
2. Resource Optimization: Supply chain management, logistics.
3. Automated Planning: Planning, decision-making. 4. Game Playing: Strategic
decision-making.
Backtracking Search
Backtracking Search is a fundamental algorithm for solving Constraint Satisfaction
Problems (CSPs). It's a popular method for finding solutions, especially when
constraint propagation is insufficient.
Algorithm Steps:
P a g e 17 | 25
AI NOTES
Key Components:
1. Variable Ordering: Selecting the next variable to assign.
2. Value Ordering: Selecting the next value to try.
Benefits:
1. Completeness: Guaranteed to find a solution if one exists.
2. Flexibility: Handles various CSPs and constraints.
3. Simple Implementation: Easy to understand and code.
Challenges:
Key Components:
1. Initial Solution: Generate an initial assignment.
2. Neighborhood Function: Define neighboring solutions.
3. Evaluation Function: Assess solution quality.
P a g e 18 | 25
AI NOTES
Disadvantages:
1. No guarantee of optimality 2. Sensitive to initial solution
3. May get stuck in local optima 4. Requires parameter tuning
Real-World Applications:
1. Scheduling: Resource allocation, timetabling.
2. Resource Optimization: Supply chain management, logistics.
3. Automated Planning: Planning, decision-making.
4. Game Playing: Strategic decision-making, game playing.
Hill Climbing
P a g e 19 | 25
AI NOTES
Hill Climbing is a popular local search algorithm for optimization problems. It's
simple, efficient, and effective.
Key Components:
1. Initial Solution: Generate an initial assignment.
2. Neighborhood Function: Define neighboring solutions.
3. Insert: Add a new value to a variable. 4. Delete: Remove a value from a variable.
Evaluation Functions:
1. Constraint Violations: Count unsatisfied constraints.
2. Penalty Functions: Assign costs to constraint violations.
Problem Representation
P a g e 21 | 25
AI NOTES
Alpha-Beta Pruning
Key Concepts:
1. Alpha (α): Best possible score for MAX (maximizing player).
2. Beta (β): Best possible score for MIN (minimizing player).
3. Pruning: Eliminating branches that won't affect the outcome.
Alpha-Beta Pruning Algorithm:
P a g e 24 | 25
AI NOTES
4. Optimization problems.
P a g e 25 | 25