0% found this document useful (0 votes)
5 views

Module II (part 1)

The document discusses various problem-solving methods in artificial intelligence (AI) and machine learning (ML), focusing on state-space search and its applications. It contrasts uninformed search methods like Breadth-First Search (BFS) and Depth-First Search (DFS) with informed search techniques, highlighting their advantages and disadvantages. The document also covers advanced search algorithms such as bidirectional search and iterative deepening search, emphasizing their efficiency and use cases.

Uploaded by

SHUBH SINGH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Module II (part 1)

The document discusses various problem-solving methods in artificial intelligence (AI) and machine learning (ML), focusing on state-space search and its applications. It contrasts uninformed search methods like Breadth-First Search (BFS) and Depth-First Search (DFS) with informed search techniques, highlighting their advantages and disadvantages. The document also covers advanced search algorithms such as bidirectional search and iterative deepening search, emphasizing their efficiency and use cases.

Uploaded by

SHUBH SINGH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Problem Solving Methods

Introduction to AI and ML

CSA2001
Introduction

• One of the major application of AI is the


problem solving. That is how to solve a particular
problem.
• During 1970s, the main research in AI happened
in the area of problem solving.
• We wanted to solve problems such as tick-tack-
toe, 8-Queen problem, chess, GO games using
machines or agents. This is called problem
solving area.
• In this, state space search is one important
concept.
State-Space Search
❑ The number of states in which the problem can go is
referred to as state-space search.

❑ Example 1:
Final year project
❑ Example 2:
Planning a destination in google maps.

❑ To represent the problem precisely in machines.

❑ Once we represent problem precisely, it is easy to


analyze the game either for human or for machines.
State-Space Search
❑ It is represented with S : {S, A, Result(S, a), Cost(S, a)}.
❑ In the above equation, S = Start, Goal, Intermediate
states
❑ For example, take 8-puzzle problem.

2 3 4
1 2 3
5 1
8 4
3X3, consists 8 7 6
7 6 5
of 8 tiles, one
empty space Start state Goal state
❑ In state-space search, we will explore various
intermediate sates of the start sate in order to achieve
goal state. We will compare each intermediate state
with the goal state.
State-Space Search
❑ A: Actions:
A →All possible actions: Up, 2 3 4 1 2 3
down, left, right. 5 1 8 4
Legal moves: Only empty space
8 7 6
should move 7 6 5
Illegal move: Other tiles should Start state Cost: 1
not move. Goal state

2 3 4 2 3 4 2 4 2 3 4
5 1 5 1 5 3 1 5 7 1
8 7 6 8 7 6 8 7 6 8 6
Resultant states
❑ Result(s, a) → resultant states
❑ Cost(s, a) → Cost for moving each tile or for
reaching next state. In above example it is 1
unit(time, distance etc).
State-Space Search
❑ The state-space search can be done in two ways:
• Uninformed or Blind search, which is Exponentially
computationally expensive 𝑂(𝑏 𝑑 ) . Here, b –
branching factor, d- depth
• Informed search uses local benefit or local
information.
Uninformed Vs Informed Search
Uniformed or brute-force Informed search
❑ . search
Search without Search with information
information
No knowledge Use Knowledge to find
steps to solution.
Time consuming Quick solution

More Complexity(Time, Less complexity(Time,


space) space)
DFS, BFS etc. are A*, Heuristic Best First
examples Search are examples.
Uninformed vs Informed
❑ In brute-force, we only have start and goal state
information and explore all the possibilities without
any knowledge or domain information or guide.
G
S

❑ In Informed search, we use information or heuristic


❑ Example: Travelling sales man problem.
Uninformed vs Informed
❑ Travelling sales man problem (TSP).
▪ Start from one city and cover all other cities with
minimum distance.
Brute force Informed
▪ (n-1) ! Possibilities to solve. • Heuristic search. Nearest
Ex: (5-1) ! = 24 searching neighbor distance used
spaces to get optimal sol. as heuristic.
▪ Exponential Time • Polynomial time problem.
complexity. If n=100, 1000 • In TSP, we can take local
Tc increases. guide information to
▪ It is also called Non- reach destination.
polynomial (NP) problem • Optimal solution may or
▪ No heuristic or guide or may not reach.
information.
Uninformed Search
Breadth First Search (BFS)
❑ It is a uninformed or brute-force or blind search in
which we don’t have any domain knowledge.
❑ It is a First In First Out (FIFO) method which uses
Queue Data structure.

