|
|
|
|
%ej.4.10仓库选址
|
|
|
|
|
clc; clear; close all; format long g;
|
|
|
|
|
a=zeros(6);
|
|
|
|
|
a(1,[2,5])=[20,15];
|
|
|
|
|
a(2,[1,3,4,5])=[20,20,60,25];
|
|
|
|
|
a(3,[2,4,5])=[20,30,18];
|
|
|
|
|
a(4,[2,3])=[60,30];
|
|
|
|
|
a(5,[1,2,3,6])=[15,25,18,15];
|
|
|
|
|
a(6,5)=15;
|
|
|
|
|
s = cellstr(strcat('销售点',int2str([1:6]')));
|
|
|
|
|
G=graph(a,s);
|
|
|
|
|
P = plot(G,'layout','force','EdgeColor','k','NodeFontSize',12);
|
|
|
|
|
D=[];
|
|
|
|
|
for i=1:6
|
|
|
|
|
temp=0;
|
|
|
|
|
for j=1:6
|
|
|
|
|
[path,d]=shortestpath(G,i,j);
|
|
|
|
|
if d>temp;
|
|
|
|
|
temp=d;
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
D=[D,temp];
|
|
|
|
|
end
|
|
|
|
|
[minVal,minInd]=min(D);
|
|
|
|
|
disp('仓库应建在销售点');
|
|
|
|
|
disp(minInd);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%答案版
|
|
|
|
|
clc; clear; close all;
|
|
|
|
|
|
|
|
|
|
% 初始化邻接矩阵
|
|
|
|
|
a = zeros(6);
|
|
|
|
|
a(1, [2, 5]) = [20, 15];
|
|
|
|
|
a(2, 3:5) = [20, 60, 25];
|
|
|
|
|
a(3, [4, 5]) = [30, 18];
|
|
|
|
|
a(5, 6) = 15;
|
|
|
|
|
|
|
|
|
|
% 创建节点标签
|
|
|
|
|
s = cellstr(strcat('V', int2str([1:6]')));
|
|
|
|
|
|
|
|
|
|
% 创建图
|
|
|
|
|
G = graph(a, s, 'upper');
|
|
|
|
|
|
|
|
|
|
% 计算所有节点之间的最短路径距离矩阵
|
|
|
|
|
d = distances(G);
|
|
|
|
|
|
|
|
|
|
% 计算从每个节点到其他节点的最大最短路径距离
|
|
|
|
|
D = max(d, [], 2)'; %第三个参数 2 指定沿着矩阵的第二个维度(即列)进行操作。
|
|
|
|
|
|
|
|
|
|
% 找到最大最短路径距离中的最小值及其下标
|
|
|
|
|
[minD, minIndex] = min(D);
|
|
|
|
|
|
|
|
|
|
% 绘制图
|
|
|
|
|
plot(G, 'EdgeLabel', G.Edges.Weight, 'Layout', 'force');%设置边的标签
|
|
|
|
|
|
|
|
|
|
% 显示结果
|
|
|
|
|
fprintf('从每个节点到任意其他节点的最大最短路径距离: \n');
|
|
|
|
|
disp(D);
|
|
|
|
|
fprintf('最大最短路径距离中的最小值为: %f\n', minD);
|
|
|
|
|
fprintf('对应的节点为: V%d\n', minIndex);
|
|
|
|
|
|
|
|
|
|
%设备更新问题;
|
|
|
|
|
clc;clear;close all;
|
|
|
|
|
a=zeros(6);
|
|
|
|
|
a(1,[2:6])=[15,20,27,37,54];
|
|
|
|
|
a(2,[3:6])=[15,20,27,37];
|
|
|
|
|
a(3,[4:6])=[16,21,28];
|
|
|
|
|
a(4,[5:6])=[16,21];
|
|
|
|
|
a(5,6)=17;
|
|
|
|
|
s=cellstr(strcat(int2str((1:6)'),'年初'));
|
|
|
|
|
G=digraph(a,s);
|
|
|
|
|
P = plot(G,'layout','force','EdgeColor','k','NodeFontSize',12);
|
|
|
|
|
[path,d]=shortestpath(G,1,6);%注意参数个数
|
|
|
|
|
highlight(P,path,"EdgeColor","red",'LineWidth',3.5);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%最小生成树
|
|
|
|
|
clc;clear;
|
|
|
|
|
a=zeros(9);
|
|
|
|
|
a(1,[2:9])=[2 1 3 4 4 2 5 4]; % 顶点1到其他顶点的边的权重
|
|
|
|
|
a(2,[3 9])=[4 1]; % 顶点2到顶点3、顶点9的边的权重
|
|
|
|
|
a(3,4)=1; % 同上。因为写过1到3,和2到3的边的权重,无需重复设
|
|
|
|
|
a(4,5)=1;
|
|
|
|
|
a(5,6)=5;
|
|
|
|
|
a(6,7)=2;
|
|
|
|
|
a(7,8)=3;
|
|
|
|
|
a(8,9)=5;
|
|
|
|
|
s=cellstr(strcat('节点',int2str([1:9]')));
|
|
|
|
|
G=graph(a,s,'upper');
|
|
|
|
|
P=plot(G,'layout','force');
|
|
|
|
|
T=minspantree(G,'Method','sparse');
|
|
|
|
|
L=sum(T.Edges.Weight);
|
|
|
|
|
highlight(P,T,"EdgeColor","red",'LineWidth',2.5);
|