简体   繁体   English

具有二级成本限制的 Dijkstra 算法

[英]Dijkstra's algorithm with secondary cost limitation

As part of a school assignment, I am required to implement Dijkstra's algorithm on a weighted, cyclic graph with a caveat - there exist a secondary weight/cost (henceforth referred to as B ) on each edge, and the algorithm is to find the shortest path from a source node to a target node while keeping total B to be under a certain constant value.作为学校作业的一部分,我需要在加权循环图上实现 Dijkstra 算法,但需要注意的是 - 每条边都存在次要权重/成本(以下称为B ),该算法是找到最短的从源节点到目标节点的路径,同时保持总B在某个恒定值以下。

On top of basic Dijkstra's, I have added an additional condition to the relaxation/update condition, which is that the current shortest path must be under the constant B value given.在基本的 Dijkstra 之上,我在松弛/更新条件中添加了一个附加条件,即当前最短路径必须低于给定的恒定B值。

if (no shortest path yet exists to neighbour node X or the current shortest path to the neighbour node is longer than the current shortest path to current node + current edge to neighbour node) and total **B** of current shortest path to current node + current edge to neighbour node is less than input **B** value:

   update current shortest path to neighbour node

However, the result returned, is neither the shortest path or has the lowest B .但是,返回的结果既不是最短路径,也不是最低的B In other words, there exists other paths with lower distance as well as cost B .换言之,存在其他距离更短且成本为B的路径。 May I ask what is the problem with my understanding?请问我的理解有什么问题吗?

Edit: Current Code:编辑:当前代码:

class Node:
    def __init__(self, node, p): 
        self.node = node
        self.p = p

    def __lt__(self, other):
        return self.p < other.p
    

def b_dijkstra(G, S, T, constraint):
    start = [Node(S, 0.0)]                       # Creating initial start node using HeapQ and setting its value to 0.0
    goal = set()
    pred = dict()                                                         # Dictionary to store visited nodes in a graph
    dist = dict()                                                     # Dictionary to store distance from point to point
    b = dict()
    pred[S] = None
    dist[S] = 0.0
    b[S] = 0.0
    count = 0
    while start:
        count += 1
        C = hq.heappop(start).node                   # Pop the smallest item off the heap, maintaining the heap invariant.
        if C == T:
            print('Nodes searched:',count)
            print(' b:', b[C])
            return traversal(T, pred)
        goal.add(C)
        #for each neighbour
        for pointer in G[C]:
            if pointer in goal:
                continue
            dist_temp = dist[C] + G[C][pointer]['weight']
            b_temp =  b[C] + G[C][pointer]['b']
            if (pointer not in dist or dist[pointer] > dist_temp) and constraint >= b_temp: #if stored distance is greater than the calculated distance
                dist[pointer] = dist_temp
                pred[pointer] = C
                b[pointer] =  b_temp
                hq.heappush(start, Node(pointer, dist[C] + G[C][pointer]['weight']))          # Adding vertex to queue
    return []

Let's assume you have this graph (weights given as weight/B) with a limit on B of 15. What will your algorithm do?假设您有此图(权重以权重/B 的形式给出),B 的限制为 15。您的算法将做什么? If I understand you correctly, it will first find the shortest path to A costing 1/10 and then fail to find any path to End satisfying the condition on B. Is my understanding correct?如果我理解正确的话,它会首先找到成本为 1/10 的 A 的最短路径,然后找不到任何满足 B 条件的 End 路径。我的理解是否正确? If yes, it should be clear what is wrong with your algorithm.如果是,应该清楚你的算法有什么问题。

示例图

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

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