Module 3
Module 3
Algorithms
Gahan A V
Assistant Professor
Department of Electronics and Communication Engineering
Bangalore Institute of Technology
BANGALORE INSTITUTE OF TECHNOLOGY
Module 3
2
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING
BANGALORE INSTITUTE OF TECHNOLOGY
Best-First Search
• Best-first search is a method where we pick the next move based on which seems the "best" according to a specific
evaluation function, f(n).
• This function estimates how close a given position (or node) is to the goal. We always choose the position with the
lowest (or "best") estimated cost to expand next.
• The heuristic function is a quick estimate of the remaining cost from the current position to the goal. For example, if
you’re trying to reach a city, you might use the straight-line distance to it as a heuristic.
• The heuristic helps the search prioritize paths that seem closer to the goal. A good heuristic makes the search more
efficient by focusing on promising paths while avoiding less promising ones.
Key Points
•Best-First Search: Expands nodes based on the "best" estimated path to the goal.
•Heuristic Function: An estimate used to guide the search by predicting the cost to reach the goal from each
position.
• Greedy best-first search is a search strategy that tries to get as close to the goal as quickly as possible by expanding the
node that seems closest to the goal based on a heuristic estimate, denoted as h(n).
• Here, the function f(n)=h(n) is used to make decisions, focusing solely on the heuristic.
• Using the straight-line distance heuristic (how close each city is to Bucharest in a straight line), greedy search will
always pick the next closest city.
• For instance, starting from Arad, it will choose Sibiu because it’s closer than other options. This process repeats, picking
nodes that appear closest to Bucharest, until it reaches the destination.
• While greedy search can sometimes find a solution quickly, it isn’t always the best solution. In our example, this
approach finds a path but not the shortest one.
• That's why it's called "greedy"—it focuses on immediate gains, which can lead to suboptimal paths.
• Another problem is that it’s incomplete in some cases (similar to depth-first search). For instance, if there’s a dead
end, it might get stuck or loop back instead of finding the right path.
• If the search problem has a finite space, graph search (a version that avoids re-expanding nodes) can handle it, but
if there are infinite paths, even graph search may struggle.
Complexity
• The worst-case time and space complexity of greedy best-first search is O(b^m), where b is the branching factor
(average number of choices per step) and mmm is the maximum depth. However, a well-chosen heuristic can make
the search much faster by focusing on the most promising paths.
To ensure that A* search finds the optimal (cheapest) solution, we need the heuristic function, h(n)h(n)h(n), to meet two key
conditions: admissibility and consistency.
1. Admissibility
• An admissible heuristic is one that never overestimates the cost to reach the goal. This means it’s always either correct or
optimistic (thinking it will cost less than it actually might).
• For example, using straight-line distance as an estimate is admissible because the shortest path between two points is
always a straight line—it can't overestimate the actual path cost.
• When h(n) is admissible, f(n)=g(n)+h(n) will never be higher than the true cost to the goal, ensuring A* doesn’t choose a
path that looks cheaper but actually isn’t.
• Consistency is a slightly stronger condition that ensures the heuristic keeps making steady progress toward the
goal, especially in graph search.
• A heuristic is consistent if, for any node and any neighbor n′, the estimated cost h(n) to the goal is no greater
than the actual step cost c(n,n′) to reach n′, plus the heuristic h(n′) at n′: h(n)≤c(n,n′)+h(n′)
• This follows the triangle inequality rule, meaning it’s impossible to “shortcut” to the goal in a way that breaks
the logical flow of costs.
• Consistency guarantees that once A* reaches a node, it won’t need to revisit it, making the search more
efficient.
This section explains why A* search is optimal when using a consistent heuristic in graph search. Here’s a
breakdown of the key points in a simpler way:
This combination of consistency and lowest-cost expansion makes A* search optimal in graph search.
To reduce memory usage in heuristic search, one approach is to use Iterative-Deepening A* (IDA*), which adapts the
iterative deepening concept for A* search. Here’s a simple explanation of how it works, as well as a summary of
Recursive Best-First Search (RBFS), which is another memory-efficient algorithm:
1. Iterative-Deepening A* (IDA*)
•In IDA* search, instead of limiting depth as in regular iterative deepening, the limit is based on the f-cost (total cost
estimate, f=g+h
•IDA* is practical for problems with simple step costs (like one move at a time) and reduces the memory overhead that
would come from managing a large queue of nodes.
•However, with real-valued costs (non-whole numbers), IDA* may struggle, just like uniform-cost search, as cost
calculations can lead to many small incremental steps.
•RBFS is a recursive algorithm that mimics best-first search while using only linear memory.
•It works like depth-first search but tracks an f-limit (the best alternative path’s f-cost from any ancestor node).
•If the f-cost of the current path exceeds this limit, it backtracks and explores alternative paths instead.
• As it backtracks, RBFS saves the best f-cost of nodes in the path it’s leaving, so it can decide later if re-exploring
that path is worth it.
•This technique allows RBFS to "remember" the most promising path without using a lot of memory. For example, while
solving a route to Bucharest, RBFS can backtrack if a particular path’s f-cost becomes too high but can return to it if it
becomes a more favorable option later.
Both IDA* and RBFS are ways to manage search memory efficiently by controlling the search depth (in IDA*) or by
backtracking smartly (in RBFS), helping tackle larger problems with limited memory.
RBFS is a recursive, memory-efficient approach that uses f-values to guide its search, focusing
on promising paths while backtracking if costs exceed the current limit. This method keeps
memory use manageable while exploring large search spaces effectively.
The Recursive Best-First Search (RBFS) algorithm is a search method that tries to find the best path to a goal
without using too much memory. Here’s how it works in simple steps:
1.Check Goal: If the current node is the goal, it returns the solution.
2.Create Next Steps: If not, it creates all possible next steps (successors) and calculates their costs.
3.Choose the Best Option: It picks the option with the lowest cost as the "best" path to explore next. If this cost is
higher than allowed, it returns failure and the new cost limit.
4.Recursively Search: RBFS then calls itself on this best option with an updated limit, keeping track of the next-best
option as an "alternative" in case it needs to backtrack.
RBFS keeps memory low by focusing on the most promising path and only backtracking when necessary, making it
efficient for large searches.
1. Object-Level State Space: This is the main problem space the agent is exploring, like finding a route between
cities on a map (e.g., Romania).
2. Metalevel State Space: This is the space where the agent is learning how to search better. Each state here
represents the internal workings of the algorithm itself (e.g., the search tree being built by A*).
2. How It Works:
1. As an algorithm like A* runs, it makes choices about which paths to explore. Sometimes, it takes steps that aren’t
useful or promising (like expanding a city that doesn’t lead closer to the goal).
2. Metalevel learning means that the algorithm can analyze these choices to recognize patterns and avoid
unnecessary steps in the future.
HEURISTIC FUNCTIONS
• In the 8-puzzle, the goal is to slide tiles around on a 3x3 grid to match a specific target arrangement. You can only
move the tiles horizontally or vertically into the empty space.
• The puzzle is a classic example of a problem where we need to find efficient ways to search for the solution, and
heuristics help with that by estimating the best way to reach the goal.
• To solve the puzzle efficiently using A*, we need a heuristic function—a way to estimate how close the current
state is to the goal.
• The heuristic should never overestimate the actual number of moves required, which makes it admissible.
1. This heuristic counts how many tiles are not in their correct positions.
2. For example, if all 8 tiles are out of place, h1=8
3. This is admissible because every misplaced tile will require at least one move to reach its correct position.
1.h2: Manhattan Distance:
1. This heuristic calculates the sum of the distances each tile is from its target position, counting only
horizontal and vertical moves (no diagonal moves).
2. For example, if a tile is 3 steps away horizontally and 2 steps away vertically, its Manhattan distance is 5.
3. For the start state in the 8-puzzle, h2=18
4. This is also admissible because each tile can only move closer to its goal by one step at a time, so the
heuristic never overestimates the actual number of moves.
Both of these heuristics help guide the search process in the right direction by estimating how far the puzzle is from the
goal without overestimating the required steps.
In heuristic search, the effective branching factor b^* measures how efficiently the algorithm expands nodes. It is
a way to understand the number of nodes generated by the search, given the solution depth.
• In the context of heuristic search, h1 (misplaced tiles) and h2 (Manhattan distance) are both good heuristics for the 8-
puzzle, with h2 being better because it provides a more accurate estimate of the number of moves needed to solve the
puzzle.
How can h2 be derived?
•h2 is a heuristic that calculates the total distance each tile is away from its goal position, using the Manhattan distance
(the sum of horizontal and vertical moves).
•A way to generate h2 (or other good heuristics) is through relaxed problems:
• Relaxing the rules of a problem means removing some restrictions to make it easier to solve.
• For the 8-puzzle, relaxing the rules can lead to different simplified versions of the puzzle that are easier to solve,
such as allowing tiles to move anywhere (rather than just to adjacent spaces).
• By solving these simplified versions, we can create admissible heuristics (which never overestimate the actual
solution) for the original problem.
• This composite heuristic will always use the most accurate estimate from the available heuristics, ensuring
that it provides the best guidance for the search.
Key Takeaways:
In heuristic search, admissible heuristics (which don't overestimate the cost to reach the goal) can be derived from
subproblems of the original problem. One of the powerful methods to do this is through pattern databases.
• The cost to solve this smaller subproblem is a lower bound (an estimate that is never too high) for the entire problem.
This makes it a good heuristic.
•The idea is to precompute the optimal solutions for many possible configurations of a subproblem (like the positions of
tiles 1, 2, 3, 4) and store them in a database.
•During search, when you encounter a state of the puzzle, you can look up the cost of solving the corresponding
subproblem in the database. This gives you a good estimate of how close you are to the goal.
•The database is created by searching backward from the goal, recording the cost to solve each possible configuration.
•You can create multiple pattern databases for different groups of tiles (e.g., one for tiles 1-2-3-4, another for tiles 5-6-7-8).
•Disjoint pattern databases are where the subproblems don't overlap. For example, the moves to solve tiles 1-2-3-4 don't
interfere with solving tiles 5-6-7-8. In such cases, the sum of the costs from both databases can be added together to create
a stronger heuristic.
•Pattern databases can make heuristics much more accurate and help find solutions faster.
•For example, using a disjoint pattern database for the 15-puzzle can reduce the number of nodes searched by a factor
of 10,000 compared to using a simpler heuristic like Manhattan distance.
Limitations:
•Pattern databases work well when the problem can be divided into subproblems where each move affects only one part
(like sliding tiles).
•For more complex problems, like Rubik’s Cube, it's harder to divide the problem this way, so similar techniques are
used but haven't been as effective.
To create a heuristic function h(n), which estimates the cost of reaching the goal from a given state n, one can use two
main approaches:
1.Relaxed Problems: As discussed earlier, relaxed problems simplify the problem to make it easier to calculate the cost.
For example, relaxing the 8-puzzle by allowing tiles to move anywhere can give a simpler way to estimate the solution
cost.
2.Learning from Experience: Instead of manually designing a heuristic, we can learn a heuristic by observing many
examples of solving the problem.
For instance, by solving multiple 8-puzzles, we collect data about each state encountered during the solution and its
actual solution cost.
Combining Features:
•We combine these features to predict the cost, using a linear combination like:
•h(n)=c1×x1(n)+c2×x2(n) where c1 and c2 are constants that adjust based on the data to make the prediction as
accurate as possible.
•The values c1 and c2 are typically positive because more misplaced tiles or incorrect adjacent pairs make the puzzle
harder to solve.
Key Points:
•This method learns from data to create a heuristic that predicts the cost of solving from a given state.
•The heuristic may not always be admissible (it might overestimate the cost) or consistent (it might not always
satisfy the triangle inequality), but it can still be useful for improving search efficiency.
•This approach is used in machine learning techniques like neural networks and decision trees.
In which we design agents that can form representations of a complex world, use a
process of inference to derive new representations about the world, and use these
new representations to deduce what to do.
KNOWLEDGE-BASED AGENTS
A knowledge-based agent in AI uses a knowledge base (KB), which is a collection of sentences (not necessarily like
sentences in English, but structured statements about the world). The main idea is that the agent can know things and
use that knowledge to reason about its environment and make decisions.
Key Points:
1.Knowledge Base (KB): The agent’s knowledge is stored as sentences in a knowledge representation language,
which allows the agent to make sense of the world. These sentences could be facts or rules about the world. For
example, "The sky is blue" or "If it rains, the ground gets wet."
2.TELL and ASK:
1. TELL: The agent adds new information to its knowledge base. This is like the agent learning or being given
new facts.
2. ASK: The agent can ask questions from its knowledge base to figure out what action to take. For example, it
might ask, "What should I do next?" and the KB will help answer based on what it knows.
3.Inference: When the agent asks a question, it may need to reason (called inference) to come up with the answer.
For example, if it knows that "If it rains, the ground gets wet," and it also knows "It is raining," it can infer that "The
ground is wet."
4.Agent Behavior: The agent works by taking in percepts (what it senses), TELLing them to its knowledge base,
and then ASKing the KB for the best action to take. Once it knows the action, it TELLs the KB that it performed the
action.
5.Declarative vs. Procedural:
1. Declarative knowledge is when we tell the agent facts or rules directly (e.g., "The sky is blue").
2. Procedural knowledge is when we program the agent's behaviors step-by-step. Nowadays, successful
agents combine both types of knowledge—using facts in the KB (declarative) and rules for specific actions
(procedural).
6.Learning: A knowledge-based agent can also learn new knowledge from its experiences, becoming more
autonomous over time.
1.Wumpus: A dangerous creature lurking in one of the rooms. If the agent enters the room with the Wumpus, it gets eaten.
2.Pits: Some rooms have bottomless pits that will trap the agent if it steps in them.
3.Gold: There's a piece of gold hidden somewhere in the cave, and the agent's goal is to find it.
•Actuators (Actions):
• Move: The agent can move forward or turn left/right by 90°.
• Shoot: The agent can shoot an arrow in the direction it's facing (only one shot available).
• Grab: Pick up the gold if it's in the same room.
• Climb: The agent can climb out of the cave from the starting room [1,1].
•Sensors (What the agent can sense):
• Stench: If the agent is in the same room as the Wumpus or nearby.
• Breeze: If there's a pit nearby.
• Glitter: If the agent is in the room with the gold.
• Bump: If the agent tries to move into a wall.
• Scream: If the Wumpus is killed, it makes a noise that the agent can hear anywhere in the cave.
The agent uses these sensors to make decisions about where to move and what actions to take in the game
LOGIC
• We cover the syntax of propositional logic and its semantics—the way in which the truth of sentences is
determined.
• Then we look at entailment—the relation between a sentence and another sentence that follows from it—and
see how this leads to a simple algorithm for logical inference.
Syntax
• This grammar follows BNF (Backus-Naur Form) notation, which is a standard way of specifying syntax.
• However, BNF grammar can sometimes be ambiguous, meaning the same sentence can be interpreted in different
ways depending on how it's parsed.
Semantics
In propositional logic, semantics defines how to find the truth value (true or false) of a sentence based on a
given model.
In the wumpus world, we use propositional logic to build a knowledge base. This lets us describe facts about each
location in the grid. Here’s a quick overview of the symbols used:
In propositional logic, we can check if a statement follows logically from a knowledge base (KB) in two main ways:
1.Model Checking: This method tests all possible models (assignments of truth values) to see if a sentence is true in
each one. If so, it’s entailed by the KB. However, this can be slow if there are many models.
2.Theorem Proving: This involves using rules of inference to directly prove the sentence based on what’s in the KB,
without testing every model. This can be faster, especially when the proof is short.
In propositional logic, we use inference rules to draw conclusions from a knowledge base (KB) without
checking all possible models. Here are some key inference rules:
Proof Search
We can automate proof-finding by defining:
•Initial State: Starting knowledge base.
•Actions: Applying inference rules.
•Goal: The statement we want to prove.
Monotonicity
The monotonicity property means that adding new information to the KB can only add more possible conclusions;
it doesn’t undo previous conclusions. So, once a fact is proven, it remains valid no matter what new facts are
added.
Proof by resolution
This section introduces resolution as a powerful inference rule for propositional logic. Resolution allows us to
derive conclusions by combining clauses (disjunctions of literals) to eliminate complementary literals (a literal and
its negation).
This section highlights Horn clauses as a special type of logical clause that makes inference simpler and faster.
Here’s a quick breakdown: