Vous êtes sur la page 1sur 43

Topic 2 - Search

Prof. Asoka S Karunananda


Faculty of Computing
KDU

1
Introduction

Problem solving
Search an approach to problem solving
Uninformed search
Informed search
Local Search

2
Problem solving

Problem solving as a process of transforming


a set of states through actions/operations
Components of a problem
States
Actions/operations/successor function
Goal test
Path cost

3
State space

State space consists of all possible states


together with actions
States are represented by nodes, while
actions are by arcs
State space can be a tree or a graph
A path from initial state to goal state in the
state space is called a solution

4
Examples state space

Robot navigation
Water jug problem
Farmer, wolf, goat and cabbage
8-puzzel

5
Search - real world applications

Route finding problem


Traveling salesman problem
VLSI layout design
Automatic assembling

6
Search algorithms

A search algorithm defines how to find a path from


initial state to goal state
All algorithms tell how to move from the current
node to next node
Some algorithms provide just systematic ways to
explore the state space, while others tell how to
explore effectively
In implementing search, it is easy to handle two list
as
OPEN node to be visited (fringe)
CLOSED node already visited

7
Evaluating Search algorithms

Completeness ability to find a solution


when there is a solution
Optimality ability to find the highest quality
solution
Time complexity how long does it take
Space complexity how much memory is
required

8
Search strategies

Uninformed search (blind search)


Systematic way to explore a state space
No information (uninformed) to explore effectively
There are six algorithms
Informed search (heuristic search)
Preference of the use of operators
Best-first search

9
Uninformed search

Breadth-first
Uniform cost
Depth-first
Depth limited
Iterative deepening
Bidirectional

10
Breadth-first

Explore the state space layer by layer


Algorithm using OPEN and CLOSE lists
Example

11
Breadth-first search algorithms

Start
Put S in OPEN
If OPEN empty fails
Otherwise Remove first node from OPEN and
put it in CLOSE (say n)
Expand n, put children at the end of OPEN
If any child = goal then success
Otherwise step 3

12
Features-BFS

Find the shallowest goal first


Its Optimal and Complete

13
Features BFS contd..

Space complicity
Consider a tree of branching factor b
Nodes at level 1 = b, level 2 = b2
If the goal is at depth d
Expand all nodes, except one containing the goal
Thus total no. of nodes stored in memory when goal is
at the level d
b + b2 + b3 + bd + (bd+1 b) = O(bd+1)
Time complexity - O(bd)
Contd

14
Features BFS contd..

Take b=10, 10,000 nodes/second, 1000


bytes/node
Depth Nodes Time Memory
2 1100 .11seconds 1MB
4 111,100 11seconds 106MB
6 107 19min 10GB
8 109 31hours 1TB
12 1013 35years 10petabytes

15
Features BFS contd..

Memory requirement is a bigger problem than


time
E.g. for depth 8, 31 hours is not too big yet 1
terabytes is a huge memory requirement
Time requirement is still important
E.g. For depth 12, 35 years

16
Uniform Cost Search

Version of BFS
Expand the lowest cost node rather than
lowest-depth node on the fringe
Note that each time total cost must be
considered
Example

17
Features -UCS

All values of cost must be positive otherwise,


we might come back
Zero cost leads to create a loop
As such path cost increase as we go down
UCS is also
Complete
guarantee to find the minimum cost pat
Space complexity - O(bd+1)
Time complexity - O(bd)

18
Depth-first search

Explore a branch deep in


Algorithm using OPEN and CLOSE
Example

19
Depth-first search algorithm

Start
Put S in OPEN
If OPEN empty fails
Otherwise Remove first node from OPEN and put it
in CLOSE (say n)
Expand n, put children at the front of OPEN
If any child = goal then success
Otherwise step 3

20
Features of DFS

Not complete
Not optimal
Space complexity O(bd)
Modest memory requirement
Consider a tree with branching factor 3
Time complexity O(bd)

21
Depth limited search

Depth-first search with predetermined depth


limit, say l
Neither complete nor optimal unless by
chance

22
Iterative deepening depth-first search

Increase the depth limit iteratively until we


find a goal node
It brings advantages of DFS and BFS
So memory requirement is modest as DFS
Complete and optimal as BFS
It may be wasteful, due to exploring of each
level (except the final level) more than once?
Yet not a big issue

23
Comparison of IDS with BFS

Total nodes generated when the goal is at level d


Using IDS, N(IDS) = (d)b+ (d-1)b2 + ..+ (1) bd
Using BFS,
N(BFS) = b + b2 + b3 + bd + (bd+1 b)
For b=10 and d=5,
N(IDS)=123,450 and N(BFS)=1,111,100
So in general IDS is still better than BFS, especially
for large search space with unknown depth for a
solution

24
Comparison
Criteria BFS UCS DFS DLS IDS

