You are on page 1of 48

Introduction to Data

Science and Artificial


Intelligence
Solving Problems by Search:
Uninformed Search and
Informed Search
Assoc Prof Bo AN

Research area: artificial intelligence,


computational game theory, optimization
www.ntu.edu.sg/home/boan
Email: boan@ntu.edu.sg
Office: N4-02b-55
Lesson Outline

• Uninformed search strategies


• Informed search strategies
• Greedy search
• A * search
Review: Well-Defined Formulation
Definition of a The information used by an agent to decide what to do
problem
Specification • Initial state
• Action set, i.e. available actions (successor functions)
• State space, i.e. states reachable from the initial state
• Solution path: sequence of actions from one state to another
• Goal test predicate
• Single state, enumerated list of states, abstract properties
• Cost function
• Path cost g(n), sum of all (action) step costs along the path
Solution A path (a sequence of operators leading) from the Initial-State to a state
that satisfies the Goal-Test
Search Algorithms
• Exploration of state space by
1
generating successors of
already-explored states Arad
• Frontier: candidate nodes
for expansion
• Explored set
Search Algorithms
• Exploration of state space by
2
generating successors of
already-explored states Arad
• Frontier: candidate nodes
for expansion
• Explored set Zerind Timisoara

Sibiu
Search Algorithms
• Exploration of state space by
3
generating successors of Arad
already-explored states
Zerind Timisoara
• Frontier: candidate nodes
for expansion Sibiu

• Explored set Rimnicu


Arad
Vilcea

Oradea
Fagaras
Search Algorithms
• Exploration of state space by
4
generating successors of Arad
already-explored states Timisoara
Zerind
• Frontier: candidate nodes Sibiu
for expansion
Arad Rimnicu
• Explored set Vilcea
Fagaras
Oradea

Sibiu Bucharest
Search Strategies
• A strategy is defined by picking the order of node expansion.
• Strategies are evaluated along the following dimensions:

Completeness Does it always find a solution if one exists?


Time How long does it take to find a solution: the
Complexity number of nodes generated
Space Maximum number of nodes in memory
Complexity
Optimality Does it always find the best (least-cost) solution?
Search Strategies
• Branching factor
• Maximum number of successors of any node
• Or average branching factor
Uninformed vs Informed
Uninformed search strategies Informed search strategies
• Use only the information • Use problem-specific
available in the problem knowledge to guide the
definition search
1. Breadth-first search • Usually more efficient
2. Uniform-cost search
3. Depth-first search
4. Depth-limited search
5. Iterative deepening search
Breadth-First Search
Expand shallowest unexpanded node which can be implemented by a
First-In-First-Out (FIFO) queue

1 A 2 A 3 A 4 A

B C B C B C

D E D E F G

Denote • b: maximum branching factor of the search tree


• d: depth of the least-cost solution
• Complete: Yes
• Optimal: Yes when all step costs equally
Complexity of BFS
• Hypothetical state-space, where every node can be expanded into b new
nodes, solution of path-length d
• Time: 1 + 𝑏𝑏 + 𝑏𝑏2 +𝑏𝑏3 + ... + 𝑏𝑏𝑑𝑑 = 𝑂𝑂(𝑏𝑏𝑑𝑑 )
• Space: (keeps every node in memory) 𝑂𝑂(𝑏𝑏𝑑𝑑 )are equal
Depth Nodes Time Memory
0 1 1 millisecond 100 bytes
2 111 0.1 seconds 11 kilobytes
4 11111 11 seconds 1 kilobytes
6 6 18 minutes 111 megabyt
10
e
8 108 31 hours 11 gigabytes
10 1010 128 days 1 terabyte
12 12 35 years 111 terabytes
10
14 1014 3500 years 11111 terabytes
Uniform-Cost Search
To consider edge costs, expand
unexpanded node with the least path
A
cost g
1 10
• Modification of breath-first search
5 B 5
• Instead of First-In-First-Out (FIFO) S G
queue, using a priority queue with 15 5
path cost g(n) to order the elements
C
• BFS = UCS with g(n) = Depth(n)
Uniform-Cost Search

