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

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h> typedef struct MGraph
{
int vertice;
int * edge;
bool * visited;
}MGraph, * pMGraph;
typedef struct Queue
{
int * elem;
int head;
int tail;
int size;
}Queue, * pQueue; pMGraph initGraph(int vn);
void link(pMGraph pG, int v1, int v2);
void DFS(pMGraph pG, int v); void BFS(pMGraph pG, int v);
pQueue createQueue(int vn);
bool isEmpty(pQueue pQ);
void inQueue(pQueue, int v);
int outQueue(pQueue); int main()
{
// freopen("in.txt", "r", stdin); // for test
int i, N, E;
scanf("%d%d", &N, &E);
pMGraph pG;
pG = initGraph(N); int v1, v2;
for(i = ; i < E; i++)
{
scanf("%d%d", &v1, &v2);
link(pG, v1, v2);
} for(i = ; i < N; i++)
{
if(!pG->visited[i])
{
printf("{ ");
DFS(pG, i);
printf("}\n");
}
}
memset(pG->visited, false, pG->vertice * sizeof(bool));
for(i = ; i < N; i++)
{
if(!pG->visited[i])
{
printf("{ ");
BFS(pG, i);
printf("}\n");
}
}
// fclose(stdin); // for test
return ;
} pMGraph initGraph(int vn)
{
int len;
len = vn * (vn - ) / ; pMGraph pG;
pG = (pMGraph)malloc(sizeof(MGraph));
pG->vertice = vn;
pG->edge = (int *)malloc(len * sizeof(int));
memset(pG->edge, , len * sizeof(int));
pG->visited = (bool *)malloc(vn * sizeof(bool));
memset(pG->visited, false, vn * sizeof(bool)); return pG;
} void link(pMGraph pG, int v1, int v2)
{
int index; if(v1 > v2)
{
v1 += v2;
v2 = v1 - v2;
v1 -= v2;
}
index = v2 * (v2 - ) / + v1;
pG->edge[index] = ;
} void DFS(pMGraph pG, int v)
{
int row, col, index; pG->visited[v] = true;
printf("%d ", v);
for(col = ; col < v; col++)
{
index = v * (v - ) / + col;
if(pG->edge[index] && pG->visited[col] == false)
DFS(pG, col);
}
for(row = v + ; row < pG->vertice; row++)
{
index = row * (row - ) / + v;
if(pG->edge[index] && pG->visited[row] == false)
DFS(pG, row);
}
} void BFS(pMGraph pG, int v)
{
pQueue pQ;
pQ = createQueue(pG->vertice); int row, col, index;
pG->visited[v] = true;
printf("%d ", v);
inQueue(pQ, v);
while(!isEmpty(pQ))
{
v = outQueue(pQ);
for(col = ; col < v; col++)
{
index = v * (v - ) / + col;
if(pG->edge[index] && pG->visited[col] == false)
{
pG->visited[col] = true;
printf("%d ", col);
inQueue(pQ, col);
}
}
for(row = v + ; row < pG->vertice; row++)
{
index = row * (row - ) / + v;
if(pG->edge[index] && pG->visited[row] == false)
{
pG->visited[row] = true;
printf("%d ", row);
inQueue(pQ, row);
}
}
}
} pQueue createQueue(int vn)
{
pQueue pQ;
pQ = (pQueue)malloc(sizeof(Queue));
pQ->size = vn + ;
pQ->head = pQ->tail = ;
pQ->elem = (int *)malloc(pQ->size * sizeof(int)); return pQ;
} bool isEmpty(pQueue pQ)
{
if(pQ->head != pQ->tail)
return false;
else
return true;
} void inQueue(pQueue pQ, int v)
{
pQ->tail = (pQ->tail + ) % pQ->size;
pQ->elem[pQ->tail] = v;
} int outQueue(pQueue pQ)
{
pQ->head = (pQ->head + ) % pQ->size; return pQ->elem[pQ->head];
}

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 }

