我怎么知道在哪个级别的搜索我使用BFS(广度优先搜索)?

时间:2016-07-26 13:57:27

标签: c++ graph breadth-first-search graph-traversal

所以这是我使用的算法,我想知道我使用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); 
    }
 }

我能做什么?

1 个答案:

答案 0 :(得分:1)

为了了解您现在的深度,您应该考虑额外的数组深度 depth 大小等于图中顶点的数量,并包含每个顶点的深度,从您启动BFS的顶点开始计算。当你穿过父母的孩子时,你应该穿上 深度 [孩子] = 深度 [父母] + 1