简体   繁体   English

Dijsktra的最短路径算法

[英]Dijsktra's Shortest Path Algorithm

When I run this, the end output is a table with columns: 运行此命令时,最终输出是带有列的表:

Vertex - DisVal - PrevVal - Known.

The two nodes connected to my beginning node show the correct values, but none of the others end up getting updated. 连接到我的起始节点的两个节点显示正确的值,但其他节点最后都没有更新。 I can include the full program code if anyone wants to see, but I know the problem is isolated here. 如果有人想看的话,我可以提供完整的程序代码,但是我知道问题在这里是孤立的。 I think it may have to do with not changing the index the right way. 我认为这可能与不正确地更改索引有关。 This is a simple dijsktra's btw, not the heap/Q version. 这是简单的dijsktra的btw,而不是heap / Q版本。

Here's the rest of the code: http://ideone.com/UUOUn8 这是其余的代码: http : //ideone.com/UUOUn8

The adjList looks like this: [1: 2, 4, 2: 6, 3, ...] where it shows each node connected to a vertex. adjList看起来像这样:[1:2,4,2:6,6,3,...]其中显示了连接到顶点的每个节点。 DV = distance value (weight), PV = previous value (node), known = has it bee visited DV =距离值(重量),PV =先前值(节点),已知=蜜蜂是否来过

def dijkstras(graph, adjList):
    pv = [None] * len(graph.nodes)
    dv = [999]*len(graph.nodes)
    known = [False] * len(graph.nodes)

    smallestV = 9999
    index = 0
    dv[0] = 0
    known[0] = True
    for i in xrange(len(dv)):
        if dv[i] < smallestV and known[i]:
            smallestV = dv[i]
            index = i
        known[index] = True
        print smallestV
        print index
        for edge in adjList[index]:
            if (dv[index]+graph.weights[(index, edge)] < dv[edge]):
                dv[edge] = dv[index] + graph.weights[(index, edge)]
                pv[edge] = index

    printTable(dv, pv, known)

The first iteration sets smallestV and index to 0 unconditionally, and they never change afterwards (assuming non-negative weights). 第一次迭代将无条件地将minimateV和index设置为0,并且此后它们再也不会更改(假定非负权重)。

Hard to tell what you are trying to do here. 很难说出您要在这里做什么。

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

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