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

  • 格式:doc
  • 大小:73.92 KB
  • 文档页数:18

下载文档原格式

  / 18
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验名称:第十一章最短路问题

一、实验内容与要求

掌握Dijkstra算法和Floyd算法,并运用这两种算法求一些最短路径的问题。

二、实验软件

MATLAB7.0

三、实验内容

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

V1 1 V2 3 V3 1 V5 6 V6

V4 2 V7 4 V8

程序:

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

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 f2

min=f2;

end

if f3

min=f3;

end

if f4

min=f4;

end

min

f1

f2

f3

f4

实验结果:

v1到v8的最短时间路径为15,路径为1-2-4-7-8.

2、求如图所示中每一结点到其他结点的最短路。V110 V3V59 V6

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)

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

. k =

1

D =

0 3 10 Inf Inf Inf Inf Inf

3 0 13 5 Inf Inf Inf Inf

10 13 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 1 4 5 6 7 8

1 1 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 k =

2

D =

0 3 10 8 Inf Inf Inf Inf

3 0 13 5 Inf Inf Inf Inf

10 13 0 6 Inf Inf Inf Inf

8 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 2 5 6 7 8

1 2 1 4 5 6 7 8

1 1 3 4 5 6 7 8

2 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

k =

3

D =