UNIT2
UNIT2
Problem-solving agents:
In Artificial Intelligence, Search techniques are universal problem-solving
methods. Rational agents or Problem-solving agents in AI mostly used these search
strategies or algorithms to solve a specific problem and provide the best result.
Problem-solving agents are the goal-based agents and use atomic representation.
Space Complexity: It is the maximum storage space required at any point during the
search, as the complexity of the problem.
The goal of the 8-queens problem is to place eight queens on a 8 X 8 chessboard such that no
queen attacks any other. (A queen attacks another queen if they are in the same row, column
or diagonal.) The figure below shows an attempted solution that fails: the queen in the
rightmost column is attacked by the queen at the top left.
Although efficient special-purpose algorithms exist for this problem and for the whole n-
queens family, it remains a useful test problem for search algorithms. There are two main kinds
of formulation. An incremental formulation involves operators that augment the state
INCREMENTAL FORMULATION description, starting with an empty state; for the 8-
queens problem, this means that each action adds a queen to the state. A complete-state
formulation starts with all 8 queens on COMPLETE-STATE FORMULATION the board
and moves them around. In either case, the path cost is of no interest because only the final
state counts. The first incremental formulation one might try is the following:
o Breadth-first search
o Uniform cost search
o Depth-first search
o Iterative deepening depth-first search
o Bidirectional Search
Informed Search
Informed search algorithms use domain knowledge. In an informed search, problem
information is available which can guide the search. Informed search strategies can
find a solution more efficiently than an uninformed search strategy. Informed search
is also called a Heuristic search.
A heuristic is a way which might not always be guaranteed for best solutions but
guaranteed to find a good solution in reasonable time.
Informed search can solve much complex problem which could not be solved in
another way.
1. Breadth-first Search
2. Depth-first Search
3. Depth-limited Search
4. Iterative deepening depth-first search
5. Uniform cost search
6. Bidirectional Search
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:
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.
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:
BFS ALGORITHM
Algorithm BFS(graph, start_vertex)
{
queue = create an empty queue
visited = create an empty set or array to track visited vertices
enqueue start_vertex into the queue
add start_vertex to visited
while queue is not empty:
current_vertex = dequeue from the queue
process(current_vertex) // e.g., print or perform operations on the current_vertex
for each neighbour in graph[current_vertex]:
if neighbour is not in visited:
enqueue neighbour into the queue
add neighbour to visited
}
Algorithm for BFS
The steps involved in the BFS algorithm to explore a graph are given as follows -
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS =
1) and set their STATUS = 2
(waiting state)
[END OF LOOP]
Step 6: EXIT
In the above graph, minimum path 'P' can be found by using the BFS that will start from Node A and
end at Node E. The algorithm uses two queues, namely QUEUE1 and QUEUE2. QUEUE1 holds all the
nodes that are to be processed, while QUEUE2 holds all the nodes that are processed and deleted
from QUEUE1.
Step 2 - Now, delete node A from queue1 and add it into queue2. Insert all neighbors of node A to
queue1.
QUEUE1 = {B, D}
QUEUE2 = {A}
Step 3 - Now, delete node B from queue1 and add it into queue2. Insert all neighbors of node B to
queue1.
QUEUE1 = {D, C, F}
QUEUE2 = {A, B}
Step 4 - Now, delete node D from queue1 and add it into queue2. Insert all neighbours of node D
to queue1. The only neighbour of Node D is F since it is already inserted, so it will not be inserted
again.
QUEUE1 = {C, F}
QUEUE2 = {A, B, D}
Step 5 - Delete node C from queue1 and add it into queue2. Insert all neighbours of node C to
queue1.
QUEUE1 = {F, E, G}
QUEUE2 = {A, B, D, C}
Step 5 - Delete node F from queue1 and add it into queue2. Insert all neighbours of node F to
queue1. Since all the neighbours of node F are already present, we will not insert them again.
QUEUE1 = {E, G}
QUEUE2 = {A, B, D, C, F}
Step 6 - Delete node E from queue1. Since all of its neighbours have already been added, so we will
not insert them again. Now, all the nodes are visited, and the target node E is encountered into
queue2.
QUEUE1 = {G}
QUEUE2 = {A, B, D, C, F, E}
2. DEPTH-FIRST SEARCH:
Depth-First Search or DFS algorithm is a recursive algorithm that uses the backtracking principle. It
entails conducting exhaustive searches of all nodes by moving forward if possible and backtracking, if
necessary. To visit the next node, pop the top node from the stack and push all its nearby nodes into
a stack.
Algorithm
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
Step 3: Repeat Steps 4 and 5 until STACK is empty
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
Step 5: Push on the stack all the neighbors of N that are in the ready state (whose STATUS = 1) and set
their STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
Step 2 - POP the top element from the stack, i.e., H, and print it. Now, PUSH all the neighbors of H
onto the stack that are in ready state.
Print: H
STACK: A
Step 3 - POP the top element from the stack, i.e., A, and print it. Now, PUSH all the neighbors of A
onto the stack that are in ready state.
Print: A
STACK: B, D
Step 4 - POP the top element from the stack, i.e., D, and print it. Now, PUSH all the neighbors of D
onto the stack that are in ready state.
Print: D
STACK: B, F
Step 5 - POP the top element from the stack, i.e., F, and print it. Now, PUSH all the neighbors of F
onto the stack that are in ready state.
Print: F
STACK: B
Step 6 - POP the top element from the stack, i.e., B, and print it. Now, PUSH all the neighbors of B
onto the stack that are in ready state.
Print: B
STACK: C
Step 7 - POP the top element from the stack, i.e., C, and print it. Now, PUSH all the neighbors of C
onto the stack that are in ready state.
Print: C
STACK: E, G
Step 8 - POP the top element from the stack, i.e., G and PUSH all the neighbors of G onto the stack
that are in ready state.
Print: G
STACK: E
Step 9 - POP the top element from the stack, i.e., E and PUSH all the neighbors of E onto the stack
that are in ready state.
Print: E
STACK:
Now, all the graph nodes have been traversed, and the stack is empty.
Informed Search Algorithms
So far we have talked about the uninformed search algorithms which looked through search
space for all possible solutions of the problem without having any additional knowledge about
search space. But informed search algorithm contains an array of knowledge such as how far
we are from the goal, path cost, how to reach to goal node, etc. This knowledge help agents
to explore less to the search space and find more efficiently the goal node.
The informed search algorithm is more useful for large search space. Informed search
algorithm uses the idea of heuristic, so it is also called Heuristic search.
Heuristics function: Heuristic is a function which is used in Informed Search, and it finds the
most promising path. It takes the current state of the agent as its input and produces the
estimation of how close agent is from the goal. The heuristic method, however, might not
always give the best solution, but it guaranteed to find a good solution in reasonable time.
Heuristic function estimates how close a state is to the goal. It is represented by h(n), and it
calculates the cost of an optimal path between the pair of states. The value of the heuristic
function is always positive.
Here h(n) is heuristic cost, and h*(n) is the estimated cost. Hence heuristic cost should be less
than or equal to the estimated cost.
On each iteration, each node n with the lowest heuristic value is expanded and generates all
its successors and n is placed to the closed list. The algorithm continues unit a goal state is
found.
In the informed search there are two main algorithms which are given below:
• Best First Search Algorithm (Greedy search)
• A* Search Algorithm
•
1.) Best-first Search Algorithm (Greedy Search):
Greedy best-first search algorithm always selects the path which appears best at that moment.
It is the combination of depth-first search and breadth-first search algorithms. It uses the
heuristic function and search. Best-first search allows us to take the advantages of both
algorithms. With the help of best-first search, at each step, we can choose the most promising
node. In the best first search algorithm, we expand the node which is closest to the goal node
and the closest cost is estimated by heuristic function, i.e.
f(n)= g(n).
Were, h(n)= estimated cost from node n to the goal.
The greedy best first algorithm is implemented by the priority queue.
Advantages:
• Best first search can switch between BFS and DFS by gaining the advantages of both
the algorithms.
• This algorithm is more efficient than BFS and DFS algorithms.
Disadvantages:
• It can behave as an unguided depth-first search in the worst-case scenario.
• It can get stuck in a loop as DFS.
• This algorithm is not optimal.
EXAMPLE:
Consider the below search problem, and we will traverse it using greedy best-first search. At
each iteration, each node is expanded using evaluation function f(n)=h(n) , which is given in
the below table: H(n)= estimated cost from node n to the goal.
In this search example, we are using two lists which are OPEN and CLOSED Lists. Following are
the iteration for traversing the above example.
In A* search algorithm, we use search heuristic as well as the cost to reach the node. Hence we can
combine both costs as following, and this sum is called as a fitness number. At each point in the search
space, only those node is expanded which have the lowest value of f(n), and the algorithm terminates
when the goal node is found.
Algorithm of A* search:
Step1: Place the starting node in the OPEN list.
Step 2: Check if the OPEN list is empty or not, if the list is empty then return failure and stops.
Step 3: Select the node from the OPEN list which has the smallest value of evaluation function (g+h),
if node n is goal node then return success and stop, otherwise
Step 4: Expand node n and generate all of its successors, and put n into the closed list. For each
successor n', check whether n' is already in the OPEN or CLOSED list, if not then compute evaluation
function for n' and place into Open list.
Step 5: Else if node n' is already in OPEN and CLOSED, then it should be attached to the back pointer
which reflects the lowest g(n') value.
Step 6: Return to Step 2.
Advantages:
• A* search algorithm is the best algorithm than other search algorithms.
• A* search algorithm is optimal and complete.
• This algorithm can solve very complex problems.
Disadvantages:
• It does not always produce the shortest path as it mostly based on heuristics and
approximation.
• A* search algorithm has some complexity issues.
• The main drawback of A* is memory requirement as it keeps all generated nodes in the
memory, so it is not practical for various large-scale problems.
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.
SOLUTION
Initialization: {(S, 5)}
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.
HEURISTIC FUNCTION
A heuristic function, also simply called a heuristic, is a function that ranks alternatives in search
algorithms at each branching step based on available information to decide which branch to follow.
For example, it may approximate the exact solution.
Heuristics are mental shortcuts for solving problems in a quick way that delivers a result that is
sufficient enough to be useful given time constraints. Investors and financial professionals use a
heuristic approach to speed up analysis and investment decisions.
OPTIMIZATION PROBLEMS
Optimization is the problem of finding a set of inputs to an objective function that results in a maximum
or minimum function evaluation. It is the challenging problem that underlies many machine learning
algorithms, from fitting logistic regression models to training artificial neural networks.
We can distinguish between two different types of optimization methods: Exact optimization methods
that guarantee finding an optimal solution and heuristic optimization methods where we have no
guarantee that an optimal solution is found.
➢ Generate and Test variant: Hill Climbing is the variant of Generate and Test method. The
Generate and Test method produce feedback which helps to decide which direction to move
in the search space.
➢ Greedy approach: Hill-climbing algorithm search moves in the direction which optimizes the
cost.
➢ No backtracking: It does not backtrack the search space, as it does not remember the previous
states.
On Y-axis we have taken the function which can be an objective function or cost function, and state-
space on the x-axis. If the function on Y-axis is cost then, the goal of search is to find the global
minimum and local minimum. If the function of Y-axis is Objective function, then the goal of the search
is to find the global maximum and local maximum.
DIFFERENT REGIONS IN THE STATE SPACE LANDSCAPE:
Local Maximum: Local maximum is a state which is better than its neighbour states, but there
is also another state which is higher than it.
Global Maximum: Global maximum is the best possible state of state space landscape. It has
the highest value of objective function.
Flat local maximum: It is a flat space in the landscape where all the neighbour states of
current states have the same value.
2. Plateau: A plateau is the flat area of the search space in which all the neighbor
states of the current state contains 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.
Solution: 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.
There are mainly three basic components in the constraint satisfaction problem:
Variables: The things that need to be determined are variables. Variables in a CSP are the objects
that must have values assigned to them in order to satisfy a particular set of constraints. Boolean,
integer, and categorical variables are just a few examples of the various types of variables Variables,
for instance, could stand in for the many puzzle cells that need to be filled with numbers in a sudoku
puzzle.
Domains: The range of potential values that a variable can have is represented by domains. Depending
on the issue, a domain may be finite or limitless. For instance, in Sudoku, the set of numbers from 1 to
9 can serve as the domain of a variable representing a problem cell.
Constraints: The guidelines that control how variables relate to one another are known as constraints.
Constraints in a CSP define the ranges of possible values for variables. Unary constraints, binary
constraints, and higher-order constraints are only a few examples of the various sorts of constraints.
Constraint Satisfaction Problems (CSP) representation:
The finite set of variables V1, V2, V3 ……………..Vn.
Non-empty domain for every single variable D1, D2, D3 …………..Dn.
The finite set of constraints C1, C2 …….…, Cm.
• where each constraint Ci restricts the possible values for variables,
e.g., V1 ≠ V2
• Each constraint Ci is a pair <scope, relation>
Example: <(V1, V2), V1 not equal to V2>
• Scope = set of variables that participate in constraint.
• Relation = list of valid variable value combinations.
• There might be a clear list of permitted combinations. Perhaps a relation that is
abstract and that allows for membership testing and listing.
For example, in a crossword puzzle it is only required that words that cross each other have the same
letter in the location where they cross. It would be a general search problem if we require, say, that
we use at most 15 vowels.
For instance, in a sudoku problem, the restrictions might be that each row, column, and 3×3 box can
only have one instance of each number from 1 to 9. Each square must contain a single number, from
1 to 9. The same number can't appear in the same row twice. The same number can't appear in the
same column twice. The grid is broken down into 9 distinct sub-grids of 9 squares each. Considering
all rows/columns and major blocks leads to 54 such constraints, exactly once in each row, but also in
each block