第十三周项目3-Dijkstra算法的验证
来源:互联网 发布:淘宝怎样不被厂家投诉 编辑:程序博客网 时间:2024/06/05 14:37
/* * Copyright (c) 2016, 烟台大学计算机与控制工程学院 * All rights reserved。 * 文件名称 :1.cpp * 作 者 :杨俊杰 * 完成日期 :2016年 11月24日 * 版 本 号 :v1.0 * 问题描述 : Dijkstra算法的验证 * 输出描述 : */
-
-
- #ifndef GRAPH_H_INCLUDED
- #define GRAPH_H_INCLUDED
-
- #define MAXV 100 //最大顶点个数
- #define INF 32767 //INF表示∞
- typedef int InfoType;
-
-
- typedef struct
- {
- int no;
- InfoType info;
- } VertexType;
-
- typedef struct
- {
- int edges[MAXV][MAXV];
- int n,e;
- VertexType vexs[MAXV];
- } MGraph;
-
-
- typedef struct ANode
- {
- int adjvex;
- struct ANode *nextarc;
- InfoType info;
- } ArcNode;
-
- typedef int Vertex;
-
- typedef struct Vnode
- {
- Vertex data;
- int count;
- ArcNode *firstarc;
- } VNode;
-
- typedef VNode AdjList[MAXV];
-
- typedef struct
- {
- AdjList adjlist;
- int n,e;
- } ALGraph;
-
-
-
-
-
- void ArrayToMat(int *Arr, int n, MGraph &g);
- void ArrayToList(int *Arr, int n, ALGraph *&);
- void MatToList(MGraph g,ALGraph *&G);
- void ListToMat(ALGraph *G,MGraph &g);
- void DispMat(MGraph g);
- void DispAdj(ALGraph *G);
-
- #endif // GRAPH_H_INCLUDED
-
-
- #include <stdio.h>
- #include <malloc.h>
- #include "graph.h"
-
-
-
-
-
- void ArrayToMat(int *Arr, int n, MGraph &g)
- {
- int i,j,count=0;
- g.n=n;
- for (i=0; i<g.n; i++)
- for (j=0; j<g.n; j++)
- {
- g.edges[i][j]=Arr[i*n+j];
- if(g.edges[i][j]!=0 && g.edges[i][j]!=INF)
- count++;
- }
- g.e=count;
- }
-
- void ArrayToList(int *Arr, int n, ALGraph *&G)
- {
- int i,j,count=0;
- ArcNode *p;
- G=(ALGraph *)malloc(sizeof(ALGraph));
- G->n=n;
- for (i=0; i<n; i++)
- G->adjlist[i].firstarc=NULL;
- for (i=0; i<n; i++)
- for (j=n-1; j>=0; j--)
- if (Arr[i*n+j]!=0)
- {
- p=(ArcNode *)malloc(sizeof(ArcNode));
- p->adjvex=j;
- p->info=Arr[i*n+j];
- p->nextarc=G->adjlist[i].firstarc;
- G->adjlist[i].firstarc=p;
- }
-
- G->e=count;
- }
-
- void MatToList(MGraph g, ALGraph *&G)
-
- {
- int i,j;
- ArcNode *p;
- G=(ALGraph *)malloc(sizeof(ALGraph));
- for (i=0; i<g.n; i++)
- G->adjlist[i].firstarc=NULL;
- for (i=0; i<g.n; i++)
- for (j=g.n-1; j>=0; j--)
- if (g.edges[i][j]!=0)
- {
- p=(ArcNode *)malloc(sizeof(ArcNode));
- p->adjvex=j;
- p->info=g.edges[i][j];
- p->nextarc=G->adjlist[i].firstarc;
- G->adjlist[i].firstarc=p;
- }
- G->n=g.n;
- G->e=g.e;
- }
-
- void ListToMat(ALGraph *G,MGraph &g)
-
- {
- int i,j;
- ArcNode *p;
- g.n=G->n;
- g.e=G->e;
- for (i=0; i<g.n; i++)
- for (j=0; j<g.n; j++)
- g.edges[i][j]=0;
- for (i=0; i<G->n; i++)
- {
- p=G->adjlist[i].firstarc;
- while (p!=NULL)
- {
- g.edges[i][p->adjvex]=p->info;
- p=p->nextarc;
- }
- }
- }
-
- void DispMat(MGraph g)
-
- {
- int i,j;
- for (i=0; i<g.n; i++)
- {
- for (j=0; j<g.n; j++)
- if (g.edges[i][j]==INF)
- printf("%3s","∞");
- else
- printf("%3d",g.edges[i][j]);
- printf("\n");
- }
- }
-
- void DispAdj(ALGraph *G)
-
- {
- int i;
- ArcNode *p;
- for (i=0; i<G->n; i++)
- {
- p=G->adjlist[i].firstarc;
- printf("%3d: ",i);
- while (p!=NULL)
- {
- printf("-->%d/%d ",p->adjvex,p->info);
- p=p->nextarc;
- }
- printf("\n");
- }
- }
-
- #include <stdio.h>
- #include <malloc.h>
- #include "graph.h"
- #define MaxSize 100
- void Ppath(int path[],int i,int v)
- {
- int k;
- k=path[i];
- if (k==v) return;
- Ppath(path,k,v);
- printf("%d,",k);
- }
- void Dispath(int dist[],int path[],int s[],int n,int v)
- {
- int i;
- for (i=0; i<n; i++)
- if (s[i]==1)
- {
- printf(" 从%d到%d的最短路径长度为:%d\t路径为:",v,i,dist[i]);
- printf("%d,",v);
- Ppath(path,i,v);
- printf("%d\n",i);
- }
- else printf("从%d到%d不存在路径\n",v,i);
- }
- void Dijkstra(MGraph g,int v)
- {
- int dist[MAXV],path[MAXV];
- int s[MAXV];
- int mindis,i,j,u;
- for (i=0; i<g.n; i++)
- {
- dist[i]=g.edges[v][i];
- s[i]=0;
- if (g.edges[v][i]<INF)
- path[i]=v;
- else
- path[i]=-1;
- }
- s[v]=1;
- path[v]=0;
- for (i=0; i<g.n; i++)
- {
- mindis=INF;
- for (j=0; j<g.n; j++)
- if (s[j]==0 && dist[j]<mindis)
- {
- u=j;
- mindis=dist[j];
- }
- s[u]=1;
- for (j=0; j<g.n; j++)
- if (s[j]==0)
- if (g.edges[u][j]<INF && dist[u]+g.edges[u][j]<dist[j])
- {
- dist[j]=dist[u]+g.edges[u][j];
- path[j]=u;
- }
- }
- Dispath(dist,path,s,g.n,v);
- }
-
- int main()
- {
- MGraph g;
- int A[6][6]=
- {
- {0,50,10,INF,45,INF},
- {INF,0,15,INF,5,INF},
- {20,INF,0,15,INF,INF},
- {INF,20,INF,0,35,INF},
- {INF,INF,INF,30,0,INF},
- {INF,INF,INF,3,INF,0},
- };
- ArrayToMat(A[0], 6, g);
- Dijkstra(g,0);
- return 0;
- }
附:测试用图结构
运行结果:
0 0