Shortest path algorithm - Dijkstra (Dijkstra)

ALGORITHM


  • Setting G = (V, E) be a directed graph with weight
  • The set V of vertices in the graph into two groups
  • The first group (represented as a set of vertices with S shortest path has been determined, initially only a source S, each obtained after a shortest path, it will be added to the set S, until all the vertices are added to the S the algorithm is over)
  • The second group (represented by U) is the set of vertices of the shortest path remaining undetermined
  • Ascending order of the shortest path length are sequentially added to the apex of the second group in the S
  • During the addition, the total holding from the source S v to the vertices of the shortest path length is not greater than the length of any shortest path from the source vertex point v to the U
  • In addition, each vertex corresponding to a distance from the vertex S of this is that the length of the shortest path from the source vertex v
  • Distance from the apex of the U, this is from the source vertex point v includes only vertex S is the shortest path length of the current vertex intermediate

 

C language pseudocode described Dijkstra


void ShortestPath_DIJ( MGraph G, int v0, PathMatrix &P, ShortPathTable &D){
    // 用Dijkstra算法求有向网G的v0顶点到其余顶点v的最短路径P[v]及其带权长度D[v]。
    // 若P[v][w]为TRUE,则w是从v0到v当前求得最短路径上的顶点。
    // final[v]为TRUE当且仅当v∈S,即已经求得从v0到v的最短路径。
    for (v = 0; v < G.vexnum; ++ v){
        final[v] = FALSE; D[v] = G.arcs[v0][v];
        for (w = 0; w < G.vexnum; ++ ww) P[v][w] = FALSE;// 设空路径
        if (D[v] < INFINITY) {P[v][v0] = TRUE; P[v][v] = TRUE;}
    }// for
    D[v0] = 0; final[v0] = TRUE;// 初始化,v0顶点属于S集
    // 开始主循环,每次求得v0到某个v顶点的最短路径,并加v到S集
    for (i = 1; i < G.vexnum; ++ i) {// 其余G.vexnum - 1个顶点
        min = INFINITY;// 当前所知离v0顶点的最近距离
        for (w = 0; w < G.vexnum; ++ w)
            if(!final[w])// w顶点在V-S中
                if(D[w] < min) {v = w; min = D[w];}// w顶点离v0顶点更近
        final[v] = TRUE;// 离v0顶点最近的v加入S集
        for(w = 0; w < G.vexnum; ++ w)// 更新当前最短路径及距离
            if(!final[w] && (min + G.arcs[v][w] < D[w])){// 修改D[w]和P[w],x∈V-S
                D[w] = min + G.arcs[v][w];
                P[w] = P[v]; P[w][w] = TRUE;// P[w] = P[v] + P[w]
            }// if
    }// for

}// ShortestPath_DIJ

 

C language programming algorithm described


void Dijkstra(float cost[][n], int v){
    // 求源点v到其余顶点的最短路径及其长度,cost为有向网的带权邻接矩阵
    // 设max值为32767,代表一个很大的数
    v1 = v - 1;
    for(i = 0; i < n; i ++){
        dist[i] = cost[v1][i];// 初始化dist
        if(dist[i] < max) pre[i] = v; else pre[i] = 0;
    }
    pre[v1] = 0;
    for(i = 0; i < n; i ++) S[i] = 0;// 第一组开始为空集
    S[v1] = 1;// 源点v并入第一组
    for(i = 0; i < n; i ++){// 扩充第一组
        min = max;
        for(j = 0; j < n; j ++)
            if(!S[j] && (dist[j] < min)) {min = dist[j]; k = j;}
        S[k] = 1;// 将k+1加入第一组
        for(j = 0; j < n; j ++)
            if(!S[j] && (dist[j] > dist[k] + cost[k][j])){// 修正第二组各顶点的距离值
                disk[j] = disk[k] + cost[k][j]; pre[j] = k + 1;// k + 1是j + 1的前趋
            }// 所有顶点均已扩充到S中
        for(j = 0; j < n; j ++){// 打印结果
            printf("%f\n%d", dist[i], i + 1);
            p = pre[i];
            while(p != 0){// 继续找前趋顶点
                printf("<--%d", p);
                p = pre[p - 1];
            }
        }
    }// Dijkstra

}
Published 28 original articles · won praise 3 · Views 5284

Guess you like

Origin blog.csdn.net/u012632105/article/details/105093196