有人能告诉我这个Dijkstra算法中优先队列的空间复杂度吗?请注意,一个顶点可能会被添加到队列多次。但由于visited集合的存在,它不会被处理多次。这就是为什么我想知道队列的最大大小。
def shortestReach(n, edges, start,target):
adjList = collections.defaultdict(list)
for parent, child, cost in edges:
parent -= 1
child -= 1
adjList[parent].append((child, cost))
adjList[child].append((parent, cost))
priorityQueue = queue.PriorityQueue()
priorityQueue.put((0, start))
visited = set()
while priorityQueue.qsize() > 0:
costPar, parent = priorityQueue.get()
if parent == target:
return costPar
if parent not in visited:
for adj in adjList[parent]:
child, cost = adj
priorityQueue.put((cost + costPar, child))
visited.add(parent)