06-图1 List Components
这题主要涉及到了队列,无向图的邻接矩阵表示,图的深度和广度优先搜索。又是糙哥,参考了他的程序(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的更多相关文章
- DS博客作业06—图
1.本周学习总结 1.1思维导图 1.2学习体会 2.PTA实验作业 2.1 图着色问题 图着色问题是一个著名的NP完全问题.给定无向图G=(V,E),问可否用K种颜色为V中的每一个顶点分配一种颜色, ...
- DS博客作业06——图
1.本周学习总结(0--2分) 1.思维导图 2.谈谈你对图结构的认识及学习体会. 这章学习了图,学习了图的两种存储结构:邻接矩阵和邻接表.这两种存储结构都用到了之前学c时学到的结构体,将结构体充分运 ...
- Vue2 轮播图组件 slide组件
Vue2原生始轮播图组件,支持宽度自适应.高度设置.轮播时间设置.左右箭头按钮控制,圆点按钮切换,以及箭头.圆点按钮是否显示. <v-carousel :slideData="slid ...
- 如何让 FFmpeg 支持异步并行转码、截图等等操作?
直接贴代码了: ffmpegTest02.cs public partial class ffmpegTest02 : FormBase { private static readonly strin ...
- Flex ObjectHandles 构建绘图程序!
模型 主画布组件:com/components/graph/GraphContainer.mxml <?xml version="1.0" encoding="ut ...
- Playmaker Input篇教程之PlayMaker菜单概述
Playmaker Input篇教程之PlayMaker菜单概述 Playmaker InputPlayMaker菜单概述 Playmaker插件被导入游戏项目以后,会自动为Unity编辑器添加一个名 ...
- ISE和Modelsim联合仿真(转)
相信很多人会遇到过这个问题,不知如何让ISE调用Modelsim进行仿真.我也迷糊了不少时间,查查找找,终于弄明白了,所以有了本文,和大家分享一下.我尽量讲得详细点儿,多多上图. 我的环境:Windo ...
- 基于mapreducer的图算法
作者现就职阿里巴巴集团1688技术部 引言 周末看到一篇不错的文章"Graph Twiddling in a MapReduce world" ,介绍MapReduce下一些图算法 ...
- 实现react路由动态加载的组件
import React, { Component } from 'react'; import Loading from '../../base/nc_Loading'; /* * date: 20 ...
- [精彩] 关于DB2的内存分配
这两天在看DB2的内存管理的内容,看的很是模糊,有以下问题不明白,请教 是不是数据库管理器的共享内存就是DB2能够使用的最大内容呢,然后数据库全局内存从管理器内存那里获得分配的内存,然后应用程序全局内 ...
随机推荐
- DirectX中的纹理及其创建
正如大多初学者会遇到一个问题, 导入的图片为何不是原来的尺寸?例如800*600的实际上通过D3DXCreateTextureFromFile后变成的是1024*1024,即宽和高默认都会自动扩展为2 ...
- struts2文件下载相关信息
struts.xml文件配置: <span style="font-size:16px;"><?xml version="1.0" encod ...
- jQuery图片延迟加载插件jQuery.lazyload使用方法(转)
使用方法 1.引用jquery和jquery.lazyload.js到你的页面 <script src="jquery-1.11.0.min.js"></scri ...
- jmeter 构建一个LDAP测试计划
添加用户 第一步你想做的每一个JMeter测试计划是添加一个线程组元素. 线程组告诉JMeter的用户数量你想模拟,用户应该发送的次数 请求,他们应该发送的请求的数量. 继续添加ThreadGroup ...
- CentOS下更新python版本
执行#Python或#python -V或#python --version,看到版本号是2.7.5,到官网https://www.python.org/ftp/python/查看了下最新版本都到了2 ...
- QT-- MainWindow外的cpp文件调用ui
这几天在学习QT,想写一个类似VIM的小软件,刚开始不注重代码结构,全部实现都写在MainWindow文件中,导致MianWindow文件十分的长而且很难去阅读,就想着把函数按照功能分到不同的cpp文 ...
- 《JavaScript权威指南》读书笔记(二)
日期:2015-12-04 js 的原型::闭包:闭包这是个相当复杂的东西...现在初步理解: http://segmentfault.com/a/1190000000652891 闭包有 ...
- UserAgent:通过浏览器获取用户浏览器等信息
User Agent的含义 User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本.CPU 类型.浏览器及版本.浏览器渲染引擎.浏 ...
- was7 安装遇到问题(Linux平台Redhat 6)
1../launchpad.sh 无法启动安装,提示无法找到浏览器 解决:直接进入WAS文件夹,执行install cd WAS ./install 2.安装时中文乱码 设置区域为美国: LANG=e ...
- Octopus系列之接下来的任务
更新默认国家[已实现] 更新每页显示条数的后台控制[已实现] 更新国家和区域的Ajax的关联[已实现] 更新详情页面的 属性选择 脚本提示[已实现 可以做到和兰亭一样的效果了] 增加优惠方案的设置和批 ...