图的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 // // ...
随机推荐
- Parity game POJ - 1733 带权并查集
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; <& ...
- 【剑指Offer】01、二维数组中的查找
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- C# 一次循环获取树的两种方法
第一种方法好些 第二种方法如果中间断开就会成为一级 private static List<Menu> MenuTree() { , ParentId = , Name = "a ...
- Acer4315笔记本CPU升级
终于有时间升级一下不怎么用的旧笔记本Acer4315了.在计划升级前了解了一下,芯片组是GL960,支持可升级的CPU有: CM560 CM570 T1600 T1700 T2310 T2330 T2 ...
- 报错Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/...
首先我的jdk是11.05的 这个是由于: 这个是 由于缺少了javax.xml.bind,在jdk10.0.1中没有包含这个包,所以我自己去网上下载了jdk 8,然后把jdk10.0.1换成jdk ...
- C 库函数 - modf()
C 库函数 - modf() C 标准库 - <math.h> 描述 C 库函数 double modf(double x, double *integer) 返回值为小数部分(小数点后的 ...
- linux 配置compoer
配置默认php 删除 rm -f /usr/bin/php 改到php7.3版本的composer /bin/php /usr/bin/php 多版本支持 配置php7专用composer70 cd ...
- sqlalchemy_mptt一次调优
问题背景: 我用sqlalchemy_mptt构建了一个多级分类项目,数据库用了sqlite.随着数据条数越来越多,写入速度逐渐变慢,一棵树的插入甚至需要1分钟,远远不能满足需求 分析思路: 1. 批 ...
- 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(8.7)Standard Vendor Specific Command Format
8.7 Standard Vendor Specific Command Format 标准的厂商特定命令格式 Controller可以支持Figure 106中定义的标准的Vendor Specif ...
- php执行shell脚本
本次想要配置webhook钩子, 做钩子大多是走 ssh 协议, coding 里配置部署公钥 之前用 docker 写钩子, 也是 ssh 权限的问题 包工具: 1.composer r ...