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:

  1. 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.

  2. If all the adjacent vertices are visited then only pop the current vertex from the queue.

Consider this graph,

BFS Graph Example 1

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.


广度优先搜索是一个逐层的顶点遍历过程。 像一棵树一样,所有图都具有顶点,但是图却具有循环,因此为了避免出现相同的顶点,我们选择了BFS

算法:

为了实现BFS,我们使用队列和数组数据结构。

该算法有两种情况:

  1. 每当我们访问顶点时,都会将其标记为已访问,并将其相邻的未访问顶点推入队列,然后从队列中弹出当前顶点。

  2. 如果访问了所有相邻的顶点,则仅从队列中弹出当前顶点。

考虑一下这张图,

BFS图示例1

根据我们的算法,遍历继续像

因此,所有顶点都将被访问,然后仅执行弹出操作,并且队列最终将为空。


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 ;
}

输出量





评论和讨论

广告:您是博主吗? 加入我们的Blogging论坛


翻译自: https://www.includehelp.com/data-structure-tutorial/breadth-first-search-bfs-of-a-graph.aspx

bfs广度优先搜索算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值