1. 程式人生 > >【漫漫科研路\Matlab】最小跳數最大權重演算法

【漫漫科研路\Matlab】最小跳數最大權重演算法

上週,實驗室國際友人讓我幫忙實現滿足條件的最小跳數最大權重的演算法。他的具體問題如下:
給定一個權重圖(如下圖所示),給出節點之間最小跳數最大權重矩陣,其中任意兩點之間跳數小於等於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 all
clc   
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 matrix IND=AdjMatrix<inf&AdjMatrix>0; AdjMatrix(IND)=1;% adjacent matrix ResMatrix=zeros(size(AdjMatrix));%ouput matrix: the
weights between each pair of nodes N=length(AdjMatrix);% the number of nodes for 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 end end ResMatrix

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 vectors
dist(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
    end
end



sp=[];
stack=[];
num=[];
%backup
stack = [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)];
    end


end

執行main指令碼檔案,可得最小跳數最大權重矩陣如下:
這裡寫圖片描述