Download as pdf or txt
Download as pdf or txt
You are on page 1of 86

Artificial Intelligence

Chapter 3: Solving Problems by Searching


Artificial Intelligence

Today: Search and AI


▪ Problem Solving by Searching
▪ Search Space, Search Trees
▪ Search in Graphs
▪ Search Methods
▪ Uninformed search
▪ Breadth-first
▪ Depth-first
Search

• One of the most basic techniques in AI


• Underlying sub-module in most AI systems

• Can solve many problems that humans are not good at


(achieving super-human performance)

• Very useful as a general algorithmic technique for solving many


non-AI problems.
Many problems can be solved by search:
Search Problems
▪ A search problem consists of:

▪ A state space

“N”, 1.0
▪ A successor function
(with actions, costs)

“E”, 1.0
▪ A start state and a goal test

▪ A solution is a sequence of actions (a plan) which


transforms the start state to a goal state
Problem Solving by Searching
Why search ?
❑ Early works of AI was mainly towards
• proving theorems
• solving puzzles
• playing games
▪ All AI is search!
▪ Not totally true (obviously) but more true than you might think.
▪ All life is problem solving !!

▪ Finding a good/best solution to a problem amongst many possible solutions.


Everyday – search examples

• Searching for the shortest route to RP?


• Searching for your keys?
• Searching for classes to take?
• Searching for where the party is?
• Searching for the best way to pack your car
Problem-Solving Agents
• agents whose task it is to solve a particular problem
• goal formulation
• what is the goal state
• what are important characteristics of the goal state
• how does the agent know that it has reached the goal
• are there several possible goal states
• are they equal or are some more preferable

• problem formulation
• It is a process of deciding what actions should be taken to achieve formulated
goal.
• what are the possible states of the world relevant for solving the problem
• what information is accessible to the agent
• how can the agent progress from state to state
Problem Formulation
• To solve a problem by search, we need to first formulate the problem.
HOW?
• Our textbook suggest the following schema to help us formulate problems
• We have seen that the basic elements of a problem definition are the states
and actions. To capture these ideas more precisely, we need the following:

1. State space
2. Initial state
3. Actions or Successor Function:
4. Goal State:
5. Path Cost:
6. Solution:
Problem Formulation
Initial state: starting point from which the agent sets out.

actions (operators, successor functions)


• describe the set of possible actions.

state space
• set of all states reachable from the initial state by any sequence of actions

path
• sequence of actions leading from one state in the state space to another

goal test
• determines if a given state is the goal state
Problem Formulation

solution
• path from the initial state to a goal state

search cost
• time and memory required to calculate a solution

path cost
• determines the expenses of the agent for executing the actions in a path
• sum of the costs of the individual actions in a path

total cost
• sum of search cost and path cost
• overall cost for finding a solution
More formally, a problem is defined by:
1.A set of states S
2.An initial state S

3.A set of actions A —Actions(s) = the set of actions that can be executed
4.Transition Model:
—s is called a successor of s
—Successors(si)* = state space
5.Goal test Goal(s)
—s is a goal state if Goal(s) is true

6.Path cost (additive)


—e.g. sum of distances, number of actions executed, …
Example Problems

toy problems real-world problems


• vacuum world • route finding
• 8-puzzle • touring problems
• 8-queens • traveling salesperson
• crypt arithmetic • VLSI layout
• vacuum agent • robot navigation
• missionaries and cannibals • assembly sequencing
• Web search
Problem Formulation for vacuum cleaner world
• State space for vacuum cleaner world: Arcs = Actions; L = Left, R = Right, S = Suck
• States:
The agent is in one of two locations – either left or
Right Square – with or without dirt. Thus 2*2^2 = 8
states

• Initial state: Any state can be assigned as initial


• Successor function: Legal states generated by actions:
L,R, Suck

• Goal Test: Check whether all squares are clean?

• Path Cost: Each step costs 1, Thus path cost = number


of steps
Example: Traveling in Romania
Currently in Arad, need to get to Bucharest by tomorrow to catch a flight. What is
the State Space?

start

end
Example: Traveling in Romania
State: We regard a problem as state space here a state is a City
Initial State: the state to start from In(Arad)
Successor Function: description of the possible actions, give state x, S(X)
returns a set of <action, successor> ordered pairs.
S(x)= { <Go(Sibiu), In(Sibiu)>, <Go(Timisoara), In(Timisoara)>,
<Go(Zerind),In(Zerind)> } start

