【问题标题】:Dijkstra algorithm to select randomly an adjacent node with same minimum weightDijkstra 算法随机选择具有相同最小权重的相邻节点
【发布时间】:2022-01-24 00:20:33
【问题描述】:

我已经实现了 Dijkstra 的算法,但我遇到了问题。它总是打印相同的最小路径,而可能有其他具有相同权重的路径。

如何更改我的算法,使其随机选择具有相同权重的邻居?

我的算法如下:

def dijkstra_algorithm(graph, start_node):
    unvisited_nodes = list(graph.get_nodes())
 
    # We'll use this dict to save the cost of visiting each node and update it as we move along the graph   
    shortest_path = {}
 
    # We'll use this dict to save the shortest known path to a node found so far
    previous_nodes = {}
 
    # We'll use max_value to initialize the "infinity" value of the unvisited nodes   
    max_value = sys.maxsize
    for node in unvisited_nodes:
        shortest_path[node] = max_value
    # However, we initialize the starting node's value with 0   
    shortest_path[start_node] = 0
    
    # The algorithm executes until we visit all nodes
    while unvisited_nodes:
        # The code block below finds the node with the lowest score
        current_min_node = None
        for node in unvisited_nodes: # Iterate over the nodes
            if current_min_node == None:
                current_min_node = node
            elif shortest_path[node] < shortest_path[current_min_node]:
                current_min_node = node
                
        # The code block below retrieves the current node's neighbors and updates their distances
        neighbors = graph.get_outgoing_edges(current_min_node)
        for neighbor in neighbors:
            tentative_value = shortest_path[current_min_node] + graph.value(current_min_node, neighbor)
            if tentative_value < shortest_path[neighbor]:
                shortest_path[neighbor] = tentative_value
                # We also update the best path to the current node
                previous_nodes[neighbor] = current_min_node
 
        # After visiting its neighbors, we mark the node as "visited"
        unvisited_nodes.remove(current_min_node)
    
    return previous_nodes, shortest_path

【问题讨论】:

    标签: python python-3.x algorithm python-2.7 graph


    【解决方案1】:
            # The code block below finds all the min nodes
            # and randomly chooses one for traversal
            min_nodes = []
            for node in unvisited_nodes: # Iterate over the nodes
                if len(min_nodes) == 0:
                    min_nodes.append(node)
                elif shortest_path[node] < shortest_path[min_nodes[0]]:
                    min_nodes = [node]
                else:
                # this is the case where 2 nodes have the same cost
                # we are going to take all of them
                # and at the end choose one randomly
                    min_nodes.append(node)
            current_min_node = random.choice(min_nodes)
    

    代码的作用如下:

    1. 它不采用第一个最小元素,而是创建一个包含所有最小元素的列表。
    2. 最后它随机选择一个最小的元素。

    这既可以保证 Dijkstra 不变性,又可以在最便宜的路径中选择一条随机路径。

    【讨论】:

      【解决方案2】:

      可能只是尝试这样的事情

      random.shuffle(neighbors)
      for neighbor in neighbors:
          ...
      

      它应该随机访问邻居(假设邻居是一个列表或元组......如果它首先是一个生成器调用列表......

      【讨论】:

        猜你喜欢
        • 2021-06-10
        • 2021-08-13
        • 1970-01-01
        • 1970-01-01
        • 2012-06-26
        • 1970-01-01
        • 1970-01-01
        • 2014-04-01
        • 2021-05-02
        相关资源
        最近更新 更多