Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help please with programming some search algorithms. I am using VSC. Need major help with the coding. When I run my code below I

Need help please with programming some search algorithms. I am using VSC. Need major help with the coding. When I run my code below I get the an error message. Need the correct outputs for the other search algorithms please. Here is the traceback

line 189, in pprint(search.depthFirstSearch('San Bernardino', 'Los Angeles', graph)) ^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'SearchAlgorithms' object has no attribute 'depthFirstSearch'. Did you mean: 'breadthFirstSearch'?

Please use the code below code and help me get the correct outputs.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

from queue import Priorityoueve from pprint import pprint \# Graph of cities with connections to each city. similar to our class exercises, you can drav it on a piece of paper * with step-by-step node inspection for better understanding aph ={ 'San ardino': ['Riverside', 'Rancho Cucamonga'], 'Riverside': ['san ardino', 'Ontario', 'Pomona'], 'Rancho Cucamonga': ['San Bernardino', 'Axusa', 'Los Angeles'], 'Ontario': ['Riverside', 'Whittier', 'Los Angeles'], 'Pombna': ['Riverside', 'Whittier', 'Axusa', 'Los Angeles'], 'Whittier': ['Ontario', 'Pomona', 'Los Angeles'], 'Azusa' : ['Rancho Cucamonga', 'Pomona', 'Arcadia'], 'ArCadia': ['Azusa', 'Los Angeles'], 'Los Angeles': ['Rancho Cucamonga', 'Ontario', 'Pomona', 'Hhittier', 'Arcadia'] \} * Heights are treated as E(n) function as we studied in our class lecture which represents the bachard oost. * In the data structure below, the key represents the oost from a source to target node. For example, the first * entry shaws that there is a oost of 2 for oing from san eardino to Riverside. weights ={ ('San dino', 'Riverside'): 2, ('San ardino', 'Rancho Cucamonga'): 1, ('Riverside', 'ontario'): 1, ('Riverside', 'Pomona'): 3, ('Rancho Cucamonga', 'Los Angeles'): 5, ('Pombna', 'Los Angeles'): 2, ('ortario', 'Whittier'): 2 , ('Ontario', 'Los Angeles'): 3, ('Rancho Cucamonga', 'Azusa'): 3, ('Pomena', 'Azusa'): 2, ('Pomona', 'Whittier'): 2, ('Azusa', 'Arcadia'): 1, ('Whittier', 'Los Angeles'): 2, ('Arcadia', 'Los Angeles'): 2 3 \# heurist is the h(n) function as we studied in our class lecture which represents the forward oost. * In the data structure below, each entry represents the h(n) value. For example, the second entry \# shaws that h(Riverside) is 2 (i.e., h value as forward oost for eaching at Riverside assuming that * your ut/start city is san adino) heuristic ={ 'Sa dino': 4, 'Riverside': 2 , 'Bancho Cucamonga' : 1, 'Ontario' : 1, 'Pomona' : 3, ' Whittier': 4, 'Axysa': 3, 'Arcadia': 2, 'Los Angeles' : g \} \# Data structure to implement search algerithme. Each function below currently has one line of oode returning empty solution with empty expanded cities. vou can rempue the current return statement and \# implement your oode to complete the functions. def uniform_oost_search(self, start, poal, graph, weights): frontier = Priorit yousue() fromtier.put(le, start)) came_from ={} oost_50_ ={} came_fiom [ start] = woe oost_so_far [start] =G while not frontier.enpty(): curent = fiontier.get ( id [1] if cuent == Boal: break for nert_node in aph.neighbors(cut): new_oost = oost_so_far [curent ]+ ETaph.oost (curent, next_node) if next_node not in oost_so_far or new_oost_ oost_so_far[next_node]: oost_30_far[nest_node] = new_oost priority = new_oost frontier.put((priority, nest_node)) came_from[nest_node] = curent t came_from, oost_so_far * wo can delete the line below once you have implemented your solution above def Astar(self, start, goal, graph, weights, heuristic): frontier = Priorit youeue() fromtier.put( (g,start)) explored = set() oost_so_far ={ start:es } came_from = start: wone while not frontier.empty(): cuent_oost, cuent_node = frontier.get () explored.add(curent_node) if curent_node == goal: path =[] while cuent_node != start: path.append(curent_node) curent_node = came_fiom[curent_node] path.append(start) path.reverse() et \{ "Retued solution: " + str(path) + "Expanded cities: " + str(list(explored))\} for eghbor in raph[cuent_node]: try; neighbor_oost = wejhts [(cuent_node, nejbor )] t : neighbor_oost = weights [( neighbor, cuent_node )] nev_oost = oost_so_far[current_node] + neighbor_oost if neighbor not in explored or new_oost ]= new_oost came_from[nejghbor] = cuent_node oombined_oost = new_oost + heuristic[neighbor ] frontier.put((oombined_oost,neighbor)) et \{"Retued solution: [], Expanded cities; \{\}".formut(explored)\}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+ Are ballots compulsory?

Answered: 1 week ago