Single-source shortest path template (from Shanghai)

Source: Internet
Author: User

Dijkstra algorithm (single-source shortest path)

The single-source shortest path is used to find the shortest path from a given vertex to any other vertex in the figure. Before figuring out how to calculate a single-source shortest path, we must find out the optimal sub-structure of the shortest path.

I. Optimal sub-structure of the shortest path

This property is described as follows: If P (I, j) = {vi .... VK .. vs... VJ} is the shortest path from vertex I to J, and K and S are an intermediate vertex in this path, so P (K, S) it must be the shortest path from K to S. The correctness of this property is proved below.

Suppose P (I, j) = {vi .... VK .. vs... VJ} is the shortest path from vertex I to J. P (I, j) = P (I, K) + P (K, S) + P (S, j ). P (K, S) is not the shortest distance from K to S, so there must be another shortest path from K to S p '(K, S ), then P' (I, j) = P (I, K) + P' (K, S) + P (S, j) <p (I, j ). It is in conflict with P (I, j) as the shortest path from I to J. Therefore, this property is proved.

Ii. Dijkstra Algorithm

From the above properties, we can see that if there is a shortest path from I to J (Vi... VK, vj), VK is a vertex before VJ. Then (Vi... VK) must be the shortest path from I to K. In order to find the shortest path, Dijkstra proposed an algorithm to generate the Shortest Path progressively with the shortest path length. For example, for Source Vertex v0, first select the vertex VI with the shortest length in its directly adjacent vertex, the shortest distance from V0 to vj vertex is known as Dist [J] = min {Dist [J], DIST [I] + matrix [I] [J]}. Based on this idea,

Assume that G = <V, E> exists, the Source Vertex is v0, u = {v0}, and DIST [I] records the shortest distance from V0 to I, path [I] records a vertex before I from V0 to I path.

1. Select vertex I that minimizes Dist [I] value from the V-U and add I to U;

2. Update the DIST value of the vertex directly adjacent to I. (Dist [J] = min {Dist [J], DIST [I] + matrix [I] [J]})

3. know u = V, stop.

Code implementation:

1/* Dijkstra find the single-source shortest path 2010.8.26 */2 3 # include <iostream> 4 # include <stack> 5 # define M 100 6 # define n 100 7 using namespace STD; 8 9 typedef struct node 10 {11 int matrix [N] [m]; // adjacent matrix 12 int n; // Number of vertices 13 int e; // Number of edges 14} mgraph; 15 16 void dijkstrapath (mgraph g, int * Dist, int * path, int V0) // V0 indicates the Source Vertex 17 {18 int I, j, k; 19 bool * visited = (bool *) malloc (sizeof (bool) * g. n); 20 for (I = 0; I <G. n; I ++) // initialize 21 {22 if (G. Matrix [V0] [I]> 0 & I! = V0) 23 {24 Dist [I] = G. matrix [V0] [I]; 25 path [I] = V0; // path records the first vertex from V0 to I on the shortest path 26} 27 else 28 {29 Dist [I] = int_max; // If I is not directly adjacent to v0, set the weight to infinity 30 path [I] =-1; 31} 32 visited [I] = false; 33 path [V0] = V0; 34 Dist [V0] = 0; 35} 36 visited [V0] = true; 37 for (I = 1; I <G. n; I ++) // cyclically extended n-1 times 38 {39 int min = int_max; 40 int U; 41 for (j = 0; j <G. n; j ++) // find the vertex with the smallest unextended weight 42 {43 If (visited [J] = false & Dist [J] <min) 44 {45 min = dis T [J]; 46 U = J; 47} 48} 49 visited [u] = true; 50 for (k = 0; k <G. n; k ++) // update the DIST array value and PATH value 51 {52 If (visited [k] = false & G. matrix [u] [k]> 0 & min + G. matrix [u] [k] <Dist [k]) 53 {54 Dist [k] = min + G. matrix [u] [k]; 55 path [k] = u; 56} 57} 58} 59} 60 61 void showpath (int * path, int V, int V0) // print the 62 {63 stack <int> S; 64 int u = V; 65 while (V! = V0) 66 {67 s. Push (V); 68 v = path [v]; 69} 70 s. Push (V); 71 while (! S. empty () 72 {73 cout <S. top () <"; 74 s. pop (); 75} 76} 77 78 int main (INT argc, char * argv []) 79 {80 int N, E; // number of input vertices and edges 81 While (CIN> N> E & E! = 0) 82 {83 int I, j; 84 int S, T, W; // indicates that an edge S-> T exists, and the weight is W 85 mgraph G; 86 int V0; 87 int * Dist = (int *) malloc (sizeof (INT) * n); 88 int * Path = (int *) malloc (sizeof (INT) * n); 89 for (I = 0; I <n; I ++) 90 for (j = 0; j <m; j ++) 91G. matrix [I] [J] = 0; 92g. N = N; 93G. E = E; 94 for (I = 0; I <E; I ++) 95 {96 CIN> S> T> W; 97g. matrix [s] [T] = W; 98} 99 CIN> V0; // Input Source Vertex 100 dijkstrapath (G, DIST, path, V0 ); 101 for (I = 0; I <n; I ++) 102 {103 if (I! = V0) 104 {105 showpath (path, I, V0); 106 cout <Dist [I] <Endl; 107} 108} 109 return 0; 110}

 

Single-source shortest path template (from Shanghai)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.