这题主要涉及到了队列,无向图的邻接矩阵表示,图的深度和广度优先搜索。又是糙哥,参考了他的程序(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. 在腾讯云上创建您的SQL Cluster(1)

    版权声明:本文由李斯达原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/247 来源:腾云阁 https://www.qclo ...

  2. 解决requestAnimationFrame的兼容问题

    写法: window.requestAnimFrame = (function () { return window.requestAnimationFrame || window.webkitReq ...

  3. IE7局部滚动区域下绝对定位或相对定位元素不随滚动条滚动的bug

    尽管在项目中测试人员已经慢慢淡化了IE6的测试,但是IE7依然还是要纳入测试范围. 最近碰到一个IE7的蛋疼bug,在页面上设置了一个局部的滚动区域,在拖动滚动条的时候,滚动区域内设置了相对定位或绝对 ...

  4. centOS wget的安装和使用

    CentOS wget是一个从网络上自动下载文件的自由工具.它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理. 所谓的自动下载是指,CentOS wget可以在用户退出系统的之后在后台执行 ...

  5. Java 集合系列 15 Map总结

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  6. Python输出内容的三种方式:print输出 python脚本执行 linux直接执行

    1.  在linux中安装python后,在linux命令行中输入python即可切换到Python命令行下 退出python命令行的命令: 老版本:ctrl+D 新版本:quit();或exit() ...

  7. MATLAB时间序列预测Prediction of time series with NAR neural network

    具体请参考:http://lab.fs.uni-lj.si/lasin/wp/IMIT_files/neural/nn05_narnet/ 神经网络预测时间序列数据,有三种模型, 这里是给出的是第二种 ...

  8. A-Making the Grade(POJ 3666)

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4656   Accepted: 2206 ...

  9. 为 Node.js 开发者准备的 8 本免费在线电子书(转)

    ode.js 是一套用来编写高性能网络服务器的JavaScript工具包,一系列的变化由此开始.比较独特的是,Node.js会假设你是在POSIX环境下运行它 Linux 或 Mac OS X.如果你 ...

  10. PHP超级全局变量——Session 变量

    PHP session 变量用于存储有关用户会话的信息,或更改用户会话的设置.Session 变量保存的信息是单一用户的,并且可供应用程序中的所有页面使用. PHP Session 变量 当您运行一个 ...