remove Queue Insert


❑ Shallowest Node Head Tail
❑ Complete
❑ Optimal
❑ Complexity
BFS:
 The root node is expanded first, then all the nodes
generated by the root node are expanded next,
and then their successors, and so on.
 In general, all the nodes at depth d in the search
tree are expanded before the nodes at depth d + 1.
 If there is a solution, breadth-first search is
guaranteed to find it, and if there are several
solutions, breadth-first search will always find the
shallowest goal state first.
BFS algorithm from the root node S to goal
node K

S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
Breadth-first search Algorithm:

➢ Insert root node onto a Queue

➢ While (queue is not empty):

a) remove a node

b) if it is a goal node

Stop Algorithm

c) if it is not a goal node

Insert all the children nodes into the Queue

➢ Return Failure
Breadth First Search (BFS)
A Remove
Insert A into
A queue.

Level 0 Insert BCD


Remove B
A BCD
CDEF Remove
Insert EFC
Level 1 B C D
DEFGH Remove
Insert GH
D

EFGHI Insert I E
Remove

Level 2 E F G H I
FGHIJK Remove
Insert JKF

GHIJK Remove G
Level 3 J K L M Remove
Insert L H
HIJKL
IJKL Remove I
Level 4 N
JKLM Remove
Insert M J
Tree
KLM Remove K

• Shallowest Node Remove


Insert N L
• Complete LMN
• Optimal MN Remove M
• Time Complexity
N Remove N
❑ BFS is implemented using FIFO queue data structure.
❑ Advantages:
• BFS will provide a solution if any solution exists.
• If there are more than one solutions for a given problem, then BFS
will provide the minimal solution which requires the least number
of steps.
❑ Disadvantages:
• It requires lots of memory since each level of the tree must be
saved into memory to expand the next level.
• BFS needs lots of time if the solution is far away from the root node.
Depth First Search (DFS)
❑ It is also an uninformed or brute-force or blind
search in which we don’t have any domain
knowledge.
❑ It is a Last In First Out (LIFO) method which uses Stack
Data structure.

❑ Deepest Node
❑ In Complete
❑ Non Optimal
Push = Insert
❑ Time Complexity Pop = Remove
Depth-first search

 Depth-first search always expands one of the nodes at the


deepest level of the tree.
 Only when the search hits a dead end does the search go
back and expand nodes at shallower levels.
 Stack function is used to represent DFS.
 Depth-first search has very modest memory requirements.
Traversing technique
(Root node--->Left node ----> right
node)
Depth-first search on a binary tree
Depth-first search Algorithm:

➢ Push root node onto a stack

➢ While (stack is not empty):

a) Pop a node

b) if it is a goal node

Stop Algorithm

c) if it is not a goal node

Push all the children nodes into the stack

➢ Return Failure
Depth First Search (DFS)
Depth Start Node Stack
G
0 A Path c c F
A A
B B B
Empty Push Pop Push Pop Push
1 B C

G
Goal F E E
2 D E F G F B
Node B D D
B
Pop Pop Pop Push Pop


A (or)
Loops

c B If G is Goal Node
B C
Shortest Path A → C → G (ACG)

→ Two ways, But mechanism same


→ Follow same direction (left to right)
▪ Deepest Node - Depth wise search
▪ Incomplete : DFS might stuck in infinite path or infinite loop. So, we
might not reach goal state some times.
▪ Non-optimal : DFS may or may not provide optimal solution.
▪ There could be another solution with low cost.
▪ Time complexity :
→ O (bd) for DFS Algorithms
b- branching factor
d- depth
E.g.: G is goal state
b = 2 (max), d = 2;  bd = 22 = 4
❑ Advantage:
 DFS requires very less memory as it only needs to store a stack of the
