如何使用BFS在C中的2个给定节点之间找到路径?

时间:2019-05-27 15:01:45

标签: c graph path

如何更改bfs算法以找到C语言中最短的路径(最小边数)?我已经尝试过自己做,但是比我想的要难。

void BFS(struct Graph *G,struct queue *q,int layer)
{
    while(q->front)       
    {
        while(q->front && (G->adjList[q->front->v]->layer == layer))   
        {
            int v = dQ(q);
            printf("%d ",v);   //afisare
            struct node *head = G->adjList[v]->next;
            while(head)
            {
                if(G->adjList[head->v]->marked)
                {
                    head = head->next;
                    continue;
                }
                G->adjList[head->v]->layer = layer + 1;
                G->adjList[head->v]->marked = 1;
                insertQ(q,head->v);
                head = head->next;
            }
        }
        layer++;
        printf("\n");
    }
}

请问有人可以帮助我了解它的工作原理吗?

0 个答案:

没有答案