What is Search? What is Search? Dr. Ali Abdallah Dr. Ali Abdallah ([email protected]) ([email protected])
Jun 21, 2015
What is Search?What is Search?
Dr. Ali Abdallah Dr. Ali Abdallah
What is Search?What is Search?
• Search is something we do all the time Search is something we do all the time without realizing it. without realizing it. – Finding this room required search.Finding this room required search.– So does finding the best insurance deal.So does finding the best insurance deal.– Or finding your keys.Or finding your keys.
• Computers are not, naturally good at Computers are not, naturally good at search.search.
• But if they could do it, they would be But if they could do it, they would be very fast!very fast!
Computers Bad At Search?Computers Bad At Search?
• Yes, they can’t decide for themselves Yes, they can’t decide for themselves how to search.how to search.
• We have to tell them how to search.We have to tell them how to search.– Even when we do tell them how, they will Even when we do tell them how, they will
still not understand what they are doing!still not understand what they are doing!
• If computers are to be efficient at If computers are to be efficient at search, we must very carefully tell search, we must very carefully tell them how to do it.them how to do it.
What is Search For?What is Search For?
• All AI is search!All AI is search!– Not totally true (obviously) but more Not totally true (obviously) but more
true than you might think.true than you might think.– Finding a good/best solution to a Finding a good/best solution to a
problem amongst many possible problem amongst many possible solutions.solutions.
• Ways of methodically deciding which Ways of methodically deciding which solutions are better than others.solutions are better than others.
Three Types of SearchThree Types of Search
• Blind Search:Blind Search:– Searching the possibilities, in some Searching the possibilities, in some
predefined order. The types of search predefined order. The types of search we do in a maze.we do in a maze.
• Heuristic Search:Heuristic Search:– Searching the possibilities in order of Searching the possibilities in order of
their perceived value.their perceived value.
• Stochastic Search:Stochastic Search:– Randomly searching in a cleaver way.Randomly searching in a cleaver way.
Generalized Search Generalized Search algorithmalgorithm
function GeneralSearch(QueuingFunction) queue = MakeInitialQueue()
loop if queue is empty then return failure node = RemoveFromFront( queue ) if GoalTest( node ) succeeds then return node queue = QueuingFunction( queue, Expand(node) ) end loop
The choice of queuing function is the main feature
Example: Arad to BucharestExample: Arad to Bucharest
start
end
Representing SearchRepresenting Search
Arad
Zerind Sibiu Timisoara
Oradea Fagaras Rimnicu VilceaArad
Sibiu Bucharest
Search TerminologySearch Terminology
• CompletenessCompleteness– solution will be found, if it existssolution will be found, if it exists
• Time complexityTime complexity– number of nodes expandednumber of nodes expanded
• Space complexitySpace complexity– number of nodes in memorynumber of nodes in memory
• OptimalityOptimality– least cost solution will be foundleast cost solution will be found
Blind (Uninformed) Blind (Uninformed) Search Search
(Where we systematically (Where we systematically explore alternatives)explore alternatives)
R&N: Chap. 3, Sect. 3.3–5R&N: Chap. 3, Sect. 3.3–5
Simple Problem-Solving-Simple Problem-Solving-Agent Agent AlgorithmAgent Agent Algorithm
1.1. ss00 sense/read state sense/read state2.2. GOAL? GOAL? select/read goal test select/read goal test3.3. SUCCESSORS SUCCESSORS read successor function read successor function
4.4. solution solution searchsearch(s(s00, G, Succ) , G, Succ) 5.5. perform(solution)perform(solution)
Searching the State SpaceSearching the State Space
Search tree
Note that some states are visited multiple
times
Basic Search ConceptsBasic Search Concepts
Search treeSearch tree Search node Search node Node expansionNode expansion Fringe of search treeFringe of search tree Search strategySearch strategy: At each stage it : At each stage it
determines which node to expanddetermines which node to expand
Search Nodes Search Nodes States States
1
2
3 4
5 6
7
8
1
2
3 4
5 6
7
8
1
2
3 4
5 6
78
1
3
5 6
8
1
3
4
5 6
7
82
4 7
2
1
2
3 4
5 6
7
8
Search Nodes Search Nodes States States
1
2
3 4
5 6
7
8
1
2
3 4
5 6
7
8
1
2
3 4
5 6
78
1
3
5 6
8
1
3
4
5 6
7
82
4 7
2
1
2
3 4
5 6
7
8
If states are allowed to be revisited,the search tree may be infinite even
when the state space is finite
If states are allowed to be revisited,the search tree may be infinite even
when the state space is finite
Data Structure of a NodeData Structure of a Node
PARENT-NODE
1
2
3 4
5 6
7
8STATE
Depth of a node N = length of path from root to N
(Depth of the root = 0)
BOOKKEEPING
55Path-CostPath-Cost
55DepthDepth
RightRightActionAction
Expanded yes...
CHILDREN
Node expansionNode expansion
The The expansionexpansion of a node N of the of a node N of the search tree consists of:search tree consists of:
1)1) Evaluating the successor function Evaluating the successor function on on STATESTATE(N)(N)
2)2) Generating a child of N for each Generating a child of N for each state returned by the functionstate returned by the function
Fringe and Search StrategyFringe and Search Strategy
The The fringefringe is the set of all search is the set of all search nodes that haven’t been expanded nodes that haven’t been expanded yet yet
1
23 45 6
78
1
23 45 6
78
1
23 45 6
78
135 6
8
13
4
5 67
824 7
2
1
23 45 6
78
Is it identical to the set of leaves?
Fringe and Search StrategyFringe and Search Strategy
The The fringefringe is the set of all search is the set of all search nodes that haven’t been expanded nodes that haven’t been expanded yet yet
It is implemented as a It is implemented as a priority queuepriority queue FRINGEFRINGE• INSERT(node,FRINGE)INSERT(node,FRINGE)• REMOVE(FRINGE)REMOVE(FRINGE)
The ordering of the nodes in FRINGE The ordering of the nodes in FRINGE defines thedefines the search strategy search strategy
Search AlgorithmSearch Algorithm
1.1. If GOAL?(initial-state) then return initial-stateIf GOAL?(initial-state) then return initial-state2.2. INSERT(initial-node,FRINGE)INSERT(initial-node,FRINGE)3.3. Repeat:Repeat:
a.a. If empty(FRINGE) then return If empty(FRINGE) then return failurefailureb.b. nn REMOVE(FRINGE) REMOVE(FRINGE)c.c. ss STATE( STATE(nn))d.d. For every state For every state s’s’ in SUCCESSORS( in SUCCESSORS(ss))
i.i. Create a new node Create a new node n’n’ as a child of as a child of nnii.ii. If GOAL?(If GOAL?(s’s’) then return ) then return path or goal path or goal
statestateiii.iii. INSERT(INSERT(n’n’,FRINGE),FRINGE)
Performance MeasuresPerformance Measures
CompletenessCompletenessA search algorithm is complete if it finds a A search algorithm is complete if it finds a solution whenever one existssolution whenever one exists[What about the case when no solution exists?][What about the case when no solution exists?]
OptimalityOptimalityA search algorithm is optimal if it returns A search algorithm is optimal if it returns an optimal solution whenever a solution an optimal solution whenever a solution existsexists
ComplexityComplexityIt measures the time and amount of It measures the time and amount of memory required by the algorithmmemory required by the algorithm
Important ParametersImportant Parameters
1)1) Maximum number of successors of any stateMaximum number of successors of any state
branching factorbranching factor bb of the search tree of the search tree
2)2) Minimal length of a path between the initial Minimal length of a path between the initial and a goal stateand a goal state
depth depth dd of the shallowest goal node in the of the shallowest goal node in the search tree search tree
Blind vs. Heuristic Blind vs. Heuristic StrategiesStrategies
BlindBlind (or (or un-informedun-informed) strategies do ) strategies do not exploit state descriptions to not exploit state descriptions to select which node to expand nextselect which node to expand next
HeuristicHeuristic (or (or informedinformed) strategies ) strategies exploits state descriptions to select exploits state descriptions to select the “most promising” node to expandthe “most promising” node to expand
ExampleExample
1
2
3 4
5 6
7
8
1 2 3
4 5 6
7 8
Goal state
1 2 3
4 5
67 8
N1
N2
STATE
STATE
For a blind strategy, N1 and N2 are just two nodes (at some depth in the search tree)
ExampleExample
1
2
3 4
5 6
7
8
1 2 3
4 5 6
7 8
Goal state
1 2 3
4 5
67 8
N1
N2
STATE
STATE
For a heuristic strategy counting the number of misplaced tiles, N2 is more promising than N1
Important RemarkImportant Remark
Some search problems, such as the Some search problems, such as the (n(n22-1)-puzzle, are NP-hard-1)-puzzle, are NP-hard
One can’t expect to solve all instances One can’t expect to solve all instances of such problems in less than of such problems in less than exponential time exponential time
One may still strive to solve each One may still strive to solve each instance as efficiently as possible instance as efficiently as possible
Blind StrategiesBlind Strategies
Breadth-firstBreadth-first• BidirectionalBidirectional
Depth-firstDepth-first• Depth-limited Depth-limited • Iterative deepeningIterative deepening
Uniform-CostUniform-Cost(variant of breadth-first)(variant of breadth-first)
Arc cost = c(action) 0
Breadth-First StrategyBreadth-First Strategy
New nodes are inserted New nodes are inserted at the endat the end of of FRINGEFRINGE
2 3
4 5
1
6 7
FRINGE = (1)
Breadth-First StrategyBreadth-First Strategy
New nodes are inserted New nodes are inserted at the endat the end of of FRINGEFRINGE
FRINGE = (2, 3)2 3
4 5
1
6 7
Breadth-First StrategyBreadth-First Strategy
New nodes are inserted New nodes are inserted at the endat the end of of FRINGEFRINGE
FRINGE = (3, 4, 5)2 3
4 5
1
6 7
Breadth-First StrategyBreadth-First Strategy
New nodes are inserted New nodes are inserted at the endat the end of of FRINGEFRINGE
FRINGE = (4, 5, 6, 7)2 3
4 5
1
6 7
EvaluationEvaluation
bb: branching factor: branching factor dd: depth of shallowest goal node: depth of shallowest goal node Breadth-first search is: Breadth-first search is:
• CompleteComplete• OptimalOptimal if step cost is 1 if step cost is 1
Number of nodes generated:Number of nodes generated: 1 + b + b 1 + b + b2 2 + … + b+ … + bdd = (b= (bd+1d+1-1)/(b-1) = -1)/(b-1) = O(bO(bdd) )
Time and space complexity isTime and space complexity is O(bO(bdd))
Time and Memory Time and Memory RequirementsRequirements
dd # Nodes# Nodes TimeTime MemoryMemory
22 111111 .01 msec.01 msec 11 Kbytes11 Kbytes
44 11,11111,111 1 msec1 msec 1 Mbyte1 Mbyte
66 ~10~1066 1 sec1 sec 100 Mb100 Mb
88 ~10~1088 100 sec100 sec 10 Gbytes10 Gbytes
1010 ~10~101010 2.8 hours2.8 hours 1 Tbyte1 Tbyte
1212 ~10~101212 11.6 days11.6 days 100 Tbytes100 Tbytes
1414 ~10~101414 3.2 years3.2 years 10,000 Tbytes10,000 Tbytes
Memory requirements vs Time requirementsAssumptions: b = 10; 1,000,000 nodes/sec; 100bytes/node
RemarkRemarkIf a problem has no solution, breadth-first If a problem has no solution, breadth-first may run for ever may run for ever (if the state space is (if the state space is infinite or states can be revisited arbitrary infinite or states can be revisited arbitrary many times)many times)
12
14
11
15
10
13
9
5 6 7 8
4321
12
15
11
14
10
13
9
5 6 7 8
4321
?
Bidirectional StrategyBidirectional Strategy2 fringe queues: FRINGE1 and FRINGE2
s
Time and space complexity is O(bd/2) O(bd) if both trees have the same branching factor b
Bidirectional StrategyBidirectional Strategy
• What happens if the branching factor is different in each direction?
Major difficulties:Major difficulties:• Need to have a predecessor functionNeed to have a predecessor function• What about multiple goals?What about multiple goals?• What about What about implicitly implicitly defined goals?defined goals?
(e.g. n-queens problem)(e.g. n-queens problem)
Depth-First StrategyDepth-First Strategy
New nodes are inserted New nodes are inserted at the frontat the front of of FRINGEFRINGE
1
2 3
4 5
FRINGE = (1)
Depth-First StrategyDepth-First Strategy
New nodes are inserted New nodes are inserted at the frontat the front of of FRINGEFRINGE
1
2 3
4 5
FRINGE = (2, 3)
Depth-First StrategyDepth-First Strategy
New nodes are inserted New nodes are inserted at the frontat the front of of FRINGEFRINGE
1
2 3
4 5
FRINGE = (4, 5, 3)
Depth-First StrategyDepth-First Strategy
New nodes are inserted New nodes are inserted at the frontat the front of of FRINGEFRINGE
1
2 3
4 5
Depth-First StrategyDepth-First Strategy
New nodes are inserted New nodes are inserted at the frontat the front of of FRINGEFRINGE
1
2 3
4 5
Depth-First StrategyDepth-First Strategy
New nodes are inserted New nodes are inserted at the frontat the front of of FRINGEFRINGE
1
2 3
4 5
Depth-First StrategyDepth-First Strategy
New nodes are inserted New nodes are inserted at the frontat the front of of FRINGEFRINGE
1
2 3
4 5
Depth-First StrategyDepth-First Strategy
New nodes are inserted New nodes are inserted at the frontat the front of of FRINGEFRINGE
1
2 3
4 5
Depth-First StrategyDepth-First Strategy
New nodes are inserted New nodes are inserted at the frontat the front of of FRINGEFRINGE
1
2 3
4 5
Depth-First StrategyDepth-First Strategy
New nodes are inserted New nodes are inserted at the frontat the front of of FRINGEFRINGE
1
2 3
4 5
Depth-First StrategyDepth-First Strategy
New nodes are inserted New nodes are inserted at the frontat the front of of FRINGEFRINGE
1
2 3
4 5
EvaluationEvaluation bb: branching factor: branching factor dd: depth of shallowest goal node : depth of shallowest goal node mm: maximal depth of a leaf node: maximal depth of a leaf node Depth-first search is:Depth-first search is:
Complete only for finite search treeComplete only for finite search tree Not optimalNot optimal
Number of nodes generated:Number of nodes generated: 1 + b + b 1 + b + b2 2 + … + b+ … + bmm = O(b = O(bmm) )
Time complexity isTime complexity is O(bO(bmm) ) Space complexity is O(bm) [or O(m)]Space complexity is O(bm) [or O(m)][Reminder: Breadth-first requires O(b[Reminder: Breadth-first requires O(bdd) time and space]) time and space]
Depth-Limited SearchDepth-Limited Search
Depth-first with Depth-first with depth cutoffdepth cutoff k (depth k (depth below which nodes are not below which nodes are not expanded)expanded)
Three possible outcomes:Three possible outcomes:• SolutionSolution• Failure (no solution)Failure (no solution)• Cutoff (no solution within cutoff)Cutoff (no solution within cutoff)
Iterative Deepening SearchIterative Deepening Search
Provides the best of both breadth-first Provides the best of both breadth-first and depth-first searchand depth-first search
Main idea:Main idea:
IDSIDSFor For kk = 0, 1, 2, … do: = 0, 1, 2, … do:
Perform depth-first search with Perform depth-first search with depth cutoff depth cutoff kk
Iterative DeepeningIterative Deepening
Iterative DeepeningIterative Deepening
Iterative DeepeningIterative Deepening
PerformancePerformance
Iterative deepening search is:Iterative deepening search is:• CompleteComplete• OptimalOptimal if step cost =1 if step cost =1
Time complexity is:Time complexity is: (d+1)(1) + db + (d-1)b(d+1)(1) + db + (d-1)b22 + … + (1) b + … + (1) bd d
= O(b= O(bdd))
Space complexity is: O(bd) or O(d)Space complexity is: O(bd) or O(d)
CalculationCalculation
db + (d-1)bdb + (d-1)b22 + … + (1) b + … + (1) bdd
== b bd d ++ 2b2bd-1 d-1 ++ 3b3bd-2 d-2 +… ++… + dbdb
= (1 + 2b= (1 + 2b-1-1 + 3b + 3b-2-2 + … + db + … + db-d-d))bbdd
((i=1,…,i=1,…, ibib(1-i)(1-i)))bbdd = b = bd d ((b/(b-1)b/(b-1)))22
d = 5 and b = 2d = 5 and b = 2
BFBF IDID
11 1 1 xx 6 = 6 6 = 6
22 2 2 xx 5 = 10 5 = 10
44 4 4 xx 4 = 16 4 = 16
88 8 8 xx 3 = 24 3 = 24
1616 16 16 xx 2 = 2 = 3232
3232 32 32 xx 1 = 1 = 3232
6363 120120
120/63 ~ 2
Number of Generated NodesNumber of Generated Nodes (Breadth-First & Iterative (Breadth-First & Iterative Deepening)Deepening)
Number of Generated Nodes Number of Generated Nodes (Breadth-First & Iterative (Breadth-First & Iterative Deepening)Deepening)
d = 5 and b = 10d = 5 and b = 10BFBF IDID
11 66
1010 5050
100100 400400
1,0001,000 3,0003,000
10,00010,000 20,00020,000
100,000100,000 100,000100,000
111,111111,111 123,456123,456 123,456/111,111 ~ 1.111
Comparison of StrategiesComparison of Strategies
Breadth-first is complete and optimal, Breadth-first is complete and optimal, but has high space complexitybut has high space complexity
Depth-first is space efficient, but is Depth-first is space efficient, but is neither complete, nor optimalneither complete, nor optimal
Iterative deepening is complete and Iterative deepening is complete and optimal, with the same space complexity optimal, with the same space complexity as depth-first and almost the same time as depth-first and almost the same time complexity as breadth-firstcomplexity as breadth-first
Revisited StatesRevisited States
8-queens
No
assembly planning
Few
1 2 34 5
67 8
8-puzzle and robot navigation
Many
search tree is finite search tree is infinite
Avoiding Revisited StatesAvoiding Revisited States
Requires comparing state descriptions Requires comparing state descriptions Breadth-first search: Breadth-first search:
• Store all states associated with Store all states associated with generatedgenerated nodes in CLOSED nodes in CLOSED
• If the state of a new node is in CLOSED, If the state of a new node is in CLOSED, then discard the nodethen discard the node
Avoiding Revisited StatesAvoiding Revisited States Depth-first search: Depth-first search: Solution 1:Solution 1:
– Store all states associated with nodes in current path Store all states associated with nodes in current path in CLOSEDin CLOSED
– If the state of a new node is in CLOSED, then discard If the state of a new node is in CLOSED, then discard the nodethe node
Only avoids loopsOnly avoids loops
Solution 2:Solution 2:– Store of all generated states in CLOSEDStore of all generated states in CLOSED– If the state of a new node is in CLOSED, then discard If the state of a new node is in CLOSED, then discard
the nodethe node Same space complexity as breadth-first !Same space complexity as breadth-first !
Uniform-Cost Search Uniform-Cost Search (Optimal)(Optimal) Each arc has some cost c > 0
The cost of the path to each fringe node N is g(N) = costs of arcs The goal is to generate a solution path of minimal cost The queue FRINGE is sorted in increasing cost
Need to modify search algorithm
S0
1A
5B
15CS G
A
B
C
5
1
15
10
5
5
G11
G10
Search AlgorithmSearch Algorithm
1.1. If GOAL?(initial-state) then return initial-stateIf GOAL?(initial-state) then return initial-state2.2. INSERT(initial-node,FRINGE)INSERT(initial-node,FRINGE)3.3. Repeat:Repeat:
a.a. If empty(FRINGE) then return If empty(FRINGE) then return failurefailureb.b. nn REMOVE(FRINGE) REMOVE(FRINGE)c.c. ss STATE( STATE(nn))d.d. For every state For every state s’s’ in SUCCESSORS( in SUCCESSORS(ss))
i.i. Create a new node Create a new node n’n’ as a child of as a child of nnii.ii. If GOAL?(If GOAL?(s’s’) then return ) then return path or goal path or goal
statestateiii.iii. INSERT(INSERT(n’n’,FRINGE),FRINGE)
Modified Search AlgorithmModified Search Algorithm
1.1. INSERT(initial-node,FRINGE)INSERT(initial-node,FRINGE)
2.2. Repeat:Repeat:
a.a. If empty(FRINGE) then return If empty(FRINGE) then return failurefailure
b.b. nn REMOVE(FRINGE) REMOVE(FRINGE)
c.c. ss STATE( STATE(nn))
d.d. If GOAL?(If GOAL?(ss) then return ) then return path or goal statepath or goal state
e.e. For every state For every state s’s’ in SUCCESSORS( in SUCCESSORS(ss))
i.i. Create a node Create a node n’n’ as a successor of as a successor of nn
ii.ii. INSERT(INSERT(n’n’,FRINGE),FRINGE)
Avoiding Revisited States Avoiding Revisited States in Uniform-Cost Searchin Uniform-Cost Search When a node N is expanded the path to N is also When a node N is expanded the path to N is also
the best path from the initial state to STATE(N) the best path from the initial state to STATE(N) if if it is the first time STATE(N) is encounteredit is the first time STATE(N) is encountered..
So:So:
• When a node is When a node is expandedexpanded, store its state into , store its state into CLOSED CLOSED
• When a new node N is generated:When a new node N is generated:
– If STATE(N) is in CLOSED, discard NIf STATE(N) is in CLOSED, discard N
– If there exits a node N’ in the fringe such that If there exits a node N’ in the fringe such that STATE(N’) = STATE(N), discard the node – N or N’ – STATE(N’) = STATE(N), discard the node – N or N’ – with the highest-cost pathwith the highest-cost path