Professional Documents
Culture Documents
Search 1
Search 2
Odarea
Neamt
Iasi
Zerind
Fararas Vaslui
Arad Sibiu
Mehadia Bucharest
Eforie
Dobreta
Craiova Giurgui
Search 3
Searching tree
◆ The initial state becomes the root node of the tree
S
3
1 5 1
A C B
4 2 2
1 1 1 2 3 3 2
D C D G E C E
4
3 2 3 0 1 2 1
3 1 2 3 3 4 1 2 3 4
G D G E G G D G E G
0 3 0 1 0 0 3 0 1 0
3 4 3 4
G G G G
0 0 0 0
Search 4
General Tree Search Algorithm
function TREE-SEARCH(problem, fringe) returns solution
fringe := INSERT(MAKE-NODE(INITIAL-STATE[problem]), fringe)
loop do
if EMPTY?(fringe) then return failure
node := REMOVE-FIRST(fringe)
if GOAL-TEST[problem] applied to STATE[node] succeeds
then return SOLUTION(node)
fringe := INSERT-ALL(EXPAND(node, problem), fringe)
Search 8
1. Breadth First Search -
Implementation A
B C
C D E
C
D
A C
B D
E E
F G D E F G
E F G
F G
G
Stack trace
A A A A
A
B C B C B C
B C
D E D E
D E D E FF G
G
Search 9
1. Breadth-First
◆ all
the nodes reachable from the current node are
explored first
◆ achieved by the TREE-SEARCH method by appending
newly generated nodes at the end of the search queue
Fringe: [] + [2,3]
Breadth-First Snapshot 2
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5
8 9
8 9 10 11
8 9 10 11 12 13
8 9 10 11 12 13 14 15
8 9 10 11 12 13 14 15
16 17
8 9 10 11 12 13 14 15
16 17 18 19
8 9 10 11 12 13 14 15
16 17 18 19 20 21
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
Fringe: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21] + [22,23]
Breadth-First Snapshot 12
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
Note:
The goal node
8 9 10 11 12 13 14 15 is “visible”
here, but we can
not perform the
goal test yet.
16 17 18 19 20 21 22 23 24 25
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
Breadth-First Snapshot 17
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [18,19,20,21,22,23,24,25,26,27,28,29,30,31]
Breadth-First Snapshot 18
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [19,20,21,22,23,24,25,26,27,28,29,30,31]
Breadth-First Snapshot 19
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [20,21,22,23,24,25,26,27,28,29,30,31]
Breadth-First Snapshot 20
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [21,22,23,24,25,26,27,28,29,30,31]
Breadth-First Snapshot 21
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [22,23,24,25,26,27,28,29,30,31]
Breadth-First Snapshot 22
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [23,24,25,26,27,28,29,30,31]
Breadth-First Snapshot 23
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [24,25,26,27,28,29,30,31]
Breadth-First Snapshot 24
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
Note:
The goal test is
8 9 10 11 12 13 14 15 positive for this
node, and a
solution is
found in 24
steps.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [25,26,27,28,29,30,31]
Evaluating Breadth First Search
◆ Observations
◆ Very systematic
Search 35
Evaluating Breadth First Search
◆ Evaluating against four criteria
◆ Complete? : Yes
◆ Optimal? : Yes
◆ Space Complexity : 1 + b + b2 + b3 + ... + bd i.e
O(bd)
◆ Time Complexity : 1 + b + b2 + b3 + ... + bd i.e. O(bd)
Search 36
Exponential Growth
◆ Exponential growth quickly makes complete
state space searches unrealistic
Search 37
Exponential Growth
Depth Nodes Time Memory
0 1 1 millisecond 100 kbytes
2 111 0.1 second 11 kilobytes
4 11,111 11 seconds 1 megabyte
6 106 18 minutes 111 megabytes
8 108 31 hours 11 gigabytes
10 1010 128 days 1 terabyte
12 1012 35 years 111 terabytes
14 1014 3500 years 11,111 terabytes
Search 38
Exponential Growth - Observations
◆ Space is more of a factor to breadth first search than time
Search 39
2. Uniform-Cost -First
Search 40
2. Uniform-Cost -First
◆ the nodes with the lowest cost are explored first
◆ similar to BREADTH-FIRST, but with an evaluation of the
cost for each reachable node
◆ g(n) = path cost(n) = sum of individual edge costs to reach
the current node
function UNIFORM-COST-SEARCH(problem) returns solution
Optimality yes
Search 41
2.Uniform Cost Search (vs BFS)
ضحالة
◆ BFS will find the optimal (shallowest) solution so long as
the cost is a function of the depth
Search 42
Uniform Cost Search - Example
A
1 10
B 5
S 5 G
15 5
• BFS will find the path SAG, with a cost of 11, but
SBG is cheaper with a cost of 10
• Uniform Cost Search will find the cheaper solution
(S, B, G). It will find S, A, G but will not see it as it
is not at the head of the queue
Search 43
Uniform-Cost Snapshot
1 Initial
Visited
4 3
Fringe
2 3 Current
7 2 2 4
Visible
Goal
4 5 6 7
Edge Cost 9
2 5 4 4 4 3 6 9
8 9 10 11 12 13 14 15
3 4 7 2 4 8 6 5 4 3 4 2 8 3 9 2
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [27(10), 4(11), 25(12), 26(12), 14(13), 24(13), 20(14), 15(16), 21(18)]
+ [22(16), 23(15)]
Uniform Cost Fringe Trace
1. [1(0)]
2. [3(3), 2(4)]
3. [2(4), 6(5), 7(7)]
4. [6(5), 5(6), 7(7), 4(11)]
5. [5(6), 7(7), 13(8), 12(9), 4(11)]
6. [7(7), 13(8), 12(9), 10(10), 11(10), 4(11)]
7. [13(8), 12(9), 10(10), 11(10), 4(11), 14(13), 15(16)]
8. [12(9), 10(10), 11(10), 27(10), 4(11), 26(12), 14(13), 15(16)]
9. [10(10), 11(10), 27(10), 4(11), 26(12), 25(12), 14(13), 24(13), 15(16)]
10. [11(10), 27(10), 4(11), 25(12), 26(12), 14(13), 24(13), 20(14), 15(16), 21(18)]
11. [27(10), 4(11), 25(12), 26(12), 14(13), 24(13), 20(14), 23(15), 15(16), 22(16), 21(18)]
12. [4(11), 25(12), 26(12), 14(13), 24(13), 20(14), 23(15), 15(16), 23(16), 21(18)]
13. [25(12), 26(12), 14(13), 24(13),8(13), 20(14), 23(15), 15(16), 23(16), 9(16), 21(18)]
14. [26(12), 14(13), 24(13),8(13), 20(14), 23(15), 15(16), 23(16), 9(16), 21(18)]
15. [14(13), 24(13),8(13), 20(14), 23(15), 15(16), 23(16), 9(16), 21(18)]
16. [24(13),8(13), 20(14), 23(15), 15(16), 23(16), 9(16), 29(16),21(18), 28(21)]
Goal reached!
Notation: [Bold+Yellow: Current Node; White: Old Fringe Node; Green+Italics: New Fringe Node].
Assumption: New nodes with the same cost as existing nodes are added after the existing node.
Breadth-First vs. Uniform-Cost
◆ breadth-first always expands the shallowest node
◆ only optimal if all step costs are equal
◆ uniform-cost considers the overall path cost
◆ optimal for any (reasonable) cost function
❖ non-zero, positive
◆ getsbogged down in trees with many fruitless, short
branches
❖ low path cost, but no goal node
◆ both are complete for non-extreme problems
◆ finite number of branches
◆ strictly positive search function
3. Depth First Search (DFS)
◆ Expand Root Node First
Search 47
Depth First Search - Implementation
◆ Use a queuing function that adds nodes to the front
of the queue
Function DEPTH-FIRST-SEARCH(problem) returns a solution or failure
Return GENERAL-SEARCH(problem,ENQUEUE-AT-FRONT)
Search 48
Depth First Search - Observations
◆ Only needs to store the path from the root to the
leaf node as well as the unexpanded nodes. For a
state space with a branching factor of b and a
maximum depth of m, DFS requires storage of
bm nodes
Search 49
Depth First Search - Observations
Search 50
Depth-First search
◆ continues exploring newly generated nodes
◆ achieved by the TREE-SEARCH method by appending
newly generated nodes at the beginning of the search
queue
❖ utilizes a Last-In, First-Out (LIFO) queue, or stack
Time Complexity bm
Space Complexity b*m b branching factor
m maximum path length
Completeness no (for infinite branch length)
Optimality no
Depth-First Snapshot
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Search 67
Map of Romania
Odarea Neamt
Zerind Iasi
Mehadia Bucharest
Dobreta Eforie
Craiova Giurgui
Search 68
Depth Limited Search - Observations
◆ Can be implemented by the general search
algorithm using operators which keep track of
the depth
Search 69
Depth Limited Search - Observations
◆ If
the depth parameter, l, is set deep enough then
we are guaranteed to find a solution if one exists
◆ Therefore it is complete if l >= d (d=depth of solution)
Search 70
Depth-Limited Search
◆ similar to depth-first, but with a limit
◆ overcomes problems with infinite paths
◆ sometimes a depth limit can be inferred or estimated from
the problem description
❖ in other cases, a good depth limit is only known when the problem
is solved
◆ based on the TREE-SEARCH method
◆ must keep track of the depth
function DEPTH-LIMITED-SEARCH(problem, depth-limit) returns solution
Time Complexity bl
b branching factor
Space Complexity b*l
l depth limit
Completeness no (goal beyond l, or infinite branch length)
Optimality no
5. Iterative Deepening
◆ applies LIMITED-DEPTH with increasing depth limits
◆ combines advantages of BREADTH-FIRST and DEPTH-
FIRST methods
◆ many states are expanded multiple times
❖ doesn’t really matter because the number of those nodes is small
◆ in practice, one of the best uninformed search methods
❖ for large search spaces, unknown depth
function ITERATIVE-DEEPENING-SEARCH(problem) returns solution
for depth := 0 to unlimited do
result := DEPTH-LIMITED-SEARCH(problem, depth-limit)
if result != cutoff then return result
Time Complexity bd
Space Complexity b*d b branching factor
◆ Setting
a depth parameter to 19 is obviously
wasteful if using DLS
Search 79
Iterative Deepening Search -
Observations
◆ IDS may seem wasteful as it is expanding the same nodes many
times. In fact, when b=10 only about 11% more nodes are expanded
than for a BFS or a DLS down to level d
◆ For large search spaces, where the depth of the solution is not
known, IDS is normally the preferred search method
Search 80
End
Search 81