Graph theory - the shortest path algorithm

For the most shorted temporarily mastered four, but I feel dijkstra complexity can be used;

1 floyd algorithm:

Is the triple cycle of violence, with each point as a transit point, each time through all the points, see if you can update through this transit point shortest path;

Advantages: n <In this way, with FIG adjacency matrix memory, any two points of rectifiable 200 is shortest; And good writing;

Disadvantages: complexity is too high, O (n ^ 3) complex, too much unnecessary calculation;

void Floyd () {
     // DIS array i to j represents the shortest path 
    for ( int K = 0 ; K <n-; K ++) // focus must be on the outside 
    for ( int i = 0 ; i <n-; i ++ )
     for ( int J = 0 ; J <n-; J ++ )
    December [i] [j] = min (DIS [i] [j], disincentives [i] [k] + December [k] [j]);

}
View Code

 

 

 2 bellman-floyd algorithm:

Whether it is possible to solve the neighbor update shortest path single-source shortest path is loop n times, each time checking each edge to see

Advantages: n <1e7 this method can be considered, using an array of storage structures to FIG, which is then calculated in parallel, are how to engage the line cycle,

Disadvantages: complexity is still quite high, each point O (n * m) complexity;

void Bellman () {
     int s, t; // s as a starting point, t is the end point 
    for ( int I = . 1 ; I <= n-; I ++) DIS [I] = INF;
     // DIS represents all points to an array of s shortest 
    DIS [S] = 0 ;
     for ( int K = . 1 ; K <= n-; K ++ )
     for ( int I = 0 ; I <CNT; I ++ ) {
     int X = E [I] .u, Y = E [I] .v;
     IF (DIS [X]> DIS [Y] + E [I] .W) { // update distance 
    DIS [X] = DIS [Y] + E [I] .W;
        }
    }
    cout<<dis[t];
}
View Code

 

3 SPFA:

Disadvantages: unstable point,

Algorithms: Search and wide like;

Learn and then update;

 4    dijkstra:

Algorithm: based on greedy thinking, that is, after each update, the shortest path to the final point must be the shortest path because the route through to the other point must be larger than the distance; the first iteration;

Advantages: Consider FIG adjacency table memory complexity is acceptable, and stable; the single-source shortest path problem solving, complexity of O (m * log (n));

int Dijkstra () {
     int S, t; // S represents a start point, t represents the end of 
    for ( int I = 0 ; I <= MAXN; I ++) DIS [I] = INF, DONE [I] = 0 ;
    DIS [s] = 0 ;
     // DIS Each point represents the shortest s, done whether the shortest recording array 
    The priority_queue <Node> Q;
    Q.push(node(s,0));
    while(!Q.empty()){
    node u=Q.top();
    Q.pop();
    if(done[u.id])continue;
    done[u.id]=1;
    for(int i=0;i<e[u.id].size();i++){
    edge y=e[u.id][i];
    if(done[y.v])continue;
    if(dis[y.v]>y.w+u.dis){
    DIS [YV] = + Y. W. u.dis; // Update the shortest neighbor 
    }
    Q.push (Node (YV, DIS [YV])); // the each neighbor node expansion come 
    }

    }
    return dis[t];
}
View Code

 

 

Forfeit problem circle, learn to add;

Guess you like

Origin www.cnblogs.com/littlerita/p/12343890.html