Figure - the shortest path problem

  From a source point to the rest of the vertices of the shortest path

Dijkstra (Dijkstra) algorithm

Dijkstra algorithm is a ascending order according to path length results in the shortest path. (Negative circle invalid algorithms)

Algorithm Description:

  1. Suppose represented by adjacency matrix arcs weighted weighted directed graph, arcs [i] [j] is the weight of the arc <vi, vj>; if <vi, vj> does not exist, arcs [i] [j ] is ∞, S is the set of vertices v0 from the end of the shortest path starting has been found, initially empty.
  2. Selecting the shortest path to the VS vertex v0 constituted <v0, vj>, vj added and S.
  3. Since vj added to S, the shortest path changes, modifications v0 shortest possible path to the VS
  4. Repeat steps 2,3 n-1 times, the figure obtained from v0 to the shortest path remaining vertices sequentially increasing sequence

The algorithm is finally obtained an incremental shortest path sequence, each cycle is to find a shortest path remaining, and then update about the rest of the shortest path, like a selection sort, like, every time behind the choice of the smallest into a front end to an ordered sequence, which is from the source to each of the shortest path in FIG vertex.

/ * 
    G.arcs: weighted adjacency matrix 
    vo: source 
    P: matrix storage path 
    D: storing vi v0 to the shortest path length 
* / 
void shortestPath_DLJ (MGraph G, int v0, P & PathMatrix, ShortPathTable & D) {
     / * 
        find the shortest path from vertex v0 remaining vertex v P [v], and with the right length D [v] 
        P [v] [w] w is true that the current is determined from v0 to v vertex on the shortest path 
        
        Final [ v] it is true that the shortest path has been obtained from v0 to v, and 
        about to vertex v is added to the concentrated S 
    * /  
    // initialization 
    for (v = 0 ; v <G.vexnum; v ++ ) { 
        Final [v] = to false ; 
        
        // If there arc v0 to v, then assign D [v] of its weight, otherwise INFINITY
         //G.arcs no vertices in two arcs, with values of INFINITY 
        D [V] = G.arcs [V0] [V]; 
        
        // blanking path 
        for (W = 0 ; W <G.vexnum; W ++) P [ v] [W] = flase; 
        
        // If D [v] value is less than INFINTY, there arc described v0 to v, v0, v naturally path 
        IF (D [v] <INFINTY) {P [v] [v0] = to true ; P [v] [v] = to true ;} 
    } 
    
    // initialization, v0 set belonging to S 
    D [v0] = 0 ; Final [v0] = to true ; 
    
    // start the main loop, each determined v0 to v the shortest path vertex v is added to the set S
     // rest G.vexnum - 1 vertices 
    for (I = . 1 ; I <G.vexnum; I ++ ) {
         // this minimum distance from v0
        = min INFINTY; 
        
        for (W = 0 ; W <G.vexnum; W ++ ) 
             // vertex in VS 
            IF {V = W; min = (Final [V]!) D [W];} 
        
        // find, v is added to the set S 
        Final [v] = to true ; 
        
        / *     update from the shortest path and 
            because v is added to the set S, a new path is generated with a vertex v 
            redetermination shortest path, the shortest path for the next find 
        * /  
        for (W = 0 ; W <G.vexnum; W ++ )
             IF (! Final [W] && G.arcs min + [V] [W] < D [W]) { 
                D [W] = min + G.arcs [ V] [W];
                 //The path P v0 to v [v] is assigned to P [W] 
                 // W also on the path 
                P [W] = P [v]; 
                P [W] [W] = to true ; 
            } 
    }     
}

to be continued. . .

 

Guess you like

Origin www.cnblogs.com/haiyuexiaozu/p/11634853.html