【漫漫科研路\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脚本文件,可得最小跳数最大权重矩阵如下:
阅读全文
0 0
- 【漫漫科研路\Matlab】最小跳数最大权重算法
- 【漫漫科研路】序言
- 抵达终点的最小跳数
- 最大矩形面积 --漫漫算法路 刷题篇
- 给定一个非负整数数组,最初定位在数组的第一个索引处。组中的每个元素表示您在该位置的最大跳跃长度。求最小跳数
- 【漫漫科研路\pgfplots】多个子图共用一个图例
- 【漫漫科研路\pgfplots】子图的多种画法
- 熵值法确定权重算法及Matlab实现
- 熵值法确定权重算法及Matlab实现
- 相反数(当前数取反后与原数相加) -- 漫漫算法路 刷题篇
- 重建二叉树 -- 漫漫算法路 刷题篇
- 拼凑钱币 --漫漫算法路 刷题篇
- 顺时针打印矩阵 -- 漫漫算法路 刷题篇
- 魔法币 -- 漫漫算法路 刷题篇
- NetBeans 6.0是史上最大升级,赶超Eclipse仍路漫漫
- 权重算法
- Matlab科研常用命令总结
- Matlab科研常用命令总结
- kubelet代码整理 (2)
- 字符串
- Spring(3):IOC--“控制反转”的剖析
- git
- HashMap源码分析
- 【漫漫科研路\Matlab】最小跳数最大权重算法
- 获取随机数并且每次和上一次的不相同
- 函数调用选择排序
- [译] 理解 LSTM 网络
- linux学习第三十七篇:安装PHP5,安装PHP7
- React技术栈之为什么选择react
- Java8之Lambda表达式
- IT风投与中国
- 冒泡法排序(c语言)