我的字典嵌套字典适用于这个Dijkstra算法吗?

2 投票
2 回答
1329 浏览
提问于 2025-04-17 15:49

我刚开始学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},
... }

2 个回答

0

我觉得这样应该可以,但具体还得看你的代码。如果你想要更详细的答案,请把剩下的代码也发上来。

另外,使用字典可能会比直接用二维数组麻烦一些。如果你真的想用字典,我建议你使用一个叫做 默认字典 的东西。

0

在这个示例代码中,权重只是一个整数,而不是一个字典。因为你的图里有一个包含“weight”这个键的字典,所以你需要根据这个情况来修改代码。

下面是你代码的正确版本:

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

撰写回答