A I P ro g ra m m ing Week Four Searching with an Agenda R ichard Price rm p@ cs.bham .ac.uk www.cs.bham.ac.uk/internal/courses/ai-prog-b/ I n S ea rc h: • We have a search space. • It may be: – Routes around a large location. – Available moves in a board game. – The best combination of items to buy. • We must search through these in a systematic fashion. • And remember where which nodes we have visited. • The order we process nodes determines our strategy. 2 D epth Firs t S ea rc h • At each level we: – – – – – Generates successors. Recurses on the first successor. If we find our goal we return our solution. Otherwise we generate more successors if they exist. If no successors exists we have reached the end of the sub-tree. • We must return to the point where we last branched off. 3 A dva nta g es • Depth first search examines one path at a time. • We keep track of one path and the successors of each node in the path. • So in terms of memory this is very efficient. – Space complexity. • The amount of memory required is proportional to the depth of our tree. • For a tree with branching factor b and maximum depth b. • The amount of memory required is O(bm). 4 D is a dva nta g es • However if our search tree contains cycles or has an infinite depth we may never find a solution. • Even in a finite search tree if our solution is on a right-hand sub-tree it may take a long time to find our solution. • Depth first search is particularly inefficient in terms of time complexity. – O(b^m) • Furthermore the returned solution may not be the most optimal. 5 C riteria • Memory - Space complexity. • Speed - Time complexity. • Quality of solution. 6 B rea dth-firs t S ea rc h • Breadth-first search (BFS) examines all the nodes at depth N. • Before examining any nodes at depth N + 1. • We need access to all the nodes at a given level. 7 B FS the G o o d & the B a d. • BFS will always find the shortest path. – Which is also the optimal solution if we want to minimise steps taken. • If a solution exists BFS will find it! • If we have a large search spaces BFS will tend to process a lot of it. • BFS must store all nodes currently being examined. – O(b^d). • This can take up a lot of memory. • BFS is inefficient in terms of space complexity. 8 T he A g enda We utilise a data structure to store states to explore. • Called the agenda. • The agenda is essentially a to-do list for our algorithm. • Defines our search direction. • Depth first search: – Takes states from the front. – Inserts successors onto the front. • Breadth first search: – Takes states from the front. – Inserts successors onto the back. 9 S uc c es s o rP a ths () define successorPaths(path)->extendedPaths; lvars state; [% for state in successors(hd(path)) do unless member(state,path) then state :: path; endunless; endfor; %] -> extendedPaths; enddefine; 10 A g enda B a s ed D epth Firs t S ea rc h define depthFirstAgendaSearch(initState,goal)->solution; lvars agenda = [^initState]; lvars path, remAgenda, succPaths; until agenda == [] do agenda --> ![?path ??remAgenda]; if isGoal(hd(path),goal) then rev(path) -> solution; return; else successorPaths(path) <> remAgenda -> agenda; endif; enduntil; ;;; searched entire space, to no avail false -> solution; enddefine; 11 B rea dth-firs t S ea rc h define breadthFirstSearch(initState,goal)->solution; lvars agenda = [^initState]; lvars path, remAgenda, succPaths; until agenda == [] do agenda --> ![?path ??remAgenda]; if isGoal(hd(path),goal) then rev(path) -> solution; return; else remAgenda <> successorPaths(path) -> agenda; endif; enduntil; ;;; searched entire space, to no avail false -> solution; enddefine; 12 A g enda A dva nta g es • Using a data structure like the agenda gives us a tremendous amount of flexibility. • The following piece of code allows us to implement both depth-first and breadth first search. define extendAgenda(oldAgenda,path,strategy)->newAgenda; lvars newPaths = successorPaths(path); if strategy = "breadth" then oldAgenda <> newPaths -> newAgenda; elseif strategy = "depth" then newPaths <> oldAgenda -> newAgenda; endif; enddefine; 13 A g enda S ea rc h() define agendaSearch(initState,goal,strategy)->solution; lvars agenda = [^initState]; lvars path, remAgenda, succPaths; until agenda == [] do agenda --> ![?path ??remAgenda]; if isGoal(hd(path),goal) then rev(path) -> solution; return; else extendAgenda(remAgenda,path,strategy) -> agenda; endif; enduntil; ;;; searched entire space, to no avail false -> solution; enddefine; 14 U nifo rm ed • Breadth-first search and Depth-first search are both uniformed search strategies. • They have require no knowledge of the application area. • Which means they can be applied to virtually anything. • Making them jacks of all trades but masters of none. • In later weeks we’ll look at more intelligent search techniques. • But there are more Uniformed search strategies. • Depth limited: – Simply limits the depth that depth-first searches to. • Iterative Deepening: – Is depth limited search. – But grows the depth of the search until a solution is found. – Simulates breadth first search using depth first search. 15
© Copyright 2025 Paperzz