Single-source shortest path problem [Dijkstra implementation]

Source: Internet
Author: User

Single-source shortest path problem [Dijkstra implementation]

I. Problems

The weighted directed graph G (E, v) is used to find the weighted minimum path from the given Source Vertex s to other vertex v.

"Shortest Path" = Minimum permission

Ii. Problem Solving:

What is the minimum path value between 1 and 5?

 

Iii. Execution Process:

 

 

If you do not quite understand the requirements for this issue, I will take you over again:

First time: from 1 --> at this time from 1 --> 3 no path all are infinite 1 --> 1 -->, then we find that the smallest group is 10, that is, 2, so we add the 2 point to the set, so the 2 point can be used as a bridge,

The second time: now we can use 1 --> 2 --> 3: 60, other 1 -->.

1 --> can be found that the minimum value should be 3 at this time, so we will add 3 to this set to do these things repeatedly, at the end, we can find that the shortest path of 1*5 is 60 (1 --> 4 --> 3 --> 5)

 

Iv. Dijkstra pseudocode:

Int Dijkstra (int s, int t) {initialized S = {empty set} d [s] = 0; other D values are positive infinity while (not t in S) {retrieve the smallest d [I]; for (all vertices not in S and adjacent to I j) if (d [J]> d [I] + cost [I] [J]) d [J] = d [I] + cost [I] [J]; ("relax" Operation) S = S + {I}; // Add the I point to the set S} return d [T];}

Why is the relaxation operation:

That is to say, if the value of 1 --> 3 is Dist [3]> Dist [2] + map [2] [3]

So Dist [3] = dits [2] + map [2] [3]

V. Code implementation:

# Include <iostream> using namespace STD; # define Max 9999999 # define Len 210int map [Len] [Len]; // The distance between two points at a certain point: int Dist [Len]; // records the shortest path length of the current point to the source point. Int mark [Len]; // set of added points // The initialized map is positive infinity void Init () {int I, j; for (I = 0; I <Len; I ++) {for (j = 0; j <Len; j ++) {map [I] [J] = max ;}}// N: How many start paths: start Point void mydijstra (int n, int start) {int I, j, Min, K; for (I = 1; I <= N; I ++) {mark [I] = 0; // no point is added to Dist [I] = map [start] [I]; // initial} mark [sta RT] = 1; // Add the start point to Dist [start] = 0; for (I = 1; I <= N; I ++) {min = max; for (j = 1; j <= N; j ++) {If (! Mark [J] & Dist [J] <min) {// retrieve the smallest Dist not in the mark [I] min = DIST [J]; k = J; // tag} If (min = max) break; Mark [k] = 1; // Add K. // perform the relaxation operation for (j = 1; j <= N; j ++) {If (! Mark [J] & Dist [J]> Dist [k] + map [k] [J]) {Dist [J] = DIST [k] + map [k] [J] ;}}} int main () {int I, j, N, line; int, b, d; CIN> N> line; // input vertex and edge Init (); for (I = 0; I <line; I ++) {CIN> A> B> D; // enter the edge weight if (Map [a] [B]> D) {map [a] [B] = map [B] [a] = D ;}} mydijstra (n, 1 ); // call the method // output the Shortest Path cout from 1 to 5 <Dist [5] <Endl; return 0 ;}

Vi. Test Data:

5 7
1 2 10
1 4 30
1 5 100
2 3 50
3 5 10
4 3 20
4 5 60

 

Result:

Related Article

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.