prim.m

来自「prim算法 kruskal算法用matlab实现 输入标准:x邻接矩阵 p」· M 代码 · 共 27 行

M
27
字号
function [u,l]=prim(x,p)
%u is the set of the point serial
%l is the set of the length of sides serial
%x is adjacing matrix
%p is the set of all the point 
u=[1];
l=[];

while length(p)~=0
    lmin=inf;
    %calculate the min length of side from u to p-u
    p=setdiff(p,u);
    lrem=0;
    urem=0;
    for ii=1:length(u)
        for jj=1:length(p)
         if lmin>x(u(ii),p(jj)) 
         lmin=x(u(ii),p(jj));
         lrem=lmin;
         urem=p(jj);
         end
        end
    end
    u=[u urem];
    l=[l lrem];
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?