[算法日志]图论: 广度优先搜索(BFS)

news/2024/7/15 5:50:30 标签: 图论, 宽度优先, 算法, 数据结构

[算法日志]图论: 广度优先搜索(BFS)

广度优先概论

​ 广度优先遍历也是一种常用的遍历图的算法策略,其思想是将本节点相关联的节点都遍历一遍后才切换到相关联节点重复本操作。这种遍历方式类似于对二叉树节点的层序遍历,即先遍历完子节点后再去遍历子节点的各个子节点。

代码框架

int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 表示四个方向
// grid 是地图,也就是一个二维数组
// visited标记访问过的节点,不要重复访问
// x,y 表示开始搜索节点的下标
void bfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int x, int y) {
    queue<pair<int, int>> que; // 定义队列
    que.push({x, y}); // 起始节点加入队列
    visited[x][y] = true; // 只要加入队列,立刻标记为访问过的节点
    while(!que.empty()) { // 开始遍历队列里的元素
        pair<int ,int> cur = que.front(); 
        que.pop(); // 从队列取元素
        int curx = cur.first;
        int cury = cur.second; // 当前节点坐标
        for (int i = 0; i < 4; i++) { // 开始想当前节点的四个方向左右上下去遍历
            int nextx = curx + dir[i][0];
            int nexty = cury + dir[i][1]; // 获取周边四个方向的坐标
            if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;  // 坐标越界了,直接跳过
            if (!visited[nextx][nexty]) { // 如果节点没被访问过
                que.push({nextx, nexty});  // 队列添加该节点为下一轮要遍历的节点
                visited[nextx][nexty] = true; // 只要加入队列立刻标记,避免重复访问
            }
        }
    }
}

通过队列辅助进行迭代操作是BFS的常用遍历手段(当然也可以使用栈),而除了会额外使用一个visited数组辅助进行路径判断,其他的步骤都可以照搬二叉树的层序遍历。

BFS的简单应用

leetcode 200(岛屿数量)

BFS示例代码

 	const int dir[4][2] = { {0, -1}, {1, 0}, {0, 1}, {-1, 0} };
	void BFS(const vector<vector<char>>& g, vector<vector<bool>>& v, queue<pair<int,int>>& h)
	{
		while (!h.empty())
		{
			int X = h.front().first;
			int Y = h.front().second;
			h.pop();
			for (int i = 0; i < 4; ++i)
			{
				int nextX = X + dir[i][0];
				int nextY = Y + dir[i][1];
				if (nextX < 0 || nextY < 0 || nextX >= g[0].size() || nextY >= g.size())
					continue;
				if (!v[nextY][nextX] && g[nextY][nextX] == '1')
				{
					v[nextY][nextX] = true;
					h.push(pair<int, int>(nextX, nextY));
				}
			}
		}
		return;
	}
	int numIslands(const vector<vector<char>>& gird)
	{
		if (gird.empty())
			return 0;
		vector<vector<bool>> visited(gird.size(), vector<bool>(gird[0].size(), false));
		queue<pair<int, int>> help;
		int count = 0;
		for (int i = 0; i < gird.size(); ++i)
			for (int j = 0; j < gird[0].size(); ++j)
			{
				if (!visited[i][j] && gird[i][j] == '1')
				{
					++count;
					help.push({j, i});
					visited[i][j] = true;
					BFS(gird, visited, help);					
				}
			}
		return count;
	}

