图的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 // // ...
随机推荐
- ASP.NET Identity系列教程-4【Identity高级技术】
https://www.cnblogs.com/r01cn/p/5194257.html 15 ASP.NET Identity高级技术 In this chapter, I finish my de ...
- svn error: "Previous operation has not finished; run 'cleanup' if it was interrupted"
出现这种问题,有几个原因 1.本身文件确实是锁住了 2.之前clean up 过很多次,但是每次都可能以失败告终,造成work queue存在缓存队列 3.svn lock 有lock记录 以下是简单 ...
- Cloud插件,链接oracle数据库
业务场景:客户需要在Cloud中获取第三方系统的数据,但是第三方系统的数据库是oracle,这是就需要连接oracle数据库获取数据了. 需要引用Oracle.ManagedDataAccess.dl ...
- 获取redis指定实例中所有的key
需求:获取redis指定的实例中所有的key的名字. 千万不要使用keys *,可以使用scan命令的递归方式获取. 以下给出自己写的脚本,经过测试效果还可以. db_ip=5.5.5.101 db_ ...
- PHP 实现时间戳转化为几分钟前、几小时前等格式
//发布时间提示 function get_last_time($time) { // 当天最大时间 $todayLast = strtotime(date('Y-m-d 23:59:59')); $ ...
- Selenium3+python自动化011-unittest生成测试报告(HTMLTestRunner)
批量执行完用例后,生成的测试报告是文本形式的,不够直观,为了更好的展示测试报告,最好是生成HTML格式的. unittest里面是不能生成html格式报告的,需要导入一个第三方的模块:HTMLTest ...
- Jmeter-简介及安装
一.Jmeter简介 Apache Jmeter 是Apache组织的开放源代码项目,是一个纯java桌面应用,用于压力测试和性能测量.它最初被设计用于Web应用测试但后来扩展到其它测试领域. Apa ...
- sql server和my sql 命令(语句)的区别,sql server与mysql的比较
sql与mysql的比较 1.连接字符串sql :Initial Catalog(database)=x; --数据库名称 Data Source(source)=x; - ...
- js和jq跳转到另一个页面或者在另一个窗口打开页面
$("#pic").click(function(){ location.href='newpage.html'; }); 上面的相当于<a href="newpa ...
- vue 项目初始化
初始化 vue init webpack-simple myproject 安裝 npm install 运行 npm run dev 访问地址 http://localhost:8080/ 安装we ...