广度优先搜索求单源最短路径

//广度优先搜索实现求单源最短路径
void BFSMINDISTANCE(AdjList G, int u) {
    InitQueue(&Q);
    for (int i = 0; i < G.vexnum; i++) {
        visited[i] = false;
        d[i] = INFINITY;
        path[i] = -1;
    }
    visit(u);
    visited[u] = true;
    EnterQueue(&Q, u);
    while (!IsEmpty(Q)) {
        DeQueue(&Q, &u);
        for (w = FirstNeighbor(G, u); w >= 0; w = NextNeighbor(G, u, w)) {
            if (!visited[w])
            {
                visited[w] = true;
                d[w] = d[u] + 1;
                path[w] = u;
                EnterQueue(&Q, w);
            }
        }
    }
}

 

posted @ 2021-08-21 17:34  #Lorraine#  阅读(135)  评论(0编辑  收藏  举报