min_path.cpp
/**

 * @brief MinPath::computeShortestPath

 * @param v0, the start vertex

 * @param pre, array to store the current vertex's pirror vertex

 * @param dist, array to store the current vertex's weight to the start vertex

 */
@brief MinPath::computeShortestPath
@param v0, the start vertex

@param pre, array to store the current vertex's pirror vertex

@param dist, array to store the current vertex's weight to the start vertex
@brief MinPath :: computeShortestPath @param v0 the start vertex @param pre array to store the current vertex ' s pirror vertex @param dist array to store the current vertex ' s weight to the start vertex
void MinPath::computeShortestPath(int v0, int *pre, int *dist) {
    bool final[MAXVERTEXNUM];
    int i;
    int w;
    int v;
    int current_tmp_min;
    for ( v=0; v<=M_vertexNum-1; v++ ) {
        final[v] = false;
        dist[v] = M_edges[v0][v];       
        pre[v] = -1;      
        if ( dist[v] < MAXWEIGHT )  
            pre[v] = v0;
    }
    dist[v0] = 0;
    final[v0] = true;       
    for ( i=1; i<M_vertexNum+1; i++ ) {
        v = -1;     
        current_tmp_min = MAXWEIGHT;    
        for ( w=0; w<M_vertexNum; w++ ) {
            if ( final[w] == false && dist[w] < current_tmp_min ) {  
                v = w;                          
                current_tmp_min = dist[v];
            }
        }
        if ( v == -1 )                      
            break;
        final[v] = true;		
        当前最短路径及距离(V 作为中间点)
        for ( w=0; w<M_vertexNum; w++ ) {
            if ( final[w] == false && (current_tmp_min+(M_edges[v][w]) < dist[w]) )  
            {
                dist[w] = current_tmp_min + (M_edges[v][w]);     最短路径长度, 此时dist[w]为 "确定值"
                pre[w] = v;     
            }
        }
    }   
    return;
}
void MinPath :: computeShortestPath ( int v0 , int * pre , int * dist ) { bool final [ MAXVERTEXNUM ] ; int i ; int w ; int v ; int current_tmp_min ; for ( v = 0 ; v <= M_vertexNum - 1 ; v ++ ) { final [ v ] = false ; dist [ v ] = M_edges [ v0 ] [ v ] ; pre [ v ] = -1 ; if ( dist [ v ] < MAXWEIGHT ) pre [ v ] = v0 ; } dist [ v0 ] = 0 ; final [ v0 ] = true ; for ( i = 1 ; i < M_vertexNum + 1 ; i ++ ) { v = -1 ; current_tmp_min = MAXWEIGHT ; for ( w = 0 ; w < M_vertexNum ; w ++ ) { if ( final [ w ] == false && dist [ w ] < current_tmp_min ) { v = w ; current_tmp_min = dist [ v ] ; } } if ( v == -1 ) break ; final [ v ] = true ; for ( w = 0 ; w < M_vertexNum ; w ++ ) { if ( final [ w ] == false && ( current_tmp_min + ( M_edges [ v ] [ w ] ) < dist [ w ] ) ) { dist [ w ] = current_tmp_min + ( M_edges [ v ] [ w ] ) ; pre [ w ] = v ; } } } return ; }
@brief MinPath::computeShortestPath
 @param v0, the start vertex
@brief MinPath :: computeShortestPath @param v0 the start vertex
// 初始化 // 如果不直接连通，dist[v]就是MAXWEIGHT,否则是相应的权值, 我称此时dist[v] 为 "估计值" // 所有的顶点都无前驱，置pre数组为 -1 // v 到 v0 (直接)连通 // 开始时V0属于Ｓ集合，默认已经找到最短路径 // (final[i] = true 相当于把i 加入S集合) // main loop // 寻找其余6个节点 // ---> if (v==-1) // 当前距V0路径最短的权值 ,初始化为不连通状态 // 寻找当前离V0最近的顶点 V // 第一次main loop 下, current_tmp_min 变化情况: dist[1] -> dist[2] // w还没找到最短路径，并且d[w]比当前min 还要小 // 更新 // 所有与V0相通的点都找到了最短路径(不满足line 108 的if )，则退出main loop // 把V 加入S集合 // 更新当前最短路径及距离(V 作为中间点) // v0 -> v -> w 比 v0 -> w 短 // 第一次main loop, dist[1] 更新成 4, 2 作为1 的前驱 // 更新最短路径长度, 此时dist[w]为 "确定值" // v作为w的前驱顶点 //end main loop