def a_star(graph, visited, wait_list): while True: try: node = wait_list.pop(0) visited.append(node) if node.v == '.': bfs_mark_path(node) return True elif node.v == '%': continue else: neighbor=graph_fun.get_neighbor(graph, node) for one in neighbor: if one not in visited: if one not in wait_list: one.save_parent(node) one.update_g(node.g+1) one.update_f() wait_list.append(one) else: if one.g<node.g+1: one.save_parent(node) one.update_g(node.g+1) one.update_f() wait_list.sort(key=operator.attrgetter('f')) except IndexError: print "Sorry! Cannot find a path to the end!" return False
def dfs(graph, visited, node): visited.append(node) if node.v=='%': return False neighbor = graph_fun.get_neighbor(graph, node) for one in neighbor: if one not in visited: if one.v=='.': return True else: if dfs(graph, visited, one): one.update_value('.') return True return False
def greedy_bfs(graph, visited, node): if node.v == '.': return True elif node.v == '%': return False else: visited.append(node) neighbor = graph_fun.get_neighbor(graph, node) neighbor.sort(key=operator.attrgetter('h')) for one in neighbor: if one not in visited: if greedy_bfs(graph, visited, one): node.update_value('.') return True return False
def bfs(graph, visited, queue): while True: try: node = queue.popleft() visited.append(node) if node.v=='.': bfs_mark_path(node) return True elif node.v=='%': continue else: neighbor = graph_fun.get_neighbor(graph, node) #add new nodes to the queue for one in neighbor: if one not in visited: if one not in queue: one.save_parent(node) queue.append(one) except IndexError: print "Sorry! Cannot find a path to the end!" return False