nodes on the path from root node to the current node.
 It takes less time to reach to the goal node than BFS algorithm (if it
traverses in the right path).
❑ Disadvantage:
 There is the possibility that many states keep re-occurring, and there
is no guarantee of finding the solution.
 DFS algorithm goes for deep down searching and sometime it may
go to the infinite loop.
Depth-Limited Search Algorithm:

❑ A depth-limited search algorithm is similar to depth-first search


with a predetermined limit.
❑ Depth-limited search can solve the drawback of the infinite
path in the Depth-first search.
❑ In this algorithm, the node at the depth limit will treat as it has
no successor nodes further.
Example:
Depth-Limited Search Algorithm:

❑ Advantages:
▪ Depth-limited search is Memory efficient.
❑ Disadvantages:
▪ Depth-limited search also has a disadvantage of
incompleteness.
▪ It may not be optimal if the problem has more than one
solution.
Bidirectional Search Algorithm:

❑ Bidirectional search algorithm runs two simultaneous searches,


one form initial state called as forward-search and other from
goal node called as backward-search, to find the goal node.
❑ Bidirectional search replaces one single search graph with two
small subgraphs in which one starts the search from an initial
vertex and other starts from goal vertex.
❑ The search stops when these two graphs intersect each other.
❑ Bidirectional search can use search techniques such as BFS,
DFS, DLS, etc.
Example:
❑ This algorithm divides one graph/tree into two sub-graphs.
❑ It starts traversing from node 1 in the forward direction and
starts from goal node 16 in the backward direction.
Bidirectional Search
❑ Completeness: Bidirectional Search is complete if we use BFS
in both searches.
❑ Time Complexity: Time complexity of bidirectional search
using BFS is O(bd).
❑ Space Complexity: Space complexity of bidirectional search
is O(bd).
❑ Optimal: Bidirectional search is Optimal.
Bidirectional Search
❑ Advantages:
▪ Bidirectional search is fast.
▪ Bidirectional search requires less memory

❑ Disadvantages:
▪ Implementation of the bidirectional search tree is difficult.
▪ In bidirectional search, one should know the goal state in
advance.
Iterative deepening search

❑ The iterative deepening algorithm is a combination of DFS and


BFS algorithms.
❑ This search algorithm finds out the best depth limit and does it by
gradually increasing the limit until a goal is found.
❑ This algorithm performs depth-first search up to a certain "depth
limit", and it keeps increasing the depth limit after each iteration
until the goal node is found.
❑ This Search algorithm combines the benefits of Breadth-first
search's fast search and depth-first search's memory efficiency.
❑ The iterative search algorithm is useful uninformed search when
search space is large, and depth of goal node is unknown.
Example:

▪ 1'st Iteration-----> A
▪ 2'nd Iteration----> A, B, C
▪ 3'rd Iteration------>A, B, D,
E, C, F, G
▪ 4'th Iteration------>A, B, D,
H, I, E, C, F, K, G
▪ In the 3rd iteration, the
algorithm will find the goal
node.
Example 2:

Four
iterations
of iterative
deepenin
g search
on a
binary
tree.
Graph with infinite nodes
 Advantages:
 It combines the benefits of BFS (when result is at
shallower node) and DFS search algorithm (stack
data structure is used) in terms of fast search and
memory efficiency respectively.
 Disadvantages:
 The main drawback of IDDFS is that it repeats all the
work of the previous phase.
8-Puzzle problem without Heuristic
❑ Uninformed search
❑ Breadth First Search (BFS)
❑ O(𝑏𝑑)
❑ Four Moves( Empty space or tile can only move Left, Right, Top or Bottom)

❑ Problem: Solve the following 8-Puzzle using BFS without Heuristic. Mobile or
board games, you might have played where you need to reach S to G
state.

1 2 3 1 2 3

4 6 4 5 6

7 5 8 7 8

S G
8 – Puzzle Problem Without Heuristic
U
S
1 2 3
1 2 3
L R
4 5 6
4 6
7 8
D 7 5 8
4 moves G
R U D