A 1 2
S S
0
1 10
A B C
5 B 5 1 5 15
S G
3 S S
15 4
5
A B C A B C
C
15 5 15
G G G
11 10 11
Here we do not expand notes that have been expanded.
Uniform-Cost Search
Complete Yes
Time # of nodes with path cost g <= cost of optimal solution
(eqv. # of nodes pop out from the priority queue)
Space # of nodes with path cost g <= cost of optimal solution
Optimal Yes
Depth-First Search
Expand deepest unexpanded node which can be implemented by a Last-In-
First-Out (LIFO) stack, Backtrack only when no more expansion
1 A 2 A 3 A 4 A 5 A 6 A

B C B C B C B C C

D E D E D E E

7 A 8 A H I I

9 10 11 12
C C

E C C C C
E
F G F G F G
J K K
L M M
Depth-First Search
Denote
• m: maximum depth of the state space

Complete • infinite-depth
No
spaces: No
• finite-depth spaces
No
with loops: No
• with repeated-state
Ye checking: Yes
• finite-depth spaces
s Yewithout loops: Yes
s
Time 𝑂𝑂 𝑏𝑏𝑚𝑚
If solutions are dense, may be much faster than breadth-first
Space 𝑂𝑂(𝑏𝑏𝑏𝑏)
No
Optimal No
Depth-Limited Search
To avoid infinite searching, Depth-first search with a cutoff on the
max depth / of a path
Complete Yes, if 𝐼𝐼 ≥ 𝑑𝑑

Time 𝑂𝑂(𝑏𝑏𝐼𝐼 )
Space 𝑂𝑂(𝑏𝑏𝑏𝑏)
Optimal No
Iterative Deepening Search
Iteratively estimate the max depth / of DLS one-by-one

Limit = 0

Limit = 1
Iterative Deepening Search
Iteratively estimate the max depth / of DLS one-by-one

Limit = 2
Iterative Deepening Search
Iteratively estimate the max depth / of DLS one-by-one

Limit = 3
Iterative Deepening Search...
Function ITERATIVE-DEEPENING-SEARCH(problem) returns a solution sequence
inputs: problem, a problem

for depth 0 to ∞ do
if DEPTH-LIMITED-SEARCH(problem, depth) succeeds then return its result
end
return failure

Complete Yes
Time 𝑂𝑂(𝑏𝑏𝑑𝑑 )
Space 𝑂𝑂(𝑏𝑏𝑏𝑏)
Optimal Yes
Summary (we make assumptions for optimality)
Breadth- Uniform- Depth-First Depth- Iterative Bidirectional
Criterion first Cost Limited Deepening (if applicable)

Time 𝑏𝑏𝑑𝑑 𝑏𝑏𝑑𝑑 𝑏𝑏𝑚𝑚 𝑏𝑏𝑙𝑙 𝑏𝑏𝑑𝑑 𝑏𝑏𝑑𝑑/2


Space 𝑏𝑏𝑑𝑑 𝑏𝑏𝑑𝑑 𝑏𝑏𝑏𝑏 𝑏𝑏𝑏𝑏 𝑏𝑏𝑏𝑏 𝑏𝑏𝑑𝑑/2
Optimal Yes Yes No No Yes Yes
Complete Yes Yes No Yes, if 𝑙𝑙 ≥ 𝑑𝑑 Yes Yes
Question to think:

• If a search strategy is optimal, is it also complete?


General Search
Uninformed search strategies Informed search strategies
• Systematic generation of new • Use problem-specific knowledge
states (Goal Test) • To decide the order of node
expansion
• Inefficient (exponential space
• Best First Search: expand the
and time complexity)
most desirable unexpanded node
• Use an evaluation function to
estimate the “desirability" of
each node
Evaluation function
• Path-cost function 𝑔𝑔(𝑛𝑛)
• Cost from initial state to current state (search-node) 𝑛𝑛
• No information on the cost toward the goal
• Need to estimate cost to the closest goal
• “Heuristic” function ℎ(𝑛𝑛)
• Estimated cost of the cheapest path from 𝑛𝑛 to a goal state ℎ(𝑛𝑛)
• Exact cost cannot be determined
• depends only on the state at that node
• ℎ(𝑛𝑛) is not larger than the real cost (admissible)
Greedy Search
Expands the node that appears to be closest to goal
• Evaluation function ℎ(𝑛𝑛):estimate of cost from 𝑛𝑛 to 𝑔𝑔𝑔𝑔𝑔𝑔𝑔𝑔
• Function Greedy-Search(problem) returns solution
• Return Best-First-Search(problem, ℎ) // ℎ 𝑔𝑔𝑔𝑔𝑔𝑔𝑔𝑔 = 0

Question: How to estimation the cost from 𝑛𝑛 to goal?

Answer: Recall that we want to use problem-specific


knowledge
Example: Route-finding from Arad to Bucharest
ℎ 𝑛𝑛 = straight-line distance from 𝑛𝑛 to Bucharest Straight-line distance to Bucharest
Arad 366
Oradea Neamt Bucharest 0
71
87 Craiova 160
75 Zerind 151 Dobreta 242
Lasi Efoire 161
Arad 140 Sibiu 92 Fagaras 176
99 Fagaras
Vaslui Giurgiu 77
118
80 Rimnicu Hirsova 151
142
Vilcea Lasi 226
Timisoara 211
Lugoj 244
Pitesti 98 Hirsova Mehadia 241
111 Lugoj 97 85
Neamt 234
70 146 Urziceni 86 Oradea 380
101
Mehadia 138 Bucharest Pitesti 98
75 90 Eforie Rimnicu Vilcea 193
Sibiu 253
Dobreta Craiova
120 Giurgiu Timisoara 329
Urziceni 80
• Useful but potentially fallible (heuristic) Vaslui 199
Zerind 374
• Heuristic functions are problem-specific
Example
a) The initial Straight-line distance to Bucharest

