第12周项目1-图基本算法库

问题:

[cpp]  view plain  copy
  1. /*  
  2. * Copyright (c)2016,烟台大学计算机与控制工程学院  
  3. * All rights reserved.  
  4. * 文件名称:项目1.cpp  
  5. * 作    者:程德泉
  6. * 完成日期:2016年11月16日  
  7. * 版 本 号:v1.0  
  8.   
  9. * 问题描述:  定义图的邻接矩阵和邻接表存储结构,实现其基本运算,并完成测试。  
  10.  
  11.  
  12. 要求:  
  13. 1、头文件graph.h中定义相关的数据结构并声明用于完成基本运算的函数。对应基本运算的函数包括: 
  14.  
  15.     void ArrayToMat(int *Arr, int n, MGraph &g); //用普通数组构造图的邻接矩阵 
  16.     void ArrayToList(int *Arr, int n, ALGraph *&); //用普通数组构造图的邻接表 
  17.     void MatToList(MGraph g,ALGraph *&G);//将邻接矩阵g转换成邻接表G 
  18.     void ListToMat(ALGraph *G,MGraph &g);//将邻接表G转换成邻接矩阵g 
  19.     void DispMat(MGraph g);//输出邻接矩阵g 
  20.     void DispAdj(ALGraph *G);//输出邻接表G 
  21.      
  22. 2、在graph.cpp中实现这些函数  
  23.  
  24. 3、用main.cpp中的main函数中完成测试。  
  25.   
  26. * 输入描述: 无  
  27. * 程序输出: 测试数据  
  28. */     
   
  • graph.h头文件代码
[cpp]  view plain  copy
  1. #ifndef GRAPH_H_INCLUDED  
  2. #define GRAPH_H_INCLUDED  
  3.   
  4. #include <stdio.h>  
  5. #include <malloc.h>  
  6. #define MAXV 100                //最大顶点个数  
  7. #define INF 32767       //INF表示∞  
  8. typedef int InfoType;  
  9.   
  10.   
  11. //以下定义邻接矩阵类型  
  12. typedef struct  
  13. {  
  14.     int no;                     //顶点编号  
  15.     InfoType info;              //顶点其他信息,在此存放带权图权值  
  16. } VertexType;                   //顶点类型  
  17.   
  18.   
  19. typedef struct                  //图的定义  
  20. {  
  21.     int edges[MAXV][MAXV];      //邻接矩阵  
  22.     int n,e;                    //顶点数,弧数  
  23.     VertexType vexs[MAXV];      //存放顶点信息  
  24. } MGraph;                       //图的邻接矩阵类型  
  25.   
  26.   
  27. //以下定义邻接表类型  
  28. typedef struct ANode            //弧的结点结构类型  
  29. {  
  30.     int adjvex;                 //该弧的终点位置  
  31.     struct ANode *nextarc;      //指向下一条弧的指针  
  32.     InfoType info;              //该弧的相关信息,这里用于存放权值  
  33. } ArcNode;  
  34.   
  35.   
  36. typedef int Vertex;  
  37.   
  38.   
  39. typedef struct Vnode            //邻接表头结点的类型  
  40. {  
  41.     Vertex data;                //顶点信息  
  42.     int count;                  //存放顶点入度,只在拓扑排序中用  
  43.     ArcNode *firstarc;          //指向第一条弧  
  44. } VNode;  
  45.   
  46.   
  47. typedef VNode AdjList[MAXV];    //AdjList是邻接表类型  
  48.   
  49.   
  50. typedef struct  
  51. {  
  52.     AdjList adjlist;            //邻接表  
  53.     int n,e;                    //图中顶点数n和边数e  
  54. } ALGraph;                      //图的邻接表类型  
  55.   
  56.   
  57. //功能:由一个反映图中顶点邻接关系的二维数组,构造出用邻接矩阵存储的图  
  58. //参数:Arr - 数组名,由于形式参数为二维数组时必须给出每行的元素个数,在此将参数Arr声明为一维数组名(指向int的指针)  
  59. //      n - 矩阵的阶数  
  60. //      g - 要构造出来的邻接矩阵数据结构  
  61. void ArrayToMat(int *Arr, int n, MGraph &g); //用普通数组构造图的邻接矩阵  
  62. void ArrayToList(int *Arr, int n, ALGraph *&); //用普通数组构造图的邻接表  
  63. void MatToList(MGraph g,ALGraph *&G);//将邻接矩阵g转换成邻接表G  
  64. void ListToMat(ALGraph *G,MGraph &g);//将邻接表G转换成邻接矩阵g  
  65. void DispMat(MGraph g);//输出邻接矩阵g  
  66. void DispAdj(ALGraph *G);//输出邻接表G  
  67.   
  68.   
  69. #endif // GRAPH_H_INCLUDED  
 