Complete? Yes Yes No No Yes

Optimal Yes Yes No No Yes

Time O(bd+1) O(bd) O(bm) O(bl) O(bd)

Space O(bd+1) O(bd+1) O(bm) O(bl) O(bm)

25
Informed Search

Informed search is also called heuristic search


General approach is best-first search
Node is selected for expansion on the basis of
evaluation function f(n).
Essential component of evaluation function is a
heuristic function h(n)
h(n) = estimated cost of the cheapest path from node
n to a goal node
If n is the goal node, then h(n) =0

26
Greedy best-first search

Expand the node that is closest to the goal


Thus f(n)=h(n)
In a route finding problem h(n) can be the
straight-line distance heuristic.
Consider the following route finding problem

27
Map
Oradea
71 Neamt
Zerind Iasi
151
87 92
Arad 75 Sibiu Fagaras
140 Vaslui

118 99 142
Timisora 80 211 Urziceni
111 Rimnicu
98 Hirsova
Petesti
Lugoj 97 85
70
101 Bucharest
Oradea
90
75 146 138
Dobreta
120 Craiova

28
Straight-line distances to Bucharest

Arad 366, Bucharest-0, Craiova-160


Dobreta-242, Fagaras-176, Giurgui-77
Iasi-226, Neamt-234, Oradea-380
Petesti-100, Rimnicu-193
Sibui-253, Timosoara-329, Zerind-374

29
Example Greedy search

(a) The initial state Arad

366

(b) After expanding Arad

Arad

Sibiu Timisoara Zerind

253 329 374

30
Greedy search- Contd
(c) After expanding Sibiu

Arad

Sibiu
Timisoara Zerind

329 374

Arad Fagaras Orade Rimnicu


a Vilcea
366 176 380 193

31
Greedy search- Contd
(d) After expanding Fagaras

Arad

Sibi
u Timisoara Zerin
d
329 374
Arad Fagara Orade Rimnicu
s a Vilcea
366 380
193
Sibiu Buchares
t
253 0
32
Comments on Greedy search

Greedy best-first serach is not optimal, path


via sibiu, Fagaras to Bucharest is 32Km
longer than Path through Rimnica and Pitesti.
Further problems:
Consider getting from Iasi to Fagaras
Heuristic suggest that Neamt be expanded first,
since it is the closet to Fagaras
But Neamt is a dead end

33
A* Search

f(n) = g(n) + h(n)


f(n) estimated cost of the cheapest solution
through n
g(n) cost to reach to the node
h(n) cost to get from the node to the goal
This is in fact a combination Greedy search
(due to h(n)), and uniform cost search (due to
g(n)).

34
Example - A*
(a) The initial state
Arad

366=0+366

(b) After expanding Arad

Arad

Sibiu
Timisoara Zerind
393=140+253
447=118+329 449=75+374

35
A*- Contd

(c) After expanding Sibiu


Arad

Sibi
u Timisoara Zerind

449=75+374

Arad Fagaras Rimnicu


Orade
Vilcea
415=239+176 a
646=280+366 413=220+193
671=291+380

36
A*- Contd
(d) After expanding Rimnicu Vilcea

Sibiu

Arad Fagaras Oradea Rimnicu


Vilcea
646=280+366 415=239+176 671=291+380

Craiova Sibiu
Pitesti
526=366+160 553=300+253
417=317+100

37
A*- Contd
(d) After expanding Fagaras

Sibiu

Arad Fagara Oradea Rimnicu


s Vilcea
646=280+366 671=291+380

Sibiu Pitesti Sibiu


Bucharest Craiov
a
591=338+253 526=366+160 553=300+253
450=450+0 417=317+100
38
A*- Contd
(f) After expanding Pitesti
Sibiu

Arad Fagaras Oradea Rimnicu


646=280+366 671=291+380 Vilcea

Sibiu Bucharest Craiova Pitesti Sibiu


591=338+253 526=366+160 553=300+253
450=450+0
Rimnicu
Bucharest Craiova
Vilcea
418=418+0 607=414+193
615=455+160
39
Comments on A*

Note how A* find the path through Rimnica


and Pitesti.
So A* is complete and optimal
Note: A* can be improved with iterative
deepening search, in which depth limit is the
f-cost

40
Local Search algorithms

Some problems do not need the path, yet


whether the searching process has reached
to a goal node
Example: Most games, optimization,
scheduling and design problems
Searching for such solutions, we just find the
best node in the vicinity (local search), but
not a global solution
Hill climbing is an example for such search

41
Adversarial Search

Searching in competitive environments


Example: Game playing
While reaching to a goal, block the chance of
opponent reaching to a goal
Minimax algorithm

42
Minimax Search

Max A 3

Min B B 2 B 2
3

A A A
A A A A A A
14 5 2
3 12 3 2 4 6

43

Vous aimerez peut-être aussi