图的bfs遍历模板(邻接矩阵存储和邻接表存储)
bfs遍历图模板伪代码:
bfs(u){ //遍历u所在的连通块
queue q;
//将u入队
inq[u] = true;
while (q非空){
//取出q的队首元素u进行访问
for (从u出发可达的所有的顶点v){
if (inq[v] == false){ //如果v未曾加入过队列
//将v入队;
inq[v] = true;
}
}
}
}
BFSTraversal(G){ //遍历图G
for (G的所有顶点u){
if (inq[u] == false){
BFS(u);
}
}
}
邻接矩阵版:
const int MAXV = ; const int INF = ; //邻接矩阵版
int n, G[MAXV][MAXV]; //n为顶点数,MAXV为最大顶点数
bool inq[MAXV] = { false };
void bfs(int u){ //遍历u所在的连通块
queue<int> q; //定义队列q
q.push(u); //将初识点u入队
inq[u] = true; //设置u已经被加入过队列
while (!q.empty()){ //只要队列非空
int u = q.front(); //取出队首元素
q.pop(); //将队首元素出队
for (int v = ; v < n; v++){
if (inq[v] == false && G[u][v] != INF){ //如果u的邻接点v未曾入过队列
q.push(v);
inq[v] = true;
}
}
}
} void BFSTraversal(){ //遍历图G
for (int u = ; u < n; u++){ //枚举所有顶点
if (inq[u] == false){ //如果u未曾加入过队列
bfs(u); //遍历u所在的连通块
}
}
}
邻接表版(顶点类型为非结构体):
vector<int> Adj[MAXV];
int n;
bool inq[MAXV] = { false };
void bfs(int u){
queue<int> q;
q.push(u);
inq[u] = true;
while (!q.empty()){
int u = q.front(); ///取出队首元素
q.pop(); //将队首元素出队
for (int i = ; i < Adj[u].size(); i++){
int v = Adj[u][i];
if (inq[v] = false){
q.push(v); //将v入队
inq[v] = true; //标记v为已经被加入过的队列
}
}
} } void BFSTraversal(){
for (int u = ; u < n; u++){
if (inq[u] = false){
bfs(u);
}
}
}
邻接表版(顶点类型为结构体):
vector<Node> Adj[MAXV];
int n;
bool inq[MAXV] = { false };
void bfs(int u){
queue<Node> q;
Node start;
start.v = u, start.w = , start.layer = ;
q.push(start);
inq[u] = true;
while (!q.empty()){
Node topNode = q.front(); ///取出队首元素
q.pop(); //将队首元素出队
for (int i = ; i < Adj[u].size(); i++){
Node node = Adj[u][i];
node.layer = topNode.layer + ;
if (inq[node.v] = false){
q.push(node); //将v入队
inq[node.v] = true; //标记v为已经被加入过的队列
}
}
} } void BFSTraversal(){
for (int u = ; u < n; u++){
if (inq[u] = false){
bfs(u);
}
}
}
注意:当顶点的属性不只一种或者边权的意义不只一种时,如顶点的属性除了“当前点所拥有的的资源量”还可能有 “当前点在图中的层次”,如边权除了“距离”这一意义还有“花费”属性,而用不同的存储图的方式一般用不同的方式处理这些多出来的属性,如果采用邻接矩阵的方式存储图:一般用增加一维数组和二维数组来应对点属性和边权意义的增加,而如果采用邻接表的方式存储图,则一般采用定义一个结构体,在结构体中增加需要的点属性和边权属性。
题型实战:
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers can view and forward his/her post, which can then be forwarded again by their followers. Now given a social network, you are supposed to calculate the maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤1000), the number of users; and L (≤6), the number of levels of indirect followers that are counted. Hence it is assumed that all the users are numbered from 1 to N. Then N lines follow, each in the format:
M[i] user_list[i]
where M[i] (≤100) is the total number of people that user[i] follows; and user_list[i] is a list of the M[i] users that followed by user[i]. It is guaranteed that no one can follow oneself. All the numbers are separated by a space.
Then finally a positive K is given, followed by K UserID's for query.
Output Specification:
For each UserID, you are supposed to print in one line the maximum potential amount of forwards this user can trigger, assuming that everyone who can view the initial post will forward it once, and that only L levels of indirect followers are counted.
Sample Input:
7 3
3 2 3 4
0
2 5 6
2 3 1
2 3 4
1 4
1 5
2 2 6
Sample Output:
4
5
题目大意要求:以某点开始,统计它L层以内所有点的个数
代码:
#include <stdio.h>
#include <queue>
#include <vector>
#include <string.h>
using namespace std; // 邻接矩阵版
const int maxv = ; int n, G[maxv][maxv] = { }; // n 为顶点数
bool inq[maxv] = { false }; // 如果对应下标的值为true, 则表示i已经被访问过了
int l, k; // 层数和查询数量 //struct Node{
// int v, layer;
//}; int layer[maxv] = { }; int BFS(int u){
int ans = ;
queue<int> q;
layer[u] = ;
q.push(u);
inq[u] = true;
while (!q.empty()){
int top = q.front();
q.pop();
for (int v = ; v <= n; v++){
if (G[top][v] != && inq[v] == false && layer[top] < ){
layer[v] = layer[top] + ;
inq[v] = true;
q.push(v);
ans++;
}
}
}
return ans;
} int main()
{
// 输入数据
// freopen("in.txt", "r", stdin);
scanf("%d %d", &n, &l);
int n2;
for (int v = ; v <= n; v++){
// 有向图,且逆着存储数据
scanf("%d", &n2);
int u;
for (int j = ; j < n2; j++){
scanf("%d", &u);
G[u][v] = ;
} } // 从不同的起点开始遍历图,返回一个点赞量
scanf("%d", &k);
for (int i = ; i < k; i++){
// 将inq数组初始化
memset(inq, false, sizeof(inq));
memset(layer, , sizeof(layer));
int u;
scanf("%d", &u);
int maxForwards = BFS(u);
printf("%d\n", maxForwards);
} // fclose(stdin);
return ;
}
图的bfs遍历模板(邻接矩阵存储和邻接表存储)的更多相关文章
- PTA 邻接表存储图的广度优先遍历(20 分)
6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(V ...
- PTA 邻接表存储图的广度优先遍历
试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ) 其中LGraph是邻接表存储的 ...
- 数据结构(11) -- 邻接表存储图的DFS和BFS
/////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...
- 邻接表存储图,DFS遍历图的java代码实现
import java.util.*; public class Main{ static int MAX_VERTEXNUM = 100; static int [] visited = new i ...
- 数据结构之---C语言实现图的邻接表存储表示
// 图的数组(邻接矩阵)存储表示 #include <stdio.h> #include <stdlib.h> #include <string.h> #defi ...
- 图->存储结构->邻接表
文字描述 邻接表是图的一种链式存储结构.在邻接表中,对图中每个顶点建立一个单链表,第i个单链表的结点表示依附顶点vi的边(对有向图是指以顶点vi为尾的弧).单链表中的每个结点由3个域组成,其中邻接点域 ...
- 图的邻接表存储表示(C)
//---------图的邻接表存储表示------- #include<stdio.h> #include<stdlib.h> #define MAX_VERTEXT_NUM ...
- 图的邻接表存储 c实现
图的邻接表存储 c实现 (转载) 用到的数据结构是 一个是顶点表,包括顶点和指向下一个邻接点的指针 一个是边表, 数据结构跟顶点不同,存储的是顶点的序号,和指向下一个的指针 刚开始的时候把顶点表初始化 ...
- DS实验题 Old_Driver UnionFindSet结构 指针实现邻接表存储
题目见前文:DS实验题 Old_Driver UnionFindSet结构 这里使用邻接表存储敌人之间的关系,邻接表用指针实现: // // main.cpp // Old_Driver3 // // ...
随机推荐
- 咸鱼的ACM之路:动态规划(DP)学习记录
按挑战程序设计竞赛介绍的顺序记录一遍学习DP的过程. 1. 01背包问题 问题如下: 有N个物品,每个物品(N[i])都有一定的体积(W[i]),和一定的价值(V[i]) 现在给定一个背包,背包的容量 ...
- 微信小程序调起支付API
官方文档: https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7 https://developers.weixin.q ...
- Wannafly Camp 2020 Day 1D 生成树 - 矩阵树定理,高斯消元
给出两幅 \(n(\leq 400)\) 个点的无向图 \(G_1 ,G_2\),对于 \(G_1\) 的每一颗生成树,它的权值定义为有多少条边在 \(G_2\) 中出现.求 \(G_1\) 所有生成 ...
- Python标准库之hashlib模块与hmac模块
hashlib模块用于加密相关的操作.在Python 3.x里代替了md5模块和sha模块,主要提供 SHA1.SHA224.SHA256.SHA384.SHA512 .MD5 算法.如果包含中文字符 ...
- C++ fstream文件操作问题记录
今天测试阿里云OSS文件上传接口,用fstream获取文件指针,代码如下 std::shared_ptr<std::iostream> content = std::make_shared ...
- NumPy迭代数组
numpy.nditer是NumPy的一个迭代器对象,提供能够灵活的访问一个或者多个属猪元素的方式. # 迭代 z=np.arange(6).reshape(3,2) for x in np.ndit ...
- (1)-Android学习笔记之:初识Android系统架构和项目结构
Android系统架构 Android程序结构 创建一个Android项目,为初学便于理解,将程序项目结构切换为Project模式,项目结构如下 .gradle和.idea:这两个目录下放的都是And ...
- Uva 1609 Feel Good
题面:给出长度为n的数列,然后算出其区间和乘区间最小数所能得到的最大值,并且输出区间 样例输入: 6 3 1 6 4 5 2 样例输出: 60 3 5 原题链接:https://vjudge.net/ ...
- Python元组详解
元组的特征 元组类型的名字是tuple 元组的一级元素不可被修改.不能增加或者删除: 元组和列表的书写区别是将中括号改成了小括号: 为方便区分元组和普通方法的参数,一般在元组的最后一个元素后保持加一个 ...
- 数据预处理 | 使用 Pandas 统一同一特征中不同的数据类型
出现的问题:如图,总消费金额本应该为float类型,此处却显示object 需求:将 TotalCharges 的类型转换成float 使用 pandas.to_numeric(arg, errors ...