Lec 2-Uninformed Searches - Part 2
Lec 2-Uninformed Searches - Part 2
INTELLIGENCE
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
function UNIFORM-COST-SEARCH(problem) returns a solution, or failure
initialize the explored set to be empty
initialize the frontier as a priority queue using node path_cost as the priority
add initial state of problem to frontier with path_cost = 0
2 C
loop do G
A 4
if the frontier is empty then 1
3
return failure S B D
4 1
choose a node and remove it from the frontier
if the node contains a goal state then
return the corresponding solution
add the node state to the explored set
for each resulting child from node
if the child state is not already in the frontier or explored set then
add child to the frontier
else if the child is already in the frontier with higher path_cost then
replace that frontier node with child
Uniform Cost Search (UCS)
• Extension of BFS:
• Expand node with lowest path cost
• Implementation: fringe = priority queue
ordered by path cost.
• UCS is the same as BFS when all step-costs are
equal.
• UCS does not care about the number of steps a path has, but
only about their total cost
Uniform Cost Search (UCS)
generalSearch(problem, priorityQueue)
# of nodes tested: 0, expanded: 0
S
start
expnd. Frontier list
node 5 2 4
{S}
A B C
9 4 6 2
6 G 1
D E goal F
H
Uniform Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 1, expanded: 1 start
expnd. Frontier list
node 5 2 4
{S:0}
A B C
S not goal {B:2,C:4,A:5}
9 4 6 2
6 G 1
D E goal F
H
Uniform Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 2, expanded: 2 start
expnd. Frontier list
node 5 2 4
{S}
A B C
S {B:2,C:4,A:5}
B not goal {C:4,A:5,G:2+6} 9 4 6 2
6 G 1
D E goal F
H
Uniform Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 3, expanded: 3 start
expnd. Frontier list
node 5 2 4
{S}
A B C
S {B:2,C:4,A:5}
B {C:4,A:5,G:8} 9 4 6 2
C not goal {A:5,F:4+2,G:8}
6 G 1
D E goal F
H
Uniform Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 4, expanded: 4 start
expnd. Frontier list
node 5 2 4
{S}
A B C
S {B:2,C:4,A:5}
B {C:4,A:5,G:8} 9 4 6 2
C {A:5,F:6,G:8}
6 G 1
A not goal {F:6,G:8,E:5+4, D E goal F
D:5+9}
7
H
Uniform Cost Search (UCS)
generalSearch(problem, priorityQueue) S
start
# of nodes tested: 5, expanded: 5
expnd. Frontier list 5 2 4
node
{S} A B C
S {B:2,C:4,A:5}
B {C:4,A:5,G:8} 9 4 6 2
C {A:5,F:6,G:8} 6 G 1
D E goal F
A {F:6,G:8,E:9,D:14
}
7
F not goal {G:4+2+1,G:8,E:9
,D:14} H
Uniform Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 6, expanded: 5 start
expnd. node Frontier list
5 2 4
{S}
A B C
S {B:2,C:4,A:5}
B {C:4,A:5,G:8} 9 4 6 2
C {A:5,F:6,G:8}
6 G 1
A {F:6,G:8,E:9,D:14} D E goal F
F {G:7,G:8,E:9,D:14} 7
G goal {G:8,E:9,D:14} H
no expand
Uniform Cost Search (UCS)
generalSearch(problem, priorityQueue)
S
# of nodes tested: 6, expanded: 5 start
expnd. Frontier list
node 5 2 4
{S}
A B C
S {B:2,C:4,A:5}
B {C:4,A:5,G:8} 9 4 6 2
C {A:5,F:6,G:8}
6 G 1
A {F:6,G:8,E:9,D:14} D E goal F
F {G:7,G:8,E:9,D:14}
G {G:8,E:9,D:14} 7
path: S,C,F,G
H cost: 7
Uniform Cost Search (UCS)
Properties
• What nodes does UCS expand?
b c1
• Processes all nodes with cost less than cheapest solution! …
• If that solution costs C* and minimum arcs cost at least , c2
C*/
then the “effective depth” is roughly C*/
“tiers” c3
• Takes time O(b C*/
) (exponential in effective depth)
• 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*)
Uniform Cost Issues
… c1
• Remember: UCS explores increasing c2
cost contours c3
• The bad:
• Explores options in every “direction” Start Goal
• No information about goal location
• Is it optimal?
• Yes! (Proof next lecture via A*)
Uniform Cost Issues
• Remember: … c1
c2
UCS explores increasing cost contours
c3
• The good:
UCS is complete and optimal!
• The bad:
• Explores options in every “direction”
• No information about goal location Start Goal
• IDS is a general strategy often used with depth first search, that finds
the best depth limit.
• Gradually increases the limit, first 0, then 1, then 2 and so on until a
goal is found.
• This occurs when the depth limit reaches d, the depth of shallowest
goal node.
Iterative-Deepening Search (IDS)
In general, iterative deepening is the preferred uninformed search method when there is a
large search space and the depth of the solution is not known.
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes tested: 1, expanded: 1 start
expnd. Frontier
node 5 2 4
{S}
S not goal {A,B,C} A B C
9 4 6 2
6 G 1
D E goal F
H
22
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes tested: 2, expanded: 1 start
expnd. Frontier
node 5 2 4
{S}
S {A,B,C} A B C
A not goal {B,C} no expand
9 4 6 2
6 G 1
D E goal F
H
23
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes tested: 3, expanded: 1 start
expnd. Frontier
node 5 2 4
{S}
S {A,B,C} A B C
A {B,C}
9 4 6 2
B not goal {C} no expand
6 G 1
D E goal F
H
24
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 1, # of nodes tested: 4, expanded: 1 start
expnd. Frontier
node 5 2 4
{S}
S {A,B,C} A B C
A {B,C}
9 4 6 2
B {C}
C not goal { } no expand- 6 G 1
D E goal F
FAIL
7
H
25
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 4, expanded: 2 start
expnd. Frontier
node 5 2 4
{S}
S {A,B,C} A B C
A {B,C}
9 4 6 2
B {C}
C {} 6 G 1
D E goal F
S no test {A,B,C}
7
H
26
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 4, expanded: 3 start
expnd. Frontier
node 5 2 4
{S}
S {A,B,C} A B C
A {B,C}
9 4 6 2
B {C}
C {} 6 G 1
D E goal F
S {A,B,C}
A no test {D,E,B,C} 7
H
27
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 5, expanded: 3 start
expnd. Frontier
node 5 2 4
{S}
S {A,B,C} A B C
A {B,C}
9 4 6 2
B {C}
C {} 6 G 1
D E goal F
S {A,B,C}
A {D,E,B,C} 7
D not goal {E,B,C} no
expand H
28
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
S
depth: 2, # of nodes tested: 6, expanded: 3 start
expnd. Frontier
node 5 2 4
{S}
S {A,B,C} A B C
A {B,C}
9 4 6 2
B {C}
C {} 6 G 1
D E goal F
S {A,B,C}
A {D,E,B,C} 7
D {E,B,C}
E not goal {B,C} no expand H
29
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
expnd. Frontier S
node start
{S} 5 2 4
S {A,B,C}
A {B,C} A B C
B {C}
C {} 9 4 6 2
S {A,B,C} 6 G 1
D E goal F
A {D,E,B,C}
D {E,B,C}
7
E {B,C}
B no test {G,C} H
30
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
depth: 2, # of nodes tested: 7, expanded: 4
expnd. Frontier
S
node start
{S}
S {A,B,C} 5 2 4
A {B,C}
A B C
B {C}
C {} 9 4 6 2
S {A,B,C}
6 G 1
A {D,E,B,C} D E goal F
D {E,B,C}
E {B,C} 7
B {G,C}
G goal {C} no expand H
31
Iterative-Deepening Search (IDS)
deepeningSearch(problem)
depth: 2, # of nodes tested: 7, expanded: 4
expnd. Frontier S
node start
{S} 5 2 4
S {A,B,C}
A {B,C} A B C
B {C}
C {} 9 4 6 2
S {A,B,C} 6 G 1
A {D,E,B,C} D E goal F
D {E,B,C}
7
E {B,C}
path: S,B,G
B {G,C} H cost: 8
G {C} 32
Iterative Deepening
• Idea: get DFS’s space advantage with BFS’s
b
time / shallow-solution advantages …
• Run a DFS with depth limit 1. If no solution…
• Run a DFS with depth limit 2. If no solution…
• Run a DFS with depth limit 3. …..
• Complete? Yes
• Space? O(bd)
34
• Notes added on LMS
• https://www.oreilly.com/library/view/graph-algorithms/
9781492047674/ch04.html
Acknowledgment
36