Unit-2 AI
Unit-2 AI
o 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.
o Greedy approach: Hill-climbing algorithm search moves in the direction
which optimizes the cost.
o No backtracking: It does not backtrack the search space, as it does not
remember the previous states.
AD
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.
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 neighbor
states of current states have the same value.
(CSEA10)AI Unit-2 by Prof. Sanchita Chourawar
Medi-caps University, Indore (M.P.)
pg. 2
Shoulder: It is a plateau region which has an uphill edge.
AD
o Step 1: Evaluate the initial state, if it is goal state then return success and
Stop.
o Step 2: Loop Until a solution is found or there is no new operator left to
apply.
o Step 3: Select and apply an operator to the current state.
o Step 4: Check new state:
a. If it is goal state, then return success and quit.
b. Else if it is better than the current state then assign new state as a current
state.
c. Else if not better than the current state, then return to step2.
o Step 5: Exit.
o Step 1: Evaluate the initial state, if it is goal state then return success and
stop, else make current state as initial state.
o Step 2: Loop until a solution is found or the current state does not change.
a. Let SUCC be a state such that any successor of the current state will be
better than it.
b. For each operator that applies to the current state:
a. Apply the new operator and generate a new state.
b. Evaluate the new state.
c. If it is goal state, then return it and quit, else compare it to the SUCC.
d. If it is better than SUCC, then set new state as SUCC.
e. If the SUCC is better than the current state, then set current state to SUCC.
o Step 5: Exit.
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.
o 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.
o Greedy approach: Hill-climbing algorithm search moves in the direction
which optimizes the cost.
o No backtracking: It does not backtrack the search space, as it does not
remember the previous states.
AD
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.
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 neighbor
states of current states have the same value.
o Step 1: Evaluate the initial state, if it is goal state then return success and
Stop.
o Step 2: Loop Until a solution is found or there is no new operator left to
apply.
o Step 3: Select and apply an operator to the current state.
o Step 4: Check new state:
a. If it is goal state, then return success and quit.
b. Else if it is better than the current state then assign new state as a current
state.
c. Else if not better than the current state, then return to step2.
o Step 5: Exit.
o Step 1: Evaluate the initial state, if it is goal state then return success and
stop, else make current state as initial state.
o Step 2: Loop until a solution is found or the current state does not change.
a. Let SUCC be a state such that any successor of the current state will be
better than it.
b. For each operator that applies to the current state:
(CSEA10)AI Unit-2 by Prof. Sanchita Chourawar
Medi-caps University, Indore (M.P.)
pg. 9
a. Apply the new operator and generate a new state.
b. Evaluate the new state.
c. If it is goal state, then return it and quit, else compare it to the SUCC.
d. If it is better than SUCC, then set new state as SUCC.
e. If the SUCC is better than the current state, then set current state to SUCC.
o Step 5: Exit.
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.
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.
Simulated Annealing:
A hill-climbing algorithm which never makes a move towards a lower value
guaranteed to be incomplete because it can get stuck on a local maximum. And if
algorithm applies a random walk, by moving a successor, then it may complete
but not efficient. Simulated Annealing is an algorithm which yields both
efficiency and completeness.
BFS algorithm
In this article, we will discuss the BFS algorithm in the data structure. Breadth-
first search is a graph traversal algorithm that starts traversing the graph from the
root node and explores all the neighboring nodes. Then, it selects the nearest node
and explores all the unexplored nodes. While using BFS for traversal, any node
in the graph can be considered as the root node.
There are many ways to traverse the graph, but among them, BFS is the most
commonly used approach. It is a recursive algorithm to search all the vertices of
a tree or graph data structure. BFS puts every vertex of the graph into two
categories - visited and non-visited. It selects a single node in a graph and, after
that, visits all the nodes adjacent to the selected node.
o BFS can be used to find the neighboring locations from a given source
location.
o In a peer-to-peer network, BFS algorithm can be used as a traversal method
to find all the neighboring nodes. Most torrent clients, such as BitTorrent,
uTorrent, etc. employ this process to find "seeds" and "peers" in the
network.
o BFS can be used in web crawlers to create web page indexes. It is one of
the main algorithms that can be used to index web pages. It starts traversing
from the source page and follows the links associated with the page. Here,
every web page is considered as a node in the graph.
o BFS is used to determine the shortest path and minimum spanning tree.
o BFS is also used in Cheney's technique to duplicate the garbage collection.
(CSEA10)AI Unit-2 by Prof. Sanchita Chourawar
Medi-caps University, Indore (M.P.)
pg. 12
o It can be used in ford-Fulkerson method to compute the maximum flow in
a flow network.
Algorithm
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.
(CSEA10)AI Unit-2 by Prof. Sanchita Chourawar
Medi-caps University, Indore (M.P.)
pg. 13
Now, let's start examining the graph starting from Node A.
1. QUEUE1 = {A}
2. QUEUE2 = {NULL}
Step 2 - Now, delete node A from queue1 and add it into queue2. Insert all
neighbors of node A to queue1.
1. QUEUE1 = {B, D}
2. QUEUE2 = {A}
Step 3 - Now, delete node B from queue1 and add it into queue2. Insert all
neighbors of node B to queue1.
1. QUEUE1 = {D, C, F}
2. QUEUE2 = {A, B}
Step 4 - Now, delete node D from queue1 and add it into queue2. Insert all
neighbors of node D to queue1. The only neighbor of Node D is F since it is
already inserted, so it will not be inserted again.
1. QUEUE1 = {C, F}
2. QUEUE2 = {A, B, D}
Step 5 - Delete node C from queue1 and add it into queue2. Insert all neighbors
of node C to queue1.
1. QUEUE1 = {F, E, G}
2. QUEUE2 = {A, B, D, C}
Step 5 - Delete node F from queue1 and add it into queue2. Insert all neighbors
of node F to queue1. Since all the neighbors of node F are already present, we
will not insert them again.
AD
1. QUEUE1 = {E, G}
2. QUEUE2 = {A, B, D, C, F}
1. QUEUE1 = {G}
2. QUEUE2 = {A, B, D, C, F, E}
The space complexity of BFS can be expressed as O(V), where V is the number
of vertices.
In this code, we are using the adjacency list to represent our graph. Implementing
the Breadth-First Search algorithm in Java makes it much easier to deal with the
adjacency list since we only have to travel through the list of nodes attached to
each node once the node is dequeued from the head (or start) of the queue.
In this example, the graph that we are using to demonstrate the code is given as
follows -
Because of the recursive nature, stack data structure can be used to implement the
DFS algorithm. The process of implementing the DFS is similar to the BFS
algorithm.
The step by step process to implement the DFS traversal is given as follows -
1. First, create a stack with the total number of vertices in the graph.
2. Now, choose any vertex as the starting point of traversal, and push that
vertex into the stack.
3. After that, push a non-visited vertex (adjacent to the vertex on the top of
the stack) to the top of the stack.
4. Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex
on the stack's top.
5. If no vertex is left, go back and pop a vertex from the stack.
6. Repeat steps 2, 3, and 4 until the stack is empty.
Algorithm
Step 1: SET STATUS = 1 (ready state) for each node in G
(CSEA10)AI Unit-2 by Prof. Sanchita Chourawar
Medi-caps University, Indore (M.P.)
pg. 16
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting
state)
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
Pseudocode
1. DFS(G,v) ( v is the vertex where the search starts )
2. Stack S := {}; ( start with an empty stack )
3. for each vertex u, set visited[u] := false;
4. push S, v;
5. while (S is not empty) do
6. u := pop S;
7. if (not visited[u]) then
8. visited[u] := true;
9. for each unvisited neighbour w of uu
10. push S, w;
11. end if
12. end while
13. END DFS()
1. STACK: H
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.
1. 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.
1. Print: A
2. 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.
AD
1. Print: D
2. 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.
1. Print: F
2. 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.
(CSEA10)AI Unit-2 by Prof. Sanchita Chourawar
Medi-caps University, Indore (M.P.)
pg. 18
1. Print: B
2. 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.
1. Print: C
2. 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.
1. Print: G
2. 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.
AD
1. Print: E
2. STACK:
Now, all the graph nodes have been traversed, and the stack is empty.
In this example, the graph that we are using to demonstrate the code is given as
follows -
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.
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 we will discuss two main algorithms which are given
below:
A* Search Algorithm:
A* search is the most commonly known form of best-first search. It uses heuristic
function h(n), and cost to reach the node n from the start state g(n). It has
combined features of UCS and greedy best-first search, by which it solve the
problem efficiently. A* search algorithm finds the shortest path through the
search space using the heuristic function. This search algorithm expands less
search tree and provides optimal result faster. A* algorithm is similar to UCS
except that it uses g(n)+h(n) instead of g(n).
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.
(CSEA10)AI Unit-2 by Prof. Sanchita Chourawar
Medi-caps University, Indore (M.P.)
pg. 21
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.
Advantages:
o A* search algorithm is the best algorithm than other search algorithms.
o A* search algorithm is optimal and complete.
o This algorithm can solve very complex problems.
Disadvantages:
o It does not always produce the shortest path as it mostly based on heuristics
and approximation.
o A* search algorithm has some complexity issues.
o 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.
Iteration 4 will give the final result, as S--->A--->C--->G it provides the optimal
path with cost 6.
Points to remember:
o A* algorithm returns the path which occurred first, and it does not search
for all remaining paths.
o The efficiency of A* algorithm depends on the quality of heuristic.
o A* algorithm expands all nodes which satisfy the condition f(n)<="" li="">
o Admissible: the first condition requires for optimality is that h(n) should
be an admissible heuristic for A* tree search. An admissible heuristic is
optimistic in nature.
o Consistency: Second required condition is consistency for only A* graph-
search.
If the heuristic function is admissible, then A* tree search will always find the
least cost path.
AND-OR Graph
In the above figure, the buying of a car may be broken down into smaller
problems or tasks that can be accomplished to achieve the main goal in the
above figure, which is an example of a simple AND-OR graph. The other task is
to either steal a car that will help us accomplish the main goal or use your own
money to purchase a car that will accomplish the main goal. The AND symbol
is used to indicate the AND part of the graphs, which refers to the need that all
subproblems containing the AND to be resolved before the preceding node or
issue may be finished.
The start state and the target state are already known in
the knowledge-based search strategy known as the AO* algorithm, and the best
path is identified by heuristics. The informed search technique considerably
Here in the above example below the Node which is given is the heuristic value
i.e h(n). Edge length is considered as 1.
Step 1
Step 3
f(C⇢H+I) is selected as the path with the lowest cost and the heuristic is
also left unchanged
because it matches the actual cost. Paths H & I are solved because the heuristic
for those paths is 0,
but Path A⇢D needs to be calculated because it has an AND.
as we can see that path f(A⇢C+D) is get solved and this tree has become a
solved tree now.
In simple words, the main flow of this algorithm is that we have to find firstly
level 1st heuristic
value and then level 2nd and after that update the values with going upward
means towards the root node.
In the above tree diagram, we have updated all the values.
In constraint satisfaction, domains are the areas wherein parameters were located
after the restrictions that are particular to the task. Those three components make
up a constraint satisfaction technique in its entirety. The pair "scope, rel" makes
up the number of something like the requirement. The scope is a tuple of variables
that contribute to the restriction, as well as rel is indeed a relationship that
contains a list of possible solutions for the parameters should assume in order to
meet the restrictions of something like the issue.
o States area
o fundamental idea while behind remedy.
AD
The definition of a state in phase space involves giving values to any or all of the
parameters, like as
o Discrete Domain: This limitless area allows for the existence of a single
state with numerous variables. For instance, every parameter may receive
a endless number of beginning states.
o It is a finite domain with continous phases that really can describe just one
area for just one particular variable. Another name for it is constant area.
o Unary restrictions are the easiest kind of restrictions because they only
limit the value of one variable.
o Binary resource limits: These restrictions connect two parameters. A value
between x1 and x3 can be found in a variable named x2.
o Global Resource limits: This kind of restriction includes a unrestricted
amount of variables.
The main kinds of restrictions are resolved using certain kinds of resolution
methodologies:
Note: The preferences restriction is a unique restriction that operates in the actual world.
Think of a Sudoku puzzle where some of the squares have initial fills of certain
integers.
You must complete the empty squares with numbers between 1 and 9, making
sure that no rows, columns, or blocks contains a recurring integer of any kind.
This solving multi - objective issue is pretty elementary. A problem must be
solved while taking certain limitations into consideration.