0%

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

上周,实验室国际友人让我帮忙实现满足条件的最小跳数最大权重的算法。他的具体问题如下:
给定一个权重图(如下图所示),给出节点之间最小跳数最大权重矩阵,其中任意两点之间跳数小于等于$3$,否则权重为inf。
图1

如图1所示, A到F的最小跳数为$2$:$A-C-F$和$A-E-F$,权重(这里权重表示为所有路径上的权重乘积,当然也可以稍加修改变成权重和)分别为$4\times1=4$、$3\times 4=12$。因此$A$到$F$的最小跳数最大权重为$12$,路径为$A-E-F$。下面给出了具体的代码实现:
主要有两个文件,测试脚本文件main.m和dijkstra_all.m函数文件:
1、测试脚本文件main.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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为最小跳数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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脚本文件,可得最小跳数最大权重矩阵如下:
图2

坚持原创技术分享,您的支持将鼓励我继续创作!