有向图转换遍历拓扑最短路径Word格式文档下载.docx
- 文档编号:16420753
- 上传时间:2022-11-23
- 格式:DOCX
- 页数:11
- 大小:17.74KB
有向图转换遍历拓扑最短路径Word格式文档下载.docx
《有向图转换遍历拓扑最短路径Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《有向图转换遍历拓扑最短路径Word格式文档下载.docx(11页珍藏版)》请在冰豆网上搜索。
//图的邻接表结构定义;
typedefstruct{
ElemType*Vex;
unsignedint**Arc;
}DMGraph;
//图的邻接矩阵结构定义;
//========================================================================================
StatusCreateDMGraph(DMGraph*DMG);
//创建图的邻接矩阵;
StatusDMG_Traver(DMGraphDMG);
//邻接矩阵的遍历;
StatusDMG_DFS(DMGraphDMG,intv,int*Visited);
//邻接矩阵深度遍历(递
归);
StatusDMG_DFS_Uni(DMGraphDMG,intv,int*Visited);
//邻接矩阵深度遍历(非递归);
StatusDMG_BFS(DMGraphDMG,intv,int*Visited);
//邻接矩阵广度遍历;
StatusDMG2DLG(DMGraphDMG,DLGraph*DLG);
//邻接矩阵转换为邻接表;
StatusDLG_Traver(DLGraphDLG);
//邻接表的遍历;
StatusDLG_DFS(DLGraphDLG,intv,int*Visited);
//邻接表深度遍历(递
StatusDLG_DFS_Uni(DLGraphDLG,intv,int*Visited);
//邻接表深度遍历(非递归);
StatusDLG_BFS(DLGraphDLG,intv,int*Visited);
//邻接表广度遍历;
//---------------------------------------------------------
StatusTopsort(DLGraphDLG,ElemType**ts);
//邻接表有向图的Topsort;
StatusDijkstra(DMGraphDMG,ElemTypev,unsignedint*dist);
//Dijkstra;
StatusPRN_DK(DMGraphDMG,unsignedint***dis);
//输出Dijkstra算法;
StatusFloyd(DMGraphDMG,unsignedint***flyd);
//Floyd;
StatusPRN_DMGraph(DMGraphDMG);
//输出邻接矩阵;
StatusPRN_DLGraph(DLGraphDLG);
//输出邻接表;
intmain(void)
{
inti,j;
DMGraphDMG;
DLGraphDLG;
ElemType*ts;
unsignedint**dist,**flyd;
printf(
"
一、创立有向图的邻接矩阵:
\n"
);
CreateDMGraph(&
DMG);
PRN_DMGraph(DMG);
printf("
\n\n二、有向图-邻接矩阵的遍历:
DMG_Traver(DMG);
\n\n三、邻接矩阵转换为邻接表:
DMG2DLG(DMG,&
DLG);
PRN_DLGraph(DLG);
\n\n四、有向图-邻接表的遍历:
DLG_Traver(DLG);
\n\n五、邻接表有向图的拓扑排序:
Topsort(DLG,&
ts);
\n\n\n"
system("
pause"
\n\n六、邻接矩阵有向图的各点最短路径:
\n\n
1.Dijkstra(迪杰斯特拉算法):
PRN_DK(DMG,&
dist);
\n\n\n
2.Floyd(弗洛伊德算法):
Floyd(DMG,&
flyd);
\n\n\nDijkstra最短路径测试输出:
\n某两点:
最短路径"
for(i=1;
i<
=DMG.VexNums;
i++)
for(j=1;
j<
j++)
if(dist[i][j]<
INT_MAX)printf("
\n[%2d,%2d]:
%5d"
i,j,dist[i][j]);
\n\nFloyd最短路径测试输出:
if(flyd[i][j]<
i,j,flyd[i][j]);
return0;
}
文件格式参见"
无向图"
说明:
//http:
//bbs.bc-
StatusCreateDMGraph(DMGraph*DMG)
charReadFileName[MaxStr];
unsignedintw;
FILE*fp;
do{
\n
输入文本文件名:
gets(ReadFileName);
}while(ReadFileName[0]=='
\0'
||!
(fp=fopen(ReadFileName,"
r"
)));
fscanf(fp,"
%d"
&
DMG->
VexNums);
//得到顶点总数;
if(!
(DMG->
Vex=(ElemType*)malloc((DMG->
VexNums+1)*sizeof(ElemType))))
{printf("
\n内存溢出。
return1;
Arc=(unsignedint**)malloc((DMG->
VexNums+1)*sizeof(unsignedint*))))
=DMG->
VexNums;
i++)
//邻接矩阵初始化;
Vex[i]=i;
//
Arc[i]=(unsignedint*)malloc((DMG->
VexNums+1)*sizeof(unsignedint))))
Arc[i][j]=INT_MAX;
while(fscanf(fp,"
%d%d%u"
i,&
j,&
w)==3)
Arc[i][j]=w;
fclose(fp);
StatusDMG2DLG(DMGraphDMG,DLGraph*DLG)
//图的邻接矩阵转换为邻接表;
ArcType*p=NULL;
DLG->
VexNums=DMG.VexNums;
(DLG->
Vex=(VexType*)malloc((DLG->
VexNums+1)*sizeof(VexType))))
FirstArc=(ArcType**)malloc((DLG->
VexNums+1)*sizeof(ArcType*))))
=DLG->
Vex[i].VNode=DMG.Vex[i];
FirstArc[i]=NULL;
Vex[i].indgree=0;
if(DMG.Arc[i][j]<
INT_MAX)
(p=(ArcType*)malloc(sizeof(ArcType))))
p->
Adj.VNode=DMG.Vex[j];
Weight=DMG.Arc[i][j];
NextArc=DLG->
FirstArc[i];
FirstArc[i]=p;
if(DMG.Arc[j][i]<
INT_MAX)
++DLG->
Vex[i].indgree;
StatusDMG_Traver(DMGraphDMG)
inti,*Visited;
(Visited=(int*)malloc((DMG.VexNums+1)*sizeof(int))))
}
1.图的深度遍历:
\n递
归:
Visited[i]=0;
if(Visited[i]==0)
DMG_DFS(DMG,i,Visited);
\n非递归:
DMG_DFS_Uni(DMG,i,Visited);
2.图的广度遍历:
\n\t"
DMG_BFS(DMG,i,Visited);
StatusDLG_Traver(DLGraphDLG)
(Visited=(int*)malloc((DLG.VexNums+1)*sizeof(int))))
=DLG.VexNums;
DLG_DFS(DLG,i,Visited);
DLG_DFS_Uni(DLG,i,Visited);
DLG_BFS(DLG,i,Visited);
StatusDMG_DFS(DMGraphDMG,intv,int*Visited)
inti;
Visited[v]=1;
%2d->
v);
if(Visited[i]==0&
&
DMG.Arc[v][i]<
StatusDLG_DFS(DLGraphDLG,intv,int*Visited)
p=DLG.FirstArc[v];
while(p!
=NULL)
if(Visited[p->
Adj.VNode]==0)
DLG_DFS(DLG,p->
Adj.VNode,Visited);
p=p->
NextArc;
StatusDMG_DFS_Uni(DMGraphDMG,intv,int*Visited)
inti,*Stack,top=-1;
(Stack=(int*)malloc((DMG.VexNums+1)*sizeof(int))))
Stack[++top]=v;
while(top!
=-1)
DMG.Arc[Stack[top]][i]<
Visited[i]=1;
i);
Stack[++top]=i;
break;
if(i==DMG.VexNums+1)
--top;
StatusDLG_DFS_Uni(DLGraphDLG,intv,int*Visited)
int*Stack,top=-1;
(Stack=(int*)malloc((DLG.VexNums+1)*sizeof(int))))
p=DLG.FirstArc[Stack[top]];
Adj.VNode]==0)
Visited[p->
Adj.VNode]=1;
p->
Adj.VNode);
Stack[++top]=p->
Adj.VNode;
if(p==NULL)
StatusDMG_BFS(DMGraphDMG,intv,int*Visited)
inti,*Queue,rear,front;
(Queue=(int*)malloc((DMG.VexNums+1)*sizeof(int))))
Visi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 转换 遍历 拓扑 路径