MATLAB的迪杰斯特拉算法求7个起始点到15个终点的最短路径! 迪杰斯特拉算法计算最短路径是否是全局最优算法

\u5982\u4f55\u7528matlab\u5b9e\u7ebfDijkstra\u7b97\u6cd5\u4ece\u4e00\u70b9\u5230\u5176\u4ed6\u5404\u70b9\u7684\u6700\u77ed\u8def\u5f84\uff1f

\u53ef\u4ee5\u7528C\u8bed\u8a00\u7f16\u554a
\u4e0b\u9762\u5c31\u662fC\u7a0b\u5e8f
#include"stdio.h"
#include "malloc.h"
#define maxium 32767
#define maxver 9
#define OK 1
struct Point
{
char vertex[3];
struct Link *work;
struct Point *next;
};
struct Link
{
char vertex[3];
int value;
struct Link *next;
};
struct Table
{
int cost;
int Known;
char vertex[3];
char path[3];
struct Table *next;
};
int Dijkstra(struct Point *,struct Table *);
int PrintTable(int,struct Table *);
int PrintPath(int,struct Table *,struct Table *);
struct Table * CreateTable(int,int);
struct Point * FindSmallest(struct Table *,struct Point *);/*Find the vertex which has the smallest value reside in the table*/
int main()
{
int i,j,num,temp,val;
char c;
struct Point *poinpre,*poinhead,*poin;
struct Link *linpre,*linhead,*lin;
struct Table *tabhead;
poinpre=poinhead=poin=(struct Point *)malloc(sizeof(struct Point));
poin->next=NULL;
poin->work=NULL;
restart:
printf("Notice:if you wanna to input a vertex,you must use the format of number!\n");
printf("Please input the number of Points:\n");
scanf("%d",&num);
if(num>maxver||num<1||num%1!=0)
{
printf("\nNumber of Points exception!");
goto restart;
}
for(i=0;i<=num;)
{
printf("Please input the Points next to point %d,end with 0:\n",i+1);
poin=(struct Point *)malloc(sizeof(struct Point));
poinpre->next=poin;
poin->vertex[0]='v';
poin->vertex[1]='0'+i+1;
poin->vertex[2]='\0';
linpre=lin=poin->work;
linpre->next=NULL;
for(j=0;j ;){
printf("The number of the %d th vertex linked to vertex %d:",j+1,i+1);
scanf("%d",&temp);
if(temp==0)
{
lin->next=NULL;
break;
}
else
{
lin=(struct Link *)malloc(sizeof(struct Link));
linpre->next=lin;
lin->vertex[0]='v';
lin->vertex[1]='0'+temp;
lin->vertex[2]='\0';
printf("Please input the value betwixt %d th Point towards %d th Point:",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;i ;){
p=(struct Table *)malloc(sizeof(struct Table));
pre->next=p;
if(i+1==vertex)
{
p->vertex[0]='v';
p->vertex[1]='0'+i+1;
p->vertex[2]='\0';
p->cost=0;
p->Known=0;
}
else
{
p->vertex[0]='v';
p->vertex[1]='0'+i+1;
p->vertex[2]='\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->vertex[1]==now->vertex[1])
break;
else
result=result->next;
}
linna=now->work->next;
while(linna!=NULL) /* update all the vertexs linked to the signed vertex*/
{
temp=linna->vertex[1];
searc=tabhead->next;
while(searc!=NULL)
{
if(searc->vertex[1]==temp) /*find the vertex linked to the signed vertex in the table and update*/
{
if((result->cost+linna->value)cost)
{
searc->cost=result->cost+linna->value;/*set the new value*/
searc->path[0]='v';
searc->path[1]=now->vertex[1];
searc->path[2]='\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->cost)
{
min=head->cost;
result=poinhead;
temp=head;
status=1;
}
head=head->next;
poinhead=poinhead->next;
}
if(status)
{
temp->Known=1;
return result;
}
else
return NULL;
}
int PrintTable(int start,struct Table *head)
{
struct Table *begin=head;
head=head->next;
while(head!=NULL)
{
if((head->vertex[1]-'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->vertex[1]-'0')!=start&&p!=NULL)
{
while(temp->vertex[1]!=p->path[1]&&temp!=NULL)
temp=temp->next;
PrintPath(start,temp,t);
printf("%s",p->vertex);
}
else
if(p!=NULL)
printf("\n%s",p->vertex);
return OK;
}

\u662f
\u5177\u4f53\u8bc1\u660e\u8bf7\u53c2\u8003\u300a\u7b97\u6cd5\u5bfc\u8bba\u300b\u5355\u6e90\u6700\u77ed\u8def\u90a3\u4e00\u7ae0

你对图论的知识有了解吧~W是关联矩阵,s和t分别是起始点和终止节点的序号。返回的d为最短的加权路径长度,p为最优路径节点的序号向量。注意,这里W矩阵为0的点权值已经自动设为无穷大了。请参考《高等应用数学问题的 MATLAB一书》。我吧程序赋给你。
你做一个M函数用吧。
function [d,path]=dijkstra(W,s,t)
[n,m]=size(W);ix=(W==0);W(ix)=inf;
if n~=m,error('Square W required');end
visited(1:n)=0; dist(1:n)=inf;parent(1:n)=0;dist(s)=0;d=inf;
for i=1:(n-1),%求出每个节点与起始点的关系
ix=(visited==0);vec(1:n)=inf;vec(ix)=dist(ix);
[a,u]=min(vec);visited(u)=1;
for v=1:n,if (W(u,v)+dist(u)<dist(v)),
dist(v)=dist(u)+W(u,v);parent(v)=u;
end;end;end
if parent(t)~=0,path=t;d=dist(t);%回溯最短路径
while t~=s,p=parent(t);path=[p path];t=p;end;
end;
希望对你有用

你看看这个:
http://zhidao.baidu.com/question/177028260.html

分别执行7次dijkstra。

  • MATLAB鐨勮开鏉版柉鐗规媺绠楁硶姹7涓捣濮嬬偣鍒15涓粓鐐圭殑鏈鐭矾寰!
    绛旓細function [d,path]=dijkstra(W,s,t)[n,m]=size(W);ix=(W==0);W(ix)=inf;if n~=m,error('Square W required');end visited(1:n)=0; dist(1:n)=inf;parent(1:n)=0;dist(s)=0;d=inf;for i=1:(n-1),%姹傚嚭姣忎釜鑺傜偣涓庤捣濮嬬偣鐨勫叧绯 ix=(visited==0);vec(1:n)=inf;...
  • matlab瀹炵幇杩澃鏂壒鎷璋冪敤c
    绛旓細matlab瀹炵幇杩澃鏂壒鎷璋冪敤c锛Matlab涓殑鍥捐妭鐐硅鐧诲綍鍚庡鍒讹紝缂栧彿鏈濂芥槸浠1寮濮嬭繛缁紪鍙凤紝涓嶈鑷繁闅忎究缂栥俿=[99112227766 554 t=[17728358685 3 43 w = [48 38 274 16 6 14 10 9 G=graph(s,t,w);plot(G锛'EdgeLabel',G.Edges.Weigh set( gca, 'XTick', [], 'YTick', [][P,d]...
  • matlab 涓浣跨敤杩澃鏂壒鎷夌畻娉
    绛旓細涓婇潰杩欎釜鐭╅樀鏄甫鏉冮偦鎺ョ煩闃碉紝鍙互鐢ㄥ畠寰楀埌鏃犲悜鎴栨湁鍚戝浘褰傝鐢诲嚭杩欎釜鍥撅紝杩樻槸鐢ㄨ蒋浠惰緝濂姐
  • 鎬庝箞鐢Matlab璁$畻寰堝鏁g偣涔嬮棿鏈鐭窛绂
    绛旓細clear;clc;x=rand(7,1);y=rand(7,1);dist=@(var) sum(sqrt((var(1)-x).^2+(var(2)-y).^2));%var(1)=x;var(2)=y var0=rand(2,1);[var,minDistance,exitflag]=fminunc(dist,var0)plot(x,y,'o','markerfacecolor','r','markersize',6);hold on;plot(var(1),var(2...
  • 鎬庝箞鐢Matlab璁$畻寰堝鏁g偣涔嬮棿鏈鐭窛绂?鐢诲嚭杩欎簺鏁g偣鏈鐭窛绂荤殑杩炵嚎,浼...
    绛旓細鐢杩澃鏂壒鎷夌畻娉鎴栦經娲涗緷寰风畻娉曘傜綉涓婃湁鍥捐宸ュ叿绠憋紝浣犲彲浠ヤ笅涓
  • 鎬庝箞鐢姹傝В涓嶇煡閬撳叿浣撳潗鏍囩殑閫夊潃闂
    绛旓細鍥锯濈粨鏋勪腑锛屾湁鈥滄渶灏忕敓鎴愭爲鈥濆拰鈥滄渶鐭矾寰勨濈瓑姒傚康銆備綘鎵鎻愬埌鐨勯棶棰橈紝鍙互鍙傝冣滄渶灏忕敓鎴愭爲鈥濋噷鐨勨淧rim(鏅噷濮)绠楁硶鈥濆拰鈥淜ruskal(鍏嬮瞾鏂崱灏)绠楁硶鈥濓紝浠ュ強鈥滄渶鐭矾寰勨濋噷鐨勨Dijkstra(杩澃鏂壒鎷)绠楁硶鈥濄傛牴鎹疄闄咃紝寤虹珛濂芥暟鎹粨鏋勫悗锛岃繖浜涚畻娉曞彲浠璁$畻鍑烘渶浼樻柟妗堬紝杩欎簺绠楁硶鐨勬濇兂锛屽彲浠ョ綉涓婃煡銆
  • 鏈灏忕敓鎴愭爲鐨绠楁硶鏃堕棿澶嶆潅搴︽渶灏忔槸澶氬皯?
    绛旓細涓嶅悓鐨勭畻娉曟椂闂村鏉傚害涓嶄竴鏍,鏅噷濮嗙畻娉昈(n^2),鍏嬮瞾鏂崱灏旂畻娉昈(eloge) 鏈洖绛旂敱鎻愰棶鑰呮帹鑽 涓炬姤| 绛旀绾犻敊 | 璇勮 13 2 涔岀煶 閲囩撼鐜:74% 鏉ヨ嚜:鑺濋夯鍥 鎿呴暱: 鏁板 C/C++ 鐗╃悊瀛 VC++ 宸ョ▼鎶鏈瀛 涓烘偍鎺ㄨ崘: prim绠楁硶姹鏈灏忕敓鎴愭爲 鏈灏忕敓鎴愭爲鐨勭畻娉 dijkstra绠楁硶 杩澃鏂壒鎷夌畻娉 鏅噷濮嗙畻娉曟渶灏...
  • 姹傛暟瀛︽ā鍨,鍚勭妯″瀷;鍚勭绠楁硶
    绛旓細1銆佽挋鐗瑰崱缃楃畻娉曪紙璇ョ畻娉曞張绉伴殢鏈烘фā鎷熺畻娉曪紝鏄氳繃璁$畻鏈轰豢鐪熸潵瑙e喅闂鐨勭畻娉锛屽悓鏃跺彲浠ラ氳繃妯℃嫙鍙互鏉ユ楠岃嚜宸辨ā鍨嬬殑姝g‘鎬э紝鏄瘮璧涙椂蹇呯敤鐨勬柟娉曪級2銆佹暟鎹嫙鍚堛佸弬鏁颁及璁°佹彃鍊肩瓑鏁版嵁澶勭悊绠楁硶锛堟瘮璧涗腑閫氬父浼氶亣鍒板ぇ閲忕殑鏁版嵁闇瑕佸鐞嗭紝鑰屽鐞嗘暟鎹殑鍏抽敭灏卞湪浜庤繖浜涚畻娉曪紝閫氬父浣跨敤Matlab浣滀负宸ュ叿锛3銆佺嚎鎬ц鍒掋...
  • 扩展阅读:迪杰斯特拉算法王道 ... matlab去马赛克算法 ... 迪杰斯特拉具体步骤 ... dijkstra算法详细步骤 ... 迪杰斯特拉表格怎么画 ... matlab绘制伯德图详细步骤 ... 高斯序列matlab ... 迪杰斯特拉算法过程 ... 迪杰斯特拉算法题详细步骤 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网