# Searching and AI ```Searching
Introduction to problem solving
• What is problem solving?
• The activities that we usually consider to require
intelligence are those that call for thinking and
reasoning– for example, solving problems,
proving theorems and playing games (e.g. chess)
• To solve a problem is to find a solution, or in
some cases the best solution, to it.
2
Example
• Consider walking from IFM, Shaaban Robert
street to Mlimani City shopping centre…
• Each location could be considered a
knowledge state, and these knowledge states
are linked by the actions performed at each of
them (going straight on, turning left etc.)
3
Example
• The route we are describing, drawn on a map
of Dar es Salaam, is a path from state to state
– starting with starting point (start state)
– to the finishing point (goal state).
• What we are doing is constructing a path
through Dar es Salaam to satisfy the need to
get to Mlimani City SC
Problem space theory
• Such problems are described by problem space
theory
• We construct a path through the problem space
(possible location points in our example) to
satisfy the need identified in the goal.
• Assuming that we do not have complete
knowledge of the problem area, we are trying to
construct a path between the problem and the
solution.
5
• An adversary problem is one in which two or
more people compete against each other.
– E.g. chess.
• Non-adversary problems are ones that do not
involve another person (except in the role of
problem setter).
– E.g. puzzle, crossword.
6
Game playing
• Game playing is a special sort of problem solving in
which the problem is to find a winning strategy or to
find the best current move.
• Successful strategies in AI research on game playing for
games with two players, in which
– each player always has complete information about the
state of play
– there is no element of chance.
• Examples
– Noughts and crosses, chess
• Games with incomplete information
7
• The 8-puzzle - a 3 x 3 square containing the
numbers 1 to 8 which must be moved via the
vacant square until they are in order.
4
2
7
5
6
3
1
2
3
1
4
5
6
8
7
8
8
• Cannibals and missionaries
– Transport three Cannibals and three Missionaries across a river in a boat
that can only hold two people, but that needs at least one to get it across
the river.
– Cannibals must never outnumber Missionaries on either bank, or the
Missionaries will be eaten.
• Jugs problems
– Three jugs, A, B and C can hold 8, 5 and 3 liters respectively.
– A is full; B and C are empty.
– Find a sequence of pourings that leaves 4 liters in A and 4 liters in B.
9
• The towers of Hanoi (Edouard Lucas, 1883)
– Three vertical pegs with four (or more) discs of decreasing size piled on one
peg.
– Transfer the discs to the second peg, moving only one disc at a time, and
never placing a larger disc on top of a smaller one.
A
C
B
10
The search problem
• We’ll consider a problem soluble by searching if
– the problem can be formulated in terms of a progression of
states from some initial state to some target (or goal) state.
– this progression can be enumerated through the application of
some given set of actions (or operators).
• these actions define some transformation of all, or part, of the state.
• In problems, such as Cannibals and Missionaries or the
Towers of Hanoi, all three are clearly specified
– AI programs are relatively successful at solving these problems
11
Search problem
• In AI there are two main approaches to
describing the search for solutions to
problems:
– problem-reduction representations
• operators that divide goals into sub-goals
– state space representations
• operators that change one state of the world into
another
12
Problem reduction representation
• In a problem reduction representation each
operator divides one goal into a set of subgoals that are easier to achieve.
• Problem reduction depends on the fact that
some sub-goals are so simple that they can be
satisfied directly.
• The aim of problem reduction is to analyse the
main goal into components of this sort.
13
Towers of Hanoi
A
B
C
• The overall goal is to move the four discs from A to B,
moving only one disc at a time, never placing a larger
disc on top of a smaller one.
• This goal can be divided into three sub-goals:
– Transfer the three smaller discs from A to C.
– Transfer the largest disc from A to B.
– Transfer the three smaller discs from C to B.
• Of these goals, (1) and (3) can be reduced further,
while (2) can be achieved directly, provided that (1)
has been achieved.
14
State space representation
• The basic component of a state space representation is a state of the
world or, rather, a state of that small part of the world that is relevant to
the problem.
• For example, in the Missionaries and Cannibals problem a typical state of
the world might be:
Left Bank
2 Missionaries
2 Cannibals
Boat
Right Bank
1 Missionary
1 Cannibal
• The states of the world are linked by operators that can change one state
into another.
15
Operators
• A solution to a problem is a sequence of
operators that provides a path between the
initial state and (one of) the goal state(s).
• To find a solution, the operators must be
applied to the initial state and/or the goal state
according to a set of rules (or search strategy),
until a path between those two states is
discovered.
16
Applying the operators
• For example, the operator
‘Take one Missionary and one Cannibal from the
Left Bank to the Right Bank in the boat’
results in the following new state:
Left Bank
2 Missionaries
2 Cannibals
Boat
Right Bank
Left Bank
1 Missionary
1 Missionary
Right Bank
1 Missionary
2 Missionaries
1 Cannibal
2 Cannibals
Boat
• From each state it is (usually) possible to reach
several others by applying different operators.
17
State spaces
• A map of all states that can be reached from
the initial state by the application of one or
more operators is called a state space.
• State spaces can be represented by tree
diagrams, with the initial state at the top, and
paths to other states branching beneath it.
18
State spaces
• For many problems such tree diagrams can be
extended indefinitely.
• Solutions to a problem, if there are any, are
represented by paths through the tree from
the initial state to (one of) the goal state(s).
Tree representation
• In a tree representation the same state will
appear more than once if it can be reached in
several ways from the initial state.
20
Tree representation
• It is sometimes useful to use state space
representations in which all occurrences of
the same state are collapsed (overlapped).
– Such representations are graphs rather than trees.
– Graph theory
• Every problem has a different search space
• We want a general solution that may be applied to any
search space.
• We need an abstract formal framework for constructing
and analysing search spaces.
Graph terminology
• A graph is defined as a set of nodes, with links
between them (also called vertices and
edges).
• Links may be directed (denoted by arrows) or
undirected.
• A path is a sequence of nodes that connects
22
Graph terminology
• An acyclic graph is a graph that contains no
cycles (paths that link a node to itself).
• The parents of a node N, are all the nodes
• The children of a node N, are all the nodes
that N provides a link to.
Example graphs
A
D
A
D
C
B
E
E
A
A (labeled) graph
D
C
E
B
An acyclic directed graph
D
B
A directed graph
F
A
F
C
C
E
B
A directed graph with a cycle
24
Tree terminology
• A tree is a graph where each node has at most
one parent.
• Trees are always directed.
• The root node of a tree is the only node that
has no parent (each tree has a root)
• For any node N, all the other nodes that have
the same parent as N are called siblings of N.
• For a node N, all the nodes whose paths pass
through N (but not N itself) are considered
descendents of N.
25
A route-finding example
School
Factory
Hospital
Newsagent
Church
Library
Park
University
• Suppose we want to find the route from the
Library (L) to the University (U).
26
Search space
• We can represent the town map
more abstractly
L (Library)
S (School)
F (Factory)
H (Hospital)
P (Park)
N (Newsagent)
U (University)
C (Church)
Do you see a problem with this tree?
27
– Each state is a location
– The only action is to drive from
one location to another.
– The initial state (L) is used as the
root of the tree, and referred to
as the start node.
– Now, rather than talking about a
route from the library to the
finding a path from the start
node (L) to the goal state - here
node (U).
Missionaries and cannibals
Key:
M = Missionary
C = Cannibal
* = Boat
LB = Left bank
RB = Right bank
LB:
RB:
Boat:
LB:
RB:
MMMCCC*
Boat:
MC
LB:
RB:
MMCC
MC*
Boat:
LB:
RB:
C
MMMCC*
C
28
CC
MMMC
CC*
Boat: CC
LB:
RB:
MMMCCC*
Well-defined problems
• A problem can be formally defined by
– The initial state
– Possible actions (operators)
– Goal test – to determine if a given state is a goal
• Explicit list of goal states
• Property of the goal state (‘chess’)
– Path cost – assigns a numeric cost to each path
• A solution to a problem is a path from the
initial state to a goal state.
29
Search nodes ≠ States
8 2
3 4 7
5 1 6
8 2 7
3 4
5 1
6
8
2
3 4 7
8 2
3 4 7
8 4
3
2
7
5 1
5 1
5 1
6
6
6
8 2
3 4 7
5 1
If states are allowed to be revisited, the search tree may be infinite even
when the state space is finite
30
6
Uninformed vs. informed search
• Uninformed (blind) search
– no information about the number of steps or the
path cost from the current state to the goal — all
they can do is distinguishing a goal state from a
non-goal state
• Informed search (or heuristic search)
– use information (heuristics) guesses about the
search space
31
Measuring performance
• Completeness
– Is the algorithm guaranteed to find a solution
when there is one?
• Optimality
– Does the strategy find the optimal (highestquality) solution when there are several different
solutions?
32
Measuring performance
• Time complexity
– How long does it take to find a solution?
• Space complexity
– How much memory does it need to perform the
search?
```