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能够使用的最大内容呢,然后数据库全局内存从管理器内存那里获得分配的内存,然后应用程序全局内 ...
随机推荐
- js上下滚屏效果,代码通过测试
这是html代码 <div class="box"> <div class="bcon"> <h1><b>领号实 ...
- JAVA通过C3P0连接数据库
配置文件: <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <na ...
- android:layout_weight的真实含义
首先声明只有在Linearlayout中,该属性才有效.之所以android:layout_weight会引起争议, 是因为在设置该属性的同时,设置android:layout_width为wrap_ ...
- 【CITE】 C#中实现拖动无边框Form窗体
首先建一个Windows应用程序 将Form1的 FormBorderStyle属性设置为None 主要是在Form1窗体触发三个事件:Form4_MouseDown,Form4_MouseMove, ...
- OSVERSIONINFO
OSVERSIONINFO结构 OSVERSIONINFO结构包含了操作系统的版本信息,包括操作系统的主版本号.副版本号.创建号.以及操作系统平台ID号和关于操作系统的其他描述信息.其定义为: typ ...
- 批量Load/Store指令的寻址方式
批量Load/Store指令用于实现在一组寄存器和一块连续的内存单元之间传输数据.也称为多寄存器寻址方式,即一条指令可以完成多个寄存器值的传送.这种寻址方式可以用一条指令最多完成传送16个通用寄存器的 ...
- kwicks插件学习
/* Kwicks for jQuery (version 1.5.1) Copyright (c) 2008 Jeremy Martin http://www.jeremymartin.name/p ...
- 必须关注的25位知名JavaScript开发者
必须关注的25位知名JavaScript开发者 发表于2012-08-07 17:30| 16215次阅读| 来源Crossrider Blog| 46 条评论| 作者Crossrider Blog ...
- J2EE开发实战基础系列一 HelloWorld【转】
开始咱们的第一个程序,首先是配置环境,按照上一章所描述的方式下载开发工具,然后配置Java环境变量,给大家看下具体的结构: 环境变量配置OK的提示,如上图. Eclipse和Tomcat的文件目录 ...
- bat产生随机数并复制文件及生成文件列表
有这样一个场景:我需要将同一个文件复制为上千个文件,并且文件名应为随机数.为了简单起见,不想写程序,直接写个BAT来,方便,简单,易用: 1. 搞定用BAT产生32位随机数,存为变量并使用,保存以下代 ...