06-图1 List Components

这题主要涉及到了队列,无向图的邻接矩阵表示,图的深度和广度优先搜索。又是糙哥,参考了他的程序(http://www.cnblogs.com/liangchao/p/4288807.html),主要是BFS那块,课件上的不太明白。有一点不太明白,图的初始化那块,利用传指向图的指针而不是通过函数返回值为什么不行?代码及题目如下

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <stdbool.h>
  5
  6 typedef struct MGraph
  7 {
  8     int vertice;
  9     int * edge;
 10     bool * visited;
 11 }MGraph, * pMGraph;
 12 typedef struct Queue
 13 {
 14     int * elem;
 15     int head;
 16     int tail;
 17     int size;
 18 }Queue, * pQueue;
 19
 20 pMGraph initGraph(int vn);
 21 void link(pMGraph pG, int v1, int v2);
 22 void DFS(pMGraph pG, int v);
 23
 24 void BFS(pMGraph pG, int v);
 25 pQueue createQueue(int vn);
 26 bool isEmpty(pQueue pQ);
 27 void inQueue(pQueue, int v);
 28 int outQueue(pQueue);
 29
 30 int main()
 31 {
 32 //    freopen("in.txt", "r", stdin); // for test
 33     int i, N, E;
 34     scanf("%d%d", &N, &E);
 35     pMGraph pG;
 36     pG = initGraph(N);
 37
 38     int v1, v2;
 39     for(i = 0; i < E; i++)
 40     {
 41         scanf("%d%d", &v1, &v2);
 42         link(pG, v1, v2);
 43     }
 44
 45     for(i = 0; i < N; i++)
 46     {
 47         if(!pG->visited[i])
 48         {
 49             printf("{ ");
 50             DFS(pG, i);
 51             printf("}\n");
 52         }
 53     }
 54     memset(pG->visited, false, pG->vertice * sizeof(bool));
 55     for(i = 0; i < N; i++)
 56     {
 57         if(!pG->visited[i])
 58         {
 59             printf("{ ");
 60             BFS(pG, i);
 61             printf("}\n");
 62         }
 63     }
 64 //    fclose(stdin); // for test
 65     return 0;
 66 }
 67
 68 pMGraph initGraph(int vn)
 69 {
 70     int len;
 71     len = vn * (vn - 1) / 2;
 72
 73     pMGraph pG;
 74     pG = (pMGraph)malloc(sizeof(MGraph));
 75     pG->vertice = vn;
 76     pG->edge = (int *)malloc(len * sizeof(int));
 77     memset(pG->edge, 0, len * sizeof(int));
 78     pG->visited = (bool *)malloc(vn * sizeof(bool));
 79     memset(pG->visited, false, vn * sizeof(bool));
 80
 81     return pG;
 82 }
 83
 84 void link(pMGraph pG, int v1, int v2)
 85 {
 86     int index;
 87
 88     if(v1 > v2)
 89     {
 90         v1 += v2;
 91         v2 = v1 - v2;
 92         v1 -= v2;
 93     }
 94     index = v2 * (v2 - 1) / 2 + v1;
 95     pG->edge[index] = 1;
 96 }
 97
 98 void DFS(pMGraph pG, int v)
 99 {
100     int row, col, index;
101
102     pG->visited[v] = true;
103     printf("%d ", v);
104     for(col = 0; col < v; col++)
105     {
106         index = v * (v - 1) / 2 + col;
107         if(pG->edge[index] && pG->visited[col] == false)
108             DFS(pG, col);
109     }
110     for(row = v + 1; row < pG->vertice; row++)
111     {
112         index = row * (row - 1) / 2 + v;
113         if(pG->edge[index] && pG->visited[row] == false)
114             DFS(pG, row);
115     }
116 }
117
118 void BFS(pMGraph pG, int v)
119 {
120     pQueue pQ;
121     pQ = createQueue(pG->vertice);
122
123     int row, col, index;
124     pG->visited[v] = true;
125     printf("%d ", v);
126     inQueue(pQ, v);
127     while(!isEmpty(pQ))
128     {
129         v = outQueue(pQ);
130         for(col = 0; col < v; col++)
131         {
132             index = v * (v - 1) / 2 + col;
133             if(pG->edge[index] && pG->visited[col] == false)
134             {
135                 pG->visited[col] = true;
136                 printf("%d ", col);
137                 inQueue(pQ, col);
138             }
139         }
140         for(row = v + 1; row < pG->vertice; row++)
141         {
142             index = row * (row - 1) / 2 + v;
143             if(pG->edge[index] && pG->visited[row] == false)
144             {
145                 pG->visited[row] = true;
146                 printf("%d ", row);
147                 inQueue(pQ, row);
148             }
149         }
150     }
151 }
152
153 pQueue createQueue(int vn)
154 {
155     pQueue pQ;
156     pQ = (pQueue)malloc(sizeof(Queue));
157     pQ->size = vn + 1;
158     pQ->head = pQ->tail = 0;
159     pQ->elem = (int *)malloc(pQ->size * sizeof(int));
160
161     return pQ;
162 }
163
164 bool isEmpty(pQueue pQ)
165 {
166     if(pQ->head != pQ->tail)
167         return false;
168     else
169         return true;
170 }
171
172 void inQueue(pQueue pQ, int v)
173 {
174     pQ->tail = (pQ->tail + 1) % pQ->size;
175     pQ->elem[pQ->tail] = v;
176 }
177
178 int outQueue(pQueue pQ)
179 {
180     pQ->head = (pQ->head + 1) % pQ->size;
181
182     return pQ->elem[pQ->head];
183 }

For a given undirected graph with N vertices and E edges, please list all the connected components by both DFS and BFS. Assume that all the vertices are numbered from 0 to N-1. While searching, assume that we always start from the vertex with the smallest index, and visit its adjacent vertices in ascending order of their indices.

Input Specification:

Each input file contains one test case. For each case, the first line gives two integers N (0<N<=10) and E, which are the number of vertices and the number of edges, respectively. Then E lines follow, each described an edge by giving the two ends. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in each line a connected component in the format "{ v1 v2 ... vk }". First print the result obtained by DFS, then by BFS.

Sample Input:

8 6
0 7
0 1
2 0
4 1
2 4
3 5

Sample Output:

{ 0 1 4 2 7 }
{ 3 5 }
{ 6 }
{ 0 1 2 7 4 }
{ 3 5 }
{ 6 }
时间: 2024-07-30 15:16:06

06-图1 List Components的相关文章

Hyper-V 2016 系列教程32 StartWind 虚拟NFS,iSCSI软件

大家在学习Hyper-V的过程中,如果企业有用来测试用iSCSI或者NFS类型存储器的话,这是最好不过的了,如果没有条件购买的话,我们这时可以用第三方模拟软件来满足我们实验的目的,iSCSI模拟软件一般有StarWind,还有就是Windows Server 2016自带了模拟iSCSI和NFS的功能,大家有兴趣可以测试一下,如果要使用微软的iSCSI服务器的话,可以参考本博客中的关于配置Windows Server 2016作为iSCSI服务器的完整过程的文章. 这里我们推荐是StarWind

基于mapreducer的图算法

作者系阿里巴巴集团1688技术部普通码农 引言 周末看到一篇不错的文章"Graph Twiddling in a MapReduce world" ,介绍MapReduce下一些图算法的实现.文章语言质朴,介绍很多实用图优化技巧.文章2009年发表,至今已经被引用183次,足以证明这篇文章价值.目前这篇文章网上已经有人对这篇文章做了介绍,但仅介绍了其中最简单的两个算法,对其中的所做优化,并没有做分析.为了加深对文章算法的理解,我重新对这篇文章的算法做了翻译,同时加了自己的理解,以及算法

机一是看好无人机的市场二是美国即将推出相关

在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packet Delivery Service) 无连接交付抽象地表示大多数分组交换网络都能提供的一种服务.简单地讲,指的是TCP/IP灰暗网按照报文上携带的地址信息把短报文从一台机器传递到另一台机制.因为无连接服务单独传递每个分组,所以不能保证可靠.有序地传递.而且,由于无连接服务通常直接映射到底层的硬件上