1 2 3 2 3 1 2 3
4 6 1 4 6 7 4 6
7 5 8 7 5 8 5 8
L R U D R D U R
S
1 2 3 1 2 3 1 3
1 2 3 2 3 1 2 3 1 2 3 1 2 3
4 6 4 6 4 2 6 4 5 6 1 4 6 4 6 4 6 7 4 6
7 5 8 7 5 8 7 5 8 7 8 7 5 8 7 5 8 7 5 8 5 8
R D L D L D L R
U U R
U
1 2 3
4 5 6
7 8

G
Heuristic in AI
❑ Heuristic is a kind of support or help which is used to find the goal state
with less computational complexity (Time and distance etc.)
❑ Heuristic in AI is also called as rule of Thumb Which gives hints to reach
destination.
❑ It is a technique designed to solve problems quickly.
❑ Example: Directions on a road or distance board of the destination on
road are examples of heuristics.
Heuristic in AI
❑ In uninformed search, we are doing blind search where as informed
search we use heuristic for finding the goal state.
❑ Uninformed search time complexity is exponential i.e. O (bd)
❑ Example:
▪ 8-Puzzle: time complexity is 3 20
▪ 15-Puzzle: time complexity is 10 15
▪ 24-Puzzle: time complexity is 10 24
▪ Chess: : time complexity is 35 80
▪ These Uninformed search problems are also called as NP(Non-
Polynomial) Problems. These problems will give Optimal solution at
the cost of higher computational complexity.
❑ In informed search, using heuristic, we try to reach the goal state
quickly without exploring all the possible states. S
10 15
• Provide good solution but not the optimal.
• Polynomial Problem.

G
How to calculate Heuristic?
❑ Euclidian distance: It is also called as straight line distance between
two nodes.
S G
(x1, y1)

❑ Man-hattan distance: (x2, y2)

1 3 2 1 2 3
▪ Example: 8-Puzzle problem
6 5 4 4 5 6
Cost = 0 + 1 + 1+ 2 + 0 + 2 +7+ 0
8 7 7 8

S G
❑ Number of Misplaced tiles:
▪ Cost is the Number of misplaced tiles of S compared to G. Cost = 5.
Informed Search
8 – Puzzle Problem With Heuristic
S G
U 1 2 3
1 2 3
Heuristic : No. of 4 5 6 1+1+1
L R 4 6 misplaced tiles =3
7 8
7 5 8
D h=3
moves R U D

1 2 3 2 3 1 2 3
4 6 h=2 1 4 6 h=4 7 4 6 h=4
7 5 8 7 5 8 5 8

S L R U D

h=3 h=3 h=3 h=1


1 2 3 1 2 3 1 3 1 2 3
4 6 4 6 4 2 6 4 5 6
7 5 8 7 5 8 7 5 8 7 8

L R U
→ After achieving goal
1 2 3 1 2 3 state no need to
4 5 6 4 5 6 Explore other states,
7 8 7 8
we can stop
G
Best First Search (BFS)
❑ It is a informed search in which we will be having heuristic to
find the goal state quickly.
❑ Algorithm:
▪ Let OPEN be a priority Queue containing Initial state.
▪ Loop
▪ If OPEN is empty return failure
▪ Otherwise Node  Remove – First(OPEN)
▪ If node is a Goal
Then return the path from initial to Node
▪ else generate all successors of Node and put the
generated Nodes into OPEN according to their
f(heuristic) values.
▪ End Loop
▪ It’s a complete algorithm
Best First Search (BFS)
Start Closed Queue Open (Priority) queue
A 7 Empty Empty Queue
11 A Remove Insert
D
B 14 D 35 Head Tail
32 B
C C 25 A
15 25
10 Insert

8 A A
F
19 E
Insert Remove
E 17
F
20 CBD
9 Insert & sort
G
H 10 0 G AC CBD
Goal
Insert Remove
Graph
FEBD
Heuristic: Insert & Sort

Euclidian or straight line distance ACF FEBD


A → G = 40 E → G = 19 Insert Remove

B → G = 32 F → G = 17 GEBD
C → G = 25 H → G = 10 Insert & Sort

D → G = 35 G→G=0 ACFG GEBD


