Aifl SPV Unit1
Aifl SPV Unit1
INTRODUCTION TO AI AND
KNOWLEDGE
REPRESENTATION
2
USES OF ARTIFICIAL INTELLIGENCE
3
Transportation: AI is used for autonomous vehicles,
traffic prediction, and route optimization.
Customer service: AI-powered chatbots are used for
customer support, answering frequently asked questions,
and handling simple requests.
8
SOLVING PROBLEMS BY SEARCHING
Search is the systematic examination of states to find path
from the start/root state to the goal state.
The process of problem-solving using searching consists of
the following steps:
- Define the problem
- Analyze the problem
- Identification of possible solutions
- Choosing the optimal solution
- Implementation
9
UNINFORMED SEARCH ALGORITHMS
10
UNINFORMED SEARCH
Breadth First Search (BFS)
- It generally starts from the root node and examines the
neighbor nodes and then moves to the next level.
- It uses First-in First-out (FIFO) strategy as it gives the
shortest path to achieving the solution.
- BFS is used where the given problem is very small.
11
- let’s take node A as the start state and node F as the goal
state.
- The BFS algorithm starts with the start state and then goes
to the next level and visits the node until it reaches the goal
state.
- In this example, it starts from A and then travel to the next
level and visits B and C and then travel to the next level and
visits D, E, F and G.
- Here, the goal state is defined as F. So, the traversal will
stop at F.
- The path of traversal is:
A —-> B —-> C —-> D —-> E —-> F
12
Advantages of BFS
- BFS will never be trapped in any unwanted nodes.
- If the graph has more than one solution, then BFS will
return the optimal solution which provides the shortest path.
Disadvantages of BFS
- BFS stores all the nodes in the current level and then go to
the next level. It requires a lot of memory to store the nodes.
- BFS takes more time to reach the goal state which is far
away.
13
EXAMPLE
14
15
16
EXAMPLE (ROUTING PROBLEM)
1 10
S G
B
5 5
15 5
17
SOLUTION OF THE ROUTING PROBLEM
USING BREADTH FIRST SEARCH
S S
Sol= {S,A,G} &
Cost = 11
A B C A B C
1 5 15 1 5 15
G
11
18
New solution found
better than the current S
G G
11 10 New solution found S
but not better than
what we have
Sol= {S,B,G} &
A B C
Cost = 10 1 5 15
G G G
11 10 20
19
DEPTH FIRST SEARCH (DFS)
- The depth-first search uses Last-in, First-out (LIFO)
strategy.
- DFS uses backtracking. That is, it starts from the initial
state and explores each path to its greatest depth before it
moves to the next path.
- DFS will follow
Root node —-> Left node —-> Right node
20
- It starts from the start state A and then travels to B and
then it goes to D.
- After reaching D, it backtracks to B. B is already visited,
hence it goes to the next depth E and then backtracks to B. as
it is already visited, it goes back to A. A is already visited.
- So, it goes to C and then to F. F is our goal state and it
stops there.
- The path of traversal is:
A —-> B —-> D —-> E —-> C —-> F
21
Advantages of DFS
- It takes lesser memory as compared to BFS.
- The time complexity is lesser when compared to BFS.
- DFS does not require much more search.
Disadvantages of DFS
- DFS does not always guarantee to give a solution.
- As DFS goes deep down, it may get trapped in an infinite
loop.
22
EXAMPLE
23
24
25
26
27
28
29
ITERATIVE DEEPENING SEARCH
- Iterative deepening depth-first search is a combination of
depth-first search and breadth-first search.
- IDDFS find the best depth limit by gradually adding the
limit until the defined goal state is reached.
30
- Consider, A as the start node and E as the goal node. Let
the maximum depth be 2.
- The algorithm starts with A and goes to the next level and
searches for E.
- If not found, it goes to the next level and finds E.
- The path of traversal is
A —-> B —-> E
31
Advantages of IDDFS
- IDDFS has the advantages of both BFS and DFS.
- It offers fast search and uses memory efficiently.
Disadvantages of IDDFS
- It does all the works of the previous stage again and again.
32
INFORMED SEARCH
- The informed search algorithm is also called heuristic
search or directed search. In contrast to uninformed search
algorithms, informed search algorithms require details such
as distance to reach the goal, steps to reach the goal, cost of
the paths which makes this algorithm more efficient.
- Here, the goal state can be achieved by using the heuristic
function.
- The heuristic function is used to achieve the goal state with
the lowest cost possible. This function estimates how close a
state is to the goal.
33
HEURISTIC FUNCTION FORMULA
34
GREEDY BEST-FIRST SEARCH
- Greedy best-first search uses the properties of both depth-
first search and breadth-first search.
- Greedy best-first search traverses the node by selecting the
path which appears best at the moment.
- The closest path is selected by using the heuristic function.
35
- A is the start node and H is the goal node.
- Greedy best-first search first starts with A and then
examines the next neighbour B and C. Here, the heuristics of
B is 12 and C is 4.
- The best path at the moment is C and hence it goes to C.
- From C, it explores the neighbours F and G. the heuristics
of F is 8 and G is 2.
- Hence it goes to G. From G, it goes to H whose heuristic is 0
which is also our goal state.
- The path of traversal is
A —-> C —-> G —-> H
36
Advantages of Greedy best-first search
- Greedy best-first search is more efficient compared with
breadth-first search and depth-first search.
37
EXAMPLE
38
39
40
41
A* SEARCH
- A* search algorithm is a combination of both uniform cost
search and greedy best-first search algorithms.
- It uses the advantages of both with better memory usage.
- It uses a heuristic function to find the shortest path.
- A* search algorithm uses the sum of both the cost and heuristic
of the node to find the best path.
42
- Let A be the start node and H be the goal node.
- First, the algorithm will start with A. From A, it can go to
B, C, H.
- Note the point that A* search uses the sum of path cost and
heuristics value to determine the path.
- Here, from A to B, the sum of cost and heuristics is 1 + 3 =
4.
From A to C, it is 2 + 4 = 6.
From A to H, it is 7 + 0 = 7.
- Here, the lowest cost is 4 and the path A to B is chosen. The
other paths will be on hold.
- Now, from B, it can go to D or E.
43
- From A to B to D, the cost is 1 + 4 + 2 = 7.
- From A to B to E, it is 1 + 6 + 6 = 13.
- The lowest cost is 7. Path A to B to D is chosen and
compared with other paths which are on hold.
- Here, path A to C is of less cost. That is 6.
- Hence, A to C is chosen and other paths are kept on hold.
- From C, it can now go to F or G.
- From A to C to F, the cost is 2 + 3 + 3 = 8.
- From A to C to G, the cost is 2 + 2 + 1 = 5.
- The lowest cost is 5 which is also lesser than other paths
which are on hold. Hence, path A to G is chosen.
- From G, it can go to H whose cost is 2 + 2 + 2 + 0 = 6.
- Here, 6 is lesser than other paths cost which is on hold.
- Also, H is our goal state. The algorithm will terminate here. 44
- The path of traversal is
A —-> C —-> G —-> H
Advantages of A* search algorithm
- This algorithm is best when compared with other
algorithms.
- This algorithm can be used to solve very complex problems
also it is an optimal one.
45
EXAMPLE
46
47
48
49
50
51
ITERATIVE DEEPENING A* (IDA*)
52
- We want to find the optimal path from node A to node F using
the IDA* algorithm.
- The first step is to set an initial cost limit. Let's use the
heuristic estimate of the optimal path, which is 7 (the sum of the
costs from A to C to F).
- Set the cost limit to 7.
- Start the search at node A.
- Expand node A and generate its neighbors, B and C.
- Evaluate the heuristic cost of the paths from A to B and A to C,
which are 5 and 6 respectively.
- Since the cost of the path to B is less than the cost limit,
continue the search from node B.
- Expand node B and generate its neighbors, D and E. 53
- Evaluate the heuristic cost of the paths from A to D and A to E,
which are 10 and 9 respectively.
- Since the cost of the path to D exceeds the cost limit, backtrack
to node B.
- Evaluate the heuristic cost of the path from A to C, which is 6.
- Since the cost of the path to C is less than the cost limit,
continue the search from node C.
- Expand node C and generate its neighbor, F.
- Evaluate the heuristic cost of the path from A to F, which is 7.
- Since the cost of the path to F is less than the cost limit, return
the optimal path, which is A - C - F.
54
HEURISTIC FUNCTION
- A heuristic function estimates the approximate cost of
solving a task.
- Determining the shortest driving distance to a particular
location can be one example.
- Informed Search uses a heuristic function to identify the
most feasible path.
- It estimates how far away the agent is from the goal using
the agent's current condition as input.
Types of heuristic functions in AI are:
57
Step 1: Start with an initial state.
Step 2: Check if the initial state is the goal. If so, return
success and exit.
Step 3: Enter a loop to search for a better state
continuously.
Select a neighboring state within the loop by applying an
operator to the current state.
Evaluate this new state:
If it’s the goal state, return success and exit.
If it’s better than the current state, update the current
state to this new state.
If it’s not better, discard it and continue the loop.
Step 4: End the process if no better state is found and the 58
59
1 A
MAX
1 B -3 C
MIN
4 D 1 E 2 F -3 G
MAX
H I J K L M N O
4 -5 -5 1 -7 2 -3 -8
60
= agent = opponent
ALPHA – BETA PRUNING
Alpha-Beta is a procedure which can prune large parts of
the search tree and allow search to go deeper.
61
CONSTRAINT SATISFACTION PROBLEM
- Variables WA, NT, Q, NSW, V, SA, T
- Domains Di = {red,green,blue}
62
CRYPT ARITHMETIC PROBLEMS
Rules:
No two letters have same value.
Sum of digits must be as shown in the problem.
There should be only one carry forward.
Digits to be assigned is from 0-9.
4) S E N D 5) E A T
M O R E T H A T
M O N E Y A P P L E
6) S O M E 7) L O G I C
T I M E L O G I C 64
S P E N T P R O L O G
PROPOSITIONAL LOGIC IN ARTIFICIAL
INTELLIGENCE
a) It is Sunday.
b) The Sun rises from West (False proposition)
c) 3+3= 7(False proposition)
d) 5 is a prime number.
65
SOME BASIC FACTS ABOUT
PROPOSITIONAL LOGIC
- Propositional logic is also called Boolean logic as it works on
0 and 1.
- In propositional logic, we use symbolic variables to
represent the logic, and we can use any symbol for a
representing a proposition, such A, B, C, P, Q, R, etc.
- Propositions can be either true or false, but it cannot be
both.
- Propositional logic consists of an object, relations or
function, and logical connectives.
- These connectives are also called logical operators.
- Connectives can be said as a logical operator which
connects two sentences.
66
- A proposition formula which is always true is
called tautology, and it is also called a valid sentence.
- A proposition formula which is always false is
called Contradiction.
- Statements which are questions, commands, or opinions are
not propositions such as "Where is Rohini", "How are you",
"What is your name", are not propositions.
67
SYNTAX OF PROPOSITIONAL LOGIC:
Compound propositions
Atomic Proposition:
- Atomic propositions are the simple propositions.
- These are the sentences which must be either true or false.
Eg:
a) 2+2 is 4, it is an atomic proposition as it is a true fact.
b) "The Sun is cold" is also a proposition as it is a false fact. 68
Compound proposition:
- Compound propositions are constructed by combining
simpler or atomic propositions, using parenthesis and logical
connectives.
Eg:
69
LOGICAL CONNECTIVES
71
PROPOSITIONAL LOGIC CONNECTIVES
72
73
Limitations of Propositional logic:
•We cannot represent relations like ALL, some, or none with
propositional logic. Example:
• All the girls are intelligent.
• Some apples are sweet.
•Propositional logic has limited expressive power. 74
Inference:
- In artificial intelligence, we need intelligent computers
which can create new logic from old logic or by evidence, so
generating the conclusions from evidence and facts is termed
as Inference.
- Inference rules are applied to derive proofs in artificial
intelligence, and the proof is a sequence of the conclusion that
leads to the desired goal.
75
INFERENCE RULES
1) Modus Ponens:
- It states that if we have a conditional statement ("If P, then
Q") and we know that P is true, then we can conclude that Q
must also be true.
- It can be represented as:
If P, then Q.
P.
Therefore, Q.
78
.
Statement -1 : "If I am sleepy then I go to bed" ==> P→ Q
Statement - 2 : "I am sleepy" ==> P
Conclusion: "I go to bed." ==> Q.
Q = "I go to bed"
(Modus Ponens
F (False) F (False) T (True)
does not apply)
F (False) T (True) T (True) (Modus Ponens
does not apply)
T (True) F (False) F (False) Invalid case
T (True) T (True) T (True) T (True)
80
2) Modus Tollens:
- It states that If P → Q (If P, then Q) is true and Q is false
(¬Q), Then P must also be false (¬P).
- It can be represented as:
If P, then Q.
Not Q.
Therefore, not P.
Q = "I go to bed"
Variables a, b, c, x, y, z...
Equality ==
Quantifier ∀, ∃
87
Connectives ∧, ∨, ¬, ⇒, ⇔
ATOMIC AND COMPLEX SENTENCES IN
FOL
Atomic sentences:
- Atomic sentences are the most basic sentences of first-order
logic.
- These sentences are formed from a predicate symbol
followed by a parenthesis with a sequence of terms.
- We can represent atomic sentences as Predicate (term1,
term2, ......, term n).
Example:
88
Complex Sentences:
- Complex sentences are made by combining atomic
sentences using connectives.
- First-order logic statements can be divided into two parts:
- Subject: Subject is the main part of the statement.
- Predicate: A predicate can be defined as a relation, which
binds two atoms together in a statement.
Example:
91
Existential Quantifier:
- Existential quantifiers are the type of quantifiers, which
express that the statement within its scope is true for at least
one instance of something.
- It is denoted by the logical operator ∃, which resembles as
inverted E. When it is used with a predicate variable then it
is called as an existential quantifier.
- If x is a variable, then existential quantifier will be ∃x or
∃(x). And it will be read as:
- There exists a 'x.'
- For some 'x.'
- For at least one 'x.'
Example: 92
93
∀x : bird(x) →fly(x).
∃x : boys(x) ∧ play(x, cricket).
∀x : man(x) → respects (x, parent).
¬∀ (x) : [student(x) → like(x, Mathematics) ∧ like(x,
Science)]
94
UNIFICATION
Unification in AI is a process used in logic and reasoning to
make different logical expressions identical by finding a
common substitution for variables.
It is widely used in automated reasoning, natural language
processing, and logic.
Unification depends on the substitution process.
Parent(x, John)
Parent(Mary, y)
Here, x and y are variables. Unification finds a substitution
that makes both statements identical.
To unify them: 95
96
Eg: Unify: p(x, F(y)) and p(a, F(g(z)))
Step 1
p(x, F(y))
p(a, F(g(z)))
Step 1: Match Predicate Names
x = a
y = g(z)
Therefore
p(a, F(g(z)) p(a, F(g(z))
Unified Successfully
99
CONDITIONS FOR UNIFICATION
Compare predicates and ensure they match.
Check arguments:
- Successfully unified
100
Eg: Q(a, g(x,a),f(y)) Q(a, g(f(b), a), x)
- sub. x with f(b) [f(b) |x]
- Q(a, g(f(b), a), f(y)) Q(a, g(f(b), a), f(b))
- sub y with b [b|y]
- Q(a, g(f(b), a), f(b)) Q(a, g(f(b), a), f(b))
- unified successfully
101
Eg:
P(x,b) and P(a,b)
Q(F(x),y) and Q(F(a),b)
R(x,y,F(y)) and R(a,b,F(b))
S(a,x) and S(b,y)
T(x) and T(F(x))
102