简体   繁体   English

使用邻接表的无向加权图上的最小成本路径

[英]Minimum cost path on a undirected weighted graph using an adjacency list

I have implemented a minimum cost path function to my undirected weighted graph using an adjacency list.我已经使用邻接表实现了到我的无向加权图的最低成本路径 function。 My approach of the minimum cost path function does not use a priority queue.我的最低成本路径 function 的方法不使用优先级队列。 My goal is to calculate and display the shortest path from a starting node.我的目标是计算并显示从起始节点开始的最短路径。

I'm able to calculate the shortest path starting at node A, however, the order of the nodes does not match the order of the nodes.我能够计算从节点 A 开始的最短路径,但是,节点的顺序与节点的顺序不匹配。 Would it be better if I implemented a priority queue?如果我实现一个优先级队列会更好吗? Any help would be appreciated!任何帮助,将不胜感激!

The minimum cost paths starting at A 
 Expected B(4) C(12) D(19) E(21) F(11) G(9) H(8) I(14) J(inf) 

 Actually B(4) H(8) C(12) G(9) I(14) D(19) F(11) E(21)
The adjacency list:
A- {'B': 4, 'H': 8}
B- {'A': 4, 'C': 8, 'H': 11}
C- {'B': 8, 'D': 7, 'F': 4, 'I': 2}
D- {'C': 7, 'E': 9, 'F': 14}
E- {'D': 9, 'F': 10}
F- {'C': 4, 'D': 14, 'E': 10, 'G': 2}
G- {'F': 2, 'H': 1, 'I': 6}
H- {'A': 8, 'B': 11, 'G': 1, 'I': 7}
I- {'C': 2, 'G': 6, 'H': 7}
J- {}

My code我的代码

class WGraph:
    def __init__(self, size=10):
        self.size = 10
        self.nodeList = {}
        self.adjacencyMatrix = [[0] * size for i in range(size)]  # initialize matrix

    def addNode(self, name):
        """Adds node to adjacency list"""
        self.nodeList[name] = {}

    def addEdge(self, startIndex, endIndex, weight):
        """Add edges for adjacency list and matrix"""
        self.nodeList[startIndex][endIndex] = weight
        self.nodeList[endIndex][startIndex] = weight

        index1 = list(self.nodeList.keys()).index(startIndex)
        index2 = list(self.nodeList.keys()).index(endIndex)
        self.adjacencyMatrix[index1][index2] = weight

    def displayAdjacency(self):
        """Displays adjacency list with edges and weight"""
        for node in self.nodeList:
            print(node + "- " + str(self.nodeList[node]))


    def minCostPaths(self, vertex):
        visited = {vertex: 0}
        queue = [(vertex, 0)]
        while queue:
            node, cost = queue.pop(0)
            for neighbor in self.nodeList[node]:
                if neighbor not in visited or cost + self.nodeList[node][neighbor] < visited[neighbor]:
                    visited[neighbor] = cost + self.nodeList[node][neighbor]
                    queue.append((neighbor, cost + self.nodeList[node][neighbor]))
        output = ""
        for node in visited:
            if node != vertex:
                output += f"{node}({visited[node]}) "
        return output[:-2] + ")"


# Driver Code
graph = WGraph()

graph.addNode('A')
graph.addNode('B')
graph.addNode('C')
graph.addNode('D')
graph.addNode('E')
graph.addNode('F')
graph.addNode('G')
graph.addNode('H')
graph.addNode('I')
graph.addNode('J')

graph.addEdge('A', 'B', 4)
graph.addEdge('A', 'H', 8)
graph.addEdge('B', 'C', 8)
graph.addEdge('B', 'H', 11)
graph.addEdge('C', 'D', 7)
graph.addEdge('C', 'F', 4)
graph.addEdge('C', 'I', 2)
graph.addEdge('D', 'E', 9)
graph.addEdge('D', 'F', 14)
graph.addEdge('E', 'F', 10)
graph.addEdge('F', 'G', 2)
graph.addEdge('G', 'H', 1)
graph.addEdge('G', 'I', 6)
graph.addEdge('H', 'I', 7)

graph.displayAdjacency()

print("The minimum cost paths starting at A ")
print(" Expected B(4) C(12) D(19) E(21) F(11) G(9) H(8) I(14) J(inf) ")
print(" Actually " + graph.minCostPaths('A'), end="\n")

In your minCostPaths() function, you need to get the list of vertices from self.nodeList , not from visited .在您的minCostPaths() function 中,您需要从self.nodeList获取顶点列表,而不是从visited获取。 The order of BFS traversal is stored in visited , which isn't the ordering you need. BFS 遍历的顺序存储在visited中,这不是您需要的顺序。

        output = ""
        for node in self.nodeList:
            if node != vertex:
                output += f"{node}({visited.get(node, 'inf')}) "
        return output[:-2] + ")"

Output: Output:

The minimum cost paths starting at A 
 Expected B(4) C(12) D(19) E(21) F(11) G(9) H(8) I(14) J(inf) 
 Actually B(4) C(12) D(19) E(21) F(11) G(9) H(8) I(14) J(inf)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM