Dijkstra算法Word下载.docx
- 文档编号:16536724
- 上传时间:2022-11-24
- 格式:DOCX
- 页数:19
- 大小:22.43KB
Dijkstra算法Word下载.docx
《Dijkstra算法Word下载.docx》由会员分享,可在线阅读,更多相关《Dijkstra算法Word下载.docx(19页珍藏版)》请在冰豆网上搜索。
DIJSTRA(G,w,s)
1 INITIALIZE-SINGLE-SOURCE(G,s)
2 S←Φ
3 Q←V[G]
4 whileQ≠Φ
5 douEXTRACT-MIN(Q)
6 S←S∪{u}
7 foreachvertexv∈Adj[u]
8 doRELAX(u,v,w)
输入输出格式
输入格式:
第1行:
一个数n,代表有n个节点
第2-n+1行:
每行n个数,代表图的邻接矩阵,没有边相连为-1
输出格式:
n-1个数,分别是1号节点到2-n号节点的最短路径
C++
#include<
fstream>
cstring>
usingnamespacestd;
constintMaxNum=1000000;
//边权最大值
intn;
//节点数目
intdist[501];
//到节点1的最短路径值
boolstate[501];
//节点被搜索过状态指示
intdata[501][501];
//邻接矩阵
//查找权值最小的节点
intfindmin()
{
intminnode=0,min=MaxNum;
for(inti=1;
i<
=n;
i++)
if((dist[i]<
min)&
&
(!
state[i]))
min=dist[i];
minnode=i;
}
returnminnode;
intmain()
ifstreamin("
dijkstra.in"
);
ofstreamout("
dijkstra.out"
memset(state,0,sizeof(state));
in>
>
n;
for(intp=1;
p<
p++)
for(intq=1;
q<
q++)
data[p][q];
if(data[p][q]==0)data[p][q]=MaxNum;
//初始化
dist[i]=data[1][i];
state[1]=true;
intdone=1;
while(done<
n)
intnode=findmin();
if(node!
=0)
done++;
//找到的点的数目加1
state[node]=true;
//标记已经找到了从节点1到节点node的最短路径
i++)//更新还没有找到的点的路径值
if((dist[i]>
dist[node]+data[node][i])&
dist[i]=dist[node]+data[node][i];
elsebreak;
for(intk=1;
k<
k++)
if(dist[k]==MaxNum)
out<
<
-1;
else
dist[k];
if(k==n)
endl;
"
"
;
in.close();
out.close();
return0;
Matlab
functionDijkstra(w,start,MAX)
%w为此图的距离矩阵
%start为起始端点下标(从1开始)
%MAX是数据输入时的∞的实际值
%2011年6月19日17:
02:
03
len=length(w);
flag=zeros(len,2);
index=zeros(1,len);
index
(1)=start;
%根据路由类型初始化路由表
R=ones(len,2)*MAX;
R(1,1)=0;
R(1,2)=start;
port=zeros(1,len);
port(start)=0;
%处理端点有权的问题
fori=1:
len
tmp=w(i,i)/2;
iftmp~=0
w(i,:
)=w(i,:
)+tmp;
w(:
i)=w(:
i)+tmp;
flag(i,1)=1;
%表示端i点有权值
flag(i,2)=tmp;
%存储端点权值的一半
end
w(i,i)=MAX;
s=sprintf('
\tv%d'
1:
len);
s_tmp=sprintf('
\t|%s\t%s\t'
'
置定端'
距离'
s=strcat(s,s_tmp);
%s\t'
路由'
\n----------------------------------------------------\n\t0'
len-1
\t∞'
\t|\t%d\t%4.1f\t%d'
start,0,start);
disp(s);
%Dijkstra算法具体实现过程
count=1;
whilecount<
s='
'
N=MAX;
%暂存每次距离比较的较大值
x=1;
%暂存每次距离比较的较大值的下标
ifisempty(find(index==i,1))%将没有在置定点的i值跳过
continue
forj=1:
if~isempty(find(index==j,1))%将在置定点的j值跳过
port(j)=R(i,1)+w(i,j);
%新距离
ifport(j)<
R(j,1)%新旧路由距离比较,如果小,则更新
R(j,1)=port(j);
R(j,2)=i;
port(j)=R(j,1);
N%通过比较,得出一次循环中,最小的距离,将相应点置定
N=port(j);
x=j;
fork=1:
len%输出格式设置(考虑端权值)
ifisempty(find(index==k,1))
ifport(k)==MAX
\t%s'
∞'
ifflag(k,1)
port(k)=port(k)-flag(k,2);
\t%2.1f'
port(k));
_'
ifflag(x,1)
R(x,1)=R(x,1)-flag(x,2);
x,R(x,1),R(x,2));
%为下次的循环设置条件——更新置定点列表+count加1
count=count+1;
index(count)=x;
示例:
输入:
b=[
0,9.2,1.1,3.5,100,100;
1.3,0,4.7,100,7.2,100;
2.5,100,0,100,1.8,100;
100,100,5.3,0,2.4,7.5;
100,6.4,2.2,8.9,0,5.1;
7.7,100,2.7,100,2.1,0
];
Dijkstra(b,1,100)
Pascal
programdijkstra;
var
state:
array[1..100]ofboolean;
data:
array[1..100,1..100]oflongint;
n,i,j,k,min,node:
longint;
begin
assign(input,'
dijkstra.in'
assign(output,'
dijkstra.out'
reset(input);
rewrite(output);
fillchar(data,sizeof(data),0);
fillchar(state,sizeof(state),0);
readln(n);
fori:
=1tondo
forj:
read(data[i,j]);
ifdata[i,j]=0thendata[i,j]:
=maxint;
end;
state[1]:
=true;
fork:
=2tondo
min:
{查找权值最小的点为node}
node:
=1;
if(data[1,i]<
min)and(state[i]=false)then
=data[1,i];
=i;
{更新其他各点的权值}
state[node]:
if(data[1,node]+data[node,j]<
data[1,j])and(state[j]=false)then
data[1,j]:
=data[1,node]+data[node,j];
=1ton-1do
ifdata[1,i]<
maxintthen
write(data[1,i],'
'
)
write(-1,'
writeln(data[1,n]);
close(input);
close(output);
end.
测试样例
SampleInput
7
00205030000000
20002500007000
50250040255000
30004000550000
00002555001000
00705000100000
00000000000000
SampleOutput
-12045307080-1
编辑本段大概过程
创建两个表,OPEN,CLOSE。
OPEN表保存所有已生成而未考察的节点,CLOSED表中记录已访问过的节点。
1.访问路网中里起始点最近且没有被检查过的点,把这个点放入OPEN组中等待检查。
2.从OPEN表中找出距起始点最近的点,找出这个点的所有子节点,把这个点放到CLOSE表中。
3.遍历考察这个点的子节点。
求出这些子节点距起始点的距离值,放子节点到OPEN表中。
4.重复2,3,步。
直到OPEN表为空,或找到目标点。
/*Dijkstra.c
Copyright(c)2002,2006byctu_85
AllRightsReserved.
*/
#include"
stdio.h"
malloc.h"
#definemaxium32767
#definemaxver9/*definesthemaxnumberofvertexswhichtheprogrammcanhandle*/
#defineOK1
structPoint
charvertex【3】;
structLink*work;
structPoint*next;
};
structLink
intvalue;
structLink*next;
structTable/*theworkbannchofthealgorithm*/
intcost;
intKnown;
charpath【3】;
structTable*next;
intDijkstra(structPoint*,structTable*);
intPrintTable(int,structTable*);
intPrintPath(int,structTable*,structTable*);
structTable*CreateTable(int,int);
structPoint*FindSmallest(structTable*,structPoint*);
/*Findthevertexwhichhasthesmallestvalueresideinthetable*/
inti,j,num,temp,val;
charc;
structPoint*poinpre,*poinhead,*poin;
structLink*linpre,*linhead,*lin;
structTable*tabhead;
poinpre=poinhead=poin=(structPoint*)malloc(sizeof(structPoint));
poin->
next=NULL;
work=NULL;
restart:
printf("
Notice:
ifyouwannatoinputavertex,youmustusetheformatofnumber!
\n"
Pleaseinputthenumberofpoints:
scanf("
%d"
&
num);
if(num>
maxver||num<
1||num%1!
\nNumberofpointsexception!
gotorestart;
for(i=0;
i{
Pleaseinputthepointsnexttopoint%d,endwith0:
i+1);
poin=(structPoint*)malloc(sizeof(structPoint));
poinpre->
next=poin;
vertex【0】='
v'
vertex【1】='
0'
+i+1;
vertex【2】='
\0'
linpre=lin=poin->
work;
linpre->
for(j=0;
j{
Thenumberofthe%dthvertexlinkedtovertex%d:
j+1,i+1);
temp);
if(temp==0)
lin->
break;
lin=(structLink*)malloc(sizeof(structLink));
next=lin;
+temp;
Pleaseinputthevaluebetwixt%dthpointtowards%dthpoint:
i+1,temp);
val);
value=val;
linpre=linpre->
next;
poinpre=poinpre->
PleaseenterthevertexwhereDijkstraalgorithmstarts:
tabhead=CreateTable(temp,num);
Dijkstra(poinhead,tabhead);
PrintTable(temp,tabhead);
returnOK;
structTable*CreateTable(intvertex,inttotal)
structTable*head,*pre,*p;
inti;
head=pre=p=(structTable*)malloc(sizeof(structTable));
p->
p=(structTable*)malloc(sizeof(structTable));
pre->
next=p;
if(i+1==vertex)
cost=0;
Known=0;
vertex【1】=
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Dijkstra 算法
![提示](https://static.bdocx.com/images/bang_tan.gif)