Dijkstra算法原理详细讲解

上传人:m**** 文档编号:148316585 上传时间:2022-09-04 格式:DOCX 页数:9 大小:164.36KB
返回 下载 相关 举报
Dijkstra算法原理详细讲解_第1页
第1页 / 共9页
Dijkstra算法原理详细讲解_第2页
第2页 / 共9页
Dijkstra算法原理详细讲解_第3页
第3页 / 共9页
点击查看更多>>
资源描述
Dijkstra算法原理详细讲解如下图,设A为源点,求A到其他各顶点(B、C、D、E、F)的最短路径。线上所标注为相邻线段之间的距离,即权值。(注:此图为随意所画,其相邻顶点间的距离与图中的目视长度不能一一对等)算法执行步骤如下表:林Cl*VfftnT1选入A 此时S= 3 上财騒昶atm 以燐中间恥刑繊U= a Cs D, L ?*f 6X-C=3A冲基也V中的独加t酎捲徳驚徑AT枫hATC叮 以C女中间克办AOSSfF藏U二 K TC T孔(此上瞬一铀JI魏1此 1眩佰为ATCT跖人冲CT46ATCT 倒ATCT耳他U中的15轿8岌腕ATCT怦取励银短3JSABi jtS= 此时聚短路径ATH ATM. atcTb以B为中间点,从A TC TE滋聴U= atcTetq W匕上而第二莎前ATCT比法幻 ttl搦頂谢配脑ATCT啊ATfTBT;HU申的頂点二8 觀UTCTIW册轆4遶人D1 TO S= 3 C、乩 删拮曲舲A-嘏hATC0 ATCTB=5f *TCT&=B以fl解间氤从ATC F德竊翩餐 开徹U= F)h -K TD TE =8 (比上面弟二#的 A TC TE =T Ife) 删菊咖更改力AFT WATtTDTF 訓 掘UTCTW权(功鞠S遶入务此BJMJ C. B,队E 时盟短鬻径ATMhATC纽 ATCT冏Tt今忖巧 E 二7此E船脚fiJUTtTE咖*殖 舲开臟U= fETF 毗(比上 ifiMBlStfUTTF =J 戢)Wil 7A TC TH TF =9叢现ATCTDTFW校嵋为扯履&选入厂此时S=0匚B, R L 1 戏时址短蚯A TAR, h -03,ATCT5TCTlt&A-KTFJ ATC -W?均g合前吃抑譯灿処敲和囲训卿川刚酣Dijkstra算法的完整实现版本之算法的源代码样例图:NPO| -94 0.1 HaI-mfirm曲1 旷nU W益*Tiifc-niJAHLri I DWUIIhifSMiriCB T 1lE 人;hIIivfnsbEg 11B4-D44*-!- 1J訝訂112 11ttpMill HUij 也举iiKar*. r FB FVlC& Turba 忙卄 IIE输入格式:BSSFlens e i_npij.t th-e nurabor pint5 ?5PlD Aff 4 LnUf tho V1UR3 丁】k n LLntiEr of t he J th FlciS tinput tT lfl is unb r tsf_ _ 一 _ PlsBAB B LHPLlt tIB Ulll! fafl tiKt 3 th |34 int Et3HJArE 匚 TIm n llher 廡 t Ibf 2 th wt上址 linked上冒 h 3 :HPlue fT Ik n Lu-tbePl#a,c e j.riput the uiilufl TP mt (hwi治詩 of the 2 th Flpdfli inQhil thr viluc I Im nn-RhBr of t lio 3 th Pltr.ic Input Chr: u nluqwortex LinkekL l口 wcrtrm 1: be tw 1 xt 1 th po int tnuartl-Z 2 veFttx linlwtt to uflvtex 1 ;4 bs Euixt 1 th p int EduArdLs 尬 ubtex Llnkedt ti-finitew: t Ha首Jn七血覽 LLitkcd. t m 9 :3ba tlf!xt 2 th point tDars 3 匸椿舛 Lii町2 Kbi (hIjcC- uh pq int t!vvftrcL& *1 wo rtex Linkcit t 口 uertok :H int n比to ipoiit d.cimrt 和 idl BiCh ert3( Linkerii to uLrtx 3 :EInput ths ad生t; ibar of t Ivd 1 thInta nKt to- iint 4.ftnva.rd_9 rttK Liokt Eq 屮0冲牛申斗 4s-3 be chIkC 弓 th twS石 ItrAriiE ? wbrtex LinkE-il t ubtLcm 1:55 betwixt 1 th rmint t(auard-i 5thpcint silathpaint ? Ethpo ini: tnext=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-vertex0=v;poin-vertex1=0+i+1;poin-vertex2=0;linpre=lin=poin-work;linpre-next=NULL;for(j=0;jnext=NULL;break;elselin=(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);scanf(%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 * CreateTable(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;elsep-vertex0=v;p-vertex1=0+i+1;p-vertex2=0;p-cost=maxium;p-Known=0;p-next=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=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 the vertex linked to thesigned 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 *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 *begin)struct Table *temp=begin-next,*p,*t;p=head;t=begin; if(p-vertex1-0)!=start&p!=NULL) while(temp-vertex1!=p-path1&temp!=NULL) temp=temp-next; PrintPath(start,temp,t); printf(%s,p-vertex);elseif(p!=NULL)printf(n%s,p-vertex);return OK;
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 建筑环境 > 建筑资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!