Goal Test: determine a given state is a goal state.


In(Sibiu) aNo. In(Zerind) aNo.…. In(Bucharest)àYes!

Path Cost: a function that assigns a numeric cost to each path. end

– e.g., sum of distances, number of actions executed, etc.


– c(x,a,y) is the step cost, assumed to be ≥ 0

Solution: a sequence of actions leading from the initial state to a goal state
{Arad aSibiu a Rimnicu Vilcea a Pitesti a Bucharest}
Example: 8-puzzle

States?? List of 9 locations-e.g., [7,2,4,5,-,6,8,3,1]


Initial state?? [7,2,4,5,-,6,8,3,1]
Actions?? {Left, Right, Up, Down}
Transition Model??...
Goal test?? Check if goal configuration is reached

Path cost?? Number of actions to reach goal


Problem Formulation
8-Puzzle Problem

Initial State Operators Goal State

2 1 3 1 2 3
Slide blank square left.
4 7 6 Slide blank square right. 4 5 6
….
5 8 7 8
Problem Formulation
8-Puzzle Problem
Representing states:

▪ For the 8-puzzle

▪ 3 by 3 array
▪ 5, 6, 7
▪ 8, 4, BLANK 5 6 7
▪ 3, 1, 2
8 4
▪ A vector of length nine 3 1 2
▪ 5,6,7,8,4, BLANK,3,1,2

▪ A list of facts
▪ Upper_left = 5
▪ Upper_middle = 6
▪ Upper_right = 7
▪ Middle_left = 8
Problem Formulation
8-Puzzle Problem
▪ Specifying operators:
▪ There are often many ways to specify the operators, some will be much easier
to implement...

• Move 1 left
• Move 1 right
• Move 1 up
• Move 1 down
• Move Blank left
• Move 2 left
• Move Blank right
• Move 2 right
• Move Blank up
• Move 2 up
• Move 2 down
• Move Blank down 5 6 7
• Move 3 left 8 4
• Move 3 right
• Move 3 up 3 1 2
• Move 3 down
• Move 4 left
• …
Problem Formulation
8-Puzzle Problem

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

Initial state Goal state

Operators: slide blank up, slide blank down, slide blank left, slide blank right

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

Solution: sb-down, sb-left, sb-up,sb-right, sb-down

Path cost: 5 steps to reach the goal


Example: 8-puzzle
Example: Measuring problem – water jug problem!
Given 3 jugs (9, 5 and 3 liters), a water pump, and a
sink, how do you get exactly 7 liters into the 9 liter
9 liter
jug? 5 liter
3 liter

Solution: A B C
0 0 0
0 5 0
3 2 0
3 0 2
3 5 2
3 0 7
Which solution do we prefer?
Example 1: water jug

Given 3 jugs (9, 5 and 3 liters), a water pump, and a sink, how do you get exactly 7 liters
into the 9 liter jug?

• State: (x y z) for liters in jugs 1, 2, and 3 integers 0 to 9 assigned to all possible


permutations of 1 2 3
• Operations: empty jug, fill jug, EX. (fill (0 5 0))
• Initial state: (0 0 0)

• Goal state: (x x 7)

