Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1559231
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2016-11-02 20:35:53


  1. //
  2. // Created by Cute on 16-10-30.
  3. // Copyright (c) 2016年 Cute. All rights reserved.
  4. //

  5. #include <stdlib.h>
  6. #include <stdio.h>

  7. #define MAX_VEXTEX_NUM 9 /* 顶点数 */
  8. #define ARC_NUM 12 /* 边数 */
  9. #define MAX_QUEUEMEM (MAX_VEXTEX_NUM+1)

  10. /* 顶点连接信息 */
  11. int GraphEdge[ARC_NUM * 2][2] = {{0,1},{1,0},{1,2},{2,1},{2,3},{3,2},{3,4},{4,3},
  12.                                  {4,5},{5,4},{5,0},{0,5},{0,6},{6,0},{6,8},{8,6},
  13.                                  {6,7},{7,6},{7,8},{8,7},{7,3},{3,7},{8,4},{4,8}};
  14. /*定义数组visited用来标示一个顶点是否被访问过*/
  15. int visited[MAX_VEXTEX_NUM]={0,0,0,0,0,0,0,0,0};

  16. /*定义除了头节点外的其他结点 */
  17. typedef struct Node {
  18.     int adjvex; /* 邻接的顶点 */
  19.     struct Node *nav; /* next adjacent vex 其他邻接点 */
  20. } Node;

  21. /* 该结构只是为了定义头结点,单独定义头结点处理可能方便点 */
  22. typedef struct VNode {
  23.     int head; /* 顶点信息 */
  24.     struct Node *fav; /* first adjacent vex第一个邻接点 */
  25. } VNode;


  26. /* 定义图的结构 */
  27. typedef struct {
  28.     VNode vex[MAX_VEXTEX_NUM];/* Linklist head array */
  29.     int vnum; /* vex number */
  30.     int anum; /* arc number */
  31. } ALGraph;

  32. void CreateGraph(ALGraph * alGraph)
  33. {
  34.     int i,j;
  35.     Node *newNd;
  36.     Node *vexNd;
  37.     
  38.     alGraph->vnum = MAX_VEXTEX_NUM;
  39.     alGraph->anum = ARC_NUM;
  40.     
  41.     /* 初始化表头 */
  42.     for(i=0;i<MAX_VEXTEX_NUM;i++)
  43.     {
  44.         alGraph->vex[i].head = i;
  45.         alGraph->vex[i].fav = NULL;
  46.     }
  47.     
  48.     for(j=0;j<2*ARC_NUM;j++)
  49.     {
  50.         i = GraphEdge[j][0];//起始
  51.         
  52.         if(alGraph->vex[i].fav==NULL)
  53.         {
  54.             newNd = ( Node * ) malloc (sizeof(Node));
  55.             newNd->adjvex = GraphEdge[j][1];
  56.             newNd->nav = NULL;
  57.             alGraph->vex[i].fav = newNd;
  58.         }
  59.         else
  60.         {
  61.             vexNd = alGraph->vex[i].fav;
  62.             while(vexNd->nav != NULL)
  63.             {
  64.                 vexNd = vexNd->nav;
  65.             }
  66.             newNd = ( Node * ) malloc (sizeof(Node));
  67.             newNd->adjvex = GraphEdge[j][1];
  68.             newNd->nav = NULL;
  69.             vexNd->nav = newNd;
  70.         }
  71.     }
  72. }

  73. void OutputGraph(ALGraph * alGraph)
  74. {
  75.     int i;
  76.     Node * vexNd;
  77.     
  78.     printf("The graph dedicated by adjacency list is:\n");
  79.     
  80.     for(i=0;i<MAX_VEXTEX_NUM;i++)
  81.     {
  82.         printf("the header is: [%d] -> ",alGraph->vex[i].head);
  83.         vexNd = alGraph->vex[i].fav;
  84.         
  85.         while(vexNd != NULL)
  86.         {
  87.             printf("[%d] -> ",vexNd->adjvex);
  88.             vexNd=vexNd->nav;
  89.         }
  90.         
  91.         printf("[END]\n");
  92.     }
  93. }

  94. void DFS(ALGraph * alGraph,int v)
  95. {
  96.     int w;
  97.     Node * vexNd;
  98.     
  99.     visited[v] = 1;
  100.     printf("[%d] -> ",v);
  101.     
  102.     vexNd = alGraph->vex[v].fav;
  103.     
  104.     while(vexNd != NULL)
  105.     {
  106.         w = vexNd->adjvex;
  107.         
  108.         if(visited[w]==0)
  109.             DFS(alGraph,w);
  110.         
  111.         vexNd = vexNd->nav;
  112.     }
  113. }

  114. void DFSTraverse(ALGraph * alGraph)
  115. {
  116.     int i;
  117.     
  118.     /*访问标志数组初始化*/
  119.     for(i=0;i<MAX_VEXTEX_NUM;i++)
  120.     {
  121.         visited[i] = 0;
  122.     }
  123.     
  124.     printf("\n");
  125.     
  126.     puts("********************************************");
  127.     puts("* the function DFSTraverse will traverse *");
  128.     puts("* the graphby Depth First Search *");
  129.     puts("********************************************");
  130.     puts("the result of DFS is:");
  131.     
  132.     for(i=0;i<MAX_VEXTEX_NUM;i++)
  133.     {
  134.         if(visited[i] == 0)
  135.             DFS(alGraph,i);
  136.     }
  137.     
  138.     printf("[end]\n");
  139. }

  140. typedef struct {
  141.     int queuemem[MAX_QUEUEMEM];
  142.     int header;
  143.     int rear;
  144. } QUEUE;

  145. void InitQueue(QUEUE *queue)
  146. {
  147.     queue->header = 0;
  148.     queue->rear = 0;
  149. }

  150. void EnQueue(QUEUE *queue,int v)
  151. {
  152.     queue->queuemem[queue->rear] = v;
  153.     queue->rear++;
  154. }

  155. int DelQueue(QUEUE *queue)
  156. {
  157.     return queue->queuemem[queue->header++];
  158. }

  159. int EmptyQueue(QUEUE *queue)
  160. {
  161.     if(queue->header == queue->rear)
  162.         return 1;
  163.     
  164.     return 0;
  165. }

  166. void BFSTraverse(ALGraph * alGraph)
  167. {
  168.     int i;
  169.     int w;
  170.     
  171.     Node * vexNd;
  172.     QUEUE queue;
  173.     InitQueue(&queue);
  174.     
  175.     /*访问标志数组初始化*/
  176.     for(i=0;i<MAX_VEXTEX_NUM;i++)
  177.     {
  178.         visited[i] = 0;
  179.     }
  180.     
  181.     printf("\n");
  182.     puts("********************************************");
  183.     puts("* the function BFSTraverse will traverse *");
  184.     puts("* the graph by Breadth First Search *");
  185.     puts("********************************************");
  186.     puts("the result of BFS is:");
  187.     
  188.     for(i=0;i<MAX_VEXTEX_NUM;i++)
  189.     {
  190.         if(visited[i] == 0)
  191.         {
  192.             visited[i] = 1;
  193.             printf("[%d] -> ",i);
  194.             EnQueue(&queue,i);
  195.             
  196.             while(!EmptyQueue(&queue))
  197.             {
  198.                 w = DelQueue(&queue);
  199.                 vexNd = alGraph->vex[w].fav;
  200.                 
  201.                 while(vexNd != NULL)
  202.                 {
  203.                     w = vexNd->adjvex;
  204.                     if(visited[w]==0)
  205.                     {
  206.                         visited[w] = 1;
  207.                         printf("[%d] -> ",w);
  208.                         EnQueue(&queue,w);
  209.                     }
  210.                     vexNd = vexNd->nav;
  211.                 }
  212.             }
  213.         }
  214.     }
  215.     
  216.     printf("[end]\n");
  217. }

  218. int main(int argc, const char * argv[])
  219. {
  220.     ALGraph alGraph;
  221.     
  222.     CreateGraph(&alGraph);
  223.     
  224.     OutputGraph(&alGraph);
  225.     
  226.     DFSTraverse(&alGraph);
  227.     
  228.     BFSTraverse(&alGraph);
  229.     
  230.     return 0;
  231. }

阅读(2239) | 评论(0) | 转发(0) |
0

上一篇:循环链表

下一篇:[20161116 ADV] Line Stone

给主人留下些什么吧!~~