微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我的输出扩展节点有一些问题我该如何解决?

如何解决我的输出扩展节点有一些问题我该如何解决?

Graph

如果从节点 i 到节点 j 存在权重 A 的链接我有上图中的输入 adj 矩阵如下 [i,j] = A>0,否则为 [i,j] = 0。

0 4 3 0 0 0 0
4 0 0 0 12 5 0
3 0 0 7 10 0 0
0 0 7 0 2 0 0
0 12 10 2 0 0 5
0 5 0 0 0 0 16
0 0 0 0 5 16 0

起始节点(self.source)为0或顶点a,目的节点为6或顶点z

这个案例的结果是:

  • 扩展节点:0 1 4 2 3 (6) | a b e c d z
  • 返回路径:0 1 4 6 | a b e z

我的输出扩展节点错误0 1 4 | a b e

我哪里做错了?我该如何解决

我的代码

def DFS(self):

    explored = []
    frontier = deque()
    frontier.append([self.source])

    temp = deque()

    while True:            
        if len(frontier) == 0:
            self.expanded = explored
            return

        temp.clear()
        path = frontier.popleft()
        node = path[-1]
        explored.append(node)

        for i,s in enumerate(self.matrix[node]):
            if s != 0:
                if i not in explored:
                    newPath = list(path)
                    newPath.append(i)
                    
                    if i == self.des:
                        self.returnPath = newPath[::-1]
                        self.expanded = explored
                        return
                    else:
                        temp.append(newPath)
        temp += frontier
        frontier = temp.copy()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。