Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Dijkstra'实施中的错误;s算法_Python_Python 3.x_Algorithm_Dijkstra - Fatal编程技术网

Python Dijkstra'实施中的错误;s算法

Python Dijkstra'实施中的错误;s算法,python,python-3.x,algorithm,dijkstra,Python,Python 3.x,Algorithm,Dijkstra,帮助学习Dijkstra的算法。任务的本质:必须找到最可能的方法。这里使用概率乘法。 输入: 1009900,1100(顶点数、弧数、起始顶点和结束顶点) 1,2,0.3(起始顶点、结束顶点和通过路径的概率) 我的结果是: 35, 35, 3, 54, 98, 100 (most likely successful path) 0.9577 (probability of passage). 正确结果: 1, 92, 35, 3, 54, 98, 100 0.9577 我认为这部分代码工

帮助学习Dijkstra的算法。任务的本质:必须找到最可能的方法。这里使用概率乘法。 输入:
1009900,1100
(顶点数、弧数、起始顶点和结束顶点)
1,2,0.3
(起始顶点、结束顶点和通过路径的概率)

我的结果是:

35, 35, 3, 54, 98, 100 (most likely successful path)
0.9577 (probability of passage).
正确结果:

1, 92, 35, 3, 54, 98, 100
0.9577 
我认为这部分代码工作不正常(构建最短路径):

while(i!=0):
对于范围内的j(结束,-1,-1):
如果(ves[i]!=0.0且四舍五入(重量[i]/ves[i],6)=重量[j]且i最小重量:
最小重量=重量[i]
ID\u min\u weight=i
对于范围(N)中的i:

if(四舍五入(权重[ID\u-min\u-weight]*矩阵[ID\u-min\u-weight][i],6)>权重[i]和i与浮点数的比较,如
ves[i]!=0.0
容易出错。您可以尝试将其转换为int,然后进行比较。不知道这是否是问题的根源,但这可能是意外行为的根源。除此之外,如果您只存储每个节点的前置项,而不是根据权重计算它,则会更容易。此外,我不明白为什么要舍入。我舍入是因为Python的计算如下:0.27/0.9=0。(3)1
 while (i!=0):
            for j in range(end, -1, -1):
                if (ves[i]!=0.0 and round(weight[i] / ves[i],6) == weight[j] and i<=test):
                    result.append(j+1)
                    test=j
            i -= 1
def Dijkstra(N, S, matrix, end):
    valid = [True] * N
    weight = [0.01] * N
    weight[S] = 1.0
    ves=[0.0]*N
    result=[]
    for i in range(N):
        min_weight = 0.0
        ID_min_weight = 0.0
        for i in range(len(weight)):
            if valid[i] and weight[i] > min_weight:
                min_weight = weight[i]
                ID_min_weight = i
        for i in range(N):
            if (round(weight[ID_min_weight] * matrix[ID_min_weight][i],6) > weight[i] and i<end+1):
                weight[i] = round(weight[ID_min_weight] * matrix[ID_min_weight][i],6)
                ves[i]=matrix[ID_min_weight][i]
        valid[ID_min_weight] = False

    result.append(end + 1)
    i = end
    test = end
    while (i!=0):
        for j in range(end, -1, -1):
            if (ves[i]!=0.0 and round(weight[i] / ves[i],6) == weight[j] and i<=test):
                result.append(j+1)
                test=j
        i -= 1


    result.reverse()
    print (*result)

    return round(weight[end],4)

str=[]
matrix2=[]
for i in input().split():
    str.append(int(i))

N = str[0]   
M = str[1]   
begin = str[2]  
end = str[3]    

matrix = [[float (j) for j in input().split()] for i in range(M)]

for i in matrix:
    if len(i)>3:
        exit()

for i in range(N):
    matrix2.append([])
    for j in range(N):
        matrix2[i].append(0.01)

for i in range(M):
    matrix2[int(matrix[i][0]-1)][int(matrix[i][1]-1)] = matrix[i][2]

print(Dijkstra(N, begin-1, matrix2,end-1))