Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2728558
  • 博文数量: 102
  • 博客积分: 1444
  • 博客等级: 中尉
  • 技术积分: 13891
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-29 10:58
文章分类

全部博文(102)

文章存档

2014年(29)

2013年(14)

2012年(59)

分类: C/C++

2012-04-11 22:23:54

转载请注明来源chengyaogen.blog.chinaunix.net
 
图的遍历是树的遍历的推广,是按照某种规则(或次序)访问图中各顶点依次且仅一次的操作,亦是将网络结构按某种规则线性化的过程。

 
由于图存在回路,为区别一顶点是否被访问过和避免顶点被多次访问,在遍历过程中,应记下每个访问过的顶点,即每个顶点对应有一个标志位,初始为False,一旦该顶点被访问,就将其置为True,以后若又碰到该顶点时,视其标志的状态,而决定是否对其访问。

对图的遍历通常有"深度优先搜索"和"广度优先搜索"方法,二者是人工智能的一个基础。

深度优先搜索(Depth First Search,简称DFS)

算法思路:

类似树的先根遍历。设初始化时,图中各顶点均未被访问,从图中某个顶点(设为V0)出发,访问V0,然后搜索V0的一个邻接点Vi,若Vi未被访问,则访问之,在 搜索Vi的一个邻接点(深度优先)...。若某顶点的邻接点全部访问完毕,则回溯(Backtracking)到它的上一顶点,然后再从此顶点又按深度优先的方法搜索下去,...,直到能访问的顶点都访问完毕为止。

设图G10如下图所示:
 
通过深度优先如下:
 
广度优先搜索(Breadth First Search),简称BFS

算法思路:

类似树的按层次遍历。初始时,图中各顶点均未被访问,从图中某顶点(V0)出发,访问V0,并依次访问V0的各邻接点(广度优先)。然后,分别从这些被访问过的顶点出发,扔仍按照广度优先的策略搜索其它顶点,....,直到能访问的顶点都访问完毕为止。

为控制广度优先的正确搜索,要用到队列技术,即访问完一个顶点后,让该顶点的序号进队。然后取相应队头(出队),考察访问过的顶点的各邻接点,将未访问过的邻接点访问 后再依次进队,...,直到队空为止。

通过广度优先如下:
 