当然,对于这种岛屿问题并没有对遍历方式做限制,因此自然有DFS解法。

	const int dir[4][2] = { {0, -1}, {1, 0}, {0, 1}, {-1, 0} };
	void DFS2(vector<vector<bool>>& v, int& c,int depth ,int nextX, int nextY)
	{
		if (depth > 0 && (nextX < 0 || nextY < 0 || nextX >= v[0].size()
			|| nextY >= v.size() || v[nextY][nextX]))
			return;
		for(int i = nextY; i < v.size(); ++i)
			for (int j = nextX; j < v[0].size(); ++j)
			{
				if (v[i][j])
				{
					if (depth > 0)
						return;
					continue;
				}
				if (depth == 0)
					++c;
				v[i][j] = true;
				for (int k = 0; k < 4; ++k)
				{
					DFS2(v, c, depth + 1, j + dir[k][0], i + dir[k][1]);
				}
			}
		return;
	}
	int numIslands1(const vector<vector<char>>& grid)
	{
		if (grid.empty())
			return 0;
		vector<vector<bool>> visited(grid.size(), vector<bool>(grid[0].size()));
		int count = 0;
		for (int i = 0; i < grid.size(); ++i)
			for (int j = 0; j < grid[0].size(); ++j)
			{
				if (grid[i][j] == '1')
					visited[i][j] = false;
				else
					visited[i][j] = true;
			}
		DFS2(visited, count, 0, 0, 0);
		return count;
	}

http://www.niftyadmin.cn/n/5157546.html

相关文章

【LeetCode】每日一题 2023_11_6 最大单词长度乘积

文章目录 刷题前唠嗑题目&#xff1a;最大单词长度乘积题目描述代码与解题思路偷看大佬题解 结语 刷题前唠嗑 LeetCode? 启动&#xff01;&#xff01;&#xff01; 题目&#xff1a;最大单词长度乘积 题目链接&#xff1a;318. 最大单词长度乘积 题目描述 代码与解题思路…

【C++】map set

map & set 一、关联式容器二、键值对三、树形结构的关联式容器1. set&#xff08;1&#xff09;set 的介绍&#xff08;2&#xff09;set 的使用 2. multiset3. map&#xff08;1&#xff09;map 的介绍&#xff08;2&#xff09;map 的使用 4. multimap 四、map 和 set 的…

并查集模版以及两道例题

&#x1f4af; 博客内容&#xff1a;并查集 &#x1f600; 作  者&#xff1a;陈大大陈 &#x1f680; 个人简介&#xff1a;一个正在努力学技术的准C后端工程师&#xff0c;专注基础和实战分享 &#xff0c;欢迎私信&#xff01; &#x1f496; 欢迎大家&#xff1a;这里是C…

机器学习实战:基于sklearn的工业蒸汽量预测

文章目录 写在前面工业蒸汽量预测1.基础代码2.模型训练3.模型正则化4.模型交叉验证5.模型超参空间及调参6.学习曲线和验证曲线 写在后面 写在前面 本期内容&#xff1a;基于机器学习的工业蒸汽量预测 实验环境&#xff1a; anaconda python sklearn 注&#xff1a;本专栏内所有…

02-Sping事务实现之声明式事务基于XML的实现方式

声明式事务之XML实现方式 开发步骤 第一步: 引入AOP相关的aspectj依赖 <!--aspectj依赖--> <dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>6.0.0-M2</version> <…

volatile-日常使用场景

6.4 如何正确使用volatile 单一赋值可以&#xff0c;但是含复合运算赋值不可以&#xff08;i之类的&#xff09; volatile int a 10; volatile boolean flag true; 状态标志&#xff0c;判断业务是否结束 作为一个布尔状态标志&#xff0c;用于指示发生了一个重要的一次…

案例研究|腾讯音乐娱乐集团与JumpServer共探安全运维审计解决方案

近年来&#xff0c;得益于人民消费水平的提升以及版权意识的加强&#xff0c;用户付费意愿和在线用户数量持续增长&#xff0c;中国在线音乐市场呈现出稳定增长的发展态势。随着腾讯音乐于2018年12月上市&#xff0c;进一步推动了中国在线音乐市场的发展。 腾讯音乐娱乐集团&a…

LazyVim: 将 Neovim 升级为完整 IDE | 开源日报 No.67

curl/curl Stars: 31.5k License: NOASSERTION Curl 是一个命令行工具&#xff0c;用于通过 URL 语法传输数据。 核心优势和关键特点包括&#xff1a; 可在命令行中方便地进行数据传输支持多种协议 (HTTP、FTP 等)提供丰富的选项和参数来满足不同需求 kubernetes/ingress-n…