鱼C论坛

 找回密码
 立即注册
查看: 5731|回复: 4

[技术交流] 数据结构:图遍历的演示

[复制链接]
发表于 2015-12-13 12:09:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 SonOmiga 于 2015-12-16 19:11 编辑

这是犹豫老师布置的课题作业,
分为两个文件一个为c.cpp 一个为头文件类型  graph.h
c文件如下:
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>
  4. #include<windows.h>
  5. #include"graph.h"
  6. void main()
  7. {
  8.         int i,j,c,n,e,t,x;
  9.         MGraph g;                                //定义图g
  10.         ALGraph *G;                                //定义邻接表G
  11.         g.n=21;
  12.         g.e=21;
  13.         for(i=0;i<g.n;i++)
  14.                 for(j=0;j<g.n;j++)
  15.                         g.edges[i][j]=A[i][j];                                //将A[][]邻接矩阵转换成图g的边信息
  16.         printf("\n");
  17.         printf("\n");
  18.         printf("                               图遍历的演示:\n");
  19.         printf("                          正在从文件中读取 ");
  20.         for(i=0;i<3;i++)
  21.         {
  22.                 ::Sleep(600);
  23.                 printf(".");
  24.         }
  25.         printf("\n");
  26.         printf("                                请稍候!\n");
  27.                 for(i=0;i<3;i++)
  28.         {
  29.                 ::Sleep(600);
  30.         }
  31.         printf("                          从文件读取信息完毕!\n");
  32.         printf("\n");
  33.         MatToList(g,G);                                          //将邻接矩阵转换成邻接表
  34.         {
  35.         FILE *fp;
  36.         char str[200];
  37.         if((fp=fopen("D:\\C.txt","rt"))==NULL)   //打开C文件(C中存放图的信息)
  38.         {
  39.                 printf("\n Cannot open file strike any key exit!");
  40.         }
  41.         while(fgets(str,200,fp)!=NULL)                   //从文件中读取字符串
  42.         {
  43.                 printf("%s",str);
  44.                 printf("\n");}

  45.         }
  46.         printf("\n");
  47.         printf("\n");        printf("你想通过哪个顶点遍历DFS算法?");
  48.                                scanf("%d",&t);
  49.         printf("从顶点%d开始实现DFS算法:\n",t);
  50.         DFS(G,t);
  51.         printf("\n");
  52.         system("pause");
  53.         printf("\n你想通过哪个顶点遍历BFS算法?");
  54.         scanf("%2d",&t);
  55.         printf("从顶点%d开始实现BFS算法:\n",t);
  56.         BFS(G,t);
  57.         system("pause");
  58. for(i=0;i<G->n;i++)                                 
  59.                 visited[i]=0;

  60.         printf("\n你想通过哪个顶点输出深度优先生成树?");
  61.         scanf("%2d",&t);
  62.         DFS1(G,t);
  63.         printf("\n");
  64.         system("pause");
  65.         printf("\n你想通过哪个顶点输出广度优先生成树?");
  66.         scanf("%2d",&t);
  67.         BFS1(G,t);
  68.         printf("\n");
  69.            for(i=0;i<G->n;i++)
  70.                 visited[i]=0;
  71. system("pause");
  72. printf("你想通过哪个顶点凹入表打印树?");
  73.                         scanf("%d",&t);
  74.         printf("从顶点%d开始实现深度优先生凹入表打印树:\n",t);
  75.         DFSTest(G,t);
  76. printf("\n");
  77.         system("pause");
  78. printf("\n你想通过哪个顶点凹入表打印树?");
  79.                         scanf("%2d",&t);
  80.         printf("从顶点%d开始实现广度优先生凹入表打印树:\n",t);
  81.         BFSTest(G,t);
  82.         system("pause");
  83.        
  84. }
复制代码

