Dijkstra for the single-source shortest path algorithm

Algorithm Description:

    Dijkstra solves the single-source shortest path problem on a directed graph with weights. The algorithm requires that the weights of all edges are non-negative.

    The key information that the algorithm maintains during operation is a set of node sets S. The shortest path from the source point s to each node in the set has been found.

    The algorithm repeatedly selects the node u with the shortest path estimate from the node set VS, adds u to the set S, and then relaxes all edges starting from u.

 

Algorithm implementation:

The dist array records the shortest distance from the source point to the node in the figure. The initial distance is infinite, and set dist [start] = 0 for the algorithm to start.

When the node u joins the set S, the precursor of u in this path must already be in the set S.

int edge [N] [N]; 

int dist [N]; // the shortest distance 
int bset [N]; // whether the mark is already in the set S 
int pre [N]; // record the precursor 

const int INF = 999999999; 

void Dijkstra (start) 
{ 
    fill (dist, dist + N, INF); 
    fill (bset, bset + N, 0); 

    dist [start] = 0; 

    for (int i = 0; i <N; i ++) 
    { 
        int min = INF; 
        int u = -1; 

        for (int j = 0; j <N; j ++) 
        { 
            if (bset [j] == 0 && dist [j] <min) 
            { 
                u = j; 
                min = dist [j ]; 
            } 
        } 

        if (u == -1) continue; 
        bset [u] = 1;

        for (int j = 0; j <N; j ++) 
        { 
            if (bset [j] == 0) 
            { 
// Relaxation operation if (dist [u] + edge [u] [j] <dist [j]) { dist [j] = dist [u] + edge [u] [j]; pre [j] = u; } else if (dist [u] + edge [u] [j] == dist [j]) { / / Explain that there are multiple shortest paths to this node } } } } }

 

Proof of algorithm correctness:

    In the weighted directed graph G = (V, E), S is initially an empty set.

    We need to show that: for at each join node of the set S is u, s the distance u to the shortest distance.

    Let ud be the distance from s to u, and δ (s, u) be the shortest distance from s to u, that is to prove:

         On at each join node u set S is , ud = δ (s, u )

    Proof of counter-evidence is used here:

        Hypothesis: node u is the first node added to set S such that ud ≠ δ (s, u)

        The source node s is the first node added to the set S, and sd = δ (s, s) = 0, the node u must be different from the node s, that is u ≠ s.

        According to the algorithm, when u is added to S, there must be a path from node s to u, and the precursor node of u is in the set S, which is denoted as P1.

        By assumption understood, there is also a most from u to s of the short path, this path is not necessarily a precursor of u set S (otherwise relaxation process will find the path), the path is referred to as P2.

        At present, u should be put into S, and b is the node outside the set S.

        

        Knowing from the assumption: xd = δ (s, x)

                  a.d = δ(s,x)

        Because P2 <P1, dist [b] <dist [u] can be deduced, so b will enter before u, creating a contradiction.

    

Guess you like

Origin www.cnblogs.com/yanghh/p/12685278.html