You are on page 1of 81

Searching for solution

▪ Having formulated problems, we now need to


solve them

• This is done by searching through the state space


• Search tree is generated by taking initial state and
applying successor function to it

Search 1
Search 2
Odarea
Neamt

Iasi
Zerind

Fararas Vaslui
Arad Sibiu

Timisoara Rimnicu Vilcea


Urziceni
Lugoj Pitesti Hirsova

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)

◆ generate the node from the initial state of the problem


◆ repeat
◆ return failure if there are no more nodes in the fringe
◆ examine the current node; if it’s a goal, return the solution
◆ expand the current node, and add the new nodes to the fringe
Selection of a search strategy
◆ Mostof the effort is often spent on the selection of an
appropriate search strategy for a given problem
◆ Uninformed search (blind search)
❖ Number of steps, path cost unknown
❖ Agent knows when it reaches a goal
◆ Informed search (heuristic ‫ ارشادي‬search)
❖ Agent has background information about the problem
❖ Map, costs of actions
Search Strategies
◆ Uninformed Search ◆ Informed Search
◆ breadth-first ◆ best-first search
◆ depth-first ◆ search with heuristics
◆ uniform-cost search ◆ memory-bounded search
◆ depth-limited search ◆ iterative improvement search
◆ iterative deepening
◆ bi-directional search
◆ constraint satisfaction
1. Breadth-First
◆ Expand Root Node First

◆ Expand all nodes at level 1 before expanding


level 2
◆ Generally:
◆ Expand all nodes at level d before expanding
nodes at level d+1

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

function BREADTH-FIRST-SEARCH(problem) returns solution

return TREE-SEARCH(problem, FIFO-QUEUE())

Time Complexity bd+1


Space Complexity bd+1 b branching factor
d depth of the tree
Completeness yes (for finite b)
Optimality yes (for non-negative
path costs)
Breadth-First Snapshot 1
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal

Fringe: [] + [2,3]
Breadth-First Snapshot 2
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5

Fringe: [3] + [4,5]


Breadth-First Snapshot 3
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7

Fringe: [4,5] + [6,7]


Breadth-First Snapshot 4
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7

8 9

Fringe: [5,6,7] + [8,9]


Breadth-First Snapshot 5
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7

8 9 10 11

Fringe: [6,7,8,9] + [10,11]


Breadth-First Snapshot 6
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7

8 9 10 11 12 13

Fringe: [7,8,9,10,11] + [12,13]


Breadth-First Snapshot 7
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7

8 9 10 11 12 13 14 15

Fringe: [8,9.10,11,12,13] + [14,15]


Breadth-First Snapshot 8
1 Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7

8 9 10 11 12 13 14 15

16 17

Fringe: [9,10,11,12,13,14,15] + [16,17]


Breadth-First Snapshot 9
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

Fringe: [10,11,12,13,14,15,16,17] + [18,19]


Breadth-First Snapshot 10
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

Fringe: [11,12,13,14,15,16,17,18,19] + [20,21]


Breadth-First Snapshot 11
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

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

Fringe: [13,14,15,16,17,18,19,20,21] + [22,23]


Breadth-First Snapshot 13
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

Fringe: [14,15,16,17,18,19,20,21,22,23,24,25] + [26,27]


Breadth-First Snapshot 14
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

Fringe: [15,16,17,18,19,20,21,22,23,24,25,26,27] + [28,29]


Breadth-First Snapshot 15
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: [15,16,17,18,19,20,21,22,23,24,25,26,27,28,29] + [30,31]


Breadth-First Snapshot 16
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: [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

◆ Ifthere is a solution breadth first search is guaranteed


to find it

◆ Ifthere are several solutions then breadth first search


will always find the shallowest goal state first and if
the cost of a solution is a non-decreasing function of
the depth then it will always find the cheapest solution

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)

◆ Where b is the branching factor and d is the depth of


the search tree

◆ Note : The space/time complexity could be less as the


solution could be found anywhere on the dth level.

Search 36
Exponential Growth
◆ Exponential growth quickly makes complete
state space searches unrealistic

◆ Ifthe branch factor was 10, by level 5 we would


need to search 100,000 nodes (i.e. 105)

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

