prim';邻接矩阵julia中的s算法

prim';邻接矩阵julia中的s算法,julia,Julia,我试图在julia中实现prim算法 我的函数使用权重获取邻接矩阵,但工作不正常。我不知道我要改变什么。附加有一些问题!我想是功能 是否有其他/更好的方法通过传递邻接矩阵来实现该算法 多谢各位 function prims(AD) n = size(AD) n1 = n[1] # choose initial vertex from graph vertex = 1 # initialize empty edges array and empty

我试图在julia中实现prim算法

我的函数使用权重获取邻接矩阵,但工作不正常。我不知道我要改变什么。附加有一些问题!我想是功能

是否有其他/更好的方法通过传递邻接矩阵来实现该算法

多谢各位

function prims(AD)


    n = size(AD)
    n1 = n[1]


    # choose initial vertex from graph
    vertex = 1

    # initialize empty edges array and empty MST
    MST = []
    edges = []
    visited = []
    minEdge = [nothing, nothing, float(Inf)]

    # run prims algorithm until we create an MST
    # that contains every vertex from the graph
    while length(MST) != n1 - 1

        # mark this vertex as visited 
        append!(visited, vertex)

        # add each edge to list of potential edges
        for r in 1:n1
            if AD[vertex][r] != 0
                append!(edges, [vertex, r, AD[vertex][r]])
            end
        end
        # find edge with the smallest weight to a vertex
        # that has not yet been visited
        for e in 1:length(edges)
            if edges[e][3] < minEdge[3] && edges[e][2] not in visited
                minEdge = edges[e]
            end
        end
        # remove min weight edge from list of edges  
        deleteat!(edges, minEdge)

        # push min edge to MST     
        append!(MST, minEdge)

        # start at new vertex and reset min edge
        vertex = minEdge[2]
        minEdge = [nothing, nothing, float(Inf)]
    end

    return MST

end
我明白了

ERROR: BoundsError
Stacktrace:
 [1] getindex(::Int64, ::Int64) at .\number.jl:78
 [2] prims(::Array{Int64,2}) at .\untitled-8b8d609f2ac8a0848a18622e46d9d721:70
 [3] top-level scope at none:0
我想我必须以这样的形式“重塑”我的矩阵C

D = [ [0,2,3,0,0,0], [2,0,5,3,4,0], [3,5,0,0,4,0], [0,3,0,0,2,3], [0,4,4,2,0,5], [0,0,0,3,5,0
]]
但同样的,我也得到了同样的错误。

首先要注意的是,实现了这个算法。你可以找到代码

我还对您的算法做了一些说明,以使其正常工作,并指出在不改变其总体结构的情况下的潜在改进:

using DataStructures # library defining PriorityQueue type

function prims(AD::Matrix{T}) where {T<:Real} # make sure we get what we expect
    # make sure the matrix is symmetric
    @assert transpose(AD) == AD
    n1 = size(AD, 1)
       vertex = 1
    # it is better to keep edge information as Tuple rather than a vector
    # also note that I add type annotation for the collection to improve speed
    # and make sure that the stored edge weight has a correct type
    MST = Tuple{Int, Int, T}[]
    # using PriorityQueue makes the lookup of the shortest edge fast
    edges = PriorityQueue{Tuple{Int, Int, T}, T}()
    # we will eventually visit almost all vertices so we can use indicator vector
    visited = falses(n1)
    while length(MST) != n1 - 1
        visited[vertex] = true
        for r in 1:n1
            # you access a matrix by passing indices separated by a comma
            dist = AD[vertex, r]
            # no need to add edges to vertices that were already visited
            if dist != 0 && !visited[r]
                edges[(vertex, r, dist)] = dist
            end
        end
        # we will iterate till we find an unvisited destination vertex
        while true
            # handle the case if the graph was not connected
            isempty(edges) && return nothing
            minEdge = dequeue!(edges)
            if !visited[minEdge[2]]
                # use push! instead of append!
                push!(MST, minEdge)
                vertex = minEdge[2]
                break
            end
        end
    end
    return MST
end
使用数据结构#定义优先级队列类型的库

函数prims(AD::Matrix{T}),其中{T感谢您的帮助。我想将数组每行的第一个和第二个元素提取到另一个数组中。仅第三个元素也是如此。对于第三个元素,我在MST中使用[x[3]for x进行了此操作。使用两个元素是否有相同的方法?您可以使用
[x[1:2]对于MST中的x]
获取元组向量,或者对于MST中的x,
[x[i],i:1:2]
获取矩阵。
using DataStructures # library defining PriorityQueue type

function prims(AD::Matrix{T}) where {T<:Real} # make sure we get what we expect
    # make sure the matrix is symmetric
    @assert transpose(AD) == AD
    n1 = size(AD, 1)
       vertex = 1
    # it is better to keep edge information as Tuple rather than a vector
    # also note that I add type annotation for the collection to improve speed
    # and make sure that the stored edge weight has a correct type
    MST = Tuple{Int, Int, T}[]
    # using PriorityQueue makes the lookup of the shortest edge fast
    edges = PriorityQueue{Tuple{Int, Int, T}, T}()
    # we will eventually visit almost all vertices so we can use indicator vector
    visited = falses(n1)
    while length(MST) != n1 - 1
        visited[vertex] = true
        for r in 1:n1
            # you access a matrix by passing indices separated by a comma
            dist = AD[vertex, r]
            # no need to add edges to vertices that were already visited
            if dist != 0 && !visited[r]
                edges[(vertex, r, dist)] = dist
            end
        end
        # we will iterate till we find an unvisited destination vertex
        while true
            # handle the case if the graph was not connected
            isempty(edges) && return nothing
            minEdge = dequeue!(edges)
            if !visited[minEdge[2]]
                # use push! instead of append!
                push!(MST, minEdge)
                vertex = minEdge[2]
                break
            end
        end
    end
    return MST
end