06-图1 List Components的更多相关文章

  1. DS博客作业06—图

    1.本周学习总结 1.1思维导图 1.2学习体会 2.PTA实验作业 2.1 图着色问题 图着色问题是一个著名的NP完全问题.给定无向图G=(V,E),问可否用K种颜色为V中的每一个顶点分配一种颜色, ...

  2. DS博客作业06——图

    1.本周学习总结(0--2分) 1.思维导图 2.谈谈你对图结构的认识及学习体会. 这章学习了图,学习了图的两种存储结构:邻接矩阵和邻接表.这两种存储结构都用到了之前学c时学到的结构体,将结构体充分运 ...

  3. Vue2 轮播图组件 slide组件

    Vue2原生始轮播图组件,支持宽度自适应.高度设置.轮播时间设置.左右箭头按钮控制,圆点按钮切换,以及箭头.圆点按钮是否显示. <v-carousel :slideData="slid ...

  4. 如何让 FFmpeg 支持异步并行转码、截图等等操作?

    直接贴代码了: ffmpegTest02.cs public partial class ffmpegTest02 : FormBase { private static readonly strin ...

  5. Flex ObjectHandles 构建绘图程序!

    模型 主画布组件:com/components/graph/GraphContainer.mxml <?xml version="1.0" encoding="ut ...

  6. Playmaker Input篇教程之PlayMaker菜单概述

    Playmaker Input篇教程之PlayMaker菜单概述 Playmaker InputPlayMaker菜单概述 Playmaker插件被导入游戏项目以后,会自动为Unity编辑器添加一个名 ...

  7. ISE和Modelsim联合仿真(转)

    相信很多人会遇到过这个问题,不知如何让ISE调用Modelsim进行仿真.我也迷糊了不少时间,查查找找,终于弄明白了,所以有了本文,和大家分享一下.我尽量讲得详细点儿,多多上图. 我的环境:Windo ...

  8. 基于mapreducer的图算法

    作者现就职阿里巴巴集团1688技术部 引言 周末看到一篇不错的文章"Graph Twiddling in a MapReduce world" ,介绍MapReduce下一些图算法 ...

  9. 实现react路由动态加载的组件

    import React, { Component } from 'react'; import Loading from '../../base/nc_Loading'; /* * date: 20 ...

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

    这两天在看DB2的内存管理的内容,看的很是模糊,有以下问题不明白,请教 是不是数据库管理器的共享内存就是DB2能够使用的最大内容呢,然后数据库全局内存从管理器内存那里获得分配的内存,然后应用程序全局内 ...

随机推荐

  1. xcode6 AsynchronousTesting 异步任务测试

    xcode集成了非常方便的测试框架,XCTest 在xcode6之后,提供了 <XCTest/XCTestCase+AsynchronousTesting.h> 利用此我们可以直接在XCT ...

  2. hibernate执行sql的三种方式

    方式一:直接使用HibernateTemplate的find()方法,find方法支持执行hql语句 List<T> list = this.getHibernateTemplate(). ...

  3. 《Java程序设计》 实验一 实验报告

    实验一 Java开发环境的熟悉(Windows + IDEA) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.完成 ...

  4. Qt之加载QSS文件

    简述 Qt中关于样式的使用很常见,为了降低耦合性(与逻辑代码分离),我们通常会定义一个QSS文件,然后编写各种部件(例如:QLable.QLineEdit.QPushButton)的样式,最后使用QA ...

  5. python中range和xrange的区别

    1.range生成一个列表:xrange生成一个生成器 2.用法都差不多  

  6. Echart多图联动

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  7. PowerDesigner中导出设计说明文档

    点击下图的新建按钮,新建一个导出内容的模板 模板设计界面分为2栏,左边是可选的模板内容,右侧是模板,双击左侧条目会添加到右侧,最后生成的文件中就有此项内容. 已经添加到右侧的内容可以编辑,双击右侧的条 ...

  8. SQL语句技巧(上个样式太差了)

      以下并非本人整理,但是看后感觉相当不错,特此分享. 1.应用程序中,保证在实现功能的基础上,尽量减少对数据库的访问次数:通过搜索参数,尽量减少对表的访问行数,最小化结果集,从而减轻网络负担:能够分 ...

  9. 部署步骤“回收 IIS 应用程序池”中出现错误: <nativehr>0x80070005</nativehr><nativestack></nativestack>拒绝访问。

    解决方法:以sharepoint管理员身份进入主站点,修改站点的网站集管理员.

  10. 使用Camera进行拍照

    Android应用提供了Camera来控制拍照,使用Camera进行拍照的步骤如下: 1.调用Camera的open()方法打开相机. 2.调用Camera的getParameters()方法获取拍照 ...