Insert Goal State found
Best First search (BFS) works on ‘Heuristic’
but not on ‘cost’ Path : A → C → F → G
Beam Search Algorithm
❑ It is an informed search technique.
❑ Take care of space complexity (Constant)
❑ Beam Width (Beta) is given in advance for every
problem.
❑ It is the extension of Best first search method.
❑ Searching will be done based on Minimum heuristic
value.
❑ Its an incomplete algorithm since we are pruning
branches based on beam width Beta value.
❑ If beta = 1, then it is called hill climbing algorithm.
Beam Search technique
Closed Queue Open (Priority) queue
=2
Start Empty Empty Queue
7 A
D 32 35 Remove Inset
A
25 D Head Tail
11 14 18 B
25 A
C
Insert
B C F 35
15 10
19 A A
17
8 20 D Remove
E F
Insert
E CB
G 0 Insert & sort
9 Goal only 2 elements
H 10
G
AC CB
Graph Insert Remove

FEB
Insert & Sort
Straight line distance
ACF FEB
A → G = 40 E → G = 19 Insert Remove
B → G = 32 F → G = 17
C → G = 25 H → G = 10 GEB
Insert & Sort
D → G = 35 G→G=0
ACFG GEB
Insert Goal State ‘G’
found
Path : A → C → F → G
Hill climbing Algorithm
❑ It is a local search algorithm which does not has global
domain knowledge.
❑ It is greedy approach which moves in the direction of best
heuristic value otherwise it will stop.
❑ Back tracking: Like uninformed and informed search
techniques that discussed so far it can not back track If
algorithm does not find best solution in a particular branch.
❑ It is a informed search technique.
❑ Take care of space complexity (Constant), i.e. Beam Width
(Beta) is 1.
❑ It is the extension of Beam search method.
❑ Searching will be done based on Minimum heuristic value.
❑ Its an incomplete algorithm since we are pruning all branches
except keeping one with minimum heuristic value.
Hill climbing Algorithm
❑ Algorithm steps:
1. Evaluate initial state.
2. Loop Until a solution is found or there are no
operators (Nodes) left
-- select and apply a new operator (node)
-- evaluate the new state:
-- if goal then quit
-- If better than current state then it is new current
state.
Hill climbing technique
Closed Queue Open (Priority) queue
Start =1 A Pruning
7
A D Nodes Empty Empty
32 35 Remove Inset
11 14 18 25 D
25 B Head Tail
B C F C A
15 10 40
35 Insert
8 20
19 17 A A
E D A Insert Remove
G E F
9 Goal 0
C
H 1 Insert
Graph 0
G AC C
Insert Remove

Straight line distance: F


Stop Insert

ACF F
A → G = 40 E → G = 19 Insert Remove
B → G = 32 F → G = 17
G
C → G = 25 H → G = 10 Insert
D → G = 35 G→G=0
ACFG G
Insert Remove
Goal state ‘G’ found
Path : A → C → F → G
Hill Climbing Example
Heuristic: Misplaced tiles

1 2 4 1 4 7
(S) 5 7 2 5 8 (G) Problems in Hill Climbing:
3 6 8 3 6
• Local Maximum Global
Max
(6) D In Local Search, we don’t Local
L1 L (4) R (5) U (5)
have complete information Max
1 2 4 1 2 4 1 4 1 2 4
of the domain, Because of
5 7 5 7 5 2 7 5 6 7
that, we may or may not
3 6 8 3 6 8 3 6 8 3 8
reach global optimum

L2 U (5) D (5) R (5)


• Plateu/ Flat Maximum
2 4 1 2 4 1 2 4 ////
1 5 7 3 5 7 5 7
If all the heuristic values in a
state are same it is flat Max.
3 6 8 6 8 3 6 8 Global Max
B
• Ridge Local Max
A
Special case of local
z
maximum.

