最小生成树

prim算法

 

  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. #define OK 1
  5. #define OVERFLOW -2
  6. #define MAX 1000
  7. typedef int Status;
  8. typedef char VertexType;
  9. typedef int* Inpoint;
  10. typedef int VRType;
  11. typedef enum{DG,DN,UDG,UDM} GraphKind;
  12. typedef struct {
  13.         VertexType *vexs;
  14.         int **arcs;
  15.         int vexnum,arcnum;
  16.         GraphKind kind;
  17.         }MGraph;
  18.         
  19. int LocateVex(MGraph G,VertexType v){
  20.     for(int i=0;i<G.vexnum;i++)
  21.         if(v==G.vexs[i]) return i;
  22.     return G.vexnum;}
  23. Status  CreateUDM(MGraph &G){
  24.         G.kind=UDM;
  25.         cin>>G.vexnum>>G.arcnum;
  26.         G.vexs=new VertexType[G.vexnum];
  27.     G.arcs=new Inpoint[G.vexnum];
  28.     if(!G.vexs||!G.arcs)    exit(OVERFLOW);
  29.     int i,j,k;
  30.     for(i=0;i<G.vexnum;i++){
  31.         if(!(G.arcs[i]=new int[G.vexnum]))  exit(OVERFLOW);
  32.         for(j=0;j<G.vexnum;j++)
  33.             if(i!=j) G.arcs[i][j]=MAX;
  34.             else     G.arcs[i][j]=0;}
  35.     VertexType v1,v2;
  36.         for(i=0;i<G.vexnum;i++)
  37.                 cin>>G.vexs[i];
  38.         for(k=0;k<G.arcnum;k++){
  39.                 do{
  40.                       cin>>v1>>v2;
  41.                       i=LocateVex(G,v1);
  42.                       j=LocateVex(G,v2);
  43.                       cin>>G.arcs[i][j];
  44.                       G.arcs[j][i]=G.arcs[i][j];
  45.                       }while(i==G.vexnum||j==G.vexnum);
  46.         }
  47.     return OK;
  48. }
  49. void MiniSpanTree(MGraph G){
  50.    typedef struct{
  51.     VertexType adjvex;
  52.     VRType lowcost;}Node,*cclosedge;
  53.    cclosedge closedge;
  54.    if(!(closedge=new Node[G.vexnum])) exit(OVERFLOW);
  55.    int i,j,k,min;
  56.    for(i=0;i<G.vexnum;i++) {                  
  57.        closedge[i].lowcost=G.arcs[0][i];
  58.        closedge[i].adjvex=G.vexs[0];} 
  59.     for(i=1;i<G.vexnum;i++){ 
  60.             min=MAX;
  61.         j=1;
  62.         while(j<G.vexnum) {
  63.           if(closedge[j].lowcost&&closedge[j].lowcost<min){
  64.             min=closedge[j].lowcost;
  65.             k=j;}
  66.           j++;}
  67.         cout<<closedge[k].adjvex<<"/t"<<G.vexs[k]<<"/t"<<min<<endl;;
  68.         closedge[k].lowcost=0;
  69.         for(j=1;j<G.vexnum;j++)                  
  70.            if(G.arcs[k][j]<closedge[j].lowcost) {
  71.               closedge[j].lowcost=G.arcs[k][j];
  72.               closedge[j].adjvex=G.vexs[k];}
  73.         }
  74. }
  75. int main(){
  76.     MGraph G;
  77.     CreateUDM(G);
  78.     MiniSpanTree(G);
  79.         return 0;}
  80. 输入:
  81. 6 10
  82. a b c d e f
  83. a b 6
  84. a d 5
  85. a c 1
  86. b c 5
  87. c d 5
  88. b e 3
  89. c e 6
  90. c f 4
  91. d f 2
  92. e f 6
  93. 输出:
  94. a       c       1
  95. c       f       4
  96. f       d       2
  97. c       b       5
  98. b       e       3
  99. 请按任意键继续. . .

kruskal算法

 

  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. #define MAX 1000
  5. #define OVERFLOW -2
  6. typedef char VertexType;
  7. typedef struct{
  8.        VertexType v1,v2;
  9.        int cost;}EdgeType,*PEdge;       
  10. typedef struct{
  11.         VertexType *vexs;     //顶点数组
  12.         PEdge edges;         //边数组
  13.         int vexnum,arcnum;}Graph;
  14. int Cmp(const void *a,const void *b){
  15.     return (*(PEdge)a).cost-(*(PEdge)b).cost;}
  16. void Create(Graph &G){
  17.      cin>>G.vexnum>>G.arcnum;
  18.      if(!(G.vexs = new VertexType[G.vexnum])||!(G.edges = new EdgeType[G.arcnum])) 
  19.           exit(OVERFLOW);
  20.      int i,j,k;
  21.      for(i=0;i<G.vexnum;i++)
  22.           cin>>G.vexs[i];
  23.      for(i=0;i<G.arcnum;i++)
  24.           cin>>G.edges[i].v1>>G.edges[i].v2>>G.edges[i].cost;
  25.      qsort(G.edges,G.arcnum,sizeof(EdgeType),Cmp); 
  26.       //对边数组按权值从小到大排序
  27. }
  28. int LocateVex(Graph G,VertexType v){ 
  29.     for(int i=0;i<G.vexnum;i++) 
  30.         if(v==G.vexs[i]) return i; 
  31.     return G.vexnum;} 
  32. int Find(Graph G,int *tree,VertexType v){//返回V的根节点
  33.     int t=LocateVex(G,v);
  34.     while(tree[t]>=0)
  35.          t=tree[t];
  36.     return t;}    
  37. void Kruskal(Graph &G){
  38.      int *tree;
  39.      if(!(tree=new int[G.vexnum])) exit(OVERFLOW);
  40.      int i,j=1,V1,V2;
  41.      for(i=0;i<G.vexnum;i++) tree[i]=-1;
  42.      i=0;
  43.      while(j<G.vexnum){
  44.          V1=Find(G,tree,G.edges[i].v1);
  45.          V2=Find(G,tree,G.edges[i].v2);
  46.          if(V1!=V2){
  47.                     tree[V2]=V1;   //V1作为V2的根节点
  48.                     j++;
  49.                     cout<<G.edges[i].v1<<"/t"<<G.edges[i].v2<<"/t"<<G.edges[i].cost<<endl;}
  50.          i++;}
  51.      }
  52. int main(){
  53.     Graph G;
  54.     Create(G);
  55.     Kruskal(G);
  56.     return 0;}
  57. 输入:
  58. 6 10
  59. a b c d e f
  60. a b 6
  61. a d 5
  62. a c 1
  63. b c 5
  64. c d 5
  65. b e 3
  66. c e 6
  67. c f 4
  68. d f 2
  69. e f 6
  70. 输出:
  71. a       c       1
  72. d       f       2
  73. b       e       3
  74. c       f       4
  75. b       c       5
  76. 请按任意键继续. . .
  77.     

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值