Xtuner X500 better than Vpecker Easydiag

Xtuner X500 is a kind of Auto Diagnostic Tool which performs same function as Vpecker Easydiag. But X500 is the first Android based OBDII diagnostic scanner with extra special functions like Oil reset, DPF, battery, ABS, EPB, TPMS reset, injector etc

ISE和Modelsim联合仿真(转)

ISE和Modelsim联合仿真(转) 地址:http://www.cnblogs.com/feitian629/archive/2013/07/13/3188192.html 相信很多人会遇到过这个问题,不知如何让ISE调用Modelsim进行仿真.我也迷糊了不少时间,查查找找,终于弄明白了,所以有了本文,和大家分享一下.我尽量讲得详细点儿,多多上图. 我的环境:Windows 7 64位,Xilinx ISE Design Suite 13.4(D:\Xilinx\13.4),Modelsi

Log和Canny边缘检测(附Matlab程序)

  一. 实验目的 (1) 通过实验分析不同尺度下LOG和Canny边缘提取算子的性能. (2) 研究这两种边缘提取方法在不同参数下的边缘提取能力. (3) 使用不同的滤波尺度和添加噪声能量(噪声水平),通过与无噪声图像对比,选择最能说明自己结论的滤波尺度和噪声水平,并做出分析说明. 二. 实验原理 边缘的含义:在数字图像中,边缘是指图像局部变化最显著的部分,边缘主要存在于目标与目标,目标与背景之间,是图像局部特性的不连续性,如灰度的突变.纹理结构的突变.颜色的突变等.尽管图像的边缘点产生的原因

