def dvosmerno_bfs(root, end): global st_obiskanih_vozlisc oce_od_elementa = collections.defaultdict(tuple) oce_od_elementa_nzj = collections.defaultdict(tuple) g = Graph() ng = Graph() vrsta = [] vrsta_nzj = [] seen = set() seen_nzj = set() # dodam root vrsta.append(list_to_tuple(root)) vrsta_nzj.append(list_to_tuple(end)) seen.add(str(root)) seen_nzj.add(str(end)) # kopija = naredi_matriko(root) #kopija start resitve = (list_to_tuple(end), ) konci = (list_to_tuple(root), ) napolni(g, root) napolni(ng, end) stevilo_pregledanih_vozlisc = 1 while vrsta: vozlisce = vrsta.pop(0) vozlisce_nzj = vrsta_nzj.pop(0) for neighbour in ng.get(vozlisce_nzj): if str(neighbour) not in seen_nzj: stevilo_pregledanih_vozlisc += 1 oce_od_elementa_nzj[neighbour] = vozlisce_nzj napolni(ng, neighbour) vrsta_nzj.append(neighbour) seen_nzj.add(str(neighbour)) resitve += (neighbour, ) if neighbour in konci: st_obiskanih_vozlisc = stevilo_pregledanih_vozlisc return find_path(g, neighbour, oce_od_elementa, root) + find_path_reverse( ng, neighbour, oce_od_elementa_nzj, end) for neighbour in g.get(vozlisce): if str(neighbour) not in seen: stevilo_pregledanih_vozlisc += 1 oce_od_elementa[neighbour] = vozlisce napolni(g, neighbour) vrsta.append(neighbour) seen.add(str(neighbour)) konci += (neighbour, ) if neighbour in resitve: st_obiskanih_vozlisc = stevilo_pregledanih_vozlisc return find_path(g, neighbour, oce_od_elementa, root) + find_path_reverse( ng, neighbour, oce_od_elementa_nzj, end)
def BFS(root, end): oce_od_elementa = collections.defaultdict(tuple) graf = Graph() vrsta = [] seen = set() # dodam root vrsta.append(list_to_tuple(root)) seen.add(str(root)) # kopija = naredi_matriko(root) #kopija start napolni(graf, root) stevilo_pregledanih_vozlisc = 1 while vrsta: vozlisce = vrsta.pop(0) for neighbour in graf.get(vozlisce): if str(neighbour) not in seen: stevilo_pregledanih_vozlisc += 1 oce_od_elementa[neighbour] = vozlisce napolni(graf, neighbour) vrsta.append(neighbour) seen.add(str(neighbour)) if tuple_to_list(neighbour) == end: print("Stevilo pregledanih vozlisc:", stevilo_pregledanih_vozlisc) return find_path(graf, neighbour, oce_od_elementa, root)
def A_star(root): graf = Graph() queue = [list_to_tuple(root)] oce_od_elementa = collections.defaultdict(tuple) g_score = collections.defaultdict(int) g_score[list_to_tuple(root)] = 0 f_score = collections.defaultdict(int) #f_score[list_to_tuple(root)] = eucledian_distance(root, NxP_end) f_score[list_to_tuple(root)] = wrong_place(root, NxP_end) #worng place napolni(graf, root) #napolni vse moznosti iz root-a stevilo_pregledanih_vozlisc = 1 while queue: current = min_value_dict(queue, f_score) #get lowest score in f_score dict if current == list_to_tuple(NxP_end): print('Stevilo pregledanih vozlisc:', stevilo_pregledanih_vozlisc) return find_path( graf, current, oce_od_elementa, NxP_start ) # current # find_path(graf, current, oce_od_elementa) queue.remove(current) for neighbour in graf.get(current): #dodam soseda v g_score z max value g_score[neighbour] = sys.maxsize stevilo_pregledanih_vozlisc += 1 zacasen_g_score = g_score[list_to_tuple(current)] + distance( current, neighbour) if zacasen_g_score < g_score[neighbour]: if neighbour not in oce_od_elementa: oce_od_elementa[neighbour] = current g_score[neighbour] = zacasen_g_score #f_score[neighbour] = g_score[neighbour] + eucledian_distance(current, NxP_end) f_score[neighbour] = g_score[neighbour] + wrong_place( current, NxP_end) #dodam v graf vse podvozisse vozlisc neigbour napolni(graf, neighbour) if neighbour not in queue: queue.append(neighbour) return "Fail"
# # Copyright (C) 2009-2012 Poio Project # Author: António Lopes <*****@*****.**> # URL: <http://media.cidles.eu/poio/> # For license information, see LICENSE.TXT import sys from graf import Node, Graph, Edge, FeatureStructure, Annotation, GrafRenderer # Create three nodes node_one = Node('node_one') node_two = Node('node_two') node_three = Node('node_three') # Create the Annotation Graph and set the values graph = Graph() #Adding the nodes graph.nodes.add(node_one) graph.nodes.add(node_two) graph.nodes.add(node_three) # Create the edge edge = Edge(node_one, node_three) # Add an the edge #graph.add_edge(edge) # Add Features feature_strct = FeatureStructure() # Structure
def setUp(self): self.graph = Graph()