Exemple #1
0
def a_star_search(graph, start, goal):
    searchFrontier = PriorityQueue()
    searchFrontier.put(start, heuristic[start])
    currentCost = {}
    currentCost[start] = 0

    path = ""

    while not searchFrontier.empty():
        current = searchFrontier.get()

        # add node in path
        path += current + " "

        if current == goal:
            break

        for next in graph.neighbors(current):
            newCost = currentCost[current] + weights[(current, next)]
            if next not in currentCost or newCost < currentCost[next]:
                currentCost[next] = newCost
                priority = newCost + heuristic[next]  # set priority
                searchFrontier.put(next,
                                   priority)  # add node in priority queue

    return path
Exemple #2
0
def bestfs(graph, start, goal):
    searchFrontier = PriorityQueue()
    searchFrontier.put(start, heuristic[start])
    predecessor = {}
    predecessor[start] = None

    path = ""

    while not searchFrontier.empty():
        current = searchFrontier.get()

        path += current + " "

        if current == goal:
            break

        for next in graph.neighbors(current):
            if next not in predecessor:
                priority = heuristic[next]
                searchFrontier.put(next, priority)
                predecessor[next] = current

    return path