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.
广度优先搜索是一个逐层的顶点遍历过程。 像一棵树一样,所有图都具有顶点,但是图却具有循环,因此为了避免出现相同的顶点,我们选择了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论坛 。
翻译自: https://www.includehelp.com/data-structure-tutorial/breadth-first-search-bfs-of-a-graph.aspx
bfs广度优先搜索算法