深度广度优先搜索

void DFS( int x )
{
    visit[x] = true;
    for(int i=0; i<k; i++)
    {
        if(!visit[i] && G[x][i])
            DFS(i);
    }
}
queue <int> Q;
void BFS ( int s )
{
    Visit[s] = true;
    Q.push(s);
    while( !Q.empty() )
    {
        int V = Q.front();
        Q.pop();
        for( W=0; W<n; W++ )
        {
            if( !Visist[W] && G[V][W] )
            {
                Visit[W] = true;
                Q.push( W );
            }
        }
    }
}
void BFS( int n )
{
    /* for(int i = 0; i < m; i++)
    {
        for(node *p = l[i] -> next; p; p = p -> next)
        {
            for(node *q = p -> next; q; q = q -> next)
            {
                if(p -> data > q -> data)
                {
                    int tmp = p -> data;
                    p -> data = q -> data;
                    q -> data = tmp;
                }
            }
        }
    }*/
    Q.push(n);
    visit[n] = true;
    while ( !Q.empty() )
    {
        now = Q.front();
        //cout << now << " ";
        cun[sum++] = now;
        Q.pop();
        p = l[now]->next;

        while(p)
        {
            if(!visit[p->data])
            {
                visit[p->data] = true;
                Q.push(p->data);
            }
            p = p->next;
        }
    }
}

 

posted @ 2018-07-25 10:13  Gaojinman  阅读(119)  评论(0)    收藏  举报