Python 3.x Python-Prim';s算法的数组实现

Python 3.x Python-Prim';s算法的数组实现,python-3.x,prims-algorithm,Python 3.x,Prims Algorithm,我试图用Python3实现Prim的算法,该算法计算它生成的MST的总重量。我正在做一些不寻常的事情,使用“数组”跟踪未访问的节点 这是我的密码: def Prim(Graph): # row 1 is "still in R" # row 2 is the connector vertex # row 3 is the cost total = 0 A = [] n = len(Graph) A = [[None for x in ran

我试图用Python3实现Prim的算法,该算法计算它生成的MST的总重量。我正在做一些不寻常的事情,使用“数组”跟踪未访问的节点

这是我的密码:

def Prim(Graph):
    # row 1 is "still in R"
    # row 2 is the connector vertex
    # row 3 is the cost
    total = 0
    A = []
    n = len(Graph)
    A = [[None for x in range(0, n)] for y in range(1, 4)]
    #Debugging purposes
    #print(A)
    for x in range(1, n):
        A[0][x] = 'Y'
        A[1][x] = 0
        A[2][x] = 0

    for neighbour in Graph[1]: 
        A[1][neighbour-1] = 1
        A[2][neighbour-1] = Graph[1][neighbour]
        #Debugging purposes
        #print("Neighbour: ", neighbour, "Weight: ", Graph[1][neighbour])
    current = 1
    T = [current]
    MST_edges = {}
    count = 0
    while len(T) < n:
        x = search_min(current, A)
        T.append(x)
        MST_edges[x] = A[1][x]
        A[0][x] = 'N'
        total += A[2][x]

        #print(Graph)
        #print(A)
        for neighbour in Graph[x]:
            #print(neighbour)
            #print(A[2][neighbour-1])
            if A[0][neighbour-1] != 'N':
                if Graph[x][neighbour] < A[2][neighbour-1]:
                    A[1][neighbour-1] = x
                    A[2][neighbour-1] = Graph[x][neighbour]
        count += 1
        current = T[count]
    return total



def search_min(current, A):
    minimum_cost = 100
    minimum_vertex = 1
    for x in range(1,len(A[0])):
        if A[1][x] != None and A[0][x] != 'N' and A[2][x] < minimum_cost:
                minimum_cost = A[2][x]
                minimum_vertex = x
                #Debugging
    ##            print("x", x)
    ##            print("cost",minimum_cost)
    ##            print("vertex",x)
    return minimum_vertex
def Prim(图形):
#第1行“仍在R中”
#第2行是连接器顶点
#第三行是成本
总数=0
A=[]
n=len(图)
A=[[x在范围(0,n)内无]y在范围(1,4)内无]
#调试目的
#印刷品(A)
对于范围(1,n)内的x:
A[0][x]=“Y”
A[1][x]=0
A[2][x]=0
对于图[1]中的邻居:
A[1][1]=1
A[2][neighbor-1]=图[1][neighbor]
#调试目的
#打印(“邻居:”,邻居,“权重:”,图形[1][邻居])
电流=1
T=[当前]
MST_边={}
计数=0
而len(T)
它有时会给我可笑的低权重,比如20(这几乎是不可能的,因为所有边的最小权重都是10)。问题可能在while循环中:

 while len(T) < n:
        x = search_min(current, A)
        T.append(x)
        MST_edges[x] = A[1][x]
        A[0][x] = 'N'
        total += A[2][x]

        #print(Graph)
        #print(A)
        for neighbour in Graph[x]:
            #print(neighbour)
            #print(A[2][neighbour-1])
            if A[0][neighbour-1] != 'N':
                if A[2][neighbour-1] != None and Graph[x][neighbour] < A[2][neighbour-1]:
                    A[1][neighbour-1] = x
                    A[2][neighbour-1] = Graph[x][neighbour]
        count += 1
        current = T[count]
而len(T)
但我不知道是哪一部分。很晚了,我头疼,任何能帮忙的人都会很好

编辑以下是它生成的MST示例。由于某些原因,有些顶点的加权边为0

图=构造图(20) Prim(图){3:0,5:0,8:0,16:0,6:5,9:3,7:8,11:5,15:11,12:11,2:8,18:2,19:2,1:19,10:19,14:10,17:5,13:16,4:1}


(仔细查看我的代码,您可以看到,对于值x:y,x是顶点的值,而y是连接边的权重。出于某种原因,有些顶点的权重为0)

在收到建议后,我更改了这行代码:

A[2][x] = 0
为此:

A[2][x] = math.inf

这样阵列就不会意外地看到“woot,边的权重为0”,因为这意味着它没有连接。因此,所有的问题都在于为非法值输入什么。

你能给出一个计算错误权重的图形示例吗?也许您应该让函数返回它找到的树,而不仅仅是权重,这样您就可以看到它出了什么问题(这可能会告诉您它在哪里出错)。试试
返回总计,A
(或带有
MST\u边的东西
)。@Blckknght您好,谢谢您的建议,我添加了一个它生成的MST示例