【漫漫科研路\Matlab】最小跳数最大权重算法

来源:互联网 发布:双十一实时数据 编辑:程序博客网 时间:2024/04/27 13:39

上周,实验室国际友人让我帮忙实现满足条件的最小跳数最大权重的算法。他的具体问题如下:
给定一个权重图(如下图所示),给出节点之间最小跳数最大权重矩阵,其中任意两点之间跳数小于等于3,否则权重为inf。
这里写图片描述
如图所示, A到F的最小跳数为2:A-C-F和A-E-F,权重(这里权重表示为所有路径上的权重乘积,当然也可以稍加修改变成权重和)分别为4*1=4、3*4=12。因此A到F的最小跳数最大权重为12,路径为A-E-F。下面给出了具体的代码实现:
主要有两个文件,测试脚本文件main.m和dijkstra_all.m函数文件:
1、测试脚本文件main.m

clear allclc   AdjMatrix=[0 inf 4 6 3 inf;           inf 0 3 2 inf 4;           4 3 0 1 1 1;           6 2 1 0 inf inf;           3 inf 1 inf 0 4;           inf 4 1 inf 4 0;];AdjMatrix1=AdjMatrix;% weight matrixIND=AdjMatrix<inf&AdjMatrix>0;AdjMatrix(IND)=1;% adjacent matrixResMatrix=zeros(size(AdjMatrix));%ouput matrix: the weights between each pair of nodesN=length(AdjMatrix);% the number of nodesfor i=1:N    for j=1:N        if(i==j)            ResMatrix(i,j)=0;        else            [sp, spcost]=dijkstra_all(AdjMatrix, i, j);% condition 1: find all the minimum hops                temp_num=sum(sp(1,:)>0);            if(temp_num<=4)% condition 2: the number of the minimum hop is less than 3                temp=ones(1,size(sp,1));% the number of the minimum hops                for m=1:size(sp,1)                    for k=1:temp_num-1                        temp(m)=temp(m)*AdjMatrix1(sp(m,k),sp(m,k+1));% Calculate the weights of all the minimum hops, change * to + for the sum of the weights                     end                end                ResMatrix(i,j)=max(temp);% condition 3: choose the maximum weight among all the minimum hops            else                ResMatrix(i,j)=inf; % the number of the minimum hop is larger than 3            end        end    endendResMatrix

2、dijkstra_all.m函数文件(sp为所有的最小跳数路径集合,spcost为最小跳数)

function [sp, spcost] = dijkstra_all(AdjMatrix, s, d)% This is an implementation of the dijkstra algorithm, wich finds the % minimal cost path between two nodes. It is used to solve the problem on % possitive weighted instances.% the inputs of the algorithm are:%AdjMatrix: the adjacent matrix of a graph% s: source node index;% d: destination node index;n=size(AdjMatrix,1);S(1:n) = 0;     %s, vector, set of visited vectorsdist(1:n) = inf;   % it stores the shortest distance between the source node and any other node;prev = zeros(50,n); % Previous node, informs about the best previous node known to reach each  network node, 50 should be changed when the path is long.count(1:n)=0;dist(s) = 0;while sum(S)~=n    candidate=[];    for i=1:n        if S(i)==0            candidate=[candidate dist(i)];        else            candidate=[candidate inf];        end    end    [u_index u]=min(candidate);    S(u)=1;    for i=1:n        if(dist(u)+AdjMatrix(u,u)+AdjMatrix(u,i))<dist(i)            dist(i)=dist(u)+AdjMatrix(u,u)+AdjMatrix(u,i);            prev(:,i)=prev(:,i).*0;            prev(1,i)=u;            count(i)=1;        else            if ((dist(u)+AdjMatrix(u,u)+AdjMatrix(u,i))==dist(i))&&(dist(i)~=inf)&&(u~=i)                        if count(i)<49                    count(i)=count(i)+1;                end                prev(count(i),i)=u;                       end        end    endendsp=[];stack=[];num=[];%backupstack = [d,zeros(1,9)];num=[1,zeros(1,9)];spcost = dist(d);while stack(1) ~= 0    if stack(1)==s        %record the path        sp=[sp;stack];        %pop        stack=[stack(2:10),0];% the first element of stack is out        num=[num(2:10),0];        continue;    end    tmp=prev(num(1),stack(1));    if tmp==0        %pop        stack=[stack(2:10),0];        num=[num(2:10),0];        continue;    else        %push        num(1)=num(1)+1;        stack=[tmp,stack(1:9)];        num=[1,num(1,1:9)];    endend

运行main脚本文件,可得最小跳数最大权重矩阵如下:
这里写图片描述

原创粉丝点击