Module II (part 1)
Module II (part 1)
Introduction to AI and ML
CSA2001
Introduction
❑ Example 1:
Final year project
❑ Example 2:
Planning a destination in google maps.
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
S---> A--->B---->C--->D---->G--->H--->E---->F---->I---->K
Breadth-first search Algorithm:
a) remove a node
b) if it is a goal node
Stop Algorithm
➢ Return Failure
Breadth First Search (BFS)
A Remove
Insert A into
A queue.
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
❑ Deepest Node
❑ In Complete
❑ Non Optimal
Push = Insert
❑ Time Complexity Pop = Remove
Depth-first search
a) Pop a node
b) if it is a goal node
Stop Algorithm
➢ 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)
❑ 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:
❑ Disadvantages:
▪ Implementation of the bidirectional search tree is difficult.
▪ In bidirectional search, one should know the goal state in
advance.
Iterative deepening search
▪ 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)
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
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
B → G = 32 F → G = 17 GEBD
C → G = 25 H → G = 10 Insert & Sort
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
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
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
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)
New Population or S A M L Y
Parents: E M I B A
Parent 1:
Parent 2: