| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3381 人关注过本帖
标题:利用普利姆算法构建最小生成树(顶点为数值),不知道为什么不能成功(整个 ...
取消只看楼主 加入收藏
听潮
Rank: 2
等 级:论坛游民
帖 子:18
专家分:10
注 册:2016-4-21
结帖率:100%
收藏
已结贴  问题点数:6 回复次数:2 
利用普利姆算法构建最小生成树(顶点为数值),不知道为什么不能成功(整个程序是几个小程序搞在一起的,选择一下就好了)
真的很郁闷,自己纠错了两天还是不行


#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);

}
搜索更多相关主题的帖子: include false 
2016-06-16 22:23
听潮
Rank: 2
等 级:论坛游民
帖 子:18
专家分:10
注 册:2016-4-21
收藏
得分:0 
回复 2楼 书生牛犊
谢谢你愿意亲自看一下!
2016-06-17 19:15
听潮
Rank: 2
等 级:论坛游民
帖 子:18
专家分:10
注 册:2016-4-21
收藏
得分:0 
已经生成完整的代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define INT_MAX                 100
#define INFINITY                INT_MAX         
#define MAX_VERTEX_NUM          20              
#define ERROR                   false
#define OK                   true
typedef int VRType;
typedef char InfoType;
typedef char VertexType;
typedef bool Status;
typedef enum {DG, DN, UDG, UDN} GraphKind;      

typedef struct ArcCell {
 VRType adj;                                 
 InfoType *info;                             
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

struct MGraph
{
 VertexType vexs[MAX_VERTEX_NUM];            
 AdjMatrix arcs;                             
 int vexnum, arcnum;                        
 GraphKind kind;                             
};

typedef struct ArcNode{
    int adjvex;
    struct ArcNode *nextarc;
    int *info;
}ArcNode;

typedef struct VNode{
    VertexType data;
    ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];

typedef struct{
    AdjList vertices;
    int vexnum,arcnum;
    int kind;
}ALGraph;

Status CreateDG(MGraph &G);
Status CreateDN(MGraph &G);
Status CreateUDG(MGraph &G);
Status CreateUDN(MGraph &G);
Status CreateDG1(ALGraph &M);
Status CreateDN1(ALGraph &M);
Status CreateUDG1(ALGraph &M);
Status CreateUDN1(ALGraph &M);

int LocateVex(MGraph G, VertexType u)
{
 
 int i;
 
 for (i=0; i<G.vexnum; ++i)
  if (G.vexs[i]==u)
   return i;
  
  return -1;
}


Status CreateGraph(MGraph &G) {
 
 
 printf("请输入你要创建的图的类型:\n1、有向图邻接矩阵;2、有向网邻接矩阵;3、无向图邻接矩阵;4、无向网邻接矩阵\n");
 scanf("%d",&G.kind);
 switch(G.kind-1) {
 case DG:  CreateDG(G);break;   
 case DN:  CreateDN(G);   break;
 case UDG:  CreateUDG(G);  break;
 case UDN:  CreateUDN(G);break;  
 default:return ERROR;
 }
}


Status CreateGraph1(ALGraph &M) {
 
 
 printf("请输入你要创建的图的类型:\n1、有向图邻接表;2、有向网邻接表;3、无向图邻接表;4、无向网邻接表\n");
 scanf("%d",&M.kind);
 switch(M.kind-1) {
 case DG:  CreateDG1(M);break;   
 case DN:  CreateDN1(M);break;   
 case UDG:  CreateUDG1(M);break;   
 case UDN:  CreateUDN1(M);break;  
 default:return ERROR;
 }
}

Status CreateDG(MGraph &G)                                
{
    G.kind=DG;
    int i, j, k;
    VertexType v1, v2;
    printf("请输入有向图的顶点和边数:\n");
    scanf("%d %d", &G.vexnum, &G.arcnum);
    getchar();
    printf("输入%d个顶点的值:", G.vexnum);
    for (i = 0;i<G.vexnum;i++) {
        scanf("%c", &G.vexs[i]);
        getchar();
    }
    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 = 0;
        }
    printf("输入%d条边的两个顶点:\n", G.arcnum);
    for (k = 0;k<G.arcnum;k++) {                                    
        scanf("%c %c", &v1, &v2);
        getchar();                           
        i = LocateVex(G, v1);
        j = LocateVex(G, v2);
        G.arcs[i][j].adj = 1;
    }
    return 1;
}

Status CreateDN(MGraph &G)                                 
{
    G.kind=DN;
    int i, j, k, w;
    VertexType v1, v2;
    printf("请输入有向网的顶点和边数:\n");
    scanf("%d %d", &G.vexnum, &G.arcnum);
    getchar();
    printf("输入%d个顶点的值:", G.vexnum);
    for (i = 0;i<G.vexnum;i++) {
        scanf("%c", &G.vexs[i]);
        getchar();
    }
    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 = 0;
        }
    printf("输入%d条边的两个顶点及权值:\n", G.arcnum);
    for (k = 0;k<G.arcnum;k++)
    {
        scanf("%c %c %d", &v1, &v2, &w);
        getchar();
        i = LocateVex(G, v1);
        j = LocateVex(G, v2);
        G.arcs[i][j].adj = w;
    }
    return 1;
}

Status CreateUDG(MGraph &G)
{
    G.kind=UDG;
    printf("请输入无向图的顶点数与边数:\n");
    scanf("%d %d",&G.vexnum, &G.arcnum);
    int i,j;
    printf("输入%d个顶点的值:",G.vexnum);
        getchar();
        for(i=0;i<G.vexnum;i++)
        {
        scanf("%c",&G.vexs[i]);
        getchar();
        }
    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;
        }
    }
    int k;
    VertexType v1, v2;
    for(k=0; k<G.arcnum; ++k)
    {  
        printf("请输入第%d条边依附的顶点:\n", k+1);  
        
        scanf("%c %c",&v1, &v2);
        getchar();
        i=LocateVex(G,v1);
        j=LocateVex(G,v2);
        if(i==-1 || j==-1)
            return ERROR;
        G.arcs[i][j].adj = 1;
        G.arcs[j][i].adj = G.arcs[i][j].adj;
    }
    return OK;  
}



Status CreateUDN(MGraph &G)                     
{
    G.kind=UDN;
    int i, j, k, w;
    VertexType v1, v2;
    printf("请输入无向网的顶点和边数:\n");
    scanf("%d %d", &G.vexnum, &G.arcnum);
    getchar();
    printf("输入%d个顶点的值:\n", G.vexnum);
    for (i = 0;i<G.vexnum;i++) {
        scanf("%c", &G.vexs[i]);
        getchar();
    }
    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 = 0;
        }
    printf("输入%d条边的两个顶点及权值:\n", G.arcnum);
    for (k = 0;k<G.arcnum;k++)
    {
        scanf("%c %c %d", &v1, &v2, &w);
        getchar();
        i = LocateVex(G, v1);
        j = LocateVex(G, v2);
        G.arcs[i][j].adj = w;
        G.arcs[j][i] = G.arcs[i][j];
    }
    return 1;
}


void PrintMGraph(MGraph &G)
{
    int i, j;
    printf("邻接矩阵为:\n");
    for (i = 0;i<G.vexnum;++i)
    {
        for (j = 0;j<G.vexnum;++j)
            printf("%5d", G.arcs[i][j]);
        printf("\n");
    }
}

Status CreateDG1(ALGraph &M){
    int i,s,d,k;
    printf("构建有向图邻接表,请输入顶点的数目和边的数目:\n");
    scanf("%d%d",&M.vexnum,&M.arcnum);
    printf("请依次输入对应的顶点的值:\n");
    for( i=0;i<M.vexnum;i++ ) {
        getchar();
    scanf("%c",&M.vertices[i].data);
    M.vertices[i].firstarc=NULL;
  }
  for( k = 0; k < M.arcnum; k++ ) {
  printf("请输入第%d条边的两个顶点的序号:\n",k+1);
  scanf("%d%d",&s,&d);
  ArcNode *p;
  p=(ArcNode*)malloc(sizeof(ArcNode));
  p->adjvex=d;
  p->nextarc=M.vertices[s].firstarc;M.vertices[s].firstarc=p;
}
}




Status CreateDN1(ALGraph &M){
    int i,s,d,k,*w;
    printf("构建有向网邻接表,请输入顶点的数目和边的数目:\n");
    scanf("%d%d",&M.vexnum,&M.arcnum);
    printf("请依次输入对应的顶点的值:\n");
    for( i = 0; i < M.vexnum; i++ ) {
        getchar();
    scanf("%c",&M.vertices[i].data);
    M.vertices[i].firstarc=NULL;
  }
  for( k = 0; k < M.arcnum; k++ ) {
  printf("请输入第%d条边的两个顶点的序号:\n",k+1);
  scanf("%d%d",&s,&d);
  ArcNode *p;
  p=(ArcNode*)malloc(sizeof(ArcNode));
  p->adjvex=d;
  printf("请输入该边权值\n");
  scanf("%d",&w);
  p->info=w;p->nextarc=M.vertices[s].firstarc;M.vertices[s].firstarc=p;
}
}



Status CreateUDG1(ALGraph &M){
    int i,s,d,k;
    printf("构建无向图邻接表,请输入顶点的数目和边的数目:\n");
    scanf("%d%d",&M.vexnum,&M.arcnum);
    printf("请依次输入对应的顶点的值:\n");
    for( i = 0; i < M.vexnum; i++ ) {
        getchar();
    scanf("%c",&M.vertices[i].data);
    M.vertices[i].firstarc=NULL;
  }
  for( k = 0; k < M.arcnum; k++ ) {
  printf("请输入第%d条边的两个顶点的序号:\n",k+1);
  scanf("%d%d",&s,&d);
  ArcNode *p,*q;
  p=(ArcNode*)malloc(sizeof(ArcNode));
  q=(ArcNode*)malloc(sizeof(ArcNode));
  p->adjvex=d;q->adjvex=s;
  p->nextarc=M.vertices[s].firstarc;M.vertices[s].firstarc=p;
  q->nextarc=M.vertices[d].firstarc;M.vertices[d].firstarc=q;
}
}


Status CreateUDN1(ALGraph &M){
    int i,s,d,k,*w;
    printf("构建无向网邻接表,请输入顶点的数目和边的数目:\n");
    scanf("%d%d",&M.vexnum,&M.arcnum);
    printf("请依次输入对应的顶点的值:\n");
    for( i = 0; i < M.vexnum; i++ ) {
        getchar();
    scanf("%c",&M.vertices[i].data);
    M.vertices[i].firstarc=NULL;
  }
  for( k = 0; k < M.arcnum; k++ ) {
  printf("请输入第%d条边的两个顶点的序号:\n",k+1);
  scanf("%d%d",&s,&d);
  ArcNode *p,*q;
  p=(ArcNode*)malloc(sizeof(ArcNode));
  q=(ArcNode*)malloc(sizeof(ArcNode));
  p->adjvex=d;q->adjvex=s;
  printf("请输入该边权值\n");
  scanf("%d",&w);
  p->info=w;p->nextarc=M.vertices[s].firstarc;M.vertices[s].firstarc=p;
  q->info=w;q->nextarc=M.vertices[d].firstarc;M.vertices[d].firstarc=q;
}
}


void PrintALGraph(ALGraph &G){
     int q;
        ArcNode *e;
        printf("所构建的邻接表如下所示\n");
        for(q=0;q<G.vexnum;++q){
            printf("%c->",G.vertices[q].data);
            e=G.vertices[q].firstarc;
        while(e){
            printf("%d->",e->adjvex);
            e=e->nextarc;
        }
        printf("\n");
    }

       }
      


