|
Floyd最短路算法
- clc % 清屏
- clear all; % 删除workplace变量
- close all; % 关掉显示图形窗口
- clc,clear,close all
- x7=[0 1 inf inf inf 2; %D0
- 1 0 4 inf inf 4;
- inf 4 0 2 inf 1;
- inf inf 2 0 3 3;
- inf inf inf 3 0 5;
- 2 4 1 3 5 0];
- n=length(x7);
- path=zeros(n);
- %flord最小距离法
- for k=1:n
- for i=1:n
- for j=1:n
- if x7(i,j)>x7(i,k)+x7(k,j) % 节点直接连接大于中间插入的节点时
- x7(i,j)=x7(i,k)+x7(k,j); % 记录更新
- path(i,j)=k; % 路由号记录
- end
- end
- end
- end
- x7
- path
复制代码 输出:- x7 =
- 0 1 3 5 7 2
- 1 0 4 6 8 3
- 3 4 0 2 5 1
- 5 6 2 0 3 3
- 7 8 5 3 0 5
- 2 3 1 3 5 0
- path =
- 0 0 6 6 6 0
- 0 0 0 3 6 1
- 6 0 0 0 4 0
- 6 3 0 0 0 0
- 6 6 4 0 0 0
- 0 1 0 0 0
复制代码
|
|