Computer Science CPSC 322
Le Lecture ture 4
Un Uninf nform
- rmed
ed Se Sear arch ch St Strat ategies, egies, Se Sear arch h wi with h Co Costs ts (Ch Ch 3. 3.1-3.5, 3.5, 3. 3.7. 7.3) 3)
1
Le Lecture ture 4 Un Uninf nform ormed ed Se Sear arch ch - - PowerPoint PPT Presentation
Computer Science CPSC 322 Le Lecture ture 4 Un Uninf nform ormed ed Se Sear arch ch St Strat ategies, egies, Se Sear arch h wi with h Co Costs ts (Ch Ch 3. 3.1-3.5, 3.5, 3. 3.7. 7.3) 3) 1 Announcements Assignment 1
Computer Science CPSC 322
1
Announcements
free to check corresponding sections on the new edition
2
Today’s Lecture
3
Input ut: a graph a set of start nodes Boolean procedure goal(n) that tests if n is a goal node frontier:= [<g>: g is a goal node]; While ile frontier is not empty: selec lect and remov emove e path <no,….,nk> from frontier; If If goal(nk) return urn <no,….,nk>; Fin ind a neighbor n of nk add add n to frontier; end end
Bog
us Ver ersi sion
ener eric ic Sea earch ch Algo gorithm thm
4
Input ut: a graph a set of start nodes Boolean procedure goal(n) that tests if n is a goal node frontier:= [<g>: g is a goal node]; While ile frontier is not empty: selec lect and remov emove e path <no,….,nk> from frontier; If If goal(nk) return urn <no,….,nk>; Fin ind a neighbor n of nk add add n to frontier; end end
Bog
us Ver ersi sion
ener eric ic Sea earch ch Algo gorithm thm
5
Slide 6
Com
paring ring Sea earch ching ng Algo gorithms: thms: Will it fi t find nd a s a sol
ution
? th the bes e best t on
e?
whenever there is at least one solution, the algorithm is is guar arant ntee eed d to fin ind it it within a finite amount of time. Def.: A search algorithm is optimal imal if when it finds a solution, it is the best st one Def.: The tim ime complexity
the worst rst- case se amount of time it will take to run, expressed in terms of
Def.: The space ace complex
ity of a search algorithm is the worst rst-case case amount of memory that the algorithm will use (i.e., the maximum number of nodes on the frontier), also expressed in terms of m and b.
Today’s Lecture
7
Il Illu lustrati strative ve Gr Graph: ph: DFS FS
DFS explores each path on the frontier until its end (or until a goal is found) before considering any other path.
Shaded nodes represent the end of paths on the frontier
8
Input ut: : a graph a set of start nodes Boolean procedure goal(n) testing if n is a goal node frontier:= [<s>: s is a start node]; While le frontier is not empty: select lect and remove e path <no,….,nk> from frontier; If If goal(nk) return <no,….,nk>; For ever ery neighbor n of nk, add add <no,….,nk, n> to frontier; end end
thm
9
last-in-first-out stack Let’s see how this works in
Slide 10
Sample Problem (good idea to start with the “Simple Tree” problem)
in the toolbar is set to “Depth-First Search”.
“Step” buttons in the toolbar
happening during each step
See ava vaila ilable ble help lp pages ges and vid ideo eo tutor
ials ls for mor
e details ails on how w to use e the Sear arch ch applet plet (http: ttp://ww www. w.ais aispace.o ace.org/se g/search arch/index. index.sh shtml ml)
NOTE: p2 is only selected when all paths extending p1 have been explored.
Slide 11
Example:
the Search Applet in AI Space
Analysis lysis of f DFS FS
12
whenever there is at least one solution, the algorithm is guaranteed to find it within a finite amount of time. Is DFS complete?
Analysis lysis of f DFS FS
Is DFS complete?
No
adding a cycle to “Simple Tree”
“Solve” and see what happens
13
whenever there is at least one solution, the algorithm is guaranteed to find it within a finite amount of time.
Analysis lysis of f DFS FS
14
Is DFS optimal? Def.: A search algorithm is optimal if when it finds a solution, it is the best one (e.g., the shortest)
15
Analysis lysis of f DFS FS
16
Is DFS optimal?
A Yes B No
Def.: A search algorithm is optimal if when it finds a solution, it is the best one (e.g., the shortest)
Analysis lysis of f DFS FS
17
Is DFS optimal?
No
paths before it gets to shorter ones.
Def.: A search algorithm is optimal if when it finds a solution, it is the best one (e.g., the shortest)
Analysis lysis of f DFS FS
18
Def.: The space complexity of a search algorithm is the worst-case amount of memory that the algorithm will use (i.e., the maximal number of nodes on the frontier), expressed in terms of
b ?
See how this works in
Analysis lysis of f DFS FS
O(bm)
terms of m and b ?
DFS maintains a path to its unexplored siblings in the search tree
maximum of b-1 alterative paths per node along the path
19
Def.: The space complexity of a search algorithm is the worst-case amount of memory that the algorithm will use (i.e., the maximal number of nodes on the frontier), expressed in terms of
Analysis lysis of f DFS FS
21
Def.: The time complexity of a search algorithm is the worst-case amount of time it will take to run, expressed in terms of
Analysis lysis of f DFS FS
22
search tree m steps away from the start
Def.: The time complexity of a search algorithm is the worst-case amount of time it will take to run, expressed in terms of
D. O(b+m) A. O(bm) C. O(bm) B. O(mb)
23
Analysis lysis of f DFS FS
24
every node in the tree
Def.: The time complexity of a search algorithm is the worst-case amount of time it will take to run, expressed in terms of
O(bm)
To To Summarize marize
Complete Optimal Time Space DFS NO NO O(bm) O(bm)
25
Slide 26
Analysis is of DFS: Summary
is complete for finite acyclic graphs.
shorter ones.
length is m and the maximum branching factor is b ?
goal.
bm)
maintain a fringe of size b.
DFS is appropriate when
It is a poor method when
Analysi alysis s of f DFS FS (cont.) nt.)
27
Slide 28
Why hy DFS S ne need ed t to be
udied ed an and d un unde dersto stood
aspects of searching
and useful search algorithms (e.g. Iterative Deepening)
Today’s Lecture
29
Breadth eadth-first first search arch (BFS) FS)
before looking at path of length l + 1
30
Input: ut: a graph a set of start nodes Boolean procedure goal(n) testing if n is a goal node frontier:= [<s>: s is a start node]; While frontier is not empty: select lect and remove ve path <no,….,nk> from frontier; If If goal(nk) return urn <no,….,nk>; Else For r every y neighbor n of nk, add add <no,….,nk, n> to frontier; end end
thm
In BFS, the frontier is a first-in-first-out queue Let’s see how this works in AIspace
in the Search Applet toolbar , set “Search Options -> Search Algorithms” to “Breadth-First Search”.
31
Slide 32
Breadth eadth-first first Search: arch: BFS FS
Example:
As for DFS, you can get a much better sense of how BFS works by looking at the Search Applet in AI Space
Analysis lysis of f BFS FS
33
whenever there is at least one solution, the algorithm is guaranteed to find it within a finite amount of time.
Is BFS complete?
Analysis lysis of f BFS FS
34
Is BFS complete? Yes
will be explored before any other path of length l + 1
Graph Examples” or by adding a cycle to “Simple Tree”
whenever there is at least one solution, the algorithm is guaranteed to find it within a finite amount of time.
Analysis lysis of f BFS FS
35
Is BFS optimal? Def.: A search algorithm is optimal if when it finds a solution, it is the best one
boxes
Analysis lysis of f BFS FS
36
Is BFS optimal?
Yes
Def.: A search algorithm is optimal if when it finds a solution, it is the best one
boxes
box N7) will be reached before goals at lower levels
Analysis lysis of f BFS FS
37
Def.: The time complexity of a search algorithm is the worst-case amount of time it will take to run, expressed in terms of
38
Analysis lysis of f BFS FS
39
Def.: The time complexity of a search algorithm is the worst-case amount of time it will take to run, expressed in terms of
O(bm)
must examine every node in the tree
Analysis lysis of f BFS FS
40
Def.: The space complexity of a search algorithm is the worst case amount of memory that the algorithm will use (i.e., the maximal number of nodes on the frontier), expressed in terms of
Analysis lysis of f BFS FS
41
Def.: The space complexity of a search algorithm is the worst case amount of memory that the algorithm will use (i.e., the maximal number of nodes on the frontier), expressed in terms of
O(bm)
terms of m and b ?
level m
Wh When en to to use e BFS FS vs. . DFS FS?
42
Wh When en to to use e BFS FS vs. . DFS FS?
43
DFS BFS BFS BFS DFS
To To Summarize marize
How can we achieve an acceptable (linear) space complexity while maintaining completeness and optimality? Key Idea: a: re-compute elements of the frontier rather than saving them. Complete Optimal Time Space DFS NO NO O(bm) O(bm) BFS YES YES O(bm) O(bm)
44
depth = 1 depth = 2 depth = 3
Iterative Deepening DFS (IDS) in a Nutshell
– For depth D, ignore any paths with longer length – Depth-bounded depth-first search
If no goal l re-star art t from scratc atch h and get to depth h 2 If no goal l re-star art t from scratc atch h and d get to depth th 3 If no goal l re-star art t from scratc atch h and d get to depth th 4
(Ti Time) e) Complexity mplexity of f ID IDS
Depth Total al # of paths hs at that t level #time mes s created ted by BFS (or DFS) #time mes s created ted by IDS Total al #paths hs for IDS 1 2 . . . . . . . . . . . . . . . m-1 m
(Ti Time) e) Complexity mplexity of f ID IDS
Depth Total al # of paths hs at that t level #time mes s created ted by BFS (or DFS) #time mes s created ted by IDS Total al #paths hs for IDS 1 b 1 2 . . . m-1 m
47
m mb
(Ti Time) e) Complexity mplexity of f ID IDS
Depth Total al # of paths hs at that t level #time mes s created ted by BFS (or DFS) #time mes s created ted by IDS Total al #paths hs for IDS 1 b 1 2 b2 1 . . . m-1 m
48
m m-1 mb (m-1) b2
(Ti Time) e) Complexity mplexity of f ID IDS
Depth Total al # of paths hs at that t level #time mes s created ted by BFS (or DFS) #time mes s created ted by IDS Total al #paths hs for IDS 1 b 1 2 b2 1 . . . . . . . . . . . . . . . m-1 bm-1 1 m bm 1
49
m m-1 2 1 mb (m-1) b2 2 bm-1 bm
Solution at depth m, branching factor b Total # of paths generated: bm + 2 bm-1 + 3 bm-2 + ...+ mb = bm (1 b0 + 2 b-1 + 3 b-2 + ...+ m b1-m )
) (
1 1
m i i m
ib b
(Ti Time) e) Complexity mplexity of f ID IDS
2
1 b b bm
) (
1 ) 1 (
i i m
ib b
converges to
Overhead factor
more nodes)
take only 2 times as much as BFS
) (
m
b O
Fu Furthe her r An Analys ysis is of Itera rative tive Deepenin ning DFS FS ( (IDS) S)
51
O(mb)
Fu Furthe her r An Analys ysis is of Itera rative tive Deepenin ning DFS FS ( (IDS) S)
– cannot get trapped in infinite cycles, gets to a solution first
52
Yes Yes O(mb)
Summary mary of f Uninformed informed Search arch
Complete Optimal Time Space DFS N N O(bm) O(mb) BFS Y Y (shortest) O(bm) O(bm) IDS Y Y (shortest) O(bm) O(mb) LCFS
Slide 53
Learning Goals for today’s class
specific problems.
algorithms
55