bfs广度优先搜索算法_图的广度优先搜索(BFS)
bfs广度优先搜索算法
What you will learn?
您将学到什么?
How to implement Breath first search of a graph?
如何实现图的呼吸优先搜索?
Breadth First Search is a level-wise vertex traversal process. Like a tree all the graphs have vertex but graphs have cycle so in searching to avoid the coming of the same vertex we prefer BFS
Algorithm:
To implement the BFS we use queue and array data structure.
There are two cases in the algorithm:
Whenever we visit a vertex we mark it visited and push its adjacent non-visited vertices into the queue and pop the current vertex from the queue.
If all the adjacent vertices are visited then only pop the current vertex from the queue.
Consider this graph,
According to our algorithm, the traversal continues like,
Hence all the vertices are visited then only pop operation is performed and queue will be empty finally.
C++ Implementation:
#include <bits/stdc++.h>
using namespace std;
// Make a pair between vertex x and vertex y
void addedge(list<int> *ls,int x,int y){ls[x].push_back(y);
ls[y].push_back(x);
return;
}
//Breath First Search of a Graph
void BFS(list<int>*ls,int num,int x){bool *visit= new bool[num];
for(int i=0;i<num;i++){visit[i]=false;
}
queue<int> q;
q.push(x);
while(!q.empty()){int s=q.front();
q.pop();
if(!visit[s]){visit[s]=true;
cout<<s<<" ";
list<int>::iterator it;
for(it=ls[s].begin();it!=ls[s].end();it++){q.push(*it);
}
}
}
}
// Print the Adjacency List
void print(list<int> *ls,int num){list<int>::iterator it;
for(int i=0;i<6;i++){cout<<i<<"-->";
for(it=ls[i].begin();it!=ls[i].end();it++){cout<<*it<<"-->";
}
cout<<endl;
}
}
int main(){int num=6;
cout<<"Enter the no. of vertices : 6\n";
list<int> *ls=new list<int>[num];
addedge(ls,0,2);
addedge(ls,2,3);
addedge(ls,3,4);
addedge(ls,4,5);
addedge(ls,2,5);
addedge(ls,1,4);
addedge(ls,3,0);
cout<<"Print of adjacency list:"<<endl;
print(ls,6);
cout<<"BFS"<<endl;
BFS(ls,6,0);
return 0;
}
Output
TOP Interview Coding Problems/Challenges
Run-length encoding (find/print frequency of letters in a string)
Sort an array of 0's, 1's and 2's in linear time complexity
Checking Anagrams (check whether two string is anagrams or not)
Relative sorting algorithm
Finding subarray with given sum
Find the level in a binary tree with given sum K
Check whether a Binary Tree is BST (Binary Search Tree) or not
1[0]1 Pattern Count
Capitalize first and last letter of each word in a line
Print vertical sum of a binary tree
Print Boundary Sum of a Binary Tree
Reverse a single linked list
Greedy Strategy to solve major algorithm problems
Job sequencing problem
Root to leaf Path Sum
Exit Point in a Matrix
Find length of loop in a linked list
Toppers of Class
Print All Nodes that don't have Sibling
Transform to Sum Tree
Shortest Source to Destination Path
Comments and Discussions
Ad: Are you a blogger? Join our Blogging forum.
Please enable JavaScript to view the comments powered by Disqus.
广度优先搜索是一个逐层的顶点遍历过程。 像一棵树一样,所有图都具有顶点,但是图却具有循环,因此为了避免出现相同的顶点,我们选择了BFS
算法:
为了实现BFS,我们使用队列和数组数据结构。
该算法有两种情况:
每当我们访问顶点时,都会将其标记为已访问,并将其相邻的未访问顶点推入队列,然后从队列中弹出当前顶点。
如果访问了所有相邻的顶点,则仅从队列中弹出当前顶点。
考虑一下这张图,
根据我们的算法,遍历继续像
因此,所有顶点都将被访问,然后仅执行弹出操作,并且队列最终将为空。
C ++实现:
# include < bits/stdc++.h >
using namespace std ;
// Make a pair between vertex x and vertex y
void addedge ( list < int > * ls , int x , int y ) {
ls [ x ] . push_back ( y ) ;
ls [ y ] . push_back ( x ) ;
return ;
}
//Breath First Search of a Graph
void BFS ( list < int > * ls , int num , int x ) {
bool * visit = new bool [ num ] ;
for ( int i = 0 ; i < num ; i + + ) {
visit [ i ] = false ;
}
queue < int > q ;
q . push ( x ) ;
while ( ! q . empty ( ) ) {
int s = q . front ( ) ;
q . pop ( ) ;
if ( ! visit [ s ] ) {
visit [ s ] = true ;
cout < < s < < " " ;
list < int > :: iterator it ;
for ( it = ls [ s ] . begin ( ) ; it ! = ls [ s ] . end ( ) ; it + + ) {
q . push ( * it ) ;
}
}
}
}
// Print the Adjacency List
void print ( list < int > * ls , int num ) {
list < int > :: iterator it ;
for ( int i = 0 ; i < 6 ; i + + ) {
cout < < i < < " --> " ;
for ( it = ls [ i ] . begin ( ) ; it ! = ls [ i ] . end ( ) ; it + + ) {
cout < < * it < < " --> " ;
}
cout < < endl ;
}
}
int main ( ) {
int num = 6 ;
cout < < " Enter the no. of vertices : 6 \n " ;
list < int > * ls = new list < int > [ num ] ;
addedge ( ls , 0 , 2 ) ;
addedge ( ls , 2 , 3 ) ;
addedge ( ls , 3 , 4 ) ;
addedge ( ls , 4 , 5 ) ;
addedge ( ls , 2 , 5 ) ;
addedge ( ls , 1 , 4 ) ;
addedge ( ls , 3 , 0 ) ;
cout < < " Print of adjacency list: " < < endl ;
print ( ls , 6 ) ;
cout < < " BFS " < < endl ;
BFS ( ls , 6 , 0 ) ;
return 0 ;
}
输出量
最佳面试编码问题/挑战
游程编码(字符串中字母的查找/打印频率)
以线性时间复杂度对0、1和2的数组进行排序
检查字谜(检查两个字符串是否是字谜)
相对排序算法
查找给定总和的子数组
在给定总和K的二叉树中找到级别
检查二叉树是否为BST(二叉搜索树)
1 [0] 1个样式计数
大写一行中每个单词的第一个和最后一个字母
打印二叉树的垂直和
打印二叉树的边界和
反转单个链表
解决主要算法问题的贪婪策略
工作排序问题
根到叶的路径总和
矩阵中的出口点
在链表中查找循环长度
一流的礼帽
打印所有没有兄弟的节点
转换为求和树
最短的源到目标路径
评论和讨论
广告:您是博主吗? 加入我们的Blogging论坛 。
请启用JavaScript以查看由Disqus提供的评论。
翻译自: https://www.includehelp.com/data-structure-tutorial/breadth-first-search-bfs-of-a-graph.aspx
bfs广度优先搜索算法
bfs广度优先搜索算法_图的广度优先搜索(BFS)相关推荐
- 深度优先遍历和广度优先遍历_图与深度优先搜索和广度优先搜索
什么是图? 图是一种复杂的非线性表结构.由若干给定的点一级任意两点间的连线所构成.图通常用来描述事物之间的特定关系, 代表的就是事物, 线就是事物之间所具有的关系.例如社交网络就是一种典型的图关系, ...
- 图的深度优先遍历和广度优先遍历_图的深度优先遍历(DFS)与广度优先遍历(BFS)的c语言实现...
头文件 #pragma warning( disable : 4996)#pragma once#ifndef _GRAPH_H_#define _GRAPH_H_ #define MAX_VERTE ...
- 有向图的广度优先遍历_图的两种遍历方式
1 引言 遍历是指从某个节点出发,按照一定的的搜索路线,依次访问对数据结构中的全部节点,且每个节点仅访问一次. 在二叉树基础中,介绍了对于树的遍历.树的遍历是指从根节点出发,按照一定的访问规则,依次访 ...
- 广度优先搜索算法带图详解
1.前言 广度优先搜索https://so.csdn.net/so/search?q=%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2&am ...
- 广度优先搜索算法(Breath-first Search)是如何搜索一张图的?
算法导论(MIT 6.006 第13讲) 什么是图搜索? 搜索可以理解为探索,给定一个图上的点S和A,需要找到从S到A的一个路径 图的基础概念 一个图用 G=(V,E) 表示,V是顶点的集合,E是边的 ...
- dfs深度优先搜索_图的深度优先搜索(DFS)
dfs深度优先搜索 Depth First Search (DFS) is an algorithm that searches a graph/tree, in a depth-wise manne ...
- 广度优先搜索算法(BFS)详解
参考:https://www.cnblogs.com/tianqizhi/p/9914539.html 广度优先搜索算法(又称宽度优先搜索)是最简便的图的搜索算法之一,这一算法也是很多重要的图的算法的 ...
- 广搜(广度优先搜索BFS)
广度优先搜索 广度优先搜索算法(又称宽度优先搜索)是最简便的图的搜索算法之一,其别名又叫BFS,属于一种盲目搜寻法,目的是系统地展开并检查图中的所有节点,以找寻结果.换句话说,它并不考虑结果的可能位置 ...
- 广度优先搜索BFS进阶(一):多源BFS、优先队列BFS、双端队列BFS
一.多源BFS 在上一篇博客:广度优先搜索BFS基础中,我们接触到的BFS均是单起点(单源)的,但是对于某一些问题,其有多个起点,此类问题我们称为多源BFS问题.先思考下面一道例题: 1.腐烂的橘子 ...
最新文章
- HTML复选框和提交按钮组合设置
- vue 上传图片 input=file
- python3精要(8)-对象,变量,引用
- qpython怎么用matplotlib_将matplotlib绘图嵌入pyqt的方法示例
- X-lab 开放实验室开源创新的故事
- 搭建VS2008+OpenCV2.1开发环境
- oracle修改字段的默认,oracle系统默认的账号ORACLE修改表字段的数据类型
- CentOS下安装Tomcat并配置JRE
- 笔记——常用网站总结
- Windows使用tensorboard的一点小心得
- Hive下载安装及配置
- 图解最短路径之迪杰斯特拉算法(Java实现)
- window.location与window.open()的区别
- 音标、音节、音素、音符
- 领航跟随型编队(十四)室内定位技术概述
- 用RunASDate解决SAS 9.4许可证过期的问题
- Appid + appSecret + code 到微信方服务器 获取 session_key openid 并授权登录
- 【软件网每日新闻播报│第9-25期】
- 在Kotlin中有多个选择的交错recyclerview
- 【JS】1037- 面试前必备的 JavaScript 基础知识梳理总结