Unit 2 Problem Solving
Unit 2 Problem Solving
Unit 2 4 hrs.
Problem Solving
“It is complete set of states including start and goal states, where the answer of the problem is
to be searched”.
Problem:
“It is the question which is to be solved. For solving the problem, it needs to be precisely
defined. The definition means, defining the start state, goal state, other valid states and
transitions”.
A state space representation allows for the formal definition of a problem which makes the
movement from initial state to the goal state quite easily. So, we can say that various problems
like planning, learning, theorem proving etc. are all essentially search problems only.
Problems are often modelled as a state space, a set of states that a problem can be in. The set
of states forms a graph where two states are connected if there is an operation that can be
performed to transform the first state into the second.
1|Page
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
State space search often differs from traditional computer science search methods because the
state space is implicit: the typical state space graph is much too large to generate and store
in memory. Instead, nodes are generated as they are explored, and typically discarded
thereafter. A solution to a combinatorial search instance may consist of the goal state itself, or
of a path from some initial state to the goal state.
Problem Formulation:
It is all about deciding what action and states to be consider.
Agent sensor give it enough information to tell exactly which states it is in currently.
It knows exactly what each of its action does.
Then it can calculate exactly which state it will be in after any sequence of action.
1. Initial State
2. Actions
3. Transition Model
4. Goal Test
5. Path Cost
The eight-tile or 8-puzzle consist of a 3 by 3 (3*3) square frame board which holds 8 movable
tiles numbered 1 to 8. One square is empty, allowing the adjacent tiles to be shifted. The
objective of the puzzle is to find a sequence of tile movements that leads from a starting
configuration to a goal configuration.
The states of 8 tile puzzle are the different permutations of the tiles within frame.
2|Page
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
States: It specifies the location of each of the 8 tiles and the blank in one of the nice squares.
Goal : Many goal configurations are possible one such is shown in the figure
Legal moves ( or state) : They generate legal states that result from trying the four actions-
Path cost: Each step costs 1, so the path cost is the number of steps in the path.
1 2 3 1 2 3
4 6 4 5 6
7 5 8 7 8
Initial State Goal State
1 2 3
4 6
7 5 8
2 3 1 2 3 1 3
1 4 6 4 6 4 2 6
7 5 8 7 5 8 7 5 8
1 2 3 1 2 3 1 3
4 5 6 4 6 4 2 6
7 8 7 5 8 7 5 8
1 2 3 1 2 3
4 5 6 4 5 6
7 8 7 8
Here, we Choose only those states which has minimum path cost.
3|Page
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
Problem Types
Q
Q
Q Note: For more detail N-Queen Problem is
Q discussed in CSP problem.
Q
Q
Q
Q
4|Page
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
Bank 1 Bank 2
Bank 1 Bank 2
There are 3 missionaries, 3 cannibals, and 1 boat that can carry up to two people on one side
of a river.
Goal: Move all the missionaries and cannibals across the river.
Constraint: Missionaries can never be outnumbered by cannibals on either side of river, or
else the missionaries are killed.
State: Configuration of missionaries and cannibals and boat on each side of river.
Operators: Move boat containing some set of occupants across the river (in either direction)
to the other side.
5|Page
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
5 M, M, C, C M, C Bank 2
2 M, M, C, C, M C Bank 1
7 M, M, M C, C, C Bank 2
10 M, M, M, C C, C Bank 1
3 M, C C, C, M, M Bank 2
6 M, C, C, M C, M Bank 1
3 C, C C, M, M, M Bank 2
10 C, C, C M, M, M Bank 1
7 C M, M, M, C, C Bank 2
10 C, C M, M, M, C Bank 1
7 0 M, M, M, C, C, C Bank 2
Goal State 0 M, M, M, C, C, C Bank 2
6|Page
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
7 M, M, M, C C, C Bank 2
9 M, M, M, C, C C Bank 1
7 M, M, M C, C, C Bank 2
10 M, M, M, C C, C Bank 1
3 M, C C, C, M, M Bank 2
6 M, C, C, M C, M Bank 1
3 C, C C, M, M, M Bank 2
10 C, C, C M, M, M Bank 1
7 C M, M, M, C, C Bank 2
2 C, M M, M, C, C Bank 1
5 0 M, M, M, C, C, C Bank 2
Goal State 0 M, M, M, C, C, C Bank 2
Try to solve this problem same like Missionaries and Cannibal Problem.
Read the problem carefully to solve gently.
A farmer with wolf, goat & cabbage come to the edge of river. They want to cross the river.
There is a boat at river edge but only the farmer can row, the boat can carry two things at a
time. If the wolf is ever left alone with the goat, the wolf will eat the goat, similarly if the goat
is left alone with cabbage, the goat will eat cabbage. Schedule the things so that all four
characters arrive safely on the other side of the river.
Man → M Wolf → W Goat → G Cabbage → C
7|Page
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
Initial State
(3,3,1)
F.I
C MC
CC
(3,2,0) F.II (3,1,0) (2,2,0)
C
M F.I
CC
(3,0,0) (3,2,1)
C
(3,1,1)
MM
(1,1,0) (2,2,1) (0,2,0) (0,3,1)
MC M C
CC
(0,1,0)
F.I C
M F.II
(0,2,1) (1,1,1)
MC
F.I CC
(0,0,0)
Goal State
Here,
Flow of:
F.I = Formulation I
F.II = Formulation II
8|Page
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
Solution:
State Representation and Initial State = we will represent a state of the problem as a tuple
(x, y) where x represents the amount of water in the 4-gallon jug and y represents the amount
of water in the 3-Liter jug. Note 0 ≤ ≤ 4 and 0 ≤ ≤ 3.
Our initial state: (0,0)
Goal Predicate: state = 2, where 0 ≤ ≤ 3.
Operators: we must define a set of operators that will take us from one state to another:
#Rule Meaning Check Condition Calculation
9|Page
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
Formulation I.
Liters in Liters in Rule Meaning
4-Liter jug 3-Liter jug Applied
0 0 - Initial State
Formulation II.
Liters in Liters in Rule Meaning
4-Liter jug 3-Liter jug Applied
0 0 - Initial State
10 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
Initial State
(0,0)
(4,0) (0,3)
(2,0)
Goal State
11 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
The solution of many problems (e.g. Tic Tac Toe (noughts 0 and crosses x), timetabling, chess)
can be described by finding a sequence of actions that lead to a desirable goal. Each action
changes the state and the aim is to find the sequence of actions and states that lead from the
initial (start) state to a final (goal) state.
Initial state
Operator or successor function - for any state x returns s(x), the set of
states reachable from x with one action
State space - all states reachable from initial by any sequence of actions
Path cost - function that assigns a cost to a path. Cost of a path is the sum of
costs of individual actions along the path
Extra:
Ill-defined Problem: - Not Clear, indefinite and ill-formed or not well-formed problem.
A problem that lacks one or more of these specified properties is an ill-defined problem, and
most problems that are encountered in everyday life fall into this category.
Well-defined vs. ill-defined problems: Problems where the goal or solution is
recognizable--where there is a right answer (ex. a math or physics problem) vs.
problems where there is no "right" answer but a range of more or less acceptable
answers.
12 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
What is CSP ?
Solution to a CSP is an assignment to each variable such that each constraint is satisfied.
8-Puzzle Problem
N-Queen Problem (4x4, 8x8)
Graph Coloring/ Map Coloring/ 3-Color Problem
Crypt-Arithmetic Problem
Crossword
Sudoku (9x9)
N Queen Problem:
N-Queens problem is a well-known Constraint Satisfactory Problem of Artificial Intelligence.
In this problem, we have an NxN square grid board and we have N queens which need to be
placed on them. The queens should be placed on the board in such a way so that it satisfies the
below-mentioned constraints:
On summing up all the constraints, we can conclude that each row and each column should
contain exactly one queen in them, neither more nor less than that.
In this series of problems, mostly there are grids whose size is even in number, like 4, 6, 8 and
so on. It should be noted that the minimum number of the grid that we can have in this problem
is 4, not less than that.
Here the 4-Queen problem and the 8-Queen problem are the most popular in the N-Queen
problem series.
13 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
There can exist many solutions for solving this problem, which mean that the solution to these
problems is not unique. Yet, one of those solutions to both these types are given below:
4-Queens problem
In the 4-Queens problem, we have a 4x4 grid and we have 4 queens to place on it. The layout
for the 4-Queens problem while satisfying all the constraints is as follows:
Q Q
Q Q
Q Q
Q Q
8-Queens problem
In the 8-Queens problem, we have an 8x8 grid and we have 8-queens to place on it. The
layout for the 8-Queens problem while satisfying all the constraints is as follows:
Q Q
Q Q
Q Q
Q Q
Q Q
Q Q
Q Q
Q Q
Original Mirroring(L=>R or R=>L)
14 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
15 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
A classic CSP is the problem of coloring a map so that no adjacent regions have the same color.
V2 V3
B G
V1
R R
V4
V6 G B V5
R
B
R
G
B
R
G
16 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
Q1. Can the map of Sudur Paschim Pradesh (Province 7) be colored with 3 Color?
If possible, try it out.
17 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
Crypt-Arithmetic Problem:
Here, to convert it into numeric form, we first split each word separately and represent it as
follows:
S E N D
M O R E
--------------------------------
M O N E Y
These alphabets then are replaced by numbers such that all the constraints are satisfied. So
initially we have all blank spaces.
We first look for the MSB in the last word which is 'M' in the word 'MONEY' here. It is the
letter which is generated by carrying. So, carry generated can be only one. SO, we have M=1.
Now, we have S+M=O in the second column from the left side. Here M=1. Therefore, we
have, S+1=O. So, we need a number for S such that it generates a carry when added with 1.
And such a number is 9. Therefore, we have S=9 and O=0.
Now, in the next column from the same side we have E+O=N. Here we have O=0. Which
means E+0=N which is not possible. This means a carry was generated by the lower place
digits. So, we have:
1+E=N ----------(i)
So, for satisfying both equations (i) and (ii), we get E=5 and N=6.
Now, R should be 9, but 9 is already assigned to S, So, R=8 and we have 1 as a carry which is
generated from the lower place digits.
18 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
Now, we have D+5=Y and this should generate a carry. Therefore, D should be greater than 4.
As 5, 6, 8 and 9 are already assigned, we have D=7 and therefore Y=2.
9 5 6 7
1 0 8 5
--------------------------------
1 0 6 5 2
Here, L + L = any even number, so 1 + 1 = 2, (if it takes carry then add it)
E + E = any even number, so 5 + 5 = 10 (0 is the even number)
K A N S A S 4 9 7 2 9 2
O H I O 5 8 6 5
--------------------------------------- ---------------------------------------
O R E G O N 5 0 3 1 5 7
19 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
H E R E 9 4 5 4
S H E 8 9 4
------------------------------------ ------------------------------------
C O M E S 1 0 3 4 8
E+E=8
then,,E = 4 AND S = 8,
E+S=M
4 + 8 = 12
M=2
H + 1 = 10 so, O = 0 C = 1,
now R + H = E = 4
H=9
so R has to be 5 as 5 + 9 = 14 which leaves carry 1..
so M = 3
so R + H + O = 5 + 9 + 0 = 14
P O I N T 9 8 5 0 4
Z E R O 3 1 6 8
------------------------------------ ------------------------------------
E N E R G Y 1 0 1 6 7 2
5. GO + TO = OUT
G O 8 1
T O 2 1
------------------------------------ ------------------------------------
O U T 1 0 2
U S A 9 3 2
U S S R 9 3 3 8
------------------------------------ ------------------------------------
P E A C E 1 0 2 7 0
E V E R 5 6 5 3
S I N C E 9 7 8 2 5
---------------------------------------- ----------------------------------------
D A R W I N 1 0 3 4 7 8
21 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
E A T 4 8 0
E A T 4 8 0
E A T 4 8 0
---------------------------- ----------------------------
B E E T 1 4 4 0
8 + 8 + 8 = 24
2 carry over
4 + 4 + 4 = 12 + 2 = 14
So, BEET = 1440
Note, In 3 word addition if there is carry over then count only 2
9. E A T + T H A T = A P P L E
8 1 9
E A T
9 2 1 9
T H A T
----------------------------
----------------------------
1 0 0 3 8
A P P L E
22 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
N I N E N I N E
F I N E F I N E
---------------------------- ----------------------------
W I V E S W I V E S
9 6 0 8
W A I T
6 7 7
A L L
----------------------------
----------------------------
1 0 2 8 5
G I F T S
F O R T Y 2 9 7 8 6
T E N 8 5 0
T E N 8 5 0
---------------------------------- ----------------------------------
S I X T Y 3 1 4 8 6
S C O O B Y 1 9 4 4 2 3
D O O O 7 4 4 4
---------------------------------------- ----------------------------------------
B U S T E D 2 0 1 8 6 7
23 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
14. S C O O B Y + D O O = B L I N K S
S C O O B Y 3 6 O O 1
D O O D 2 2
---------------------------------------- ----------------------------------------
B L I N K S 3 6 2 9 5 3
Y + O = S, B + O = K, O + D = N , O = I , C = L ,S = B
so, we can take values S&B = 3, C&L = 6, O&I = 2 Y = 1, D = 7,N = 9,K = 5 .
B A N A N A 2 4 9 4 9 4
G U A V A 6 5 4 7 4
---------------------------------------- ----------------------------------------
O R A N G E 3 1 4 9 6 8
H O W 7 0 5
M U C H 9 8 3 7
---------------------------- ----------------------------
P O W E R 1 0 5 4 2
D A Y S 9 7 4 3
T O O 5 2 2
---------------------------- ----------------------------
S H O R T 1 0 2 6 5
24 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
T W O 8 9 3
D A Y S 6 4 1 2
---------------------------- ----------------------------
M O R E 7 3 0 5
T W O 9 3 8
T W O 9 3 8
---------------------------- ----------------------------
F O U R 1 8 7 6
25 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
A B C 2 6 9
D E F 1 3 8
---------------------------- ----------------------------
G H I 4 0 7
This question may have thousand solutions because the characters are distinct. Make sure
A + D must not generate a carry.
23. AB + CD = EF
A B 4 9
C D 1 8
---------------------------- ----------------------------
E F 6 7
This question may have more than hundreds of solutions because the characters are
distinct. Make sure A + C must not generate a carry.
26 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
Perfect Information: Information is shown or clear. See the exact state to the game. Chess is
an example of a game with perfect information as each player can see all the pieces on the
board at all times. Other examples of games with perfect information include tic-tac-
toe, checkers, infinite chess, and Go
Imperfect Information: Information is Hidden. Card games where each player's cards
are hidden from other players such as poker and bridge are examples of games with imperfect
information.
Game Playing Problem:
States where the game has ended are called terminal states.
In two-player game, assume one is called MAX (tries maximize utility) and one is
called MIN.
In the search tree, first layer is more by MAX, next layer by MIN, and alternate to
terminal state.
Each layer in the search is called a ply.
a ply is one turn taken by one of the players.
Game Playing is an important domain of artificial intelligence. Games don’t require much
knowledge; the only knowledge we need to provide is the rules, legal moves and the conditions
of winning or losing the game.
Both players try to win the game. So, both of them try to make the best move possible at each
turn. Searching techniques like BFS (Breadth First Search) are not accurate for this as the
branching factor is very high, so searching will take a lot of time.
So, we need another search procedure that improve –
Generate procedure so that only good moves are generated.
Test procedure so that the best move can be explored first.
Sometimes Generate and Test procedure is known as Hit and Trial.
27 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
False
Algorithm:
1. Generate a possible solution.
2. Test to see if this is actually a solution.
3. Quit if a solution has been found otherwise - return to step 1.
The most common search technique in game playing is Minimax search procedure. It is depth-
first depth-limited search procedure. It is used for games like chess and tic-tac-toe.
Examples: MiniMax algorithm, Alpha Beta Pruning.
Rule
Situation/Condition Action
LHS (Left Hand Side) RHS (Right Hand Side)
A production system is nothing but a set of rules, each rule consists of LHS and RHS where,
LHS patterns determines the applicability of rules and a RHS describes the operations to be
performed if the rule is applied.
Simple, Production system are the rules of the form C A, where LHS is known Condition
and RHS is known as Action.
LHS described as Applicability of Rule.
RHS described Operation to be Performed.
28 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]
AI - Unit: 2 Problem Solving
For Example. Water Jug Problem, Missionaries and Cannibal (These problem production
rule systems are already done. See above)
1. Monotonic Production System: In monotonic production system all conclusions are still
valid after adding information to the existing information. Example: Theorem Proving
(Pythagoras Theorem)
rules that is allowable also transform state x to state y. Example: ABCD is applied then it
takes to x=> y If I use Permutation of ABCD or Combination of ABCD like BCDA then
also it takes stare x to state y. || AxB = BxA
Q. Define and describe the difference between knowledge, belief, hypothesis and data.
Ans:
Knowledge: can be defined as the body of facts and principles accumulated by humankind or
the act, fact, or state of knowing.
Belief: It is defined as essentially any meaningful and coherent expression that can be
represented. Thus, a belief can be true or false.
Hypothesis: It is defined as justified belief that is not known to be true. Thus, a hypothesis is
a belief that is backed up with some supporting evidence, but it may still be false. In other
words, it is a preliminary assumption or tentative explanation that accounts for a set of facts,
taken to be true for the purpose of investigation and testing.
Data: Data in computer terminology mean raw facts and figures. For example, 'DBK',
11716713, 'A' are data. Data are processed to form information.
Information: Data arranged in useful and meaningful form is known as information. For
example, 'DBK, whose roll number is 11716713, has got grade A' is an information as it is
conveying some meaning.
THE END
30 | P a g e
Deepak Bhatta Kaji (MCA)
Department of Computer Science – DEC [NAST]