AI Unit-2 Notes
AI Unit-2 Notes
Problem-Solving Methods:
Problem-solving methods are systematic approaches used in artificial intelligence (AI) to find
solutions to complex problems. These methods break down problems into smaller components,
explore possible solutions, and select the most effective one. They are fundamental to AI because
they enable machines to think logically and make decisions.
Example:
Problem: You need to find the shortest route between two cities.
Solution: Using a map (state space), analyzing all possible routes (search strategies), and
selecting the shortest one (optimal solution).
Search Strategies:
Search strategies are systematic approaches used to explore the solution space of a problem and
find a path from the initial state to the goal state. They are a core concept in Artificial
Intelligence (AI) and can be categorized into two main types:
1. No Heuristics: Does not use additional information like estimated distances to the goal.
2. Systematic Exploration: Explores the state space methodically to ensure a solution is
found.
3. Guaranteed Solution (if finite): For finite state spaces, it guarantees a solution, though
not always the most optimal.
1. Navigation Systems
2. Network Routing
Example: Searching for a path between two devices in a network without prioritizing
shortest routes.
Real-Life Use: Debugging network connectivity by systematically testing connections.
3. Puzzle Solving
Example: Solving the 8-puzzle or Sudoku where all possible moves are explored blindly.
Real-Life Use: Completing jigsaw puzzles by trying all pieces at random until they fit.
4. Game AI
1. Robot Navigation
Imagine a cleaning robot in a new house with no prior map. The robot:
In this case:
Uninformed Search Strategy like BFS or DFS helps the robot systematically explore all
rooms.
The robot doesn’t "know" which room is dirty or far from its starting point, so it blindly
checks every space.
o You’re in a new city without GPS or a map. You randomly explore every street
until you find the address.
o It’s a slow process, but you’ll eventually reach your goal.
Informed Search
Informed search, also called heuristic search, uses additional knowledge about the problem to
guide the search process efficiently toward the goal. Unlike uninformed search, which explores
blindly, informed search uses heuristics—an estimate of how far a state is from the goal or how
costly it might be to reach the goal from a given state.
1. Uses Heuristics: Estimates guide the search, making it smarter and faster.
2. Goal-Oriented: Focuses on promising paths rather than exploring the entire state space.
3. Efficient: Often finds optimal or near-optimal solutions faster than uninformed methods.
1. Navigation Systems
3. Robotics
Example: A robot navigating through a cluttered warehouse to pick up an item.
How it Works: Heuristics like "distance to item" or "obstacle density" guide the robot to
its goal.
Real-Life Use: Robots used in Amazon’s warehouses for efficient product retrieval..
5. Medical Diagnosis
1. Scenario:
You’re in a new city and want to reach a specific restaurant.
2. Informed Search Steps:
o The map app calculates various routes using heuristics like "shortest distance" or
"least traffic."
o Based on these heuristics, it selects the best route and guides you.
Steps in BFS
1. Initialization:
o Start with the initial node (root).
o Mark the node as visited and enqueue it.
2. Exploration:
o Dequeue the front node.
o Check if it's the goal. If yes, stop.
o Otherwise, explore its neighbors:
Add unvisited neighbors to the queue and mark them as visited.
3. Repeat:
o Continue until the queue is empty or the goal is found.
Example: 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:
1. S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
Applications of BFS
Advantages of BFS
Disadvantages of BFS
Real-World Analogy
You search all rooms on the first floor (current level) before moving to the next floor.
This systematic approach ensures you don’t miss any room and find your friend in the
fewest moves.
1. Initialization:
Start at the initial node.
Mark the node as visited.
2. Exploration:
Move to an unvisited neighbor of the current node.
Repeat this process until you reach a dead end (no unvisited neighbors).
3. Backtracking:
When a dead end is reached, backtrack to the previous node and explore other unvisited
neighbors.
4. Repeat:
Continue this process until all nodes are visited or the goal is found.
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.
Applications of DFS
1. Pathfinding in Mazes
DFS dives deep into one path, backtracking if it encounters a dead end, making it suitable
for solving mazes.
Example: Finding a way out of a labyrinth.
2. Cycle Detection in Graphs
DFS can identify cycles in a graph by revisiting nodes that were already visited.
Example: Detecting infinite loops in dependency graphs.
3. Topological Sorting
DFS is used to order tasks in a directed acyclic graph (DAG).
Example: Scheduling tasks where some tasks depend on the completion of others.
4. Solving Puzzles
DFS explores potential moves deeply before backtracking.
Example: Navigating a chessboard or solving Sudoku puzzles.
5. Web Crawling
DFS can explore all links on a website by diving deep into the links on each page.
Example: Crawling pages deeply linked within a website.
Advantages of DFS
Disadvantages of DFS
o Starting at the entrance, DFS dives deeply into one path until it reaches the exit or
encounters a dead end.
o If it reaches a dead end, DFS backtracks and explores another path.
o Starting from a person, DFS deeply explores each branch (child, grandchild, etc.)
before backtracking to explore other branches.
Uniform-cost Search
Uniform-cost search is a searching algorithm used for traversing a weighted tree or graph. This
algorithm comes into play when a different cost is available for each edge. The primary goal of
the uniform-cost search is to find a path to the goal node which has the lowest cumulative cost.
Uniform-cost search expands nodes according to their path costs form the root node. It can be
used to solve any graph/tree where the optimal cost is in demand. A uniform-cost search
algorithm is implemented by the priority queue. It gives maximum priority to the lowest
cumulative cost. Uniform cost search is equivalent to BFS algorithm if the path cost of all edges
is the same.
Algorithm Steps
1. Initialize the priority queue with the start node, assigning it a path cost of 0.
2. While the priority queue is not empty:
o Dequeue the node with the lowest cost.
o If this node is the goal, return it as the solution.
o For each neighbor of the node:
Calculate the cumulative cost to reach the neighbor.
If the neighbor has not been visited or a cheaper path is found, add/update
the neighbor in the queue.
3. Repeat until the goal is found or the queue is empty.
Advantages:
o Uniform cost search is optimal because at every state the path with the least cost is
chosen.
o It is an efficient when the edge weights are small, as it explores the paths in an order that
ensures that the shortest path is found early.
o It's a fundamental search method that is not overly complex, making it accessible for
many users.
o It is a type of comprehensive algorithm that will find a solution if one exists. This means
the algorithm is complete, ensuring it can locate a solution whenever a viable one is
available. The algorithm covers all the necessary steps to arrive at a resolution.
Disadvantages:
o It does not care about the number of steps involve in searching and only concerned about
path cost. Due to which this algorithm may be stuck in an infinite loop.
o When in operation, UCS shall know all the edge weights to start off the search.
o This search holds constant the list of the nodes that it has already discovered in a priority
queue. Such is a much weightier thing if you have a large graph. Algorithm allocates the
memory by storing the path sequence of prioritizes, which can be memory intensive as
the graph gets larger.With the help of Uniform cost search we can end up with the
problem if the graph has edge's cycles with smaller cost than that of the shortest path.
o The Uniform cost search will keep deploying priority queue so that the paths explored
can be stored in any case as the graph size can be even bigger that can eventually result in
too much memory being used.
Example:
Real-Life Example
A delivery company wants to deliver a package from its warehouse to a customer. Each road
(edge) has a different cost based on fuel consumption, distance, or toll charges. UCS helps find
the route with the minimum delivery cost.
Example:
UCS will choose the second route as it has the lower cost.
Heuristic Function
Heuristic function is a technique used to estimate the cost or distance from a given state to the
goal state. It helps guide search algorithms, particularly in solving optimization and pathfinding
problems, by making the search process more efficient.
Heuristics are integral to informed search algorithms like A Search* and Greedy Best-First
Search:
In A Search*, the heuristic function is combined with the cost function g(n), which
represents the cost from the start node to the current node. The formula used is:
f(n)=g(n)+h(n)
Here:
1. Heuristic-Based:
o BFS uses a heuristic function, h(n), which estimates the cost to reach the goal
from a given node n.
o The heuristic helps the algorithm decide which path to explore first.
2. Priority Queue:
o It uses a priority queue to manage nodes. Nodes with the lowest heuristic values
are given the highest priority.
3. Optimality and Completeness:
o The algorithm's effectiveness depends on the heuristic function. If h(n) is well-
designed, BFS can be optimal and complete.
1. Initialization:
o Start from the source node.
o Insert the source node into a priority queue, assigning its heuristic value as the
priority.
2. Traversal:
o Remove the node with the lowest heuristic value from the queue (most promising
node).
oIf this node is the goal, terminate the search.
oOtherwise, explore its neighbors:
Calculate the heuristic value for each neighbor.
Add unvisited neighbors to the priority queue, prioritized by their heuristic
values.
3. Repeat:
o Continue the process until the goal is found or the queue is empty.
In this example, the cost is measured strictly using the heuristic value. In other words, how close
it is to the target.
C has the lowest cost of 6. Therefore, the search will continue like so:
U has the lowest cost compared to M and R, so the search will continue by exploring U.
Finally, S has a heuristic value of 0 since that is the target node:
The total cost for the path (P -> C -> U -> S) evaluates to 11. The potential problem with a
greedy best-first search is revealed by the path (P -> R -> E -> S) having a cost of 10, which is
lower than (P -> C -> U -> S). Greedy best-first search ignored this path because it does not
consider the edge weights.
Advantages
Faster Exploration: Expands nodes closer to the goal, often leading to faster solutions in
large search spaces.
Simple and Easy Implementation: Simple to implement with only a heuristic function,
making it quick to set up.
Low Memory Usage: Requires less memory since it stores only nodes close to the goal
in the open list.
Efficient for Certain Problems: Works well when the heuristic is accurate and the goal
is easily identified.
Disadvantages
Non-optimal Solution: Since the algorithm only considers the heuristic value and
ignores edge weights, it may find a solution that is not the shortest or least costly. This
can lead to suboptimal paths.
Incomplete: The search may fail to find a solution, especially if there are dead ends or if
the goal node is unreachable. Greedy Best-First Search does not always explore all
possible paths.
Doesn’t Consider Edge Weights: By ignoring edge weights, the algorithm may miss
paths that are less heuristic-optimal but ultimately cheaper in terms of cost. This can lead
to inefficient pathfinding.
Sensitive to Heuristic Quality: The algorithm’s effectiveness is heavily dependent on
the accuracy of the heuristic function. A poorly designed heuristic can result in inefficient
search or failure to find the goal.
Can Get Stuck in Local Minima: Greedy Best-First Search may get stuck in local
minima, focusing too much on immediate low-cost paths and overlooking potentially
better, longer paths that lead to the goal.
1. Pathfinding:
o Used in GPS navigation systems to find the shortest route between locations.
2. Artificial Intelligence:
o Applied in AI for games and problem-solving (e.g., chess, puzzle solving).
3. Robotics:
o Helps robots navigate in real-world environments by finding optimal paths.
Real-Life Example
A* Algorithm
The A* algorithm is a graph traversal and search algorithm used to find the shortest path from a
starting node to a goal node. It is widely used in artificial intelligence, robotics, and computer
games due to its efficiency and accuracy.
A* combines the advantages of Greedy Best-First Search and Uniform-Cost Search by using
both the actual cost from the start node and the estimated cost to the goal to guide its search.
o The exact cost of the path from the start node to the current node n.
o Combines the actual cost and heuristic value: f(n) = g(n) + h(n)
1. Initialization:
o Create two lists:
Open list: Stores nodes to be explored.
Closed list: Stores nodes already explored.
o Add the start node to the open list with f(n) = g(n) + h(n)
2. Node Selection:
o Pick the node from the open list with the lowest f(n).
3. Goal Check:
o If the selected node is the goal, terminate the search and return the path.
4. Expand Node:
o Generate all successors (neighboring nodes).
o For each successor:
Calculate g(n), h(n) and f(n).
If the successor is not in the open or closed list, add it to the open list.
If the successor is already in the open list with a higher f(n), update its
values.
5. Repeat:
o Move the current node to the closed list and repeat the process.
6. Termination:
o If the open list is empty and the goal is not found, return failure.
Example:
In this example, we will traverse the given graph using the A* algorithm. The heuristic value of
all states is given in the below table so we will calculate the f(n) of each state using the formula
f(n)= g(n) + h(n), where g(n) is the cost to reach any node from start state.Here we will use
OPEN and CLOSED list.
Initialization: {(S, 5)}
Iteration1: {(S--> A, 4), (S-->G, 10)}
Iteration2: {(S--> A-->C, 4), (S--> A-->B, 7), (S-->G, 10)}
Iteration3: {(S--> A-->C--->G, 6), (S--> A-->C--->D, 11), (S--> A-->B, 7), (S-->G, 10)}
Iteration 4 will give the final result, as S--->A--->C--->G it provides the optimal path
with cost 6.
Advantages of A*:
1. Optimal: Finds the shortest path if the heuristic h(n) is admissible (does not overestimate
costs).
2. Complete: Guaranteed to find a solution if one exists.
3. Efficient: Balances actual cost and heuristic estimation, making it faster than uninformed
algorithms.
Disadvantages of A*:
1. Memory Intensive: Needs to store all explored nodes in memory, making it unsuitable
for very large graphs.
2. Heuristic Dependence: Performance depends on the quality of the heuristic function.
Applications of A*:
1. Pathfinding:
o GPS systems to find the shortest routes.
2. Robotics:
o Navigating obstacles to reach a target location.
3. Games:
o AI opponents finding the shortest path to a player.
The Hill Climbing Algorithm is a heuristic search algorithm used for optimization problems. It
focuses on iteratively improving the current solution by moving to a neighboring state that has a
higher objective value. The process continues until no better neighbor exists, at which point it
stops at a local or global optimum.
1. Local Maximum: A local maximum is a peak state in the landscape, which is better than each
of its neighboring states, but there is another state also present which is higher than the local
maximum.
How to overcome: Backtracking technique can be a solution of the local maximum in state
space landscape. Create a list of the promising path so that the algorithm can backtrack the
search space and explore other paths as well.
2. Plateau: A plateau is the flat area of the search space in which all the neighbor states of the
current state contain the same value, because of this algorithm does not find any best direction to
move. A hill-climbing search might be lost in the plateau area.
How to overcome: The solution for the plateau is to take big steps or very little steps while
searching, to solve the problem. Randomly select a state which is far away from the current state
so it is possible that the algorithm could find non-plateau region.
3. Ridges: A ridge is a special form of the local maximum. It has an area which is higher than its
surrounding areas, but itself has a slope, and cannot be reached in a single move.
Advertisement
How to overcome: With the use of bidirectional search, or by moving in different directions, we
can improve this problem.
Game Playing in Artificial Intelligence
Game Playing is an important domain of artificial intelligence. Games don’t require much
knowledge; the only knowledge we need to provide is the rules, legal moves and the conditions
of winning or losing the game. Both players try to win the game. So, both of them try to make
the best move possible at each turn. Searching techniques like BFS(Breadth First Search) are not
accurate for this as the branching factor is very high, so searching will take a lot of time.
Game playing in AI is an active area of research and has many practical applications, including
game development, education, and military training. By simulating game playing scenarios, AI
algorithms can be used to develop more effective decision-making systems for real-world
applications.
The most common search technique in game playing is Minimax search procedure. It is depth-
first depth-limited search procedure. It is used for games like chess and tic-tac-toe.
Minimax Algorithm
The Minimax Algorithm is a decision-making algorithm used in game theory and Artificial
Intelligence to determine the optimal move for a player, assuming that the opponent also plays
optimally. It is widely used in two-player, zero-sum games like Chess, Tic-Tac-Toe, and
Checkers.
Key Concepts
1. Two Players:
o Maximizing Player: Aims to maximize the score or utility.
o Minimizing Player: Aims to minimize the opponent's score (equivalently,
maximize their own loss).
2. Game Tree:
o The game is represented as a tree structure.
o Root Node: Represents the current game state.
o Child Nodes: Represent possible moves from the current state.
o Leaf Nodes: Represent the end of the game with an outcome (win, lose, or draw).
3. Utility Function:
o Assigns a numeric value to a terminal state (leaf node).
Example: +10 for a win, -10 for a loss, 0 for a draw.
o Intermediate nodes are assigned values based on the Minimax computation.
Real-Life Example
Chess AI:
In Chess, the algorithm evaluates possible moves and counter-moves to find the best
strategy.
It uses Minimax for decision-making, often combined with Alpha-Beta Pruning and
advanced heuristics to handle the large search space.
Alpha-Beta Pruning
Alpha-Beta Pruning is an optimization technique used in the Minimax Algorithm to reduce
the number of nodes evaluated in a game tree. It improves the efficiency of Minimax by skipping
branches that cannot affect the final decision, effectively reducing the search space.
Key Concepts
1. Pruning:
Cutting off parts of the game tree that do not influence the outcome of the decision.
2. Alpha:
The best value that the maximizing player can guarantee so far.
o Initially set to −∞.
o Updated during the evaluation process.
3. Beta:
The best value that the minimizing player can guarantee so far.
o Initially set to +∞.
o Updated during the evaluation process.
4. Purpose:
o To avoid evaluating branches of the tree that will not influence the final decision.
o Ensures that the algorithm skips unnecessary computations.
Example
Consider a Simple Game Tree:
(MAX)
/ \
(MIN) (MIN)
/|\ /|\
3 5 6 2 9 1
Steps with Alpha-Beta Pruning:
1. Root Node (MAX):
o Initially, α= −∞, β = +∞.
2. Evaluate the left child node (MIN):
o Visit the first child: 3 (β=3).
o Visit the second child: 5 (β=3, prune as 5 > 3).
o Result for left child (MIN): 3 (β=3).
3. Evaluate the right child node (MIN):
o Visit the first child: 2 (β=2).
o Visit the second child: 9 (β=2, prune as 9 > 2).
o Result for right child (MIN): 2 (β=2).
4. Root node selects the maximum: max (3,2) =3.
Pruned Nodes:
Second and third children of the left MIN.
Second and third children of the right MIN.
Real-Life Example
Chess AI:
In Chess, Alpha-Beta Pruning helps evaluate only the most promising moves instead of
analyzing all possible moves, allowing deeper and faster exploration of strategies.
Stochastic Games
A Stochastic Game is a generalization of game theory that incorporates both strategic decision-
making and uncertainty in outcomes. Unlike traditional games where actions have deterministic
results, stochastic games involve probabilistic outcomes that depend on the current state, the
actions of players, and chance.
1. S:
A set of states representing different configurations of the game.
2. A:
A set of actions available to the players. Each player can select from their action set at
any given state.
3. P (s′ ∣ s, a):
A transition probability function that defines the probability of moving to state s′ from
state ‘s’ after actions a are taken.
4. R (s, a):
A reward function that gives the payoff for taking action ‘a’ in state ‘s’.
5. γ:
A discount factor (0≤γ≤1) that determines the importance of future rewards. A lower ‘γ’
emphasizes immediate rewards, while a higher ‘γ’ values long-term gains.
Solution Methods
1. Value Iteration:
o Iteratively calculates the optimal value of each state until convergence.
2. Policy Iteration:
o Alternates between evaluating a policy and improving it until the optimal policy is
found.
3. Reinforcement Learning:
o Uses algorithms like Q-Learning and Deep Q-Networks (DQN) to learn optimal
strategies through exploration and exploitation.
4. Monte Carlo Methods:
o Uses random sampling to estimate state values and policy performance.
Real-Life Example
Autonomous Vehicles:
Stochastic games can model interactions between self-driving cars at intersections.
States: Positions of all cars at the intersection.
Actions: Accelerate, decelerate, stop, or turn.
Transition Probability: Depends on traffic patterns and other cars' actions.
Reward: Minimize time to cross the intersection while avoiding collisions.
Constraint Propagation
Constraint propagation is a technique used in solving Constraint Satisfaction Problems (CSPs). It
involves systematically reducing the domains of variables by enforcing constraints between
them. The goal is to simplify the problem by eliminating inconsistent values from the variable
domains before or during the search process.
Components of a CSP
1. Variables (X):
o A set of variables that need to be assigned values.
o Example: In a Sudoku puzzle, each cell is a variable.
2. Domains (D):
o Each variable has a domain that defines the set of possible values it can take.
o Example: In a map-coloring problem, the domain might be {Red, Green, Blue}.
3. Constraints (C):
o Rules that specify valid combinations of values for variables. Constraints can be
unary (involving one variable), binary (involving two variables), or higher-order
(involving more than two variables).
o Example: Adjacent regions on a map must have different colors.
Representation of CSP
A CSP is typically represented as a graph, where:
Nodes represent variables.
Edges represent constraints between variables.
Solving a CSP
CSPs can be solved using various techniques:
1. Backtracking Search
A depth-first search algorithm.
Assigns values to variables one at a time, backtracking when a constraint is violated.
3. Constraint Propagation
Reduces the domains of variables by enforcing constraints during the search process.
Techniques include arc consistency, node consistency, and path consistency.
4. Local Search
Starts with an initial assignment and iteratively improves it by minimizing constraint
violations.
Example: Hill climbing or simulated annealing.
Representation:
Variables: A, B, C.
Domains: {Red, Green, Blue}.
Constraints: A≠B, B≠C, A≠C.
Steps:
1. Assign A=Red.
2. Update domains: B= {Green, Blue}, C= {Green, Blue}.
3. Assign B=Green.
4. Update domains: C={Blue}.
5. Assign C=Blue.
6. Solution: A=Red, B=Green, C=Blue.
Applications of CSPs
1. Scheduling:
Assigning time slots to tasks or events while satisfying constraints like no overlap.
2. Puzzle Solving:
Sudoku, crosswords, N-queens problem.
3. Map Coloring:
Assigning colors to regions on a map such that adjacent regions have different colors.
4. Resource Allocation:
Allocating resources like rooms, equipment, or staff to tasks while satisfying capacity and
timing constraints.
5. Configuration Problems:
Designing valid configurations of products or systems, such as circuit boards.
In the 8 Puzzle, only tiles adjacent to the blank space can be moved. The following moves are
allowed:
Move the blank space up.
Move the blank space down.
Move the blank space left.
Move the blank space right.
The solution to the problem requires rearranging the tiles from the initial state to the goal state by
making a series of these legal moves.