如何更改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");
}
}
请问有人可以帮助我了解它的工作原理吗?