Пример #1
0
def solve_tile_puzzle(board):
    parent_visitor = AggregateVisitor(visitors=[Parent])
    traverse(root_vertices = [board],
             visitor = parent_visitor, 
             generator = best_first_generator(tile_puzzle_graph(), 
                                              manhattan_distance())
             )
    return parent_visitor
Пример #2
0
    def compile(self, graph, edge_weight=None, source=None, target=None):
        if target is not None:
            graph = ReversedGraph(graph)
            self.tree_type = "from_target"
            self.initial_vertex = target
        else:
            self.tree_type = "from_source"
            self.initial_vertex = source

        if self.description == "breadth_first_search":
            v = AggregateVisitor(visitors={ParentEdge: None}, backend=graph)
            traverse(root_vertices=[self.initial_vertex], visitor=v, generator=breadth_first_generator(graph))
            self.tree = v.ParentEdge
        elif self.description == "dijkstra":
            self.tree = dijkstra.make_tree(graph, source=self.initial_vertex, edge_weight=edge_weight)
        elif self.description == "bellman_ford":
            self.tree = bellman_ford.make_tree(graph, source=self.initial_vertex, edge_weight=edge_weight)
Пример #3
0
 def compile(self, graph, edge_weight=None):
     initial_vertex = graph.vertices().__iter__().next()
     if self.description == 'depth_first_search':
         self.tree = traverse([initial_vertex],
                              TreeFindingVisitor(),
                              depth_first_generator(graph))
     elif self.description == 'kruskal':
         self.forest = forest.Forest()
         for edge in kruskal.spanning_tree_edges(graph=graph,
                                                 edge_weight=edge_weight):
             self.forest.add_edge(*edge)