当前位置:文档之家› 数学建模实验报告-第十一章-最短路问题

数学建模实验报告-第十一章-最短路问题

.
整理文本
实验名称: 第十一章 最短路问题

一、实验内容与要求
掌握Dijkstra算法和Floyd算法,并运用这两种算法求一些最短
路径的问题。

二、实验软件
MATLAB7.0

三、实验内容
1、在一个城市交通系统中取出一段如图所示,其入口为顶点v1,出
口为顶点v8,每条弧段旁的数字表示通过该路段所需时间,每次转弯
需要附加时间为3,求v1到 v8的最短时间路径。

V1 1 V2 3 V3 1 V5 6 V
6

3

V4 2 V7 4 V
8

程序:

function y=bijiaodaxiao(f1,f2,f3,f4)

2 2
.
整理文本
v12=1;v23=3;v24=2;v35=1;v47=2;v57=2;v56=6;v68=3;v78=4;turn=3;
f1=v12+v23+v35+v56+turn+v68;
f2=v12+v23+v35+turn+v57+turn+v78;
f3=v12+turn+v24+turn+v47+v78;
f4=v12+turn+v24+v47+turn+v57+turn+v56+turn+v68;
min=f1;
if f2min=f2;
end
if f3min=f3;
end
if f4min=f4;
end
min
f1
f2
f3
f4
.

整理文本
实验结果:
v1到v8的最短时间路径为15,路径为1-2-4-7-8.

2、求如图所示中每一结点到其他结点的最短路。
V1 10 V3 V5 9 V
6
.
整理文本
4
V2 5 V4 10 V7 6 V
8

floy.m中的程序:
function[D,R]=floyd(a)

n=size(a,1);
D=a
for i=1:n
for j=1:n
R(i,j)=j;
end
end
R

for k=1:n
for i=1:n
for j=1:n
if D(i,k)+D(k,j)

3 6 4 5 3
.
整理文本
D(i,j)=D(i,k)+D(k,j);
R(i,j)=R(i,k);
end
end
end
k
D
R
end

程序:
>> a=[0 3 10 inf inf inf inf inf;3 0 inf 5 inf inf inf inf;10 inf 0 6 inf inf inf
inf;inf 5 6 0 4 inf 10 inf ;
inf inf inf 4 0 9 5 inf ;inf inf inf inf 9 0 3 4;inf inf inf 10 5 3 0 6;inf inf inf inf
inf 4 6 0;];
[D,R]=floyd(a)

实验结果:
.

整理文本
D =

0 3 10 Inf Inf Inf Inf Inf
3 0 Inf 5 Inf Inf Inf Inf
10 Inf 0 6 Inf Inf Inf Inf
Inf 5 6 0 4 Inf 10 Inf
Inf Inf Inf 4 0 9 5 Inf
Inf Inf Inf Inf 9 0 3 4
Inf Inf Inf 10 5 3 0 6
Inf Inf Inf Inf Inf 4 6 0

R =
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8

相关主题