繁体   English   中英

我如何知道我在使用BFS(宽度优先搜索)的搜索级别?

[英]how do i know on which level of search am i using BFS (Breadth-first search)?

所以这是我正在使用的算法,我想知道我在使用BFS的深度级别

void bfs(int n)
 {

   vis[n]=1; //marks n visited
   d=0;
   while(!adj[n].empty()) //adj is the array containing the adjacency lists
   {if( !(vis[adj[n].front()]))
    {
      q.push(adj[n].front());  //q is the queue 
    }
    adj[n].pop_front();
   }
 if(!q.empty()){
   n=q.front();
   cout<<n<< "->";
   q.pop();
   bfs(n); 
    }
 }

我能做什么?

为了知道您现在的深度,您应该考虑添加额外的array depth 深度大小等于图形中的顶点数,并且包含每个顶点的深度(从您开始BFS的顶点开始计算)。 遍历父级的子级时,应将深度 [子级] = 深度 [父级] + 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM