




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Dijkstra算法原理详细讲解如下图,设A为源点,求A到其他各顶点(B、C、D、E、F)的最短路径。 线上所标注为相邻线段之间的距离,即权值。(注:此图为随意所画,其相邻顶点间的距离与图中的目视长度不能一一对等)算法执行步骤如下表:S集合中AbiWs炳景潺役ATMO以A为中I簸,从A开嫩球合中怕。a RmAH6A *3中的IS点.二8划UTC=3的J湖短谊入mtnsK。c、&D也粗枳),),ATC0B&H以D力申同点, 从ATCT谜醯位 开蹴逸入E,此BJS富A C. B,D, !*tWftO2A-M0i-4C=3T*5,R ATCTE =7以E肿间点,从ATCTE寸这条觥
2、炳刑g 明) 岫到P&值映力ATCTDTP $发现HDTP珀瑚为炳6 AFUtB!S=U C. Bf以F供合己如查醍毕ttWit短路注ATM0 ATC=3,ATT 方ATC*6A C匕9枷故命独顾IB瞄城1AcutWs=a,c短签径ATMO,ATC二3以C为中词,从AT“3这条狼SJF速入B,此时S工&C、B牧时H短格经A-HW, ATC,3,ATCThS以B为铜点,从ATCTX5这必理V= A TC T45 (比上血* 一殉A晏短)CtW到8枷WA-HT邱ATCTHATC*7ATCTHIAU中的顶冬 8发现A HTg权值初t)g _iTlA TC TB TD *(比上面第二扳
3、的A TC TD,6要长)峋到D网更改力ATCTRATCTBTJC眦中的腹点=00溯UTCTDK成值为费短i.rV=A KTDTE 4 (比上面第二缁A K H H菱长)岫到E珈贼为 ATCTEm?的发现 ATCT5 权值为网ATCTET? M2(tt上面算四的ATCTDTP力Dijkstra算法的完整实现版本之算法的源代码样例图:输入格式:Turbu C*+ IDEvleAHEiiriiMJL Elie ouRber- fjf gjoints:F|,4I右噬tntt tIw M ntiAbfir af t|i 1*4e insult the uA1114 M mmh” of tlw 2 th
4、 t hi? Vliiu he ifit* 白驻rd氏3i vertex linkedLQ4:5hfrt lyt 4酎polmt t:,MAirdn; 1|1 ho nunhisr of th tlh hefkiiflber of ilk le aac inpji t Ihe he mm her of th I era雷甘inpi t t hp he nLtjn.ber of th1cAHtr input the w.h*炉oft|fep白占匕。iHlMll t. heIbe3 betwixt 2 (Milm土toWflrtis: 3 wrtM linked to ve rtrx 2:4 be
5、 1 wixl 2 Eli |o iAE E awardO- 4VD-rtox Iinkvd to vertex 2:H nrMt to ic! int 1 wit b Rsiwint 1 thvaLum r # (K蛔i3 th t: tM ONIU-输出格式:thth|M3 iffkt =4k TuEhn d IDEPleaiiB- iniHLt I heU,H i n 11 neMt la pu ini. 9史MH wit h U: The nufibr of tlw 1 th vtrte linked to uorttv 3:5 Plaavfrinput t he vditue bnr
6、twijet 3 th pa int towards th pc int ;4 i he- nikpibvr nf die 2 th vertex inknd to vArtv-x;J* Le-as E 4h|LLL Che lairit s. heitt tx linke! to vertciM 4-3Pleaurt he Vallie4 t)i pa int. t swrctn- 3 t)i |w int: 9The ibimber1of tIMT3 IhUBFLBXlinked La ver-leM 4-SPl4t IniMJit theM-twixt 4 t)i glmt tdwjua
7、 5 tJi point :2I ho nun hr r of tlw 4 th vvrtflx I inked to vcrtoM 41 s 8P I a 1 n 1411 f Iw points n.葛上t峪po int& 电nd. ui h BThe timber of tlw? 1 th vertex 1 inlu*l to yerte-K 5siPleatv inpu.1 I he value betwixt S tli pa Ifkt tdtidrdft & th w Mt:;:;? The nufiliBr of tlNt 2 th urtax jinkt*d t
8、o veteM Sf3Plflnext=NULL;poin-work=NULL;restart:printf(Notice:if you wanna to input a vertex,you must use the format ofnumber!n);printf(Please input the number of points:n);scanf(%d,&num);if(nummaxver|num1|num%1!=0)(printf(nNumber of points exception!);goto restart;for(i=0;inext=poin;poin-vertex
9、0=v;poin-vertex1=0+i+1;poin-vertex2=0;linpre=lin=poin-work;linpre-next=NULL;for(j=0;jnext=NULL; break; else(lin=(struct Link *)malloc(sizeof(struct Link); linpre-next=lin;lin-vertex0=v;lin-vertex1=0+temp;lin-vertex2=0;printf(Please input the value betwixt %d th point towards %d thpoint:,i+1,temp);sc
10、anf(%d,&val); lin-value=val;linpre=linpre-next;lin-next=NULL;poinpre=poinpre-next;poin-next=NULL;printf(Please enter the vertex where Dijkstra algorithm starts:n);scanf(%d”,&temp);tabhead=CreateTable(temp,num);Dijkstra(poinhead,tabhead);PrintTable(temp,tabhead);return OK; struct Table * Crea
11、teTable(int vertex,int total)(struct Table *head,*pre,*p;int i;head=pre=p=(struct Table *)malloc(sizeof(struct Table); p-next=NULL;for(i=0;inext=p;if(i+1=vertex)( p-vertex0=v;p-vertex1=0+i+1;p-vertex2=0;p-cost=0; p-Known=0; else (p-vertex0=v;p-vertex1=0+i+1;p-vertex2=0; p-cost=maxium; p-Known=0; p-n
12、ext=NULL; pre=pre-next; return head; int Dijkstra(struct Point *p1,struct Table *p2)/* Core of the programm*/ (int costs;char temp;struct Point *poinhead=p1,*now;struct Link *linna;struct Table *tabhead=p2,*searc,*result;while(1)(now=FindSmallest(tabhead,poinhead);if(now=NULL) break;result=p2;result
13、=result-next;while(result!=NULL)( if(result-vertex1=now-vertex1) break;else result=result-next; linna=now-work-next;while(linna!=NULL) /* update all the vertexs linked to the signedvertex*/(temp=linna-vertex1; searc=tabhead-next;while(searc!=NULL)( if(searc-vertex1=temp)/*find thevertex linked to th
14、e signed vertex in the table and update*/(if(result-cost+linna-value)cost)( searc-cost=result-cost+linna-value;/*set the new value*/searc-path0=v;searc-path1=now-vertex1;searc-path2=0;break; else searc=searc-next; linna=linna-next;return 1; struct Point * FindSmallest(struct Table*head,struct Point
15、*poinhead) (struct Point *result;struct Table *temp;int min=maxium,status=0;head=head-next;poinhead=poinhead-next;while(head!=NULL)( if(!head-Known&head-costcost;result=poinhead;temp=head;status=1;head=head-next;poinhead=poinhead-next; if(status) (temp-Known=1;return result;elsereturn NULL;int PrintTable(int start,struct Table *head)(struct Table *begin=head;head=head-next;while(head!=NULL)(if(head-vertex1-0)!=start)PrintPath(start,head,begin); head=head-next; return OK; int PrintPath(int start,struct Table*head,struct Table
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 经济师考试重点回顾试题及答案
- 毕设绘本设计答辩
- 2025届齐齐哈尔市富裕县三年级数学第一学期期末综合测试试题含解析
- 行政管理经济法实务试题及答案
- 确保市政工程考试复习高效的试题及答案
- 行政管理中的公共关系案例分析试题及答案
- 经济法考试的知识点概述试题及答案
- 水利水电工程哲学思考与实践试题及答案
- 电子信息行业个人工资证明(8篇)
- 行政管理与公共关系的实践模式题及答案
- 部编人教版语文四年级下册《习作:我的“自画像”》课件
- GB/T 15823-1995氦泄漏检验
- GB/T 13891-2008建筑饰面材料镜向光泽度测定方法
- GB 13326-1991组合式空气处理机组噪声限值
- 生命周期评价课件
- 2022年质量员考试题库高分300题有解析答案(浙江省专用)
- 玉米精播机设计说明书
- 包工不包料建设房屋合同
- 单身职工信息登记表
- 网店运营与管理课件
- 餐饮店运营方案完整篇
评论
0/150
提交评论