python实现node迭代和反向迭代

一.实现Node迭代

构建了一个自定义容器Node对象,里面包含有列表、元组或其他可迭代对象,可以通过定义一个iter () 方法,将迭代操作代理到容器内部的对象上去,具体如下源码:

class Node:
    def __init__(self, value):
        self._value = value
        self._children = []

    def __repr__(self):
        return 'Node({!r})'.format(self._value)

    def add_child(self, node):
        self._children.append(node)

    def __iter__(self):
        return iter(self._children)

    def depth_first(self):
        yield self
        for c in self:
            yield from c.depth_first()

if __name__ == "__main__":
    root = Node(0)
    child1 = Node(1)
    child2 = Node(2)
    root.add_child(child1)
    root.add_child(child2)
    child1.add_child(Node(3))
    child1.add_child(Node(4))
    child2.add_child(Node(5))

    for ch in root.depth_first():
        print(ch)

result如下:

Node(0)
Node(1)
Node(3)
Node(4)
Node(2)
Node(5)

Process finished with exit code 0

总结:Python 的迭代协议要求一个 iter () 方法返回一个特殊的迭代器对象,这个迭代器对象实现了 next () 方法并通过 StopIteration 异常标识迭代的完成.

二.反向迭代

可以通过在自定义类上实现迭代.如下:

class Countdown:
    
    def __init__(self, start):
        self.start = start
        

    def __iter__(self):
        n = self.start
        while n > 0:
            yield n
            n -= 1

    def __reversed__(self):
        n = 1
        while n <= self.start:
            yield n
            n += 1

def use_count():
    for val in reversed(Countdown(10)):
        print(val)
    print("--------------")
    for val in Countdown(10):
        print(val)

if __name__ == "__main__":
    use_count()

result如下:

1
2
3
4
5
6
7
8
9
10
--------------
10
9
8
7
6
5
4
3
2
1

Process finished with exit code 0

总结:定义一个反向迭代器可以使得代码非常的高效,因为它不再需要将数据填充到一个
列表中然后再去反向迭代这个列表

 

