AI-Unit2
AI-Unit2
UNIT 2
1
Unit 2
• Unit-2- Basic Introduction to Data Structure and Search Algorithms 9 Hour
• Basic introduction to stacks, queues, trees and graphs –
• General Search Algorithms –
• Searching for solutions –
• Problem-solving agents –
• Control Strategies –
17
1. Basic introduction to stacks, queues, trees and graphs
Feature Stacks Queues Trees Graphs
Data Structure Linear Linear Hierarchical Non-linear
Principle Last-In-First-Out (LIFO) First-In-First-Out (FIFO) Rooted hierarchical Vertices and edges
Operations Push, Pop Enqueue, Dequeue Insert, Delete, Search Add Vertex, Add Edge
17
Problem Solving Agents
17
Types of AI agents
1. Simple reflex agents
Simple reflex agents are the simplest agent form that grounds actions on
current perception. This agent does not hold any memory, nor does it
interact with other agents if it is missing information. These agents
function on a set of so-called reflexes or rules. This means that the agent is
preprogrammed to perform actions that correspond to certain conditions
being met.
17
Types of AI agents
2. Model-based reflex agents
Model-based reflex agents use both their current perception and memory
to maintain an internal model of the world. As the agent continues to
receive new information, the model is updated. The agent’s actions
depend on its model, reflexes, previous precepts and current state.
17
Types of AI agents
4. Utility-based agents
Utility-based agents select the sequence of actions that reach the goal and
also maximize utility or reward. Utility is calculated using a utility function.
This function assigns a utility value, a metric measuring the usefulness of
an action or how “happy” it will make the agent, to each scenario based
on a set of fixed criteria.
The criteria can include factors such as progression toward the goal, time
requirements, or computational complexity. The agent then selects the
actions that maximize the expected utility. Hence, these agents are useful
in cases where multiple scenarios achieve a desired goal and an optimal
one must be selected.
Example: A navigation system that recommends the route to your
destination that optimizes fuel efficiency and minimizes the time spent in
traffic and the cost of tolls. This agent measures utility through this set of
criteria to select the most favorable route. 17
Types of AI agents
5. Learning agents
Learning agents hold the same capabilities as the other agent types but
are unique in their ability to learn. New experiences are added to their
initial knowledge base, which occurs autonomously. This learning
enhances the agent’s ability to operate in unfamiliar
environments. Learning agents may be utility or goal-based in their
reasoning and are comprised of four main elements
17
Types of AI agents
•Learning: This improves the agent’s knowledge by learning from the
environment through its precepts and sensors.
•Critic: This provides feedback to the agent on whether the quality of its
responses meets the performance standard.
•Performance: This element is responsible for selecting actions upon
learning.
•Problem generator: This creates various proposals for actions to be
taken.
Example: Personalized recommendations on e-commerce sites. These
agents track user activity and preferences in their memory. This
information is used to recommend certain products and services to the
user. The cycle repeats each time new recommendations are made. The
user’s activity is continuously stored for learning purposes. In doing so, the
agent improves its accuracy over time.
17
General Search Algorithm
17
General Search Algorithm-Control strategy
• Control Strategy in Artificial Intelligence scenario is a technique or strategy, tells us
about which rule has to be applied next while searching for the solution of a problem
within problem space.
• It helps us to decide which rule has to apply next without getting stuck at any point.
• These rules decide the way we approach the problem and how quickly it is solved and
even whether a problem is finally solved.
• Control Strategy helps to find the solution when there is more than one rule or fewer
rules for finding the solution at each point in problem space.
17
2. General Search Algorithm-Control strategy
A good Control strategy has two main characteristics:
Control Strategy should cause Motion
• Each rule or strategy applied should cause the motion because if there will be no
motion than such control strategy will never lead to a solution.
• Motion states about the change of state and if a state will not change then there be no
movement from an initial state and we would never solve the problem.
17
Informed and Uninformed Search
• Generates all possible states in the state space and checks for the goal state.
Uninformed • time consuming due to large state space
search • used where error in the algorithm has severe consequences
16
Search
• Search is a general algorithm that helps in finding the path in state space
• The path may lead to the solution or dead end.
• Control strategies- overall rules and approach towards searching
i) forward search(data directed)
Starts search from initial state towards goal state.
Ex: locating a city from current location
ii) backward search(goal directed)
Search stars from goal state towards a solvable initial state.
Ex: start from target city
17
Informed and Uninformed Search
18
Search
BFS
Breadth First Search (BFS) algorithm traverses a graph in a breadth
ward motion and uses a queue to remember to get the next vertex
to start a search when a dead end occurs in any iteration.
DFS
Depth First Search (DFS) algorithm traverses a graph in a depth
ward motion and uses a stack to remember to get the next vertex
to start a search when a dead end occurs in any iteration.
19
Parag Kulkarni, Prachi Joshi, Artificial Intelligence –Building Intelliegent Systems
Uniform Cost Search Algorithm (UCS)
Uniform Cost Search (UCS) explores graphs by expanding nodes from the start to the goal based on edge costs. It
finds the lowest-cost path, essential when step costs vary for optimal solutions.
In this figure, consider S to be the start node and G to be the
goal state. From node S we look for a node to expand, and we
have nodes A and G, but since it’s a uniform cost search, it’s
expanding the node with the lowest step cost, so node A
becomes the successor rather than our required goal node G.
From A we look at its children nodes B and C. Since C has the
lowest step cost, it traverses through node C. Then we look at
the successors of C, i.e., D and G. Since the cost to D is low, we
expand along with node D. Since D has only one child G which
is our required goal state we finally reach the goal state D by
implementing UFS Algorithm. If we have traversed this way,
definitely our total path cost from S to G is just 6 even after
traversing through many nodes rather than going to G directly
where the cost is 12 and 6<<12(in terms of step cost). But this
may not work with all cases.
17
Informed Search
17
Greedy Search algorithm( Best first search)
• Greedy Best-First Search is an AI search algorithm that attempts to find the most
promising path from a given starting point to a goal. It prioritizes paths that
appear to be the most promising, regardless of whether or not they are actually
the shortest path.
• The algorithm works by evaluating the cost of each possible path and then
expanding the path with the lowest cost. This process is repeated until the goal is
reached.
17
How Greedy Search algorithm works
• Greedy Best-First Search works by evaluating the cost of each possible path and
then expanding the path with the lowest cost. This process is repeated until the
goal is reached.
• The algorithm uses a heuristic function to determine which path is the most
promising.
• The heuristic function takes into account the cost of the current path and the
estimated cost of the remaining paths.
• If the cost of the current path is lower than the estimated cost of the remaining
paths, then the current path is chosen. This process is repeated until the goal is
17
reached.
An example of the best-first search algorithm is below graph,
suppose we have to find the path from A to G
• The values in red color represent the heuristic value of reaching the goal node G
from current node
1) We are starting from A , so from A there are direct path to node B( with heuristics value of 32 ) , from
A to C ( with heuristics value of 25 ) and from A to D( with heuristics value of 35 ) .
2) So as per best first search algorithm choose the path with lowest heuristics value , currently C has
lowest value among above node . So we will go from A to C.
17
An example of the best-first search algorithm is below graph,
suppose we have to find the path from A to G
3) Now from C we have direct paths as C to F( with heuristics value of 17 ) and C to E( with heuristics
value of 19) , so we will go from C to F.
17
An example of the best-first search algorithm is below graph,
suppose we have to find the path from A to G
4) Now from F we have direct path to go to the goal node G ( with heuristics value of 0 ) , so we
will go from F to G.
17
An example of the best-first search algorithm is below graph,
suppose we have to find the path from A to G
5) So now the goal node G has been reached and the path we will follow is A->C->F->G .
17
Advantage of Greedy Search algorithm
• Simple and Easy to Implement: Greedy Best-First Search is a relatively straightforward algorithm,
making it easy to implement.
• Fast and Efficient: Greedy Best-First Search is a very fast algorithm, making it ideal for applications
where speed is essential.
• Low Memory Requirements: Greedy Best-First Search requires only a small amount of memory,
making it suitable for applications with limited memory.
• Flexible: Greedy Best-First Search can be adapted to different types of problems and can be easily
extended to more complex problems.
• Efficiency: If the heuristic function used in Greedy Best-First Search is good to estimate, how close
a node is to the solution, this algorithm can be a very efficient and find a solution quickly, even in
large search spaces.
17
A* Search algorithm
• A* (pronounced "A-star") is a powerful graph traversal and pathfinding algorithm widely used in
artificial intelligence and computer science. It is mainly used to find the shortest path between
two nodes in a graph, given the estimated cost of getting from the current node to the destination
node. The main advantage of the algorithm is its ability to provide an optimal path by exploring
the graph in a more informed way compared to traditional search algorithms such as Dijkstra's
algorithm.
• Algorithm A* combines the advantages of two other search algorithms: Dijkstra's algorithm and
Greedy Best-First Search. Like Dijkstra's algorithm, A* ensures that the path found is as short as
possible but does so more efficiently by directing its search through a heuristic similar to Greedy
Best-First Search. A heuristic function, denoted h(n), estimates the cost of getting from any given
node n to the destination node.
17
A* Search algorithm
The main idea of A* is to evaluate each node based on two parameters:
• g(n): the actual cost to get from the initial node to node n. It represents the sum of the costs of
node n outgoing edges.
• h(n): Heuristic cost (also known as "estimation cost") from node n to destination node n. This
problem-specific heuristic function must be acceptable, meaning it never overestimates the
actual cost of achieving the goal. The evaluation function of node n is defined as f(n) = g(n) + h(n).
• Algorithm A* selects the nodes to be explored based on the lowest value of f(n), preferring the
nodes with the lowest estimated total cost to reach the goal. The A* algorithm works:
17
A* Search algorithm
Evaluation Function
The evaluation function, f(x), for the A* search algorithm is the following:
For the algorithm to generate the correct result, the evaluation function must be
admissible, meaning that it never overestimates the cost to arrive at the goal node.
17
A* Search algorithm
The A* algorithm is implemented in a similar way to Dijkstra’s algorithm. Given a
weighted graph with non-negative edge weights, to find the lowest-cost path from a
start node S to a goal node G, two lists are used:
An open list, implemented as a priority queue, which stores the next nodes to be
explored. Because this is a priority queue, the most promising candidate node (the
one with the lowest value from the evaluation function) is always at the top. Initially,
the only node in this list is the start node S.
A closed list which stores the nodes that have already been evaluated. When a node
is in the closed list, it means that the lowest-cost path to that node has been found.
17
A* Search algorithm
To find the lowest cost path, a search tree is constructed in the following way:
1. Initialize a tree with the root node being the start node S.
2. Remove the top node from the open list for exploration.
3. Add the current node to the closed list.
4. Add all nodes that have an incoming edge from the current node as child
nodes in the tree.
5. Update the lowest cost to reach the child node.
6. Compute the evaluation function for every child node and add them to the
open list.
17
A* Search algorithm
Just like in Dijkstra’s algorithm, the lowest cost is updated as the algorithm
progresses in the following way:
current_lowest_cost=min(current_lowest_cost,parent_node_cost+edge_weight)
All nodes except for the start node start with a lowest cost of infinity. The start
node has an initial lowest cost of 0.
The algorithm concludes when the goal node G is removed from the open list
and explored, indicating that a shortest path has been found. The shortest path
is the path from the start node S to the goal node G in the tree.
Note: The algorithm ends when the goal node G has been explored, NOT when it
is added to the open list. 17
A* Search algorithm
Example
Consider the following example of trying to find the shortest path from S to G in
the following graph:
17
A* Search algorithm
Each edge has an associated weight, and each node has a heuristic cost (in parentheses).
An open list is maintained in which the node S is the only node in the list. The search tree can
now be constructed : Exploring S
17
A* Search algorithm
Exploring D:
17
A* Search algorithm
Exploring F:
17
A* Search algorithm
Notice that the goal node G has been found. However, it hasn’t been explored, so the algorithm
continues because there may be a shorter path to G. The node B has two entries in the open list:
one at a cost of 16 (child of S) and one at a cost of 18 (child of A). The one with the lowest cost is
explored next:
17
A* Search algorithm
Exploring C:
17
A* Search algorithm
The next node in the open list is again B. However, because B has already been
explored, meaning the shortest path to B has been found, it is not explored
again, and the algorithm continues to the next candidate.
17
A* Search algorithm
The next node to be explored is the goal node G, meaning the shortest path
to G has been found! The path is constructed by tracing the graph backwards
from G to S:
17