利用普利姆算法构建最小生成树(顶点为数值),不知道为什么不能成功(整个程序是几个小程序搞在一起的,选择一下就好了)
真的很郁闷,自己纠错了两天还是不行#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define INT_MAX 1000
#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20
#define ERROR false
#define OK true
typedef struct ArcCell {
int adj;
char *info;
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct {
int vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum, arcnum;
int kind;
}MGraph;
typedef struct ArcNode{
int adjvex;
struct ArcNode *nextarc;
int *info;
}ArcNode;
typedef struct VNode{
int data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct{
AdjList vertices;
int vexnum,arcnum;
int kind;
}ALGraph;
//邻接矩阵 (udn)
int LocateVex(MGraph G, int u)
{
int i;
for (i=0; i<G.vexnum; ++i){
if (G.vexs[i]==u)
return i;
}
return -1;
}
int CreateDG(MGraph &G) {
int i,j,k;
int v1,v2;
printf("构建有向图,请输入顶点的数目和边的数目:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请依次输入对应的顶点值:\n");
for( i = 0; i < G.vexnum; ++i ) {
scanf("%d",&G.vexs[i]);
}
for( i = 0; i < G.vexnum; ++i ) {
for( j = 0; j <G.vexnum; ++j ) {
G.arcs[i][j].adj = 0;
G.arcs[i][j].info = NULL;
}
}
for( k = 0; k < G.arcnum; ++k ) {
printf("请输入第%d条边的第一个顶点的值:",k+1);
scanf("%d",&v1);
printf("请输入第%d条边的第二个顶点的值:",k+1);
scanf("%d",&v2);
i=LocateVex(G,v1);
j=LocateVex(G,v2);
G.arcs[i][j].adj=1;
}
}
int CreateDN(MGraph &G) {
int i,j,k,w;
int v1,v2;
printf("构建有向网,请输入顶点的数目和边的数目:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请依次输入对应的顶点的值:\n");
for( i = 0; i < G.vexnum; ++i ) {
scanf("%d",&G.vexs[i]);
}
for( i = 0; i < G.vexnum; ++i ) {
for( j = 0; j <G.vexnum; ++j ) {
G.arcs[i][j].adj = INFINITY;
G.arcs[i][j].info = NULL;
}
}
for( k = 0; k < G.arcnum; ++k ) {
printf("请输入第%d条边的第一个顶点的值:",k+1);
scanf("%d",&v1);
printf("请输入第%d条边的第二个顶点的值:",k+1);
scanf("%d",&v2);
printf("请输入权值\n");
scanf("%d",&w);
i=LocateVex(G,v1);
j=LocateVex(G,v2);
G.arcs[i][j].adj=w;
}
}
int CreateUDG(MGraph &G) {
int i,j,k;
int v1,v2;
printf("构建无向图,请输入顶点的数目和边的数目:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请依次输入对应的顶点的值:\n");
for( i = 0; i < G.vexnum; ++i ) {
scanf("%d",&G.vexs[i]);
}
for( i = 0; i < G.vexnum; ++i ) {
for( j = 0; j <G.vexnum; ++j ) {
G.arcs[i][j].adj = 0;
G.arcs[i][j].info = NULL;
}
}
for( k = 0; k < G.arcnum; ++k ) {
printf("请输入第%d条边的第一个顶点的值:",k+1);
scanf("%d",&v1);
printf("请输入第%d条边的第二个顶点的值:",k+1);
scanf("%d",&v2);
i=LocateVex(G,v1);
j=LocateVex(G,v2);
G.arcs[i][j].adj=1;
}
}
int CreateUDN(MGraph &G) {
int i,j,k,w;
int v1,v2;
printf("构建无向网,请输入顶点的数目和边的数目:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请依次输入对应的顶点的值:\n");
for( i = 0; i < G.vexnum; ++i ) {
scanf("%d",&G.vexs[i]);
}
for( i = 0; i < G.vexnum; ++i ) {
for( j = 0; j <G.vexnum; ++j ) {
G.arcs[i][j].adj = INFINITY;
G.arcs[i][j].info = NULL;
}
}
for( k = 0; k < G.arcnum; ++k ) {
printf("请输入第%d条边的第一个顶点的值:",k+1);
scanf("%d",&v1);
printf("请输入第%d条边的第二个顶点的值:",k+1);
scanf("%d",&v2);
printf("请输入权值\n");
scanf("%d",&w);
i=LocateVex(G,v1);
j=LocateVex(G,v2);
G.arcs[i][j].adj=w;
}
}
//邻接表
int CreateUDG2(ALGraph &G){
int i,s,d;
ArcNode *p,*q;
printf("请输入顶点数,边数:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请输入顶点值(字符)\n");
for(i=0;i<G.vexnum;i++){
getchar();
scanf("%c",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;
}
printf("请输入顶点序号:\n");
for(i=0;i<G.arcnum;i++){
scanf("%d%d",&s,&d);
p=(ArcNode*)malloc(sizeof(ArcNode));
q=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=d;
q->adjvex=s;
p->nextarc=G.vertices[s].firstarc;
G.vertices[s].firstarc=p;
q->nextarc=G.vertices[d].firstarc;
G.vertices[d].firstarc=q;
}
}
int CreateDG2(ALGraph &G){
int i,s,d;
ArcNode *p;
printf("请输入顶点数,边数:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请输入顶点值(字符)\n");
for(i=0;i<G.vexnum;i++){
getchar();
scanf("%c",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;
}
printf("请输入顶点序号:\n");
for(i=0;i<G.arcnum;i++){
scanf("%d%d",&s,&d);
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=d;
p->nextarc=G.vertices[s].firstarc;
G.vertices[s].firstarc=p;
}
}
int CreateDN2(ALGraph &G){
int i,s,d,IncInfo;
ArcNode *p;
printf("请输入顶点数,边数:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请输入顶点值(字符)");
for(i=0;i<G.vexnum;i++){
getchar();
scanf("%c",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;
}
printf("请输入顶点序号:\n");
for(i=0;i<G.arcnum;i++){
scanf("%d%d",&s,&d);
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=d;
p->nextarc=G.vertices[s].firstarc;
G.vertices[s].firstarc=p;
printf("此边是否含有其他信息?(0.是 1. 否)");
scanf("%d",&IncInfo);
if(IncInfo){
printf("请输入相关信息\n");
scanf("%s",p->info);
}
}
}
int CreateUDN2(ALGraph &G){
int i,s,d,IncInfo;
ArcNode *p,*q;
printf("请输入顶点数,边数:\n");
scanf("%d%d",&G.vexnum,&G.arcnum);
printf("请输入顶点值(字符)\n");
for(i=0;i<G.vexnum;i++){
getchar();
scanf("%c",&G.vertices[i].data);
G.vertices[i].firstarc=NULL;
}
printf("请输入顶点序号:\n");
for(i=0;i<G.arcnum;i++){
scanf("%d%d",&s,&d);
p=(ArcNode*)malloc(sizeof(ArcNode));
q=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=d;
q->adjvex=s;
p->nextarc=G.vertices[s].firstarc;
G.vertices[s].firstarc=p;
q->nextarc=G.vertices[d].firstarc;
G.vertices[d].firstarc=q;
printf("此边是否含有其他信息?(0.是 1. 否)");
scanf("%d",&IncInfo);
if(IncInfo){
printf("请输入相关信息\n");
scanf("%s",p->info);
}
}
}
int CreateGraph2(ALGraph &G) {
printf("请输入你要创建的图的类型:\n1、有向图;2、有向网;3、无向图;4、无向网\n");
scanf("%d",&G.kind);
switch(G.kind) {
case 1: CreateDG2(G);break;
case 2: CreateDN2(G);break;
case 3: CreateUDG2(G);break;
case 4: CreateUDN2(G);break;
default:return ERROR;
}
}
int CreateGraph(MGraph &G) {
printf("请输入你要创建的图的类型:\n1、有向图;2、有向网;3、无向图;4、无向网\n");
scanf("%d",&G.kind);
switch(G.kind) {
case 1: CreateDG(G);break;
case 2: CreateDN(G);break;
case 3: CreateUDG(G);break;
case 4: CreateUDN(G);break;
default:return ERROR;
}
}
int(*VisitFunc)(int v,ALGraph G);
bool visited[MAX_VERTEX_NUM];
//DFS
int DFS(ALGraph G,int v){
ArcNode *p;
visited[v]=true;
(*VisitFunc)(v,G);
for(p=G.vertices[v].firstarc;p;p=p->nextarc){
if(!visited[p->adjvex])
DFS(G,p->adjvex);
}
}
//DFSraverse
int DFSTraverse(ALGraph G,int(*Visit)(int v,ALGraph G)){
int v;
VisitFunc=Visit;
for(v=0;v<G.vexnum;++v)
visited[v]=false;
for(v=0;v<G.vexnum;++v){
if(!visited[v])
DFS(G,v);
}
}
//v函数
int Printelem(int v,ALGraph G){
printf("%c ",G.vertices[v].data);
}
//prim算法
typedef struct{
char adjvex;
int lowcost;
}CLOSEDGE,closedge[MAX_VERTEX_NUM];
int minimum(closedge SZ,MGraph G)
{
int i=0,j,k,min;
while(SZ[i].lowcost!=0)
i++;
min=SZ[i].lowcost;
k=i;
for(j=i+1;j<G.vexnum;j++)
if(SZ[j].lowcost>0)
if(min>SZ[j].lowcost)
{
min=SZ[j].lowcost;
k=j;
}
return k;
}
int MiniSpanTree_PRIM(MGraph G,int u){
int k,j,i;
closedge T;
k=LocateVex(G,u);
for(j=0;j<G.vexnum;++j){
if(j!=k)
{T[j].adjvex=u;
T[j].lowcost=G.arcs[k][j].adj;
}
}
T[k].lowcost=0;
for(i=1;i<G.vexnum;++i){
k=minimum(T,G);
printf("(%d-%d)",T[k].adjvex,G.vexs[k]);
T[k].lowcost=0;
for(j=0;j<G.vexnum;++j){
if(G.arcs[k][j].adj<T[j].lowcost)
{
T[j].adjvex=G.vexs[k];
T[j].lowcost=G.arcs[k][j].adj;
}
}
}
}
int main(){
int t,i;
do{
printf("请选择进行的操作:\n1.构建图 2.遍历 3.构建最小生成树\n");
scanf("%d",&i);
switch(i){
case 1:printf("请选择存储结构:1.邻接矩阵 2.邻接表\n");
scanf("%d",&t);
if(t==1){
MGraph G;
CreateGraph(G);
}
else {
ALGraph G;
CreateGraph2(G);
}
printf("\n");
break;
case 2:printf("请先构建图\n");
ALGraph K;
CreateGraph2(K);
DFSTraverse(K,Printelem);
printf("\n");
break;
case 3:printf("请先构建无向网\n");
MGraph M;
char o;
CreateUDN(M);
printf("请输入最小生成树的起点\n");
scanf("%d",&o);
printf("\n");
MiniSpanTree_PRIM(M,o);
printf("\n");
break;
}
}while(i);
}