### 回答1: 以下是双向A星算法的Python实现: ```python from queue import PriorityQueue def astar(start, goal, graph): # initialize start and goal nodes start_node = Node(start, None) goal_node = Node(goal, None) # initialize open and closed lists for forward search open_list_f = PriorityQueue() closed_list_f = set() # initialize open and closed lists for backward search open_list_b = PriorityQueue() closed_list_b = set() # add start and goal nodes to their respective open lists open_list_f.put(start_node) open_list_b.put(goal_node) while not open_list_f.empty() and not open_list_b.empty(): # get node with lowest f score from forward open list current_node_f = open_list_f.get() # check if current node is in backward closed list if current_node_f.state in closed_list_b: # path found return path(current_node_f, goal_node) # add current node to forward closed list closed_list_f.add(current_node_f.state) # expand current node's children for child_state, cost in graph[current_node_f.state].items(): # create child node child_node = Node(child_state, current_node_f) child_node.g = current_node_f.g + cost child_node.h = heuristic(child_state, goal) # check if child node is in forward closed list if child_node.state in closed_list_f: continue # check if child node is in forward open list if child_node in open_list_f.queue: # update existing node if new path is better for n in open_list_f.queue: if n == child_node and n.g > child_node.g: n.g = child_node.g n.parent = child_node.parent break else: # add child node to forward open list open_list_f.put(child_node) # get node with lowest f score from backward open list current_node_b = open_list_b.get() # check if current node is in forward closed list if current_node_b.state in closed_list_f: # path found return path(start_node, current_node_b) # add current node to backward closed list closed_list_b.add(current_node_b.state) # expand current node's children for child_state, cost in graph[current_node_b.state].items(): # create child node child_node = Node(child_state, current_node_b) child_node.g = current_node_b.g + cost child_node.h = heuristic(child_state, start) # check if child node is in backward closed list if child_node.state in closed_list_b: continue # check if child node is in backward open list if child_node in open_list_b.queue: # update existing node if new path is better for n in open_list_b.queue: if n == child_node and n.g > child_node.g: n.g = child_node.g n.parent = child_node.parent break else: # add child node to backward open list open_list_b.put(child_node) # no path found return None def heuristic(state, goal): # calculate Manhattan distance between state and goal return abs(state[0] - goal[0]) + abs(state[1] - goal[1]) def path(start_node, goal_node): # generate path from start to goal node path = [] current_node = start_node while current_node != goal_node: path.append(current_node.state) current_node = current_node.parent path.append(goal_node.state) return path[::-1] class Node: def __init__(self, state, parent): self.state = state self.parent = parent self.g = 0 self.h = 0 def __lt__(self, other): return self.g + self.h < other.g + other.h def __eq__(self, other): return self.state == other.state def __hash__(self): return hash(self.state) ``` 这是一个双向A星算法的实现,用于寻找从起点到终点的最短路径。它使用了两个优先队列,一个用于正向搜索,另一个用于反向搜索。每次迭代,它从正向队列中取出f值最小的节点,并检查它是否在反向队列的闭合列表中。如果是,则找到了一条路径。否则,它将该节点添加到正向闭合列表中,并扩展其子节点。然后,它从反向队列中取出f值最小的节点,并检查它是否在正向闭合列表中。如果是,则找到了一条路径。否则,它将该节点添加到反向闭合列表中,并扩展其子节点。这个过程会一直持续,直到找到一条路径或者两个队列都为空。 ### 回答2: 双向A*算法是一种启发式搜索算法,用于寻找图形上两个节点之间最短路径。与传统的A*算法相比,双向A*算法从起点和终点分别开始搜索,通过两个方向的搜索相互协作,以减少搜索的时间和空间复杂度。 以下是一个用Python实现的双向A*算法: 首先,我们需要定义一个Node类表示图中的节点,包括节点的位置坐标、从起点到该节点的实际代价g和从该节点到目标节点的预估代价h。 接下来,我们定义一个双向A*算法的函数,输入为起点节点和终点节点。我们使用两个open列表分别存放起点和终点搜索的开放节点,以及两个closed列表存放已经搜索过的节点。 然后,我们初始化两个open列表,将起点和终点节点添加进去,并计算起点和终点节点的初始代价。 接下来,我们进入循环直到两个open列表都为空或者找到了相遇的节点。在每次循环中,我们分别从起点和终点的open列表中选取代价最小的节点进行扩展。 在扩展节点时,我们先判断是否找到了相遇的节点。如果节点已经在另一个方向的closed列表中,说明找到了相遇的节点,可以结束搜索。否则,我们计算节点的邻居节点,并更新节点的代价和父节点。 最后,如果找到了相遇的节点,我们可以通过回溯父节点,从起点到终点重构最短路径。 双向A*算法相较于传统的A*算法,通过同时从起点和终点进行搜索,可以显著减少搜索的范围和时间复杂度,提高搜索效率。 ### 回答3: 双向A*算法是一种路径搜索算法,它以起点和终点为中心,同时从起点和终点分别进行扩展搜索,直到两个搜索路径相交或找到最优路径。 实现双向A*算法的关键是要同时维护起点和终点的OPEN集合和CLOSED集合。具体步骤如下: 1. 初始化起点和终点的OPEN集合和CLOSED集合,起点的OPEN集合中只包含起点,终点的OPEN集合中只包含终点。 2. 从起点和终点的OPEN集合中分别选取f值最小的节点进行搜索。 3. 对于选取的节点,计算它到起点和终点的估计代价值f值,将它从OPEN集合中移至CLOSED集合,表示已经搜索过。 4. 遍历选取的节点的相邻节点,计算相邻节点到起点和终点的估计代价值g值和h值,计算f值,更新节点的父节点信息。 5. 如果在遍历相邻节点时发现有节点已经存在于起点和终点的CLOSED集合中,说明找到了双向搜索路径的交叉点,可以结束搜索,并根据交叉点的父节点信息构建最优路径。 6. 如果起点和终点的OPEN集合为空,表示没有找到路径,搜索失败。 7. 如果起点和终点的OPEN集合中存在相同的节点,表示找到了双向搜索路径的中间节点,可以结束搜索,并根据中间节点的父节点信息构建最优路径。 8. 返回最优路径。 在Python实现双向A*算法,需要使用一个节点类来表示节点对象,包含节点坐标、g值、h值和f值等信息。同时,需要使用一个地图类来表示地图对象,包含地图的大小、起点和终点坐标等信息,以及一些辅助函数来计算节点的估计代价值和更新节点信息。 总之,双向A*算法的Python实现可以根据上述步骤进行编写,通过维护起点和终点的OPEN集合和CLOSED集合,并使用节点类和地图类来实现算法的具体细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值