可以修改此代码以使优先级队列在O(logn)时间内减小其密钥吗?

时间:2019-02-17 01:27:21

标签: python algorithm graph-algorithm priority-queue dijkstra

试图用python编写dijkstras。当我无法修改元组时,如何在O(logn)中实现reduce键操作?

我正在编写一个片段来解决邻接表上的dijkstras,并且需要优先级队列实现。我目前正在将(优先级,值)元组传递给pq,因此heapify为我处理推送和弹出。但是,我无法修改元组,因此无法高效地降低任何项目的优先级,并且必须弹出所有项目直到找到所需的项目,然后将所有项目读取到pq,这使得时间复杂度为O(V ^ 2)。有什么方法可以修改此代码,以便reduceKey操作可以在O(logn)时间工作吗?最好不涉及任何课程。我不能使用列表而不是元组。否则不会堆积

def decrease_key(pq, v, val):
    li = []
    u = heapq.heappop(pq)

    while u[1] is not v:
        li.append(u)
        u = heapq.heappop(pq)

    for l in li:
        heapq.heappush(pq, l)

    heapq.heappush(pq, (val, v))


def dijkstra(G, src, dist, V):

    for v in range(V):
        dist[v] = float('inf')
    dist[src] = 0

    pq = []

    for k, v in dist.items():
        pq.append((v, k))

    while pq:
        u = heapq.heappop(pq)[1]
        for v, w in G[u]:
            if dist[u] + w < dist[v]:
                dist[v] = dist[u] + w
                decrease_key(pq, v, dist[v])

O(n ^ 2)与所需的O(nlogn)

2 个答案:

答案 0 :(得分:1)

正如@DavidEisenstat在注释中指示的那样,如果您只是向堆中添加多个记录,则不需要reduce_key。

您还需要跟踪从优先级队列中弹出的节点,因此您只能在第一次看到该节点时对其进行处理。 (否则,最坏情况下的复杂性就会增加)

最后,如果您避免将这些无限成本推入堆中,并且在实际降低成本时仅推入一个新节点,那就很好了。总之,像这样:

def dijkstra(G, src, dist, V):

    for v in range(V):
        dist[v] = float('inf')
    dist[src] = 0

    pq = [(0,src)]
    seen = [False]*V

    while pq:
        u = heapq.heappop(pq)[1]
        if seen[u]:
            continue
        seen[u] = True
        for v, w in G[u]:
            if dist[u] + w < dist[v]:
                dist[v] = dist[u] + w
                heapq.heappush( pq, (dist[v],v) )

答案 1 :(得分:0)

已经有一段时间了,但我认为堆的目的是保留最小距离节点的队列以重新计算和更新距离。

import heapq

def dijkstra(graph, src):
    # Read all graph nodes
    nodes = list(graph.keys())

    # Initialize all distances to infinity and build a queue of unvisted nodes
    dist = {}
    pq = []
    for node in nodes:
        dist[node] = float('inf')
    dist[src] = 0
    heapq.heappush(pq, (0, src))

    # While shorter distances to nodes, recalculate neighbor distances
    while pq:
        src_dist, unvisited_node = heapq.heappop(pq)

        # Recalculate total distance for each neighbor
        unvisted_neighbors = graph.get(unvisited_node, [])
        for n_node, n_dist in unvisted_neighbors:
            test_dist = src_dist + n_dist

            # If test distance is shorted, update and enqueue
            if test_dist < dist[n_node]:
                dist[n_node] = test_dist
                heapq.heappush(pq, (test_dist, n_node))

    return dist

test_graph = {
    'a': (('b',  7), ('c',  9), ('f', 14)),
    'b': (('a',  7), ('d', 15), ('c', 10)),
    'c': (('a',  9), ('b', 10), ('d', 11), ('f',  2)),
    'd': (('b', 15), ('c', 11), ('e',  6)),
    'e': (('d',  6), ('f',  9)),
    'f': (('c',  2), ('e',  9))
}
'''Lazy graph structure.

key: source node name
value: adjacent node name and distance pairs

https://www.bogotobogo.com/python/images/Graph/graph_diagram.png
'''
src_node = 'e'
d = dijkstra(test_graph, src_node)
for k, v in d.items():
    print(f'{src_node}->{k}: {v}')