graph.h是图的一个算法库集合,里面声明了常用到的各个功能函数。
  • graph.cpp文件代码
[cpp]  view plain  copy
  1. //图基本运算函数  
  2. #include "graph.h"  
  3.   
  4.   
  5. //功能:由一个反映图中顶点邻接关系的二维数组,构造出用邻接矩阵存储的图  
  6. //参数:Arr - 数组名,由于形式参数为二维数组时必须给出每行的元素个数,在此将参数Arr声明为一维数组名(指向int的指针)  
  7. //      n - 矩阵的阶数  
  8. //      g - 要构造出来的邻接矩阵数据结构  
  9. void ArrayToMat(int *Arr, int n, MGraph &g)  
  10. {  
  11.     int i,j,count=0;  //count用于统计边数,即矩阵中非0元素个数  
  12.     g.n=n;  
  13.     for (i=0; i<g.n; i++)  
  14.         for (j=0; j<g.n; j++)  
  15.         {  
  16.             g.edges[i][j]=Arr[i*n+j]; //将Arr看作n×n的二维数组,Arr[i*n+j]即是Arr[i][j],计算存储位置的功夫在此应用  
  17.             if(g.edges[i][j]!=0)  
  18.                 count++;  
  19.         }  
  20.     g.e=count;  
  21. }  
  22.   
  23.   
  24. void ArrayToList(int *Arr, int n, ALGraph *&G)  
  25. {  
  26.     int i,j,count=0;  //count用于统计边数,即矩阵中非0元素个数  
  27.     ArcNode *p;  
  28.     G=(ALGraph *)malloc(sizeof(ALGraph));  
  29.     G->n=n;  
  30.     for (i=0; i<n; i++)                 //给邻接表中所有头节点的指针域置初值  
  31.         G->adjlist[i].firstarc=NULL;  
  32.     for (i=0; i<n; i++)                 //检查邻接矩阵中每个元素  
  33.         for (j=n-1; j>=0; j--)  
  34.             if (Arr[i*n+j]!=0)      //存在一条边,将Arr看作n×n的二维数组,Arr[i*n+j]即是Arr[i][j]  
  35.             {  
  36.                 p=(ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p  
  37.                 p->adjvex=j;  
  38.                 p->info=Arr[i*n+j];  
  39.                 p->nextarc=G->adjlist[i].firstarc;      //采用头插法插入*p  
  40.                 G->adjlist[i].firstarc=p;  
  41.             }  
  42.   
  43.   
  44.     G->e=count;  
  45. }  
  46.   
  47.   
  48. void MatToList(MGraph g, ALGraph *&G)  
  49. //将邻接矩阵g转换成邻接表G  
  50. {  
  51.     int i,j;  
  52.     ArcNode *p;  
  53.     G=(ALGraph *)malloc(sizeof(ALGraph));  
  54.     for (i=0; i<g.n; i++)                   //给邻接表中所有头节点的指针域置初值  
  55.         G->adjlist[i].firstarc=NULL;  
  56.     for (i=0; i<g.n; i++)                   //检查邻接矩阵中每个元素  
  57.         for (j=g.n-1; j>=0; j--)  
  58.             if (g.edges[i][j]!=0)       //存在一条边  
  59.             {  
  60.                 p=(ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p  
  61.                 p->adjvex=j;  
  62.                 p->info=g.edges[i][j];  
  63.                 p->nextarc=G->adjlist[i].firstarc;      //采用头插法插入*p  
  64.                 G->adjlist[i].firstarc=p;  
  65.             }  
  66.     G->n=g.n;  
  67.     G->e=g.e;  
  68. }  
  69.   
  70.   
  71. void ListToMat(ALGraph *G,MGraph &g)  
  72. //将邻接表G转换成邻接矩阵g  
  73. {  
  74.     int i,j;  
  75.     ArcNode *p;  
  76.     for (i=0; i<g.n; i++)   //先初始化邻接矩阵  
  77.         for (j=0; j<g.n; j++)  
  78.             g.edges[i][j]=0;  
  79.     for (i=0; i<G->n; i++)  //根据邻接表,为邻接矩阵赋值  
  80.     {  
  81.         p=G->adjlist[i].firstarc;  
  82.         while (p!=NULL)  
  83.         {  
  84.             g.edges[i][p->adjvex]=p->info;  
  85.             p=p->nextarc;  
  86.         }  
  87.     }  
  88.     g.n=G->n;  
  89.     g.e=G->e;  
  90. }  
  91.   
  92.   
  93. void DispMat(MGraph g)  
  94. //输出邻接矩阵g  
  95. {  
  96.     int i,j;  
  97.     for (i=0; i<g.n; i++)  
  98.     {  
  99.         for (j=0; j<g.n; j++)  
  100.             if (g.edges[i][j]==INF)  
  101.                 printf("%3s","∞");  
  102.             else  
  103.                 printf("%3d",g.edges[i][j]);  
  104.         printf("\n");  
  105.     }  
  106. }  
  107.   
  108.   
  109. void DispAdj(ALGraph *G)  
  110. //输出邻接表G  
  111. {  
  112.     int i;  
  113.     ArcNode *p;  
  114.     for (i=0; i<G->n; i++)  
  115.     {  
  116.         p=G->adjlist[i].firstarc;  
  117.         printf("%3d: ",i);  
  118.         while (p!=NULL)  
  119.         {  
  120.             printf("-->%d/%d ",p->adjvex,p->info);  
  121.             p=p->nextarc;  
  122.         }  
  123.         printf("\n");  
  124.     }  
  125. }  
graph.cpp对应 graph.h中声明的各个功能函数,给出了各个功能函数的实现方法。
  • main.cpp文件代码
[cpp]  view plain  copy
  1. #include "graph.h"  
  2.   
  3. int main()  
  4. {  
  5.     MGraph g1,g2;  
  6.     ALGraph *G1,*G2;  
  7.     int A[6][6]=  
  8.     {  
  9.         {0,5,0,7,0,0},  
  10.         {0,0,4,0,0,0},  
  11.         {8,0,0,0,0,9},  
  12.         {0,0,5,0,0,6},  
  13.         {0,0,0,5,0,0},  
  14.         {3,0,0,0,1,0}  
  15.     };  
  16.   
  17.   
  18.     ArrayToMat(A[0], 6, g1);  //取二维数组的起始地址作实参,用A[0],因其实质为一维数组地址,与形参匹配  
  19.     printf(" 有向图g1的邻接矩阵:\n");  
  20.     DispMat(g1);  
  21.   
  22.   
  23.     ArrayToList(A[0], 6, G1);  
  24.     printf(" 有向图G1的邻接表:\n");  
  25.     DispAdj(G1);  
  26.   
  27.   
  28.     MatToList(g1,G2);  
  29.     printf(" 图g1的邻接矩阵转换成邻接表G2:\n");  
  30.     DispAdj(G2);  
  31.   
  32.   
  33.     ListToMat(G1,g2);  
  34.     printf(" 图G1的邻接表转换成邻接邻阵g2:\n");  
  35.     DispMat(g2);  
  36.     printf("\n");  
  37.     return 0;  
  38. }  
main.cpp中根据需要添加各个函数,以便实现相应功能。

运行结果:

知识点总结:
定义图的算法库。
学习心得:
图在生活中很普遍,算法库也多种多样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值