bool visited[MAX_VERTEX_NUM+1];   
int (*VisitFunc)(MGraph &G,int v);   
int FirstAdjVex(MGraph &G, VertexType v)  
{   
    int m = LocateVex(G,v);  
    if(m== -1)  
        return -1;  
    int adjValue = 0;   
    if(G.kind == DN || G.kind == UDN)  
        adjValue = INFINITY;
    for(int i=0; i < G.vexnum; ++i)  
    {  
        if(G.arcs[m][i].adj != adjValue)  
            return i;  
    }  
    return -1;  
}  
  
int NextAdjVex(MGraph &G, VertexType v, VertexType w)  
{  
   
    int m=LocateVex(G,v);  
    int n=LocateVex(G,w);
   
    if(m == -1 || n== -1)  
    {  
        return -1;  
    }  
    int adjValue = 0;   
    if(G.kind == DN || G.kind == UDN)  
        adjValue = INFINITY;  
    for(int i=n+1; i < G.vexnum; ++i)  
    {  
        if(G.arcs[m][i].adj != adjValue)  
        {
            return i;
       }
    }
    return -1;   
}

VertexType GetVex(MGraph &G, int v)  
{  
   
    if(v >= G.vexnum || v < 0)
        return 0;
    return G.vexs[v];
}

void DFS(MGraph &G, int v)  
{
    visited[v] = true;
    VisitFunc(G,v);
    for(int w = FirstAdjVex(G,GetVex(G,v)); w >= 0; w = NextAdjVex(G,GetVex(G,v),GetVex(G,w)))
        if(!visited[w])
            DFS(G,w);
}

void DFSTraverse(MGraph &G, int(*Visit)(MGraph &G,int v))
{  
    int v=0;char y='\0';
    printf ("\n请输入遍历首顶点:");
    getchar ();
    scanf ("%c",&y);
    printf ("深度优先搜索遍历的结果为:");
    VisitFunc=Visit;
    for ( ;v<G.vexnum;v++) visited[v]=0;
        v=LocateVex (G,y);
        DFS (G,v);
    for (v=0;v<G.vexnum;v++)
        if (!visited[v])
        DFS (G,v);
}

int VisitVex(MGraph &G,int v)  
{
    printf("%3c",G.vexs[v]);
    return OK;
}

typedef struct{
        char adjvex;
        int lowcost;
    }CLOSEDGE,closedge[MAX_VERTEX_NUM];

int minimum(closedge closedge,MGraph G)
{
    int min=INFINITY,k=0,i;
    for(i=0;i<G.vexnum;i++)
    {
        if(closedge[i].lowcost != 0 && closedge[i].lowcost < min)
        {
            min = closedge[i].lowcost;
            k = i;
        }
    }
    return k;
}
int MiniSpanTree_PRIM(MGraph G,char 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("(%c,%c) ",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()
{   
    MGraph G;
    ALGraph M;
    int a,k;
    char u;
  for(k=1;;k++)
    {
     printf("1.创建邻接矩阵 2.创建邻接表 3.DFS遍历 4.邻接矩阵构建最小生成树(先构建无向网)\n");
     printf("请输入您的选择:\n");
     scanf("%d",&a);
     getchar();
     switch(a)
          {
        case 1:
             CreateGraph(G);
             PrintMGraph(G);
             break;
        case 2:
             CreateGraph1(M);
             PrintALGraph(M);
             break;
        case 3:
             DFSTraverse(G,VisitVex);
             printf("\n");
             break;
        case 4:
            printf("请输入最小生成树的起点:");
            scanf("%c",&u);
        MiniSpanTree_PRIM(G,u);
        printf("\n");break;
        default:exit(0); break;
        }
    }
    return 0;
}
2016-06-17 19:16
快速回复:利用普利姆算法构建最小生成树(顶点为数值),不知道为什么不能成功( ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051194 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved