0% found this document useful (0 votes)
22 views75 pages

Ai Unit 1

The document discusses various search algorithms including uninformed search, heuristic search, and constraint satisfaction. It provides details on breadth-first search, depth-first search, A* search, hill climbing, and best-first search algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views75 pages

Ai Unit 1

The document discusses various search algorithms including uninformed search, heuristic search, and constraint satisfaction. It provides details on breadth-first search, depth-first search, A* search, hill climbing, and best-first search algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 75

ARTIFICIAL

INTELLIGENCE
UNIT I
INDEX
• Uninformed Search (Breadth-First, Depth-First Search, Depth-first
with Iterative Deepening
• Heuristic Search (Hill Climbing, Generic Best-First, A*)
• Constraint Satisfaction (Backtracking, Local Search)
UNINFORMED SEARCH (BREADTH-FIRST, DEPTH-
FIRST SEARCH, DEPTH-FIRST WITH ITERATIVE
DEEPENING
• Uninformed search is a class of general-purpose search algorithms
which operates in brute force-way.
• Uninformed search algorithms do not have additional information
about state or search space other than how to traverse the tree, so it is
also called blind search.
1. Breadth-First Search Algorithms

• BFS is a search operation for finding the nodes in a tree.


• The algorithm works breadthwise and traverses to find the desired
node in a tree.
• It starts searching operation from the root nodes and expands the
successor nodes at that level before moving ahead and then moves
along breadth wise for further expansion.
About BFS
• It occupies a lot of memory space, and time to execute when the
solution is at the bottom or end of the tree and uses the FIFO queue.
• Time Complexity of BFS is expressed as :
T (n) = 1+n2+n3+…….+ nd= O (nd) and;
• Space Complexity of BFS is O (nd).
• The breadth-first search algorithm is complete.
• The optimal solution is possible to obtain from BFS.
• Starts from the root node
• Moves to the left node and then to the
right node.
• The above repeats until the leaf node is
reached
Procedure
1.Start by putting any one of the graph's vertices at the back of a queue.
2.Take the front item of the queue and add it to the visited list.
3.Create a list of that vertex's adjacent nodes. Add the ones which aren't
in the visited list to the back of the queue.
4.Keep repeating steps 2 and 3 until the queue is empty.
Pseudocode for BFS
create a queue Q
mark v as visited and put v into Q
while Q is non-empty
remove the head u of Q
mark and enqueue all (unvisited) neighbours of u
Mechanism
Start at the root node – push it into the stack and mark as visited.
Follow the order and choose the adjacent nodes mark as visited(Push
into stack once visited)
Pop the element if no adjacent node is present .
Repeat the above steps until every node is marked as visited.
Start popping the elements of the stack following LIFO .
Emptying the stack will terminate the algorithm.
Pseucode for DFS
DFS(visited, u):
visited[u]=True
Print u
for each v in adj[u]:
if(visited[v]=False)
DFS(visited,v)
Depth first Search with Iterative
Deepening
Since H node nas no sub tre
we need to move to the right
subtree of B
We need to keep a
restriction at every level
before we reach each
node.
HEURISTIC SEARCH ALGORITHMS
• A heuristic is a technique that is used to solve a problem faster than the classic
methods.
• These techniques are used to find the approximate solution of a problem when
classical methods do not.
• Heuristics are said to be the problem-solving techniques that result in
practical and quick solutions.
• Heuristics are strategies that are derived from past experience with similar
problems.
• Heuristics use practical methods and shortcuts used to produce the solutions
that may or may not be optimal, but those solutions are sufficient in a given
limited timeframe.
• In hill climbing algorithm , a node is created at a point and generates
new nodes adjacent to it.
• Among them , it chooses the best node , moves to it and erases the
remaining nodes.
• The above steps repeats iteratively.
A* SEARCH ALGORITHM
• The A* algorithm is fastest and provide the
best shortest path.
• . It uses heuristic to find the path. In present
era only one directional search algorithm are
used, we will use the Bi-directional search
method for searching the path instead of
Dijkstra algorithm due to demand of time
and situations because of its robustness as
well as its variants
• Dijkstra algorithm due to demand of time
and situations because of its robustness as
well as its variants. This method reduces the
searching time of the system and gets the
fastest and best path.
• A* algorithm is a graph search algorithm that finds a path from a
given initial node to a given goal node.
• It employs a "heuristic estimate" h(x) that gives an estimate of the
best route that goes through that node.
• It visits the nodes in order of this heuristic estimate
• It follows the approach of best first search.
Unidirectional:
• It works by maintaining two lists; the open list and the closed list.
• The open list initially contains the start node; it contains all nodes that are yet to be
considered.
• If the open list becomes empty then there is no possible path.
• The closed list starts out empty and contains all nodes that have already been
considered /visited.
• The core loop of the algorithm selects a node from the open list with the lowest
estimated cost to reach the goal.
• If the selected node is not the goal it puts all valid neighbouring nodes into the open
list and repeats the process.
• Part of the magic in the algorithm is that all nodes that are created keep a reference to
their parent.
• This means that from any node we can track backwards to get a path from the node
and the start node
Pseudo code
1) At initialization add the starting location to the open list and empty the closed list
2) While there are still more possible next steps in the open list and we haven’t found the
target:
A) Select the most likely next step (based on both the heuristic and path costs)
B) Remove it from the open list and add it to the closed C) Consider each neighbor of the
step.
For each neighbour:
i) calculate the path cost of reaching the neighbour
ii) If the cost is less than the cost known for this location then remove it from the open or
closed lists (since we’ve now found a better route)
iii) If the location isn’t in either the open or closed list then record the costs for the
location and add it to the open list (this means it’ll be considered in the next search).
Record how we got to this location
The loop ends when we either find a route to the destination or we run out of steps. If a route
is found we back track up the records of how we reached each location to determine the path.
EXAMPLE
Bi-directional search
• As the name suggests, bidirectional search suggests running 2
simultaneous searches, one from the initial state and the other from
the goal state, those 2 searches stop when they meet each other at
some point in the middle of the graph..
Pseudo code
1) At initialization add the starting and goal location to the open list and
empty the closed list
2) Begin with a starting node with options for further travel: Also have a list
of coordinates with nodes demonstrating movement options towards goal
intersection
3) Consider each neighbour of the step.
For each neighbor:
i) calculate the path cost of reaching the neighbor
ii) If the cost is less than the cost known for this location then remove
it from the open or closed lists (since we’ve now found a better route)
iii) If the start and goal path cost is same then intersect The loop
ends when we either find a route to the destination or we run out of
steps.
EXAMPLE
Best First Search Algorithm
• The Best-first search uses two sets.
• One is open dataset (those generated but not yet selected) another one is closed dataset
(already selected).
• Best-first search progresses by choosing a node with the best f-value in the Open list for
expansion.
• The node is removed from the Open list and each of its children’s are generated.
• For each child node that is generated, best-first search checks the Open and Closed lists
to ensure that it is not a duplicate of a previously generated node or, if it is, that it
represents a better path than the previously generated duplicate.
• If that is the case, then the node is inserted into the closed list and a new node is chosen
for expansion.
• This continues until a goal node is chosen for expansion.
Process Flow of Best-first Search
1. Put the start node s on a list called OPEN of unexpanded nodes.
2. If OPEN is empty exit with failure; no solutions exists.
3. Remove the first OPEN node n at which f is minimum (break ties
arbitrarily), and place it on a list called CLOSED to be used for
expanded nodes.
4. Expand node n, generating all its successors with pointers back to n.
5. If any of no’s successors is a goal node, exit successfully with the
solution obtained by tracing the path along the pointers from the
goal back to s.
6. For every successor n’ on n:
I. Calculate f (n’)
II. If n’ was neither on OPEN nor on CLOSED, add it to OPEN. Attach a
pointer from n’ back to n. Assign the newly computed f(n’) to node
n’.
III. If n’ already resided on OPEN or CLOSED, compare the newly
computed f(n) with the value previously assigned to n’. If the old
value is lower, discard the newly generated node. If the new value
is lower, substitute it for the old (n’ now points back to n instead of
to its previous predecessor). If the matching node n’ resided on
CLOSED, move it back to OPEN.
IV. Go to step 2.
Search Flow of Best-First Search
1.Open=A5; closed=[]
2.evaluate A5;open=[B4,C4,D6];closed=[A5]
3. evaluate B4;open=[C4,E5,F5,D6];closed=[B4,A5]
4. evaluate C4;open=[H3,G4,E5,F5,D6];=closed=[C4,B4, A5]
5. evaluate H3;open[O2,P3,G4,E5,F5,D6];closed=[H3,C
4,B4,A5]
6. evaluate O2;open=[P3,G4,E5,F5,D6];closed=[O2,H3,
C4,B4,A5]
7. Evaluate P3; the solution is found!
CONSTRAINT SATISFACTION
• Constraint satisfaction problem is one that requires its own solution with in some
limitations / conditions also known as constraints.
• It contains the following:
1. A finite set of variables which stores the solution ( V={v1,v2..}})
2. A set of discrete values known as domain which solution is picked(D={d1,d2,d3… dn})
3. A finite set of constraints (C={c1,c2,c3…, cn})

