最新邻接表prim算法教学教材.docx

上传人:b****6 文档编号:6088676 上传时间:2023-01-03 格式:DOCX 页数:9 大小:16.19KB
下载 相关 举报
最新邻接表prim算法教学教材.docx_第1页
第1页 / 共9页
最新邻接表prim算法教学教材.docx_第2页
第2页 / 共9页
最新邻接表prim算法教学教材.docx_第3页
第3页 / 共9页
最新邻接表prim算法教学教材.docx_第4页
第4页 / 共9页
最新邻接表prim算法教学教材.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

最新邻接表prim算法教学教材.docx

《最新邻接表prim算法教学教材.docx》由会员分享,可在线阅读,更多相关《最新邻接表prim算法教学教材.docx(9页珍藏版)》请在冰豆网上搜索。

最新邻接表prim算法教学教材.docx

最新邻接表prim算法教学教材

#include

#include

#defineMAX_VERTEX_NUM20

#defineMAX32767

#defineERROR0

#defineOK1

#defineOVERFLOW-2

typedefintStatus;

typedefcharVertexType;

typedefintPathMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedefintShortPathTable[MAX_VERTEX_NUM];

boolvisited[MAX_VERTEX_NUM];

boolfinal[MAX_VERTEX_NUM];

typedefstruct

{charadj;

intlowcost;

}closedge[MAX_VERTEX_NUM];

 

typedefstructArcNode

{intadjvex;//弧所指向的顶点的位置

structArcNode*nextarc;//指向下一条弧的指针

intweight;//权重

//InfoType*info;//该弧相关信息的指针

}ArcNode;

typedefstructVNode

{VertexTypedata;//顶点信息

ArcNode*firstarc;

}VNode,AdjList[MAX_VERTEX_NUM];

typedefstruct

{AdjListvertices;

intvexnum,arcnum;

intkind;

}ALGraph;

//--------------------------------------------------

//队列

typedefstructnode

{intdata;

structnode*next;

}QNode,*QueuePtr;

typedefstruct

{QueuePtrfront;

QueuePtrrear;

}LinkQueue;

//构造一个空队列

StatusInitQueue(LinkQueue&Q)

{Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));

if(!

Q.front)exit(OVERFLOW);

Q.front->next=NULL;

returnOK;

}

//插入元素

StatusEnQueue(LinkQueue&Q,int&e)

{QueuePtrp;

p=(QueuePtr)malloc(sizeof(QNode));

if(!

p)exit(OVERFLOW);

p->data=e;

p->next=NULL;

Q.rear->next=p;

Q.rear=p;

returnOK;

}

//出队列

StatusDeQueue(LinkQueue&Q,int&e)

{QueuePtrp;

if(Q.front==Q.rear)returnERROR;

p=Q.front->next;

e=p->data;

Q.front->next=p->next;

if(p==Q.rear)Q.rear=Q.front;

free(p);

returnOK;

}

StatusQueueEmpty(LinkQueue&Q)

{if(Q.front==Q.rear)returnOK;

else

returnERROR;

}

/*--------------------------------------------------*/

intLocateVex(ALGraphG,chare)

