bfs广度优先搜索算法_图的广度优先搜索(BFS)
bfs廣度優(yōu)先搜索算法
What you will learn?
您將學(xué)到什么?
How to implement Breath first search of a graph?
如何實(shí)現(xiàn)圖的呼吸優(yōu)先搜索?
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.
.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}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.
廣度優(yōu)先搜索是一個(gè)逐層的頂點(diǎn)遍歷過(guò)程。 像一棵樹(shù)一樣,所有圖都具有頂點(diǎn),但是圖卻具有循環(huán),因此為了避免出現(xiàn)相同的頂點(diǎn),我們選擇了BFS
算法:
為了實(shí)現(xiàn)BFS,我們使用隊(duì)列和數(shù)組數(shù)據(jù)結(jié)構(gòu)。
該算法有兩種情況:
每當(dāng)我們?cè)L問(wèn)頂點(diǎn)時(shí),都會(huì)將其標(biāo)記為已訪(fǎng)問(wèn),并將其相鄰的未訪(fǎng)問(wèn)頂點(diǎn)推入隊(duì)列,然后從隊(duì)列中彈出當(dāng)前頂點(diǎn)。
如果訪(fǎng)問(wèn)了所有相鄰的頂點(diǎn),則僅從隊(duì)列中彈出當(dāng)前頂點(diǎn)。
考慮一下這張圖,
根據(jù)我們的算法,遍歷繼續(xù)像
因此,所有頂點(diǎn)都將被訪(fǎng)問(wèn),然后僅執(zhí)行彈出操作,并且隊(duì)列最終將為空。
.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}C ++實(shí)現(xiàn):
# 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 ; }輸出量
最佳面試編碼問(wèn)題/挑戰(zhàn)
游程編碼(字符串中字母的查找/打印頻率)
以線(xiàn)性時(shí)間復(fù)雜度對(duì)0、1和2的數(shù)組進(jìn)行排序
檢查字謎(檢查兩個(gè)字符串是否是字謎)
相對(duì)排序算法
查找給定總和的子數(shù)組
在給定總和K的二叉樹(shù)中找到級(jí)別
檢查二叉樹(shù)是否為BST(二叉搜索樹(shù))
1 [0] 1個(gè)樣式計(jì)數(shù)
大寫(xiě)一行中每個(gè)單詞的第一個(gè)和最后一個(gè)字母
打印二叉樹(shù)的垂直和
打印二叉樹(shù)的邊界和
反轉(zhuǎn)單個(gè)鏈表
解決主要算法問(wèn)題的貪婪策略
工作排序問(wèn)題
根到葉的路徑總和
矩陣中的出口點(diǎn)
在鏈表中查找循環(huán)長(zhǎng)度
一流的禮帽
打印所有沒(méi)有兄弟的節(jié)點(diǎn)
轉(zhuǎn)換為求和樹(shù)
最短的源到目標(biāo)路徑
評(píng)論和討論
廣告:您是博主嗎? 加入我們的Blogging論壇 。
請(qǐng)啟用JavaScript以查看由Disqus提供的評(píng)論。
翻譯自: https://www.includehelp.com/data-structure-tutorial/breadth-first-search-bfs-of-a-graph.aspx
bfs廣度優(yōu)先搜索算法
總結(jié)
以上是生活随笔為你收集整理的bfs广度优先搜索算法_图的广度优先搜索(BFS)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 国庆礼包到底几号下架,为什么官网显示是1
- 下一篇: 弗林的计算机体系结构分类