Time and memory requirements for breadth-first search,


assuming a branching factor of 10, 100 bytes per node and
searching 1000 nodes/second

Search 38
Exponential Growth - Observations
◆ Space is more of a factor to breadth first search than time

◆ Time is still an issue. Who has 35 years to wait for an answer


to a level 12 problem (or even 128 days to a level 10 problem)

◆ It could be argued that as technology gets faster then


exponential growth will not be a problem. But even if
technology is 100 times faster we would still have to wait 35
years for a level 14 problem and what if we hit a level 15
problem!

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

return TREE-SEARCH(problem, COST-FN, FIFO-QUEUE())

Time Complexity bC*/e


b branching factor
Space Complexity bC*/e C* cost of the optimal solution
Completeness yes (finite b, step costs >= e) e minimum cost per action

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

◆ Uniform Cost Search can be used when this is not the


case and uniform cost search will find the cheapest
solution provided that the cost of the path never
decreases as we proceed along the path

◆ Uniform Cost Search works by expanding the lowest cost


node on the fringe.

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

◆ Exploreone branch of the tree before exploring


another branch

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

◆ Time complexity for DFS is bm in the worst case

Search 49
Depth First Search - Observations

◆ IfDFS goes down a infinite branch it will not


terminate if it does not find a goal state.

◆ Ifit does find a solution there may be a better


solution at a lower level in the tree. Therefore,
depth first search is neither complete nor
optimal.

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

function DEPTH-FIRST-SEARCH(problem) returns solution

return TREE-SEARCH(problem, LIFO-QUEUE())

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

Fringe: [3] + [22,23]


Search 53
Search 54
Search 55
Search 56
Search 57
Search 58
Search 59
Search 60
Search 61
Search 62
Search 63
Search 64
Depth-First vs. Breadth-First
◆ depth-first goes off into one branch until it reaches a
leaf node
◆ not good if the goal is on another branch
◆ neither complete nor optimal
◆ uses much less space than breadth-first
❖ much fewer visited nodes to keep track of smaller fringe
◆ breadth-first is more careful by checking all alternatives
◆ complete and optimal
❖ under most circumstances ‫فى معظم الظروف‬
◆ very memory-intensive
Backtracking Search
◆ variation of depth-first search
◆ only one successor node is generated at a time
❖ even better space complexity: O(m) instead of O(b*m)
❖ even more memory space can be saved by incrementally modifying
the current state, instead of creating a new one
❖ only possible if the modifications can be undone
❖ this is referred to as backtracking
❖ frequently used in planning, theorem proving
4. Depth Limited Search
◆ DFS may never terminate as it could follow a
path that has no solution on it

◆ DLSsolves this by imposing a depth limit, at


which point the search terminates that particular
branch

Search 67
Map of Romania
Odarea Neamt

Zerind Iasi

Arad Farara Vaslui


Sibiu
s
Timisoara Rimnicu Vilcea
Urziceni
Lugoj Pitesti Hirsova

Mehadia Bucharest

Dobreta Eforie
Craiova Giurgui

On the Romania map there


are 20 towns so any town is In fact, any town is reachable
reachable in 19 steps in 9 steps

Search 68
Depth Limited Search - Observations
◆ Can be implemented by the general search
algorithm using operators which keep track of
the depth

◆ Choice of depth parameter is important


◆ Too deep is wasteful of time and space
◆ Too shallow and we may never reach a goal state

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)

◆ Space requirements are O(bl)


◆ Time requirements are O(bl)
◆ DLS is not optimal

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

return TREE-SEARCH(problem, depth-limit, LIFO-QUEUE())

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

Completeness yes d tree depth

Optimality yes (all step costs identical)


Search 73
Search 74
Search 75
Search 76
Search 77
Search 78
Iterative Deepening Search
◆ Theproblem with DLS is choosing a depth
parameter

◆ Setting
a depth parameter to 19 is obviously
wasteful if using DLS

◆ IDS overcomes this problem by trying depth


limits of 0, 1, 2, …, n. In effect it is combining
BFS and DFS

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

◆ Time Complexity = O(bd)

◆ Space Complexity = O(bd)

◆ 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

You might also like