下面看一下实现代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define MAX 20

  5. //访问记录
  6. int visit[MAX];

  7. //图的结构设计
  8. typedef struct
  9. {
  10.     int vex[MAX];//记录顶点
  11.     int adjmatrix[MAX][MAX];//邻接矩阵
  12.     int n;//顶点的个数
  13. }GRAPH;

  14. //初始化图
  15. int init_graph(GRAPH *pG)
  16. {

  17.     memset(pG,0,sizeof(GRAPH));
  18.     pG->n = -1;

  19.     printf("input vex\n");
  20.     while(scanf("%d",&pG->vex[++pG->n]));
  21.     while(getchar() != '\n');

  22. #ifndef _DEBUG_
  23.     int i = 0;

  24.     for(i = 0;i < pG->n ;i ++)
  25.     {
  26.         printf("V%d ",pG->vex[i]);
  27.     }

  28.     printf("\n");    
  29. #endif
  30.     
  31.     return 0;
  32. }

  33. //获取顶点的位置
  34. int locatevex(GRAPH *pG,int vex)
  35. {
  36.     int i = 0;

  37.     for(i = 0;i < pG->n;i ++)
  38.     {
  39.         if(pG->vex[i] == vex )
  40.             return i;
  41.     }
  42.     
  43.     return 0;
  44. }

  45. //输入图的顶点之间的边
  46. int input_edge(GRAPH *pG)
  47. {
  48.     int vex1,vex2;
  49.     int i,j;

  50.     printf("input edge(i,j):\n");
  51.     //任意字母键结束
  52.     while(scanf("(%d,%d)",&vex1,&vex2))
  53.     {
  54.         getchar();
  55.         i = locatevex(pG,vex1);
  56.         j = locatevex(pG,vex2);
  57.         pG->adjmatrix[i][j] = pG->adjmatrix[j][i] = 1;
  58.     }

  59. #ifndef _DEBUG_
  60.     int m,n;

  61.     for(m = 0;m < pG->n;m ++)
  62.     {
  63.         for(n = 0;n < pG->n; n ++)
  64.         {
  65.             printf("%d ",pG->adjmatrix[m][n]);
  66.         }

  67.         printf("\n");
  68.     }

  69. #endif

  70.     return 0;
  71. }

  72. //栈的设计
  73. typedef struct
  74. {
  75.     int buf[MAX];
  76.     int n;
  77. }Stack;

  78. //创建空栈
  79. Stack *create_empty_stack()
  80. {
  81.     Stack *stack;

  82.     stack = (Stack *)malloc(sizeof(Stack));
  83.     stack->n = -1;

  84.     return stack;
  85. }

  86. //出栈
  87. int pop_stack(Stack *stack)
  88. {
  89.     int temp;

  90.     temp = stack->buf[stack->n];
  91.     stack->n --;

  92.     return temp;
  93. }

  94. //入栈
  95. int push_stack(Stack *stack,int data)
  96. {
  97.     stack->n ++;
  98.     stack->buf[stack->n] = data;

  99.     return 0;
  100. }

  101. //判断空栈
  102. int is_empty_stack(Stack *stack)
  103. {
  104.     if(stack->n == -1)
  105.         return 1;
  106.     else
  107.         return 0;
  108. }

  109. int visit_all(GRAPH *pG)
  110. {
  111.     int i = 0;
  112.     
  113.     for(i = 0;i < pG->n; i ++)
  114.     {
  115.         if(visit[i] != 1)
  116.             break;
  117.     }

  118.     if(i == pG->n)
  119.         return 1;
  120.     else
  121.         return 0;
  122. }

  123. //图的深度非递归遍历
  124. int DFS(GRAPH *pG,int v)
  125. {
  126.     Stack *stack;
  127.     int i = 0;
  128.     
  129.     stack = create_empty_stack();
  130.     push_stack(stack,pG->vex[v]);
  131.     visit[v] = 1;
  132.     printf("V%d ",pG->vex[v]);
  133.     
  134.     while(!is_empty_stack(stack) || !visit_all(pG))
  135.     {
  136.         for(i = 0;i < pG->n;i ++)
  137.         {
  138.             if(visit[i] == 0 && pG->adjmatrix[v][i] == 1)
  139.                 break;
  140.         }

  141.         if(i == pG->n)
  142.         {
  143.             v = pop_stack(stack);
  144.             
  145.         }else{
  146.         
  147.             v = i;
  148.             push_stack(stack,pG->vex[v]);
  149.             visit[v] = 1;
  150.             printf("V%d ",pG->vex[v]);
  151.         }
  152.     }

  153.     printf("\n");

  154.     return 0;
  155. }

  156. //队列的设计
  157. typedef struct node
  158. {
  159.     int data;
  160.     struct node *next;
  161.     
  162. }ListNode;

  163. typedef struct
  164. {
  165.     ListNode *front;
  166.     ListNode *rear;
  167. }Queue;

  168. //创建空队列
  169. Queue *create_empty_queue()
  170. {
  171.     Queue *queue;
  172.     ListNode *head;

  173.     queue = (Queue *)malloc(sizeof(Queue));
  174.     head = (ListNode *)malloc(sizeof(ListNode));

  175.     queue->front = queue->rear = head;

  176.     return queue;
  177. }

  178. //判断队列是否为空
  179. int is_empty_queue(Queue *queue)
  180. {
  181.     if(queue->rear == queue->front)
  182.         return 1;
  183.     else
  184.         return 0;
  185. }

  186. //入队
  187. int EnterQueue(Queue *queue,int data)
  188. {
  189.     ListNode *temp;

  190.     temp = (ListNode *)malloc(sizeof(ListNode));
  191.     temp->data = data;
  192.     temp->next = NULL;

  193.     queue->rear->next = temp;
  194.     queue->rear = temp;

  195.     return 0;
  196. }

  197. //出队
  198. int DelQueue(Queue *queue)
  199. {
  200.     ListNode *temp;

  201.     temp = queue->front;
  202.     queue->front = queue->front->next;
  203.     free(temp);
  204.     temp = NULL;

  205.     return queue->front->data;
  206. }

  207. //图的广度遍历
  208. int BFS(GRAPH *pG,int v)
  209. {
  210.     Queue *queue = create_empty_queue();
  211.     int i = 0;
  212.     
  213.     memset(&visit,0,sizeof(visit));

  214.     EnterQueue(queue,v);
  215.     visit[v] = 1;

  216.     while(!is_empty_queue(queue))
  217.     {
  218.         v = DelQueue(queue);
  219.         printf("V%d ",pG->vex[v]);
  220.                 
  221.         
  222.         for(i = 0;i < pG->n;i ++)
  223.         {
  224.             if(visit[i] == 0 && pG->adjmatrix[v][i] == 1)
  225.             {
  226.                 EnterQueue(queue,i);
  227.                 visit[i] = 1;
  228.             }
  229.         }
  230.     }

  231.     printf("\n");

  232.     return 0;
  233. }

  234. int main()
  235. {
  236.     GRAPH G;
  237.     int n;

  238.     //输入顶点,初始化图
  239.     init_graph(&G);

  240.     //初始化邻接矩阵
  241.     input_edge(&G);

  242.     //图的深度遍历
  243.     DFS(&G, 0);

  244.     //图的广度遍历
  245.     BFS(&G,0);
  246.     
  247.     return 0;
  248. }
输出结果:
 
 
阅读(16320) | 评论(3) | 转发(13) |
给主人留下些什么吧!~~

草根老师2012-08-03 18:25:15

cu9330: 陈老师,我将你的文章链接到自己博客去了可以吗?
《详解数据结构中的哈夫曼与图》  http://zhongsiboke.blog.163.com/blog/static/163318706201272532112/.....
没问题啦

cu93302012-08-02 17:42:04

陈老师,我将你的文章链接到自己博客去了可以吗?
《详解数据结构中的哈夫曼与图》  http://zhongsiboke.blog.163.com/blog/static/163318706201272532112/

小尾巴鱼1211212012-04-12 10:54:16

深度优先遍历用递归算法是比较简单。。。可以借鉴