【问题标题】:logic error in dijkstra's algorithm involving 2d arrays涉及二维数组的dijkstra算法中的逻辑错误
【发布时间】:2018-04-15 16:48:41
【问题描述】:

我的项目是创建一个 python 程序,该程序将在用户可以输入的一组节点上使用 dijkstra 的最短路径算法,我的想法是我可以在用户输入的任何大小的地图上执行此操作。 但是我没有深入了解它,因为我在开始时遇到了一个错误。

#if user wants a small map i.e. less than 27 nodes, the nodes will be named differently to if it is a large map
global array_type
#creates the list
node_list=[]
node_array=[]
#makes sure the user only inputs a valid number of nodes
def check_int(x):
    while True:
        try:
            #checks if node is integer
            int(x)
            #checks if node is negative
            if int(x)<1:
                #if it is, then it changes it to'x'
                x='x'
                #this means that it is picked up as a value error and passed to the except
                int(x)
            #returns the number of nodes if it is valid
            return(x)
        except ValueError:
            print('only a whole positive number of nodes')
            x= input('how many nodes in your map?   ')

node_no= input('how many nodes in your map?   ')
node_no=check_int(node_no)
#if there are less than 27 nodes then they can be labled a, b, c...
if int(node_no) < 27:
    #creates a list with all the nodes in
    for i in range(int(node_no)):
        node_list.append(chr(int(i)+65))
        node_array.append(node_list)
    array_type=1
    #this is what the node list should stay the entire time
    print('node list=' + str(node_list))
#if there are more than 26 nodes then they will be labled a1, a2, a3...
elif int(node_no) >26:
    #creates a list with all the nodes in
    for i in range(int(node_no)):
        node_list.append('A' + str(i+1))
        node_array.append(node_list)
    array_type=2
    print('node list=' + str(node_list))
#creates a 2d array
for i in range(len(node_list)):
    for i2 in range(len(node_list)):
        #the error is here
        #for some reason this line changes the values inside 'node_list'
        #as you can see there is nowhere that i am redifining node_list
        #have i used incorrect syntax? or is this just an incorrect method to do what i want?
#---------------------------------------------------------------------
        node_array[i][i2]=str(node_list[i])+str(node_list[i2])
#---------------------------------------------------------------------
        print('node list='+str(node_list))
        print('node array='+str(node_array))

如果你输入 2 的值,那么我想要的是数组看起来像这样:

[['AA','AB'], ['BA','BB']]

但结果是这样的:

[['AABAA', 'AABAAB'], ['AABAA', 'AABAAB']]

如果值为 3,它应该如下所示:

[['AA','AB','AC'], ['BA,'BB','BC'], ['CA','CB','CC']]

但它看起来像这样:

[['AABAABAACAABAA', 'AABAABAACAABAAB', 'AABAABAACAABAABAAC'], ['AABAABAACAABAA', 'AABAABAACAABAAB', 'AABAABAACAABAABAAC'], ['AABAABAACAABAA', 'AABAABAACAABAAB', 'AABAABAACAABAABAAC']]

我想要这样做的原因是,数组中的每个单元格都代表不同的旅程,然后我会询问您可以从哪个节点到达(不打算做方向,这将允许用户定义每个链接的权重。

我花了几个小时研究这个问题,以确保我没有使用错误的语法,但我找不到任何有用的东西,尽管我可能正在寻找错误的东西。

如果您能够解决我一直遇到的问题或提供替代解决方案,那么我将非常感激,我知道最好不要使用 try/except 和全局变量,我主要专注于获得一些在使其尽可能高效之前工作。

【问题讨论】:

    标签: python arrays logic dijkstra


    【解决方案1】:

    由于node_list 已成为node_array 的一部分,因此修改node_array 也会修改node_list。如果您不希望这种情况发生,您可以复制一份node_list,例如node_list[:]

    下面是一个简单的例子:

    >>> l = [1,2]
    >>> l2 = [l,l]
    >>> l
    [1, 2]
    >>> l2
    [[1, 2], [1, 2]]
    >>> l2[0][1]=3
    >>> l
    [1, 3]
    

    【讨论】:

    • 谢谢,我能够解决我遇到的问题:) 但不幸的是它直接导致了另一个错误,我觉得最好将它作为一个全新的问题提出,谢谢回答
    猜你喜欢
    • 1970-01-01
    • 2010-10-30
    • 2023-03-30
    • 2018-11-28
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    相关资源
    最近更新 更多