state Arad
Arad
Bucharest
366
0
Craiova 160
366 Dobreta 242
Efoire 161
Fagaras 176
Giurgiu 77
Hirsova 151
Lasi 226
Lugoj 244
Mehadia 241
Neamt 234
Oradea 380
Pitesti 98
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Urziceni 80
Vaslui 199
Zerind 374
Example
b) After Straight-line distance to Bucharest

expanding Arad
Arad
Bucharest
366
0
Arad Craiova
Dobreta
160
242
Efoire 161
Sibiu Timisoara Zerind Fagaras 176
Giurgiu 77
253 329 374 Hirsova 151
Lasi 226
Lugoj 244
Mehadia 241
Neamt 234
Oradea 380
Pitesti 98
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Urziceni 80
Vaslui 199
Zerind 374
Example
c) After Straight-line distance to Bucharest

expanding Arad Arad


Bucharest
366
0
Sibiu Craiova
Dobreta
160
242
Efoire 161
Sibiu Timisoara Zerind Fagaras 176
Giurgiu 77
329 374 Hirsova 151
Lasi 226
Arad Lugoj 244
Oradea Mehadia 241
366 Fagaras Rimnicu Neamt 234
380 Vilcea Oradea 380
176 193 Pitesti 98
Rimnicu Vilcea 193
Sibiu 253
Timisoara 329
Urziceni 80
Vaslui 199
Zerind 374
Example
d) After Straight-line distance to Bucharest

expanding Arad Arad


Bucharest
366
0
Fagaras Craiova
Dobreta
160
242
Efoire 161
Sibiu Timisoara Zerind Fagaras 176
Giurgiu 77
329 374 Hirsova 151
Lasi 226
Arad Lugoj 244
Oradea Mehadia 241
366 Fagaras Rimnicu Neamt 234
380 Vilcea Oradea 380
Pitesti 98
193
Rimnicu Vilcea 193
Sibiu 253
Sibiu Bucharest Timisoara 329
Urziceni 80
253 0 Vaslui 199
Zerind 374
Complete?
Question: Is this approach complete?
Example: Find a path from Lasi to Fagaras
Oradea
Neamt
Zerind
Lasi
Arad
Sibiu Fagaras
R Vaslui
Rimnicu
Timisoara Vilcea
Lugoj Pitesti Hirsova
Urziceni
Mehadia Bucharest

Dobreta Eforie
Craiova
Giurgiu

Answer: No
Greedy Search...
• m: maximum depth of the search space
Complete No
Time 𝑂𝑂(𝑏𝑏𝑚𝑚 )
Space 𝑂𝑂(𝑏𝑏𝑚𝑚 )(keeps all nodes in memory)
Optimal No
Question to think:

• Is it possible to combine functions g(n) and h(n) in one


search strategy?
A * Search
• Uniform-cost search
• g(n): cost to reach n (Past Experience)
• optimal and complete, but can be very
inefficient
• Greedy search
• h(n): cost from n to goal (Future Prediction)
• neither optimal nor complete, but cuts search
space considerably
A * Search
Idea: Combine Greedy search with Uniform-Cost search
Evaluation function: 𝑓𝑓 𝑛𝑛 = 𝑔𝑔 𝑛𝑛 + ℎ(𝑛𝑛)

• f (n): estimated total cost of path through n to goal (Whole Life)


• If g = 0  greedy search; If h = 0  uniform-cost search
• Function A* Search(problem) returns solution
• Return Best-First-Search(problem, 𝑔𝑔 + ℎ)
Example: Route-finding from Arad to Bucharest
Best-first-search with evaluation function 𝑔𝑔 + ℎ

(a) The initial state (b) After expanding Arad (c) After expanding Sibiu
Arad Arad Arad
366 = 0 + 366
Sibiu Zerind Sibiu Zerind
Timisoara
393 = 140 + 253 449 = 75 + 374 449 = 75 + 374
447 = 118 + 329
Timisoara Arad
Oradea
447 = 118 + 329 646 = 280 + 366 Rimnicu
671 = 291+ 380
Vilcea
Fagaras
413 = 220 + 193
415 = 239 + 176
Example: Route-finding from Arad to Bucharest
Best-first-search with evaluation function 𝑔𝑔 + ℎ

(d) After expanding


Rimnicu Vilcea Arad