Eg.: Mobile
A* Algorithm
❑ Informed search technique.
❑ A* is called as admissible which means this algorithm
gives optimal solution for sure.
❑ A* algorithm works on the following equation:
f(n) = g(n) + h(n)
where,
▪ g(n) is the actual cost from start node S to node n.
▪ f(n) is the estimated cost or heuristic cost from node
n to Goal node G.
h(n) g(n)
S n G
A* Algorithm:
Start 12
4 5
S B F
14 f(n) = g(n) + h(n) Time Complexity
16
Tc = O(bd) ( max Value)
3
12
0 g(n) = Actual cost from s → n
G SC = O(bd)
Goal h(n) = estimated cost from n → G
10 5 b = branch factor
11 C E
d = depth
4 s→s:
7
2 f(s) = g(s)+h(s) = 0+14 = 14
D
6
Graph s→B: s→C:
f(B) = g(B)+h(B) = 4+12 = 16 f(c) = g(c) + h(c) = 3 + 11 = 14
Straight line distance:
SC → E : SC → D :
S → G = 14 E→G=4 f(E) = 3+10+4 f(D) = 3+7+6
= 17 = 16
B → G = 12 F → G = 11
C → G = 11 G→G=0 SCD → E :
f(E) = 3+7+2+4
D→G=6
= 16

h(n) SCDE → G :
f(G) = 12+5+0
= 17
Genetic Algorithm
❑ Genetic algorithm is one kind of evolutionary or optimization
algorithms.
❑ It is proposed by John Holland in 1975.
❑ It is the abstraction of real biological evolution. .i.e., which is
related to the study of genes and chromosomes.
❑ Survival of the fittest, by British naturalist Charles Darwin, which
suggested that organisms best adjusted to their environment
are the most successful in surviving and reproducing.
❑ Example: How the next generation (child) will be evolved
(born) by combining the genes of past generation (parents).
❑ To solve complex Problems(Like NP problems)
❑ It mainly focuses on optimization and try to find the best
solution in search space.
❑ It finds the possible solution from the population (Search
space) for a given problem.
❑ From a group of individuals (ex: chromosomes), the best will
survive.
Genetic Algorithm
❑ In this algorithm, phenotype and genotype are two important
technical terms.

Encode

PhenoType GenoType

Decode

❑ PhenoType: Raw Data


❑ GenoType: Representation or encoding of raw data into a
proper format for further computation purpose (Ex: integer,
decimal and real number representations).
❑ If input data is not represented, it leads to non optimized or
incorrect solution. So, data should be represented properly
before the computation.
Genetic Algorithm Initial
Population Solution space
(should be diverse)
Calculate
Ex: Desired word: Make Fitness
Input we have: Take
• Fitness calculates how much input is

Genetic operations
Selection Will select parents
relevant to goal. whose fitness
• Fitness(make, take) =3 (matching) value is maximum.
• Fitness(make, abcd) =1 (matching) Cross Over
• Fitness(make, akme) =4 (matching)

• For Each individual in population we Mutation By using cross


calculate fitness value. over and
mutation we
no
will generate
Stoppin better new
g population
criteria from selected
parents.
yes
Optimal Solution
Genetic Algorithm
❑ Cross over: There are various methods for doing cross over. One
important method is one point cross over.

Parent 1: S A M B A
Parent 2: E M I L Y

New Population or S A M L Y
Parents: E M I B A

❑ In this one-point crossover, a random crossover point is selected and the


tails of its two parents are swapped to get new off-springs.
❑ By this process, we want to maximize fitness value among the newly
generated population.
Genetic Algorithm
❑ Multi point Cross over: Multi point crossover is a generalization of
the one-point crossover wherein alternating segments are swapped
to get new off-springs.

Parent 1:

Parent 2:

❑ Uniform point Cross over : In a uniform crossover, we don’t


divide the chromosome into segments, rather we treat each gene
separately. In this, we essentially flip a coin for each chromosome to
decide whether or not it’ll be included in the off-spring. We can also
bias the coin to one parent, to have more genetic material in the
child from that parent.
Genetic Algorithm
❑ Mutation: There are various methods for doing mutation.
They are swap and bit flip mutation etc.
❑ Bit Flip Mutation: In this bit flip mutation, we select one or more
random bits and flip them. This is used for binary encoded GAs.

❑ Stopping criteria: we will stop when we achieve the


highest fitness value.

You might also like