1、最新邻接表prim算法教学教材#include#include#define MAX_VERTEX_NUM 20#define MAX 32767#define ERROR 0#define OK 1#define OVERFLOW -2typedef int Status;typedef char VertexType;typedef int PathMatrix MAX_VERTEX_NUM MAX_VERTEX_NUM;typedef int ShortPathTable MAX_VERTEX_NUM;bool visitedMAX_VERTEX_NUM;bool finalMAX_VE
2、RTEX_NUM;typedef struct char adj; int lowcost; closedgeMAX_VERTEX_NUM;typedef struct ArcNodeint adjvex;/弧所指向的顶点的位置struct ArcNode *nextarc;/指向下一条弧的指针int weight;/权重/InfoType *info;/该弧相关信息的指针ArcNode;typedef struct VNodeVertexType data;/顶点信息ArcNode *firstarc;VNode,AdjListMAX_VERTEX_NUM;typedef struct Ad
3、jList vertices;int vexnum,arcnum;int kind;ALGraph;/-/队列typedef struct nodeint data;struct node *next;QNode,*QueuePtr;typedef struct QueuePtr front; QueuePtr rear;LinkQueue;/构造一个空队列Status InitQueue(LinkQueue &Q)Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode); if(!Q.front) exit(OVERFLOW); Q.front-next=N
4、ULL; return OK;/插入元素Status EnQueue(LinkQueue &Q,int &e)QueuePtr p; p=(QueuePtr)malloc(sizeof(QNode); if(!p) exit(OVERFLOW); p-data=e; p-next=NULL; Q.rear-next=p; Q.rear=p; return OK;/出队列Status DeQueue(LinkQueue &Q,int &e)QueuePtr p; if(Q.front=Q.rear)return ERROR; p=Q.front-next; e=p-data; Q.front-n
5、ext =p-next; if(p=Q.rear)Q.rear=Q.front; free(p); return OK;Status QueueEmpty(LinkQueue &Q)if(Q.front=Q.rear) return OK; else return ERROR;/*-*/int LocateVex(ALGraph G,char e) int i; for(i=0;iG.vexnum;i+) if(e=G.verticesi.data) return i; return -1;/创建图int CreateGraph(ALGraph &G)int i,j,w,k;char V1,V
6、2;printf(输入顶点数,弧数:);scanf(%d%d,&G.vexnum,&G.arcnum);for(i=0;iG.vexnum;i+) printf(输入第%d个顶点的值:,i+1); scanf(n%c,&G.verticesi.data); fflush(stdin);G.verticesi.firstarc=NULL;ArcNode* p;for(i=0;iadjvex=k; p-weight=w; p-nextarc=G.verticesj.firstarc; G.verticesj.firstarc=p;return OK;/邻接表表示的深度优先搜索算法 void DFS
7、(ALGraph G,int v)int j;ArcNode* q;visitedv=OK;printf(%c ,G.verticesv.data ); q=G.verticesv.firstarc; for(;q;q=q-nextarc) j=q-adjvex; if(!visitedj) DFS(G,j); /邻接表表示的广度优先搜索算法 void BFS(ALGraph G,int v)int i,u;ArcNode* q;for(i=0;iMAX_VERTEX_NUM;i+) visitedi=ERROR; LinkQueue Q;InitQueue(Q);for(i=v;inexta
8、rc) if(!visitedu) EnQueue(Q,u); int minium(closedge a,ALGraph G)int i=0,j,k,min; while(!ai.lowcost) i+; min=ai.lowcost; / 第一个不为0的值 k=i; for(j=i+1;j0) if(minaj.lowcost) min=aj.lowcost; k=j; return k;void MiniSpanTree_PRIM(ALGraph &G,char u) int k,j,i; ArcNode* q; closedge a; for(j=0;jadjvex.adj=G.ver
9、ticesk.data; aq-adjvex.lowcost=q-weight ; q=q-nextarc; ak.lowcost=0; printf(n最小代价生成树的各条边为:n); for(i=1;iweightadjvex.lowcost) aq-adjvex.adj=G.verticesk.data; aq-adjvex.lowcost=q-weight ; q=q-nextarc; /void ShortestPath_DIJ(ALGraph &G1,int v0,PathMatrix &p,ShortPathTable &D)int v,i,min,w,j; ArcNode* q
10、; q=G1.verticesv0.firstarc; for(v=0;vG1.vexnum;v+) finalv=false; Dv=MAX; for(w=0;wadjvex=q-weight; pq-adjvexv0=true; pq-adjvexq-adjvex=true; q=q-nextarc; Dv0=0; finalv0=true; for(i=1;iG1.vexnum;i+) min=MAX; for(w=0;wG1.vexnum;w+) if(!finalw) if(Dwadjvex&min+q-weightadjvex) Dq-adjvex=min+q-weight; fo
11、r(j=0;jadjvexj=pvj; pq-adjvexq-adjvex=true; q=q-nextarc; void main() int i,start,start1,j;char c;PathMatrix p; ShortPathTable d;for(i=0;iMAX_VERTEX_NUM;i+)visitedi=ERROR;ALGraph G;CreateGraph(G);printf(深度优先遍历开始位置:);scanf(%d,&start);DFS(G,start);printf(n广度优先遍历开始元素:); scanf(%d,&start1);BFS( G,start1); printf(nprim算法开始元素:);scanf(n%c,&c);MiniSpanTree_PRIM(G, c );ShortestPath_DIJ(G,0,p,d);for(i=0;iG.vexnum;+i) for(j=0;jG.vexnum;+j) printf(%2d,pij); printf(n); printf(%c到各顶点的最短路径长度为:n,G.vertices0.data); for(i=1;iG.vexnum;+i) printf(%c-%c:%dn,G.vertices0.data,G.verticesi.data,di);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1