for neighbor in state.neighbors():
            update_cost(graph, neighbor, state)
            if neighbor.get_label() not in list(
                    set([node.get_label() for node in frontier + explored])):
                neighbor.f = neighbor.cost + neighbor.goal_cost
                heapq.heappush(frontier, neighbor)
            elif find_by_label(frontier, neighbor) >= 0:
                update_frontier(frontier, neighbor)
                neighbor.f = neighbor.cost + neighbor.goal_cost
    return False


if __name__ == "__main__":
    graph = Graph()
    graph.add_node_from([
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
        "O"
    ])
    graph.add_edges_from([
        ("A", "B", 2),
        ("A", "C", 1),
        ("A", "D", 3),
        ("B", "E", 5),
        ("B", "F", 4),
        ("C", "G", 6),
        ("C", "H", 3),
        ("D", "I", 2),
        ("D", "J", 4),
        ("F", "K", 2),
        ("F", "L", 1),
        ("F", "M", 4),
        ("H", "N", 2),
Example #2
0
        if goalTest == state:
            return True
        for neighbor in state.neighbors():
            update_cost(graph, current_node=neighbor, prev_node=state)
            if neighbor.get_label() not in list(
                    set([e.get_label() for e in frontier + explored])):
                heapq.heappush(frontier, neighbor)
            elif find_by_label(frontier, neighbor) is not -1:
                update_frontier(frontier, neighbor)
    return False


if __name__ == "__main__":
    graph = Graph()
    graph.add_node("S")
    graph.add_node_from(["S", "A", "B", "C", "D", "E", "F", "G", "H"])
    graph.add_edges_from(
        [
            ("S", "A", 3),
            ("S", "B", 6),
            ("S", "C", 2),
            ("A", "D", 3),
            ("B", "D", 4),
            ("B", "G", 9),
            ("B", "E", 2),
            ("C", "E", 1),
            ("D", "F", 5),
            ("E", "H", 5),
            ("F", "E", 6),
            ("H", "G", 8),
            ("F", "G", 5),