mysql(connector/ODBC)

MySQL有很多的connector: 其中的 J,C++,C主要用于使用java,c++,c等语言开发连接MySQL应用时使用. ODBC:主要用于各种平台Windows,Linuxe等上的通用程序连接数据库时使用,比如PowerDesigner连接数据库时使用. ==================================================== 安装: http://jingyan.baidu.com/article/f79b7cb3a25e759144023ee7.h

Playmaker Input篇教程之PlayMaker菜单概述

Playmaker Input篇教程之PlayMaker菜单概述 ?Playmaker InputPlayMaker菜单概述 Playmaker插件被导入游戏项目以后,会自动为Unity编辑器添加一个名为PlayMaker的主菜单,如图1-14所示.熟练的使用这个主菜单,可以让我们更快的找到Playmaker提供的重要功能,以及特定行为的快捷键. 图1-14  PlayMaker菜单 Playmaker Input快速打开playMaker编辑器 playMaker编辑器是开发者使用Playma

[精彩] 关于DB2的内存分配

这两天在看DB2的内存管理的内容,看的很是模糊,有以下问题不明白,请教 是不是数据库管理器的共享内存就是DB2能够使用的最大内容呢,然后数据库全局内存从管理器内存那里获得分配的内存,然后应用程序全局内存又从数据库全局内存那里获得分配内存的,也就是说他们是一层一层的包含关系.还是我理解有问题,是并列关系的呢? PS 现在DB2好的教材比较少,找了几本书上写的都不是很清楚,谁要要好的资料共享下,先谢了  nic518 回复于:2006-12-31 12:45:01 [导读]本文将向您讲解 DB2 内

3Dx Max制作月光下的城堡CG场景

"Points of View"是一项点评从不同角度和视点能得实物的对比和不同的视觉感受的作品的名字.我设计绘景,以阐明这一论点,混合了一切强大和浪漫的光线,以提供一个对比感强烈的未来“冰冷”城市.接着我开始图像绘景,把我的想法转移到纸上.通常我都是围绕主题以铅笔绘制开始的,研究各个元素的位置,以得到一个动态的组合物.但是在绘景过程中我只在Photoshop中研究了概念,因为我已经有了一张与之相关的由Davide Scridel拍的照片.推荐学习3D MAX基础精讲 在明白了我所想沟通