{inti;

for(i=0;i

if(e==G.vertices[i].data)returni;

return-1;

}

//创建图

intCreateGraph(ALGraph&G)

{inti,j,w,k;

charV1,V2;

printf("输入顶点数,弧数:

");

scanf("%d%d",&G.vexnum,&G.arcnum);

for(i=0;i

{printf("输入第%d个顶点的值:

",i+1);

scanf("\n%c",&G.vertices[i].data);

fflush(stdin);

G.vertices[i].firstarc=NULL;

}

ArcNode*p;

for(i=0;i

{printf("输入一条边依附的顶点,权重(以逗号隔开):

");

scanf("%c,%c,%d",&V1,&V2,&w);

fflush(stdin);

p=(ArcNode*)malloc(sizeof(ArcNode));

j=LocateVex(G,V1);

k=LocateVex(G,V2);

p->adjvex=k;

p->weight=w;

p->nextarc=G.vertices[j].firstarc;

G.vertices[j].firstarc=p;

}

returnOK;

}

//邻接表表示的深度优先搜索算法

voidDFS(ALGraphG,intv)

{intj;

ArcNode*q;

visited[v]=OK;

printf("%c",G.vertices[v].data);

q=G.vertices[v].firstarc;

for(;q;q=q->nextarc)

{j=q->adjvex;

if(!

visited[j])DFS(G,j);

}

}

//邻接表表示的广度优先搜索算法

voidBFS(ALGraphG,intv)

{inti,u;

ArcNode*q;

for(i=0;i

visited[i]=ERROR;

LinkQueueQ;

InitQueue(Q);

for(i=v;i

{if(!

visited[i])

{visited[i]=true;

printf("%c",G.vertices[i].data);

EnQueue(Q,i);

}

while(!

QueueEmpty(Q))

{DeQueue(Q,u);

q=G.vertices[u].firstarc;

for(;q;q=q->nextarc)

{if(!

visited[u])

EnQueue(Q,u);

}

}

}

}

intminium(closedgea,ALGraphG)

{inti=0,j,k,min;

while(!

a[i].lowcost)

i++;

min=a[i].lowcost;//第一个不为0的值

k=i;

for(j=i+1;j

if(a[j].lowcost>0)

if(min>a[j].lowcost)

{

min=a[j].lowcost;

k=j;

}

returnk;

}

voidMiniSpanTree_PRIM(ALGraph&G,charu)

{intk,j,i;

ArcNode*q;

closedgea;

for(j=0;j

{a[j].lowcost=MAX;

}

k=LocateVex(G,u);

q=G.vertices[k].firstarc;

while(q)

{a[q->adjvex].adj=G.vertices[k].data;

a[q->adjvex].lowcost=q->weight;

q=q->nextarc;

}

a[k].lowcost=0;

printf("\n最小代价生成树的各条边为:

\n");

for(i=1;i

{k=minium(a,G);

printf("(%c-%c)\n",a[k].adj,G.vertices[k].data);

a[k].lowcost=0;

q=G.vertices[k].firstarc;

while(q)

{

if(q->weightadjvex].lowcost)

{a[q->adjvex].adj=G.vertices[k].data;

a[q->adjvex].lowcost=q->weight;

}

q=q->nextarc;

}

}

}

//

voidShortestPath_DIJ(ALGraph&G1,intv0,PathMatrix&p,ShortPathTable&D)

{intv,i,min,w,j;

ArcNode*q;

q=G1.vertices[v0].firstarc;

for(v=0;v

{final[v]=false;

D[v]=MAX;

for(w=0;w

p[v][w]=false;

}

while(q)

{D[q->adjvex]=q->weight;

p[q->adjvex][v0]=true;

p[q->adjvex][q->adjvex]=true;

q=q->nextarc;

}

D[v0]=0;

final[v0]=true;

for(i=1;i

{min=MAX;

for(w=0;w

if(!

final[w])

if(D[w]

{v=w;

min=D[w];

}

final[v]=true;//离v0最近定点v

q=G1.vertices[v].firstarc;

while(q)

{if(!

final[q->adjvex]&&min+q->weightadjvex])

{D[q->adjvex]=min+q->weight;

for(j=0;j

{p[q->adjvex][j]=p[v][j];

p[q->adjvex][q->adjvex]=true;

}

}

q=q->nextarc;

}

}

}

voidmain()

{inti,start,start1,j;

charc;

PathMatrixp;

ShortPathTabled;

for(i=0;i

visited[i]=ERROR;

ALGraphG;

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;i

{

for(j=0;j

printf("%2d",p[i][j]);

printf("\n");

}

printf("%c到各顶点的最短路径长度为:

\n",G.vertices[0].data);

for(i=1;i

printf("%c-%c:

%d\n",G.vertices[0].data,G.vertices[i].data,d[i]);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1