2016-07-26 61 views
0

所以这是算法即时通讯使用,我想知道在哪个级别的深度我使用BFS如何知道我在使用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); 
    } 
} 

我能做些什么?

+0

沿着一个额外的'depth'参数只是传递。在初次调用'bfs'时,传递0。在递归调用中,传递'depth + 1'。因此:'void bfs(int n,int depth){... bfs(n,depth + 1); '' –

回答

1

为了知道你现在的深度,你应该考虑附加阵列深度深度大小等于图中顶点的数量,并包含每个顶点的深度,从您开始BFS的顶点开始计算。当通过父母的孩子的穿越,你应该把 深度 [儿童] = 深度 [家长] + 1