【问题标题】:Will my dicts of dicts work for this Dijkstra's algorithm?我的 dicts 是否适用于这个 Dijkstra 算法?
【发布时间】:2013-02-13 22:01:38
【问题描述】:

我对 Python 还是很陌生,我开始尝试在这里实现 Dijkstra 的算法:http://thomas.pelletier.im/2010/02/dijkstras-algorithm-python-implementation/。问题是我的矩阵看起来像这样:

    {
      2845: {27026: {'weight': 0.05950338}, 83860: {'weight': 0.013386887}},
     12422: {27023: {'weight': 0.0787193}, 27026: {'weight': 0.041424256}, 59721: {'weight': 0.11553069}},
     27022: {27025: {'weight': 0.1283993}, 83860: {'weight': 0.11746721}},
     27023: {12422: {'weight': 0.0787193}, 27025: {'weight': 0.22683257}},
     27025: {27022: {'weight': 0.1283993}, 27023: {'weight': 0.22683257}, 27026: {'weight': 0.20290035}},
     27026: {2845: {'weight': 0.05950338}, 12422: {'weight': 0.041424256}, 27025: {'weight': 0.20290035}},
     59721: {12422: {'weight': 0.11553069}},
     83860: {2845: {'weight': 0.013386887}, 27022: {'weight': 0.11746721}}
}

这仍然适用于上述算法吗?或者我是否需要稍微调整一下,如果可以,怎么办?

谢谢

编辑:

这里是我实现的算法:

def dijkstra(self, graph, start, end):
        D = {} # Final distances dict
        P = {} # Predecessor dict

        for node in graph.keys():
            D[node] = -1 # Vertices are unreachable
            P[node] = ""
        D[start] = 0 # The start vertex needs no move
        unseen_nodes = graph.keys() # All nodes are unseen

        while len(unseen_nodes) > 0:
            shortest = None
            node = ''
            for temp_node in unseen_nodes:
                if shortest == None:
                    shortest = D[temp_node]
                    node = temp_node
                elif (D[temp_node] < shortest):
                    shortest = D[temp_node]
                    node = temp_node
            unseen_nodes.remove(node)
            for child_node, child_value in graph[node].items():
                if D[child_node] < D[node] + child_value:
                    D[child_node] = D[node] + child_value
                    P[child_node] = node
        path = []
        node = end
        while not (node == start):
            if path.count(node) == 0:
                path.insert(0, node) # Insert the predecessor of the current node
                node = P[node] # The current node becomes its predecessor
            else:
                break
        path.insert(0, start) # Finally, insert the start vertex
        return path

我使用的矩阵如上,算法想要的矩阵是:

... graph = {
...     'A': {'B': 10, 'D': 4, 'F': 10},
...     'B': {'E': 5, 'J': 10, 'I': 17},
...     'C': {'A': 4, 'D': 10, 'E': 16},
...     'D': {'F': 12, 'G': 21},
...     'E': {'G': 4},
...     'F': {'H': 3},
...     'G': {'J': 3},
...     'H': {'G': 3, 'J': 5},
...     'I': {},
...     'J': {'I': 8},
... }

【问题讨论】:

  • 你链接的是一个python实现,你为什么不试试呢?
  • 您是否将 dijstra 代码放入类中? (在这种情况下,您的第一个参数是 self,因此您将有四个参数)
  • 我得到错误:如果 D[child_node]
  • 看来您正在更改您链接的 Dijkstra 算法。请提供您的代码以便我们为您提供帮助

标签: python algorithm graph dijkstra


【解决方案1】:

在给定的源代码示例中,权重只是一个整数,而不是字典。由于您的图表有一个带有“权重”键的字典,因此您必须根据它更改代码。

这是您的代码的正确版本:

def dijkstra(self, graph, start, end):
        D = {} # Final distances dict
        P = {} # Predecessor dict

        for node in graph.keys():
            D[node] = -1 # Vertices are unreachable
            P[node] = ""
        D[start] = 0 # The start vertex needs no move
        unseen_nodes = graph.keys() # All nodes are unseen

        while len(unseen_nodes) > 0:
            shortest = None
            node = ''
            for temp_node in unseen_nodes:
                if shortest == None:
                    shortest = D[temp_node]
                    node = temp_node
                elif (D[temp_node] < shortest):
                    shortest = D[temp_node]
                    node = temp_node
            unseen_nodes.remove(node)
            for child_node, child_value in graph[node].items():
                if D[child_node] < D[node] + child_value['weight']:  # I changed the code here
                    D[child_node] = D[node] + child_value['weight']   # I changed the code here
                    P[child_node] = node
        path = []
        node = end
        while not (node == start):
            if path.count(node) == 0:
                path.insert(0, node) # Insert the predecessor of the current node
                node = P[node] # The current node becomes its predecessor
            else:
                break
        path.insert(0, start) # Finally, insert the start vertex
        return path

【讨论】:

    【解决方案2】:

    我认为这应该可行,但这取决于您的代码。如果您想要更完整的答案,请发布其余代码。

    此外,使用 dicts 可能比仅制作 2D 数组更令人头疼。如果您真的想使用 dicts,我建议您使用 default dict

    【讨论】:

    • 我实际上只是想从链接中获取算法以使用上面的矩阵。
    猜你喜欢
    • 1970-01-01
    • 2020-07-03
    • 2020-06-28
    • 2018-01-05
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    相关资源
    最近更新 更多