graph.h代码如下:
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #define MAXV 21
  4. int visited[MAXV];                                                        //定义全局数组 visited
  5. int A[MAXV][MAXV]=                                                // 输入A[ ][ ]的邻接矩阵

  6. {                                                                                       
  7. {0,1,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
  8.         {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  9.         {1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
  10.         {0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  11.         {0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  12.         {0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  13.         {1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  14.         {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  15.         {1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
  16.         {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
  17.         {0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0},
  18.         {0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0},
  19.         {0,0,0,0,0,0,0,0,1,0,0,1,0,1,0,0,0,0,0,0,0},
  20.         {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
  21.         {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
  22.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,0,0},
  23.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
  24.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0},
  25.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0},
  26.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
  27.         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0}
  28. };



  29. 结构体类型:

  30. typedef struct
  31. {
  32.         int no;
  33. }Vertex;                                                                        //顶点类型
  34. typedef struct
  35. {
  36.         int edges[MAXV][MAXV];                                //图中的边
  37.         int n,e;                                                                //图中顶点数n 和边数E
  38.         Vertex vexs[MAXV];
  39. }MGraph;                                                                //完整的图类型
  40. typedef struct Anode
  41. {
  42.         int adjvex;
  43.         struct Anode *nextarc;
  44. }ArcNode;                                                                //边节点类型
  45. typedef struct Vnode
  46. {
  47.         Vertex Vdata;                                                //顶点的数据信息
  48.         ArcNode *firstarc;                                        //指向第一条边对应的边节点
  49. }VNode;
  50. typedef VNode AdjList[MAXV];                        //定义一个邻接表类型的数组
  51. typedef struct
  52. {
  53.         AdjList adjlist;                                                //邻接表
  54.         int n,e;                                                                //图中顶点数n 和边数E
  55. }ALGraph;                                                                //完整的图邻接表类型



  56. 函数类型:
  57. void MatToList(MGraph g,ALGraph *&G)                 // 邻接矩阵转换邻接表
  58. {
  59.         int i,j;
  60.         ArcNode *p;
  61.         G=(ALGraph *)malloc(sizeof(ALGraph));
  62.         for(i=0;i<g.n;i++)
  63.                 G->adjlist[i].firstarc=NULL;
  64.         for(i=0;i<g.n;i++)
  65.                 for(j=g.n-1;j>=0;j--)       
  66.                         if(g.edges[i][j]!=0)
  67.                         {
  68.                                 p=(ArcNode *)malloc(sizeof (ArcNode));
  69.                                 p->adjvex =j;
  70.                                 p->nextarc=G->adjlist[i].firstarc ;
  71.                                 G->adjlist[i].firstarc =p;
  72.                         }
  73.         G->n=g.n;G->e=g.e;
  74. }



  75. void DispAdj(ALGraph *G)                                        //输出邻接表
  76. {
  77.         int i;
  78.         ArcNode *p;
  79.         for(i=0;i<G->n;i++)
  80.         {
  81.                 p=G->adjlist[i].firstarc ;
  82.                 printf("%3d->",i);
  83.                 while(p!=NULL)
  84.                 {
  85.                         printf("%3d",p->adjvex);
  86.                         p=p->nextarc ;
  87.                 }
  88.                 printf("\n");
  89.         }
  90. }

  91. void DFS2(ALGraph *G,int v)                                                        //非递归遍历BFS算法
  92. {
  93.         ArcNode *p;
  94.         ArcNode *St[MAXV];
  95.         int top=-1,w,i;
  96.         for(i=0;i<G->n;i++)
  97.                 visited[i]=0;
  98.         printf("%3d",v);
  99.         visited[v]=1;
  100.         top++;
  101.         St[top]=G->adjlist[v].firstarc;
  102.         while(top>-1)
  103.         {
  104.                 p=St[top];
  105.                 top--;
  106.                 while(p!=NULL)
  107.                 {
  108.                          w=p->adjvex;
  109.                          if(visited[w]==0)
  110.                          {
  111.                                  printf("%3d",w);
  112.                                  visited[w]=1;
  113.                                  top++;
  114.                                  St[top]=G->adjlist[w].firstarc;
  115.                                  
  116.                          }
  117.                          p=p->nextarc ;
  118.                          
  119.                 }
  120.         }
  121.         printf("\n");
  122. }






  123. void BFS(ALGraph *G,int v)                                        //遍历BFS算法
  124. {
  125.         ArcNode *p;
  126.         int queue[MAXV],front=0,rear=0;
  127.         int w,i;
  128.         for(i=0;i<G->n;i++)
  129.                 visited[i]=0;
  130.         printf("%2d",v);
  131.         visited[v]=1;
  132.         rear=(rear+1)%MAXV;
  133.         queue[rear]=v;
  134.         while(front!=rear)
  135.         {
  136.                 front=(front+1)%MAXV;
  137.                 w=queue[front];
  138.                 p=G->adjlist[w].firstarc;
  139.                 while(p!=NULL)
  140.                 {
  141.                         if(visited[p->adjvex]==0)
  142.                         {
  143.                                 printf("%2d ",p->adjvex);
  144.                                 visited[p->adjvex]=1;
  145.                                 rear=(rear+1)%MAXV;
  146.                                 queue[rear]=p->adjvex;
  147.                         }
  148.                         p=p->nextarc;
  149.                 }
  150.         }
  151.         printf("\n");
  152. }
  153. void BFS1(ALGraph *G,int v)                                         //深度优先生成树
  154. {
  155.         ArcNode *p;
  156.         int queue[MAXV],front=0,rear=0;
  157.         int w,i;
  158.         for(i=0;i<G->n;i++)
  159.                 visited[i]=0;
  160.         visited[v]=1;
  161.         rear=(rear+1)%MAXV;
  162.         queue[rear]=v;
  163.         while(front!=rear)
  164.         {
  165.                 front=(front+1)%MAXV;
  166.                 w=queue[front];
  167.                 p=G->adjlist[w].firstarc;
  168.                 while(p!=NULL)
  169.                 {
  170.                         if(visited[p->adjvex]==0)
  171.                         {
  172.                                 printf("<%d,%d>",w,p->adjvex);
  173.                                 visited[p->adjvex]=1;
  174.                                 rear=(rear+1)%MAXV;
  175.                                 queue[rear]=p->adjvex;
  176.                         }
  177.                         p=p->nextarc;
  178.                 }
  179.         }
  180.         printf("\n");
  181. }
  182. void DFS1(ALGraph *G,int v)                                         //广度优先生成树
  183. {
  184.         ArcNode *p;       
  185.         visited[v]=1;
  186.         p=G->adjlist[v].firstarc;
  187.         while(p!=NULL)
  188.         {
  189.                 if(visited[p->adjvex]==0)
  190.                 {
  191.                         printf("<%d,%d>",v,p->adjvex);
  192.                         DFS1(G,p->adjvex);
  193.                 }
  194.                 p=p->nextarc ;
  195.         }
  196. }




  197. int deep = 0;                                                                          //定义深度为0
  198. void DFSTest(ALGraph *G, int v)                                //凹入法输出广度优先生成树
  199. {
  200.     ArcNode *p;
  201.     visited[v] = 1;
  202.     int index;
  203.     deep++;
  204.     for ( index = 0; index < deep; ++index){
  205.         printf("***");
  206.     }
  207.     printf("%3d", v);
  208.     printf("\n");
  209.     p = G->adjlist[v].firstarc;
  210.     while (p != NULL)
  211.     {
  212.         if (visited[p->adjvex] == 0)
  213.             DFSTest(G, p->adjvex);
  214.         p = p->nextarc;
  215.     }
  216.     deep--;
  217. }

  218. void BFSTest(ALGraph *G, int v)                                //凹入法输出深度优先生成树
  219. {
  220.     int deepth=0;
  221.     int deepthTemp = 0;
  222.     ArcNode *p;
  223.     int queue[MAXV], front = 0, rear = 0;
  224.     int w, i;
  225.     for (i = 0; i<G->n; i++)
  226.         visited[i] = 0;
  227.     printf("***%3d", v);
  228.     printf("\n");
  229.     visited[v] = 1;
  230.     rear = (rear + 1) % MAXV;
  231.     queue[rear] = v;
  232.     deepthTemp = 0;
  233.     while (front != rear)
  234.     {
  235.         front = (front + 1) % MAXV;
  236.         if (front > deepthTemp){
  237.             deepthTemp = rear;
  238.             deepth++;
  239.         }
  240.         w = queue[front];
  241.         p = G->adjlist[w].firstarc;
  242.         while (p != NULL)
  243.         {
  244.             if (visited[p->adjvex] == 0)
  245.             {
  246.                 for (int index = 0; index < deepth; ++index){
  247.                     printf("******");
  248.                 }
  249.                 printf("%2d ", p->adjvex);
  250.                 printf("\n");
  251.                 visited[p->adjvex] = 1;
  252.                 rear = (rear + 1) % MAXV;
  253.                 queue[rear] = p->adjvex;
  254.             }
  255.             p = p->nextarc;
  256.         }
  257.     }
  258.     printf("\n");
  259. }
复制代码

以上是使用邻接矩阵转换成邻接表, 另外有手动输入的邻接表函数如下:
  1. 结构体类型:
  2. #include<stdio.h>
  3. #include<malloc.h>
  4. #define MAXV 6
  5. int visited[MAXV];
  6. typedef struct
  7. {
  8.         int no;
  9. }Vertex;                                                                //顶点信息
  10. typedef struct Anode
  11. {
  12.         int adjvex;
  13.         struct Anode *nextarc;
  14.         int info;

  15. }ArcNode;                                                        //边节点信息
  16. typedef struct Vnode
  17. {
  18.         int Vdata;                                                         //顶点的数据信息
  19.         ArcNode *firstarc;                                          //指向第一条边对应的边节点

  20. }VNode;

  21. typedef VNode AdjList[MAXV];                        //定义一个邻接表类型的数组
  22. typedef struct
  23. {
  24.         int n,e;
  25. }Graph;                                                                //图信息
  26. typedef struct
  27. {
  28.         AdjList adjlist;                                                //邻接表
  29.         int n,e;                                                        //图中顶点数n 和边数E
  30. }ALGraph;                                                        //完整的图邻接表类型
  31.        



  32. 函数类型:


  33. void Creatgraph (VNode  adjlist[],Graph &graph )                        //手动生成邻接矩阵
  34. {
  35.         int i,j,n,e,begin,end;
  36.         ArcNode *p;    // 定义边节点p
  37.         printf("请输入顶点数n=");
  38.         scanf("%d",&n);
  39.         printf("请输入边数e=");
  40.         scanf("%d",&e);
  41.         graph.n=n;
  42.         graph.e=e;
  43.         for(i=0;i<n;i++)
  44.         {
  45.                 adjlist[i].Vdata=i;
  46.                 adjlist[i].firstarc=NULL;
  47.         }
  48.         for(j=0;j<2*e;j++)
  49.         {
  50.                 printf("\n请输入第%d条边的起始点的序号\n",j);
  51.                 printf("        起点编号:");
  52.                 scanf("%d",&begin);
  53.                 printf("        终点编号:");
  54.                 scanf("%d",&end);
  55.                 p=(ArcNode*)malloc(sizeof(ArcNode*));
  56.                 p->adjvex=end;
  57.                 p->nextarc=NULL;
  58.                 p->nextarc=adjlist[begin].firstarc;
  59.                 adjlist[begin].firstarc=p;
  60.                
  61.         }
  62. }











  63. void DispAdj(VNode adjlist[],Graph &graph)                                        //输出邻接表
  64. {
  65.         int i;
  66.         ArcNode *p;
  67.         for(i=0;i<graph.n;i++)
  68.         {
  69.                 p=adjlist[i].firstarc ;
  70.                 printf("   %d",adjlist[i].Vdata);
  71.                 while(p)
  72.                 {
  73.                         printf("->");
  74.                         printf("  %d",adjlist[p->adjvex].Vdata);
  75.                         p=p->nextarc ;
  76.                 }
  77.                 printf("\n");
  78.         }

  79. }
  80. void BFS(VNode  adjlist[],Graph &graph,int v)                                //广度优先遍历
  81. {
  82.         ArcNode *p;
  83.         int queue[MAXV],front=0,rear=0;
  84.         int w,i;

  85.         for(i=0;i<graph.n;i++)
  86.                 visited[i]=0;
  87.         printf("%2d",v);
  88.         visited[v]=1;
  89.         rear=(rear+1)%MAXV;
  90.         queue[rear]=v;
  91.         while(front!=rear)
  92.         {
  93.                 front=(front+1)%MAXV;
  94.                 w=queue[front];
  95.                 p=adjlist[w].firstarc;
  96.                 while(p!=NULL)
  97.                 {
  98.                         if(visited[p->adjvex]==0)
  99.                         {
  100.                                 printf("%2d",p->adjvex);
  101.                                 visited[p->adjvex]=1;
  102.                                 rear=(rear+1)%MAXV;
  103.                                 queue[rear]=p->adjvex;
  104.                         }
  105.                         p=p->nextarc;
  106.                 }
  107.         }
  108.         printf("\n");
  109. }



  110. void DFS(VNode  adjlist[],Graph &graph,int v)                                //非递归深度优先遍历
  111. {
  112.         ArcNode *p;
  113.         ArcNode *St[MAXV];
  114.         int top=-1,w,i;
  115.         for(i=0;i<graph.n;i++)
  116.                 visited[i]=0;
  117.         printf("%2d",v);
  118.         visited[v]=1;
  119.         top++;
  120.         St[top]=adjlist[v].firstarc;
  121.         while(top>-1)
  122.         {
  123.                 p=St[top];
  124.                 top--;
  125.                 while(p!=NULL)
  126.                 {
  127.                          w=p->adjvex;
  128.                          if(visited[w]==0)
  129.                          {
  130.                                  printf("%2d",w);
  131.                                  visited[w]=1;
  132.                                  top++;
  133.                                  St[top]=adjlist[w].firstarc;
  134.                                  
  135.                          }
  136.                          p=p->nextarc ;
  137.                          
  138.                 }
  139.         }
  140.         printf("\n");
  141. }


  142. 主函数:
  143. void main()
  144. {
  145.         int i,j,t,c,n,v,e;
  146.         VNode adjlist[MAXV];
  147.         Graph graph;
  148.         FILE *fp;
  149.         char str[200];
  150.         ArcNode *P;
  151.         ALGraph *G;
  152.         printf("\n");
  153.         printf("\n");
  154.         printf("                               图遍历的演示:\n");
  155.         printf("                          正在从文件中读取... ");
  156.         printf("\n");
  157.         printf("                                请稍候!\n");
  158.         printf("                          从文件读取信息完毕!\n");
  159.         if((fp=fopen("D:\\C.txt","rt"))==NULL)   //打开C文件(C中存放图的信息)
  160.         {
  161.                 printf("\n Cannot open file strike any key exit!");
  162.         }
  163.         while(fgets(str,200,fp)!=NULL)                   //从文件中读取字符串
  164.         {
  165.                 printf("%s",str);
  166.                 printf("\n");

  167.         }
  168.         printf("\n");
  169.         printf("无向图的邻接表:\n");
  170.         Creatgraph(adjlist,graph);
  171.         DispAdj(adjlist,graph);
  172.         printf("你想通过哪个顶点遍历DFS算法?");
  173.                         scanf("%d",&t);
  174.         DFS(adjlist,graph,t);
  175.                
  176.         printf("\n你想通过哪个顶点遍历BFS算法?");
  177.         scanf("%2d",&t);
  178.         BFS(adjlist,graph,t);
  179. }
复制代码

(手动输入小型矩阵必须把无向图顶点的边都输入进去 如 V1顶点连接V2顶点 那当你输入起始编号和终点编号的时候都得输入v1 v2
和v2 v1)
若有同学有疑问,可以联系我QQ77325313,我也是学习小组布置的任务,可以互相交流.





题目要求

题目要求

自己做的一个简单的图

自己做的一个简单的图

评分

参与人数 1鱼币 +5 收起 理由
零度非安全 + 5 支持楼主!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-14 00:32:25 | 显示全部楼层
亲,可以考虑用代码格式发布代码哦~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-14 12:50:15 | 显示全部楼层
~风介~ 发表于 2015-12-14 00:32
亲,可以考虑用代码格式发布代码哦~

怎么用啊

评分

参与人数 1鱼币 +5 收起 理由
零度非安全 + 5 热爱鱼C^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-12-14 17:32:54 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-18 14:04:52 | 显示全部楼层

评分

参与人数 1鱼币 +20 收起 理由
零度非安全 + 20 支持楼主!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 16:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表