trees and search strategies and...
TRANSCRIPT
Trees and Search Strategies Trees and Search Strategies and Algorithms --and Algorithms --
Reference: Dr. Franz J. KurfessComputer Science DepartmentComputer Science Department
Cal Poly
Basic Search StrategiesBasic Search Strategiesgg
– depth-firstdepth first– breadth-first
• exerciseexercise– apply depth-first to finding a path from this
building to your favorite “feeding station” g y g(McDonalds, Jason Deli, Pizza Hut)
• is this task sufficiently specifiedi t d• is success guaranteed
• how long will it take• could you remember the pathcould you remember the path• how good is the solution
MotivationMotivationMotivationMotivation
• search strategies are important methodssearch strategies are important methods for many approaches to problem-solving
• the use of search requires an abstract• the use of search requires an abstract formulation of the problem and the available steps to construct solutionsavailable steps to construct solutions
• search algorithms are the basis for many ti i ti d l i th doptimization and planning methods
ObjectivesObjectivesObjectivesObjectives
• formulate appropriate problems as search tasksformulate appropriate problems as search tasks– states, initial state, goal state, successor functions
(operators), cost• know the fundamental search strategies and
algorithms• breadth-first, depth-first,
• evaluate the suitability of a search strategy for a problemproblem– completeness, time & space complexity, optimality
ProblemsProblems
– solutionsolution• path from the initial state to a goal state
– search costsearch cost• time and memory required to calculate a solution
– path costp• determines the expenses of the agent for
executing the actions in a path• sum of the costs of the individual actions in a path
– total cost• sum of search cost and path cost• overall cost for finding a solution
Traveling Salesperson• states
– locations / cities illegal states– illegal states
• each city may be visited only once• visited cities must be kept as state information
• initial state– starting point
no cities visited– no cities visited• successor function (operators)
– move from one location to another oneo e o o e ocat o to a ot e o e• goal test
– all locations visited– agent at the initial location
• path cost: distance between locations
Searching for SolutionsSearching for Solutions
• traversal of the search spacetraversal of the search space – from the initial state to a goal state– legal sequence of actions as defined by successor g q y
function (operators)• general procedure
– check for goal state– expand the current state
• determine the set of reachable states• determine the set of reachable states• return “failure” if the set is empty
– select one from the set of reachable states– move to the selected state
• a search tree is generated
Search TerminologySearch Terminology• search tree
– generated as the search space is traversedgenerated as the search space is traversed• the search space itself is not necessarily a tree, frequently it is a
graph• the tree specifies possible paths through the search spacethe tree specifies possible paths through the search space
– expansion of nodes• as states are explored, the corresponding nodes are expanded by
applying the successor functionapplying the successor function– this generates a new set of (child) nodes
• the fringe (frontier) is the set of nodes not yet visited– newly generated nodes are added to the fringee y ge e ated odes a e added to t e ge
– search strategy• determines the selection of the next node to be expanded• can be achieved by ordering the nodes in the fringe• can be achieved by ordering the nodes in the fringe
– e.g. queue (FIFO), stack (LIFO), “best” node w.r.t. some measure (cost)
Example: Graph SearchA4
D3
1
S3
C2
G0
1 1 1 3
5 2
E1
B2
1 3 3 4
2
• the graph describes the search (state) space– each node in the graph represents one state in the search
space. e.g. a city to be visited in a routing or touring problem
• this graph has additional informationd ti f th t t ( S 3)– names and properties for the states (e.g. S, 3)
– links between nodes, specified by the successor function• properties for links (distance, cost, name, ...)
Breadth Fi
A4
D3
1 1 3
1
First Search
S3
C2
G0
1 1 1 3
5 2
SearchE1
B2
1 3 3 4
2
S3
51 1
A4
D
1
C
1 32
C2
D G E
1
B2
C E
3 3
D3
33 2
C2
D3
G0
G0
E1
1 4
D3
G0
G0
E1
G0
3 3
C2
D3
G0
E1
1
4 E1
G0
2 4
4
G0
G0
3
G0
G0
3 4
Graph and Tree
A4
D3
1 1 3
1
and TreeS3
C2
G0
1 1 1 3
5 2• the tree is
generated by traversing the
E1
B2
1 3 3 4
2
traversing the graph
• the same node in the graph may
S3
51 1
appear repeatedly in the tree
• the arrangement A4
D
1
C
1 32
C2
D G E
1
B2
C E
3 3
of the tree depends on the traversal strategy (search method)
D3
33 2
C2
D3
G0
G0
E1
1 4
D3
G0
G0
E1
G0
3 3
C2
D3
G0
E1
1
4 E1
G0
2 4
• the initial state becomes the root node of the tree
• in the fully
4
G0
G0
3
G0
G0
3 4
in the fully expanded tree, the goal states are the leaf nodes
GreedyA4
D3
1 1 3
1
Greedy SearchS
3C2
G0
1 1 1 3
5 2
E1
B2
1 3 3 4
2S3
5
A4
1
C2
B2
1
D3
1
33 2
C2
1
1
3
4
2
D3
G0
E1
1
3 3
C2
1
4 E1
2 4
3 3
33
4
2
D3
G0
G0
E1
1
3
4
G0
G0
3 3
D3
G0
E1
1
3
G0
2 4
4
G0
G0
G0
G0
A*A4
D3
1 1 3
1
A SearchS
3C2
G0
1 1 1 3
5 2
E1
B2
1 3 3 4
2
S3
51 1
A4
D
1
C
1 32
C2
D G E
1
B2
C E
3 3
D3
33 2
C2
D3
G0
G0
E1
1 4
D3
G0
G0
E1
G0
3 3
C2
D3
G0
E1
1
4 E1
G0
2 4
4
G0
G0
3
G0
G0
3 4
General Search AlgorithmGeneral Search Algorithmfunction TREE-SEARCH(problem, fringe) returns solution
fringe INSERT(MAKE NODE(INITIAL STATE[problem]) fringe)fringe := INSERT(MAKE-NODE(INITIAL-STATE[problem]), fringe)
loop do
if EMPTY?(fringe) then return failure
node REMOVE FIRST(fringe)node := REMOVE-FIRST(fringe)
if GOAL-TEST[problem] applied to STATE[node] succeeds
then return SOLUTION(node)
fringe INSERT ALL(EXPAND(node problem) fringe)fringe := INSERT-ALL(EXPAND(node, problem), fringe)
t th d f th i iti l t t f th bl• generate the node from the initial state of the problem• repeat
– return failure if there are no more nodes in the fringereturn 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
General Search AlgorithmGeneral Search Algorithmfunction GENERAL-SEARCH(problem, QUEUING-FN) returns solution
nodes MAKE QUEUE(MAKE NODE(INITIAL STATE[problem]))nodes := MAKE-QUEUE(MAKE-NODE(INITIAL-STATE[problem]))
loop do
if nodes is empty then return failure
node REMOVE FRONT(nodes)node := REMOVE-FRONT(nodes)
if GOAL-TEST[problem] applied to STATE(node) succeeds
then return node
nodes QUEUING FN(nodes EXPAND(nodenodes := QUEUING-FN(nodes, EXPAND(node, OPERATORS[problem]))
end
Note: QUEUING-FN is a function which will be used to specify the search method
Evaluation Criteria• completeness
– if there is a solution, will it be found,• time complexity
– how long does it take to find the solutionhow long does it take to find the solution– does not include the time to perform actions
• space complexityspace complexity– memory required for the search
• optimalityoptimality– will the best solution be found
main factors for complexity considerations:p ybranching factor b, depth d of the shallowest goal node, maximum path length m
Search CostSearch Cost
• the search cost indicates how expensive itthe search cost indicates how expensive it is to generate a solution
time complexity (e g number of nodes– time complexity (e.g. number of nodes generated) is usually the main factor
– sometimes space complexity (memory usage)sometimes space complexity (memory usage) is considered as well
• path cost indicates how expensive it is to• path cost indicates how expensive it is to execute the solution found in the search
distinct from the search cost but often related– distinct from the search cost, but often related• total cost is the sum of search and path
costs
Breadth-First
• all the nodes reachable from the current
Breadth First
all the nodes reachable from the current node are explored first
achieved by the TREE SEARCH method by– achieved by the TREE-SEARCH method by appending newly generated nodes at the end of the search queueq
function BREADTH-FIRST-SEARCH(problem) returns solution
return TREE-SEARCH(problem, FIFO-QUEUE())
b branching factor
d depth of the tree
Time Complexity bd+1
Space Complexity
bd+1
d depth of the treeComplexityCompleteness yes (for finite b)Optimality yes (for non-
negative path
Breadth-First Snapshot 1Breadth First Snapshot 1InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
Fringe: [] + [2,3]
Breadth-First Snapshot 2Breadth First Snapshot 2InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
4 5
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [3] + [4,5]
Breadth-First Snapshot 3Breadth First Snapshot 3InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
4 5 6 7
Fringe: [4,5] + [6,7]
Breadth-First Snapshot 4Breadth First Snapshot 4InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
4 5 6 7
8 9
Fringe: [5,6,7] + [8,9]
Breadth-First Snapshot 5Breadth First Snapshot 5InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
4 5 6 7
8 9 10 11
Fringe: [6,7,8,9] + [10,11]
Breadth-First Snapshot 6Breadth First Snapshot 6InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
4 5 6 7
8 9 10 11 12 13
Fringe: [7,8,9,10,11] + [12,13]
Breadth-First Snapshot 7Breadth First Snapshot 7InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
4 5 6 7
8 9 10 11 12 13 14 15
Fringe: [8,9.10,11,12,13] + [14,15]
Breadth-First Snapshot 8Breadth First Snapshot 8InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 9Breadth First Snapshot 9InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 10Breadth First Snapshot 10InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 2116 17 18 19 20 21
Fringe: [11,12,13,14,15,16,17,18,19] + [20,21]
Breadth-First Snapshot 11Breadth First Snapshot 11InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 12Breadth First Snapshot 12InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
4 5 6 7
8 9 10 11 12 13 14 15
Note: The goal node is “visible” h b there, 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 13Breadth First Snapshot 13InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 14Breadth First Snapshot 14InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 15Breadth First Snapshot 15InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 16Breadth First Snapshot 16InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 17Breadth First Snapshot 17InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 18Breadth First Snapshot 18InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 19Breadth First Snapshot 19InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 20Breadth First Snapshot 20InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 21Breadth First Snapshot 21InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 22Breadth First Snapshot 22InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 23Breadth First Snapshot 23InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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 24Breadth First Snapshot 24InitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
4 5 6 7
8 9 10 11 12 13 14 15
Note: The goal test is positive for this
d dnode, 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]
Depth-First
• continues exploring newly generated
function DEPTH-FIRST-SEARCH(problem) returns solution
return TREE-SEARCH(problem, LIFO-QUEUE())
continues exploring newly generated nodes
achieved by the TREE SEARCH method by– achieved by the TREE-SEARCH method by appending newly generated nodes at the beginning of the search queueg g q
• utilizes a Last-In, First-Out (LIFO) queue, or stack
b branching factor
m maximum path length
Time Complexity bm
Space Complexity b*mC l t m maximum path lengthCompleteness noOptimality no
Depth-First SnapshotDepth First SnapshotInitialVisited
1
VisitedFringeCurrentVi ibl
2 3
VisibleGoal
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]
Depth-First vs Breadth-FirstDepth First vs. Breadth First
• depth-first goes off into one branch until itdepth first goes off into one branch until it reaches a leaf node– not good if the goal is on another branchg g– neither complete nor optimal– uses much less space than breadth-firstp
• much fewer visited nodes to keep track of• smaller fringe
b d h fi i f l b h ki• breadth-first is more careful by checking all alternatives
l t d ti l– complete and optimal– very memory-intensive