Zerind
Sibiu
Timisoara 449 = 75 + 374
Arad 447 = 118 + 329
646 = 280 + 366 Oradea Rimnicu
Fagaras 671 = 291+ 380 Vilcea

415 = 239 + 176 Sibiu


Craiova Pitesti 553 = 300 + 253
526 = 360 + 166
415 = 317 + 98
Example: Route-finding from Arad to Bucharest
Best-first-search with evaluation function 𝑔𝑔 + ℎ
Arad
(e) After expanding
Fagaras Zerind
Sibiu
Timisoara 449 = 75 + 374
Arad 447 = 118 + 329
646 = 280 + 366 Oradea Rimnicu
Fagaras 671 = 291+ 380 Vilcea

Sibiu
Sibiu Craiova Pitesti 553 = 300 + 253
Bucharest
591 = 338 + 253 526 = 360 + 166
415 = 317 + 98
450 = 450 + 0
Example: Route-finding from Arad to Bucharest
Best-first-search with evaluation function 𝑔𝑔 + ℎ
Arad

(f) After expanding Zerind


Sibiu
Pitesti Timisoara 449 = 75 + 374
Arad 447 = 118 + 329
646 = 280 + 366 Oradea Rimnicu
Fagaras 671 = 291+ 380 Vilcea

Sibiu
Sibiu Craiova Pitesti 553 = 300 + 253
Bucharest
591 = 338 + 253 526 = 360 + 166
450 = 450 + 0
Rimnicu
Bucharest Craiova Vilcea

418 = 418 + 0 615 = 455 + 160 607 = 414 + 193


Example: Route-finding in Manhattan

53𝑛𝑛𝑛𝑛 St 3 2

52𝑛𝑛𝑛𝑛 St 2 1

51𝑠𝑠𝑠𝑠 St 5 4 3 2 1
S G

50𝑡𝑡𝑡 St 7 6 5 4 3 2 1
10𝑡𝑡𝑡 Ave

6𝑡𝑡𝑡 Ave

5𝑡𝑡𝑡 Ave
9𝑡𝑡𝑡 Ave

8𝑡𝑡𝑡 Ave

4𝑡𝑡𝑡 Ave

2𝑛𝑛𝑛𝑛 Ave
3𝑟𝑟𝑟𝑟 Ave
Manhattan

7𝑡𝑡𝑡 Ave
Shortest
Path = 8
Distance
Heuristic
Example: Route-finding in Manhattan (Greedy)

53𝑛𝑛𝑛𝑛 St 3 2

52𝑛𝑛𝑛𝑛 St 2 1

51𝑠𝑠𝑠𝑠 St 5 4 3 2 1
S G

50𝑡𝑡𝑡 St 7 6 5 4 3 2 1
10𝑡𝑡𝑡 Ave

6𝑡𝑡𝑡 Ave

5𝑡𝑡𝑡 Ave
9𝑡𝑡𝑡 Ave

8𝑡𝑡𝑡 Ave

4𝑡𝑡𝑡 Ave

2𝑛𝑛𝑛𝑛 Ave
3𝑟𝑟𝑟𝑟 Ave
7𝑡𝑡𝑡 Ave
Example: Route-finding in Manhattan (UCS)

53𝑛𝑛𝑛𝑛 St 7 8

52𝑛𝑛𝑛𝑛 St 6 9

51𝑠𝑠𝑠𝑠 St 1 2 3 4 5
S G

50𝑡𝑡𝑡 St 1 2 3 4 5 6 7
10𝑡𝑡𝑡 Ave

6𝑡𝑡𝑡 Ave

5𝑡𝑡𝑡 Ave
9𝑡𝑡𝑡 Ave

8𝑡𝑡𝑡 Ave

4𝑡𝑡𝑡 Ave

2𝑛𝑛𝑛𝑛 Ave
3𝑟𝑟𝑟𝑟 Ave
7𝑡𝑡𝑡 Ave
Example: Route-finding in Manhattan (A*)

53𝑛𝑛𝑛𝑛 St 10 10

52𝑛𝑛𝑛𝑛 St 8 10

51𝑠𝑠𝑠𝑠 St 6 6 6 6 6
S G

50𝑡𝑡𝑡 St 8 8 8 8 8 8 8
10𝑡𝑡𝑡 Ave

6𝑡𝑡𝑡 Ave

5𝑡𝑡𝑡 Ave
9𝑡𝑡𝑡 Ave

8𝑡𝑡𝑡 Ave

4𝑡𝑡𝑡 Ave

2𝑛𝑛𝑛𝑛 Ave
3𝑟𝑟𝑟𝑟 Ave
7𝑡𝑡𝑡 Ave
Complexity of A*
Time Exponential in length of
solution
Space (all generated nodes
are kept in memory)
Exponential in length of
solution

With a good heuristic, significant


savings are still possible
compared to uninformed search
methods

You might also like