• Examples: Cross Word puzzle


Crypt – Arithmetic problem
Map colouring problem
BACKTRACKING ALGORITHM
• Backtracking is an algorithmic technique whose goal is to use brute
force to find all solutions to a problem.
• It entails gradually compiling a set of all possible solutions.
• Because a problem will have constraints, solutions that do not meet
them will be removed.
Backtrack(s)

ifs is not a solution

return false

if is a new solution

add to list of solutions

backtrack(expand s)
• It finds a solution by building a solution step by step, increasing levels
over time, using recursive calling.
• A search tree known as the state-space tree is used to find these
solutions.
• Each branch in a state-space tree represents a variable, and each
level represents a solution.
• A backtracking algorithm uses the depth-first search method.
• When the algorithm begins to explore the solutions, the abounding
function is applied so that the algorithm can determine whether the
proposed solution satisfies the constraints.
• If it does, it will keep looking. If it does not, the branch is removed,
and the algorithm returns to the previous level.
State-Space Tree
• A space state tree is a tree that represents all of the possible
states of the problem, from the root as an initial state to the leaf
as a terminal state.
How Does a Backtracking Algorithm Work?
• In any backtracking algorithm, the algorithm seeks a path to a feasible
solution that includes some intermediate checkpoints.
• If the checkpoints do not lead to a viable solution, the problem can
return to the checkpoints and take another path to find a solution.
Consider the following scenario:
• In this case, S represents the problem's starting point.
• You start at S and work your way to solution S1 via the midway point M1.
• However, you discovered that solution S1 is not a viable solution to our
problem.
• As a result, you backtrack (return) from S1, return to M1, return to S, and
then look for the feasible solution S2.
• This process is repeated until you arrive at a workable solution.
• S1 and S2 are not viable options in this case. According to this example, only
S3 is a viable solution.
• When you look at this example, you can see that we go through all possible
combinations until you find a viable solution.
• As a result, you refer to backtracking as a brute-force algorithmic technique.
• A "space state tree" is the above tree representation of a problem.
• It represents all possible states of a given problem (solution or non-solution).
When to Use a Backtracking Algorithm?
• There are the following scenarios in which you can use the
backtracking:
• It is used to solve a variety of problems. You can use it, for example, to
find a feasible solution to a decision problem.
• Backtracking algorithms were also discovered to be very effective for
solving optimization problems.
• In some cases, it is used to find all feasible solutions to the
enumeration problem.
• Backtracking, on the other hand, is not regarded as an optimal
problem-solving technique.
• It is useful when the solution to a problem does not have a time limit.
Types of Backtracking Algorithm 
• Backtracking algorithms are classified into two types:
1.Algorithm for recursive backtracking
2.Non-recursive backtracking algorithm
1. Algorithm Backtrack (s)
2. // Using recursion, this scheme describes the backtracking process.
3. //The first s-1 values are entered.
4. // z [1], z [2]… z [s-1] of the solution vector.
5. // z [1:n] have been assigned. Z [] and n are global.
6. {
Algorithm for 7. For (each z [s] £ T (z [1],……,z [s-1]) do
Recursive
Backtracking 8. {
9. If ( Bk (z [1], z[2],……….,z [s] != 0) then10 {
11. If (z[1], z[2], …… , z[s] is a path to an answer node )
12. Then write (z[1:s]);
13. If(s<n) then backtrack (s+1);
14. }
15. }
16. }
1. Backtracking Algorithm(s)
2. This scheme describes the process of backtracking.
3. // all solutions in z [1: n] are generated and printed
3. / all solutions in z [1: n] are generated and printed
5.{
6.s=1;
7. While (s!= 0) do
Algorithm for
8.{
Recursive
Backtracking 9.If ( there remains are untried z [1] £ X ( z [1], z [2], ….., z [s-1]) and Bk (z[1], ….., z[s]) is
true) then
10. {
11. If ( z[1], …., z[s] is a path to an answer node)
12. Then write ( z[1 : s]);
13. S = s + 1
14. }
15. Else s=s - 1 // backtrack the previous set
16.}
17.}
Applications of Backtracking Algorithm
• To Find All Hamiltonian Paths Present in a Graph.
• To Solve the N Queen Problem.
• Maze Solving Problems
• The Knight's Tour Problem

You might also like