• Solution séquence (5 0 0 (0 5 0 (0 0 0 etc….)


• Path cost: Number of actions to reach goal
Example: Measuring problem – water jug problem!

You have three jugs. They can hold 12 liters, 8 liters, and 5 liters Jug 12 Jug 8 Jug 5
of milk respectively. 12-liter jug is full of milk, but you need to
split it in half using only the jugs on hand. How can you split up A B C
the malik to give away exactly 6 liter, and keep 6 liter? 12 0 0
4 8 0
4 3 5
12 9 3 0
liter 8
liter 5 liter 9 0 3
1 8 3
1 6 5
6 6 0
Missionaries and cannibals
▪ Three missionaries and three cannibals are on the left bank of a river.
▪ There is one canoe which can hold one or two people.
▪ Find a way to get everyone to the right bank, without ever leaving a
group of missionaries in one place outnumbered by cannibals in that
place.
Near side Far side
0 Initial setup: MMMCCC B –
1. Two cannibals cross over: MMMC B CC
2. One comes back: MMMCC B C
3. Two cannibals go over again: MMM B CCC
4. One comes back: MMMC B CC
5. Two missionaries cross: MC B MMCC
6. A missionary & cannibal return: MMCC B MC
7. Two missionaries cross again: CC B MMMC
8. A cannibal returns: CCC B MMM
9. Two cannibals cross: C B MMMCC
10. One returns: CC B MMMC
11 And brings over the third: - B MMMCCC
Missionaries and cannibals
▪ States: three numbers (i,j,k) representing the number of missionaries,
cannibals, and canoes on the left bank of the river.
▪ Initial state: (3, 3, 1)
▪ Operators: take one missionary, one cannibal, two missionaries, two
cannibals, one missionary and one cannibal across the river in a given
direction (I.e. ten operators).
▪ Goal Test: reached state (0, 0, 0)
▪ Path Cost: Number of crossings.
Missionaries and cannibals
▪ States: three numbers (i,j,k) representing the number of missionaries,
cannibals, and canoes on the left bank of the river.
▪ Initial state: (3, 3, 1)
▪ Operators: take one missionary, one cannibal, two missionaries, two
cannibals, one missionary and one cannibal across the river in a given
direction (I.e. ten operators).
▪ Goal Test: reached state (0, 0, 0)
▪ Path Cost: Number of crossings.
State space Representation and search
• A state space is the set of all possible configurations of a system.
• It is a useful abstraction for reasoning about the behavior of a given system and is widely
used in the fields of artificial intelligence and game theory.
In Artificial Intelligence a state space consists of the following elements,
1. A (possibly infinite) set of states
1.1. Out of the possible states, one state represents the start state that is the initial state of the problem.
1.2. Each state represents some configuration reachable from the start state
1.3. Out of the possible states, some states may be goal states (solutions)

2. A set of rules,
2.1. Applying a rule to the current state, transforms it to another or a new state in the state space
2.2 All operators may not be applicable to all states in the state space State spaces are used extensively
in Artificial Intelligence (AI) to represent and solve problems.
State Space Search Examples:
A maze problem can be represented as a state-space;
A set of States
• Each state represents “where you are” that is the current position in the maze
• The start state or initial state represents your starting position
• The goal state represents the exit from the maze

Rules (for a rectangular maze) are: move north, move south,


move east, and move west

• Each rule takes you to a new state (maze location)


• Rules may not always apply, because of walls in the maze

Each state space takes form of tree or graph.


State Space Graphs
• Searching in graphs provides an appropriate level of abstraction within which to
study simple problem solving independent of a particular domain.
• A (directed) graph consists of a set of nodes and a set of directed arcs between
nodes. The idea is to find a path along these arcs from a start node to a goal node.
• Graph Search is a search engine that is integrated with Facebook's social
graphs. a G

• The search engine processes natural language queries to return b c


information from across the user's social network of friends and connections
or beyond, depending on the search. e
d f
• There are two basic types of graph search algorithms: S h
• depth-first and breadth-first.
• The former type of algorithm travels from a starting node to some end node p r
q
before repeating the search down a different path from the same start node
until the query is answered Tiny search graph for a tiny search
problem
Search Trees
• Search tree: A tree representation of search problem is called Search tree.
• A Tree is a connected acyclic undirected graph. There is a unique path between every pair of
vertices in G. A tree with N number of vertices contains (N-1) number of edges. The vertex
which is of 0 degree is called root of the tree.

• Tree-Search algorithm - is any particular algorithm used for


solving your search problem.
• Graph-Search algorithm - is a Tree-Search algorithm augmented
with a set of explored states. Both of these algorithms are
represented as a tree!
• Tree Traversal algorithms can be classified broadly in two
categories:

• Depth-First Search (DFS) Algorithms.


• Breadth-First Search (BFS) Algorithms.
What are applications of tree?

• The following are the applications of trees:

• Storing naturally hierarchical data:


• Trees are used to store the data in the hierarchical structure. ...

• Organize data:
• It is used to organize data for efficient insertion, deletion and searching. ...

• Trie:
• It is a special kind of tree that is used to store the dictionary.
State Space Graphs vs. Search Trees
Each NODE in in
the search tree is
State Space Graph an entire PATH in Search Tree
the state space
graph. S

a G d e p
b c
b c e h r q
e
d f a a h r p q f
S h
p q f q c G
p q r
We construct both a
q c G
on demand – and
we construct as a
little as possible.
Example: Tree Search
State space graph Search tree

A S

S C
A B

C C
B

G G G
Example: Tree Search
a G
b c
e
d f
S h
p q r
Example: Tree Search
a G
b c
e
d f
S h
p q r

S s
s→d
d e p s→e
s→p
b c e h r q s→d→b
s→d→c
a a h r p q f s→d→e
s→d→e→h
p q f q c G s→d→e→r
a s→d→e→r→f
q c G
s→d→e→r→f→c
a s→d→e→r→f→G
SEARCH TECHNIQUES
1. Search without information, 1. Search with information
2. No knowledge 2. Use knowledge to find step to
3. Time consuming Search techniques solution
4. More complexity(time, space) 3. Quick solution
4. Less complexity (Time , space)
Uninformed or Blind informed or Heuristic

Breadth Hill
Depth first A*
first climbing
Search search
Search Search
( DFS )
( BFS )
Best-First Greedy
Search Search
SEARCH TECHNIQUES
• Uninformed search, also called blind search or unguided search, is a class
of general purpose search algorithms that operate in a brute-force way.

• The term 'uninformed’ means that they have no additional information about
states beyond that provides in the problem definition.

• An uninformed search is a searching technique that has no additional


information about the distance from the current state to the goal.

• Depth-First Search (DFS) Algorithms.


• Breadth-First Search (BFS) Algorithms.
Evaluation of Search Strategies

• A search strategy is defined by picking the order of node expansion

• Strategies are evaluated along the following dimensions:

• Completeness: if there is a solution, will it be found


• Time complexity: How long does it takes to find the solution
• Space complexity: memory required for the search
• Optimality: will the best solution be found

• Time and space complexity are measured in terms of

– b: maximum branching factor of the search tree


– d: depth of the least-cost solution
– m: maximum depth of the state space (may be ∞)
Breadth-First Search

Breadth-First Search algorithm is a


graph traversing technique, where you
select a random initial node (source or
root node) and start traversing the graph
layer-wise in such a way that all the
nodes and their respective children
nodes are visited and explored.
Breadth-First
▪ It uses a queue to keep track of the next location to visit.
▪ Enqueue nodes on nodes in FIFO (first-in, first-out) order.
▪ Complete
▪ Optimal (i.e., admissible) if all operators have the same cost. Otherwise, not optimal but
finds solution with shortest path length.
▪ Exponential time and space complexity, O(bd), where d is the depth of the solution and b
is the branching factor (i.e., number of children) at each node
▪ Will take a long time to find solutions with a large number of steps because must look at
all shorter length possibilities first
▪ A complete search tree of depth d where each non-leaf node has b children, has a total of 1 + b + b2 + ...
+ bd = (b(d+1) - 1)/(b-1) nodes
▪ For a complete search tree of depth 12, where every node at depths 0, ..., 11 has 10 children and every
node at depth 12 has 0 children, there are 1 + 10 + 100 + 1000 + ... + 1012 = (1013 - 1)/9 = O(1012) nodes
in the complete search tree. If BFS expands 1000 nodes/sec and each node uses 100 bytes of storage,
then BFS will take 35 years to run in the worst case, and it will use 111 terabytes of memory!
Breadth-first search
▪ Expand shallowest unexpanded node
▪ Implementation:
▪ fringe is a FIFO queue, i.e., new successors go at end
Breadth-first search

▪ Expand shallowest unexpanded node


▪ Implementation:
▪ fringe is a FIFO queue, i.e., new successors go at
end
Breadth-first search
▪ Expand shallowest unexpanded node
▪ Implementation:
▪ fringe is a FIFO queue, i.e., new successors go at end
Breadth-first search
▪ Expand shallowest unexpanded node
▪ Implementation:
▪ fringe is a FIFO queue, i.e., new successors go at end
Breadth-First Search (BFS) Properties
s tiers
▪ Complete? Yes (if b is finite)
1 node
b
… b nodes
▪ Time? 1+b+b2+b3+… +bd + b(bd-1) = O(bd+1) b2 nodes

bs nodes
▪ Space? O(bd+1) (keeps every node in memory)
bm nodes
▪ Optimal? Yes (if cost = 1 per step)

▪ Space is the bigger problem (more than time)


Algorithm - BFS
Step 1. Visit the starting vertex and mark it visited;
Display the vertex;
Set pointer to starting vertex currently working vertex;
Step 2. If (currently vertex has adjacent unvisited vertex) then
{
visit the adjacent unvisited vertex and mark it as visited;
Insert it into a queue; (Enqueue) Initial state
display it; A
}
else
D
{ B C
Update the pointer to the first element of queue;
remove the first element from queue; (Dequeue) E F G H
}
repeat step2 until queue is empty;
Breadth-First Search
Strategy: expand a a G
shallowest node first b c
Implementation: Fringe e
d f
is a FIFO queue S h
p q r

d e p
Search
b c e h r q
Tiers
a a h r p q f

p q f q c G

q c G a

a
Example (Breadth-First Search - BFS)

Initial state
A

B C D E F
Goal state

G H I J K L M N O P

Q R S T U V W X Y Z

Press space to see a BFS of the example node set


We
Node
Thethen
B
search
isbacktrack
expanded
then moves
tothen
expand
to
removed
thenode
firstfrom
C,
nodethe Node
This
We begin
node
A is removed
is
with
thenour
expanded
from
initialthe
state:
to
queue.
reveal
the Each
node
queue.
and
in the
theThe
queue.
process
revealed
Press
continues.
nodes
space Press
to
arecontinue.
added
spaceto the
A further
revealed
labeled(unexpanded)
A.
node
Press
is added
spacenodes.
to continue
the Press
END space
of the
END of the queue. Press space. queue. Press space to continue the search.

B C D E F

G H I J K L M N O P
Node L is located and the search returns
a solution. Press space to end.
Q R S T U

Press space to continue


begin thethe
search
search

Size of Queue: 010


987651 Queue: A
B,J,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
Empty
K,
G,
C,
F,
M,
D,
E,
H,K,
I,
L, J,
G,
L,
H,
F,
D,
E,
M,
I,
N,L,
K,
G,
J,
M,
H,
I,
F,
E,
O,
N,M,
K,
L,
J,
G,
H,
I,
FN,
P,
O,K,
M,
J,
L,
N,
H
I,O,
Q,P,
K,
L,
JM,
O,
N,
P,
Q,
R,M,
LP,
O,
N,
Q,
S,
R,Q,
N
O,
P,
T,
R,
S,R
Q
U
P
ST
Nodes expanded: 11
9876543210
10 CurrentFINISHED
Action: Backtracking
Expanding
SEARCH Current level: n/a
210

BREADTH-FIRST SEARCH PATTERN


Another EXAMPLE (Breadth-First Search - BFS)
Breadth First Search examines the nodes in the following order: OPEN CLOSE
A
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U BCD A
CDEF BA
A Initial state DEFGH CBA
EFGHIJ DCBA
FGHIJKL EDCBA
GHIJKLM FEDCBA
B C D HIJKLMN GFEDCBA
IJKLMNOP HGFEDCBA
JKLMNOPQ IHGFEDCBA
KLMNOPQR JIHGFEDCBA
E F G H I J LMNOPQRS KJIHGFEDCBA
MNOPQRST LKJIHGFEDCBA
NOPQRST MLKJIHGFEDCBA
OPQRST NMLKJIHGFEDCBA
PQRST ONMLKJIHGFEDCBA
K L M N O P Q R QRSTU PONMLKJIHGFEDCBA
RSTU QPONMLKJIHGFEDCBA
STU RQPONMLKJIHGFEDCBA
TU SRQPONMLKJIHGFEDCBA
U TSRQPONMLKJIHGFEDCBA
S T Goal state U
U hold
Depth-First (DFS)
▪ Depth First Search - a traversal on graphs (or on trees since those are also graphs)
where you traverse “deep nodes” before all the shallow ones
▪ Enqueue nodes on nodes in LIFO (last-in, first-out) order. That is, nodes used as a
stack data structure to order nodes.
▪ May not terminate without a “depth bound,” i.e., cutting off search below a fixed
depth D ( “depth-limited search”)
▪ Not complete (with or without cycle detection, and with or without a cutoff depth)
▪ Exponential time, O(bd), but only linear space, O(bd)
▪ Can find long solutions quickly if lucky (and short solutions slowly if unlucky!)
▪ When search hits a dead-end, can only back up one level at a time even if the
“problem” occurs because of a bad operator choice near the top of the tree. Hence,
only does “chronological backtracking”
Depth-first search
▪ Expand deepest unexpanded node
▪ Implementation:
▪ fringe = LIFO queue, i.e., put successors at front

Depth-first search
▪ Expand deepest unexpanded node

▪ Implementation:
▪ fringe = LIFO queue, i.e., put successors at front

Depth-first search
▪ Expand deepest unexpanded node

▪ Implementation:
▪ fringe = LIFO queue, i.e., put successors at front
Depth-first search
▪ Expand deepest unexpanded node

▪ Implementation:
▪ fringe = LIFO queue, i.e., put successors at front
Depth-first search
▪ Expand deepest unexpanded node

▪ Implementation:
▪ fringe = LIFO queue, i.e., put successors at front
Depth-first search
▪ Expand deepest unexpanded node

▪ Implementation:
▪ fringe = LIFO queue, i.e., put successors at front
Depth-first search
▪ Expand deepest unexpanded node

▪ Implementation:
▪ fringe = LIFO queue, i.e., put successors at front
Depth-first search
▪ Expand deepest unexpanded node

▪ Implementation:
▪ fringe = LIFO queue, i.e., put successors at front
Depth-first search
▪ Expand deepest unexpanded node

▪ Implementation:
▪ fringe = LIFO queue, i.e., put successors at front
Depth-first search
▪ Expand deepest unexpanded node

▪ Implementation:
▪ fringe = LIFO queue, i.e., put successors at front
Depth-first search
▪ Expand deepest unexpanded node

▪ Implementation:
▪ fringe = LIFO queue, i.e., put successors at front
Depth-first search
▪ Expand deepest unexpanded node

▪ Implementation:
▪ fringe = LIFO queue, i.e., put successors at front
Algorithm - DFS
Step 1. Put the starting vertex into the stack, mark it as visited;
Display the it;
Step 2. If (stack (top) has adjacent unvisited vertex) then
{
visit the adjacent unvisited vertex and mark it as visited; 6
Push it into a stack;
display it; 1 5
}
else 4 2 7
{
Pop the top element from stack; 3 8
}
repeat step2 until stack is empty;
10 9
Depth-First Search
Strategy: expand a a G
deepest node first b c

Implementation: e
d f
Fringe is a LIFO stack S h
p q r

d e p

b c e h r q

a a h r p q f

p q f q c G

q c G a

a
Search Algorithm Properties
▪ Complete: Guaranteed to find a solution if one exists?
▪ Optimal: Guaranteed to find the least cost path?
▪ Time complexity?
▪ Space complexity? b
1 node
… b nodes
b2 nodes
▪ Cartoon of search tree:
m tiers
▪ b is the branching factor
▪ m is the maximum depth
▪ solutions at various depths
bm nodes
▪ Number of nodes in entire tree?
▪ 1 + b + b2 + …. bm = O(bm)
Depth-First Search (DFS) Properties
▪ What nodes DFS expand?
▪ Some left prefix of the tree. 1 node
b
▪ Could process the whole tree! … b nodes
▪ If m is finite, takes time O(bm) b2 nodes
m tiers
▪ How much space does the fringe take?
▪ Only has siblings on path to root, so O(bm)

▪ Is it complete? bm nodes
▪ m could be infinite, so only if we prevent
cycles (more later)

▪ Is it optimal?
▪ No, it finds the “leftmost” solution,
regardless of depth or cost
Properties of depth-first search
▪ Complete? No: fails in infinite-depth spaces,
spaces with loops b
1 node
… b nodes
▪ Modify to avoid repeated states along path
b2 nodes
→ complete in finite spaces
m tiers

▪ Time? O(bm): terrible if m is much larger


than d
bm nodes
▪ but if solutions are dense, may be much faster
than breadth-first
▪ Space? O(bm), i.e., linear space!

▪ Optimal? No
Search Algorithm Properties
EXAMPLE (Depth-First Search - DFS)
Depth First Search examines the nodes in the following order:
A, B, E, K, S, L, T, F, M, C, G, N, H, O, P, U, D, I, Q, J, R
OPEN CLOSED
Start A A
BCD A
EFCD BA
B C D KL FCD E BA
S LFCD K EBA
LFCD S KEBA
TFCD LSKEBA
E F G H I J FCD TLSKEBA
MCD FTLSKEBA
CD M FTLSKEBA
GHD C MFTLSKEBA
K L M N O P Q R NHD G CMFTLSKEBA
HD NGCMFTLSKEBA
OPD H NGCMFTLSKEBA
PD O HNGCMFTLSKEBA
S T Goal U UD P OHNGCMFTLSKEBA
Blind Search Strategies

▪ Breadth-first search
▪ Expand all the nodes of one level first.
▪ Depth-first search
▪ Expand one of the nodes at the deepest level.
Depth-first vs. Breadth-first
Advantages of depth-first: Advantages of breadth-first:
▪ Simple to implement; ▪ Guaranteed to find a solution (if one
▪ Needs relatively small memory for storing the exists) - complete;
state-space.
▪ Depending on the problem, can be
Disadvantages of depth-first: guaranteed to find an optimal
▪ Sometimes fail to find a solution (may be get solution.
stuck in an infinite long branch) - not complete; Disadvantages of breadth-first:
▪ More complex to implement;
▪ Not guaranteed to find an optimal solution
(may not find the shortest path solution); ▪ Needs a lot of memory for storing the
▪ Can take a lot longer to find a solution. state space if the search space has a
high branching factor.
Depth-first vs. Breadth-first
Would you use breadth-first or depth-first search for each of the following
problems? What would you base your choice on:

(a) A medical diagnostic program.


▪ Usually done depth-first. This allows the program to follow a line of reasoning and only change
to a new goal or hypothesis when a previous one is confirmed or denied.

(b) A program to determine the best sequence of manufacturing steps to go from


raw materials to a finished product.
▪ It should have breadth-first. (because it is optimal and complete)
Uniform Cost Search
Uniform-Cost (UCS)
▪ Enqueue nodes by path cost. That is, let g(n) = cost of the path from the
start node to the current node n. Sort nodes by increasing value of g.
▪ Called “Dijkstra’s Algorithm” in the algorithms literature and similar to
“Branch and Bound Algorithm” in operations research literature
▪ Complete (*)
▪ Optimal/Admissible (*)
▪ Admissibility depends on the goal test being applied when a node is
removed from the nodes list, not when its parent node is expanded and
the node is first generated
▪ Exponential time and space complexity, O(bd)
Uniform-cost
▪ QUEUING-FN:- insert in order of increasing path cost
‫تضاف من أجل زيادة تكلفة المسار‬

Arad

75 118
140

Zerind Sibiu Timisoara

75 140 71 151 99118 80


111
Arad
Arad Oradea
Oradea Fagaras
Arad Lugoj
Rimnicu Vilcea
Uniform Cost Search
2 a G
Strategy: expand a b c
cheapest node first: 1 8 2
2 e
3 d f
Fringe is a priority queue 9 2
S h 8
(priority: cumulative cost) 1
1 p q r
15

S 0

d 3 e 9 p 1

b 4 c e 5 h 17 r 11 q 16
11
Cost a 6 a h 13 r 7 p q f
contours
p q f 8 q c G

q 11 c G 10 a

a
Uniform Cost Search (UCS) Properties
▪ What nodes does UCS expand?
▪ Processes all nodes with cost less than cheapest solution!
b c1
▪ If that solution costs C* and arcs cost at least  , then the …
“effective depth” is roughly C*/ C*/ “tiers”
c2

▪ Takes time O(b ) (exponential in effective depth)
C*/
c3

▪ How much space does the fringe take?


▪ Has roughly the last tier, so O(bC*/)

▪ Is it complete?
▪ Assuming best solution has a finite cost and minimum arc cost
is positive, yes!

▪ Is it optimal?
▪ Yes! (Proof next lecture via A*)
Home Work
a GOAL
2 2
b c
3
2
1 8
2 e
3 d
f
9 8 2
START h
1 4 2
p 4 r
15 q

Draw the search tree and find path for BSF and DFS
Home Work

Find solution using Uniform Cost Search


Question: If you given this Context- Free Grammar :

1- Sentence Noun Phrase + Verb phrase

2- Verb phrase Verb + Noun Phrase

3- Noun Phrase Adjective + Noun Phrase

4- Noun Phrase Noun

5- Noun student , ali , school , book , computer , man , programming , …

6- Verb read , go , love , do, write, …

7- Adjective difficult , easy , clever , good , …


Questions

You might also like