设为首页 加入收藏

TOP

06-图1 列出连通集 (25分)(C语言邻接表实现)(一)
2017-10-12 17:39:12 】 浏览:1484
Tags:06- 列出 连通 25分 语言 邻接 实现

题目地址:https://pta.patest.cn/pta/test/558/exam/4/question/9495

由于边数E<(n*(n-1))/2 所以我选用了邻接表实现,优先队列用循环队列实现;

DFS基本思路:

  1:选择一个点,标志已经访问过;

  2:判断这个点的其他邻接点(访问顺序按题目是从小到大)是否访问过,来选择下一个点;

  3:重复第2点直到全部点已经访问过。

伪代码如下

DFS( Vertex v )
{   
    Visit( V ); 
    Visited[V] = true; 

    foreach(  v->neighbor )
        if ( Visited[v->neighbor ] == false )    
            DFS(v->neighbor );
}

BFS基本思路:

  1:选择一个点入队,并访问这个点,标志已经访问过;

  2:出队,将这个点未访问过的全部邻点访问并入队;

  3:重复第二点直到队列为空;

伪代码如下

BFS ( Vertex v )
{ 
  
    Visit( v );
    Visited[v] = true;
    EnQueue(Q, v); 
      
    while ( !IsEmpty(Q) ) 
    {
        w = DeQueue(Q);  
        foreach ( w->neighor )
            if ( Visited[w->neighor] == false ) 
            { 
                Visit( w->neighor );
                Visited[w->neighor] = true;
                EnQueue(Q, w->neighor);
            }
    } 
}        

具体代码

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <stdbool.h>
  4 #include <string.h>
  5 #include <dos.h>
  6 
  7 #define MaxVertexNum 10
  8 
  9 typedef int ElementType;
 10 typedef int Position;
 11 
 12 typedef struct QNode {
 13     ElementType *Data;     /* 存储元素的数组 */
 14     Position Front, Rear;  /* 队列的头、尾指针 */
 15     int MaxSize;           /* 队列最大容量 */
 16 }QNode, *Queue;
 17 
 18 
 19 typedef struct ENode
 20 {
 21     int ivex;            //该边指向的顶点位置
 22     struct ENode * next;     
 23 }ENode, *PENode;
 24 
 25 typedef int VertexType; 
 26 
 27 typedef struct VNode
 28 {
 29     VertexType data;
 30     ENode * first_edge;
 31 }VNode;
 32 
 33 typedef struct LGraph
 34 {
 35     int vexnum;            //图的顶点数目
 36     int edgnum;            //图的边的数目
 37     VNode * vexs;       //存放顶点的数组
 38 }LGraph;
 39 
 40 bool TAG;                   //用于输出格式
 41 bool visited[MaxVertexNum];
 42 
 43 LGraph * LGraph_new();
 44 void LGraph_destroy(LGraph * G);
 45 void LGraph_insert(ENode ** head, int ivex);
 46 void ResetVisit();
 47 void DFS(LGraph * G, int vex);
 48 void BFS(LGraph * G, int vex);
 49 void ListComponents(LGraph * G, void (*func)(LGraph * G, int vex));
 50 
 51 //优先队列的基本操作
 52 Queue CreateQueue( int MaxSize );
 53 bool IsFull( Queue Q );
 54 bool AddQ( Queue Q, ElementType X );
 55 bool IsEmpty( Queue Q );
 56 ElementType DeleteQ( Queue Q );
 57 
 58 int main()
 59 {
 60     LGraph * G = LGraph_new();
 61     ResetVisit();
 62     ListComponents(G, DFS);
 63     ResetVisit();
 64     ListComponents(G, BFS);
 65     system("pause");
 66     return 0;
 67 }
 68 
 69 void ListComponents(LGraph * G, void (*func)(LGraph * G, int vex))
 70 {
 71     int i;
 72     for ( i = 0; i < G->vexnum; ++i )
 73     {
 74         if (visited[i] == false)
 75         {
 76             (*func)(G, i);
 77             printf("}\n");
 78             TAG = false;
 79         }
 80     }
 81 }
 82 
 83 LGraph * LGraph_new()
 84 {
 85 
 86     LGraph * G = (LGraph *)malloc(sizeof(LGraph));
 87     scanf("%d %d", &G->vexnum, &G->edgnum);
 88     G->vexs = (VNode *)malloc(G->vexnum * sizeof(VNode));
 89     int i, v1, v2;
 90     for ( i = 0; i < G->vexnum; ++i )
 91     {
 92         G->vexs[i].data = i;
 93         G->vexs[i].first_edge = NULL;
 94     }
 95     for ( i = 0; i < G->edgnum; ++i )
 96     {
 97         scanf("%d %d", &v1, &v2);
 98         //由于顶点信息就是顶点坐标
 99         LGraph_insert(&G->vexs[v1].first_edge, v2);
100         LGraph_insert(&G->vexs[v2].first_edge, v1);
101     }
102     return G;
103 }
104 
105 void ResetVisit()
106 {
107     TAG = false;
108     memset(visited, false, sizeof(visited));
109 }
110 
111 void LGraph_insert(ENode ** head, int ivex)
112 {
113     ENode *pnew, *p1, *p2;
114     pnew = (ENode *)malloc(sizeof(ENode));
115     pnew->ivex = i
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇c语言-概述 下一篇sheep_折线分割平面

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目