Java single source shortest path (shortest path,sssp problem) Dijkstra algorithm solution

Source: Internet
Author: User

title: Given a weighted graph, and then given a vertex (source point) in the graph, the shortest distance to all other points, called single source shortest path problem.

For example, find the shortest distance from point 1 to other points

Preparation: The following data is required for the problem

int N; Save Vertex count

int M; Save the number of edges

int Max; Used to set a value that is greater than the weight of all sides to indicate that there is no connection between two points

Int[] Visit; To find the shortest distance of a vertex, set it to 1, which defaults to 0 (not found yet)

Int[][] distance; Saves the values of the edges in the graph, and the infinity between two points is set to Max

Int[] Bestmin; Save the shortest distance from the source point to other points for the final output

string[] path; Some topics will require the output path, save the output path

Algorithm steps:

① find the point that is the shortest distance from the source point, that is, traverse distance[1][1],distance[1][2],..... Distance[1][n] The minimum value, such as the title:

The distance from source point 1 to 2,4,5 is 10, 30,100, respectively. 3 cannot be reached directly at this time distance[1][3] = max. So the point of this step is to find

Vertex 2. At this point, distance[1][2] is the shortest distance from source point 1 to Vertex 2. Set visit[2] to 1 (vertex 2 complete).

② Relaxation operation,

Points identified by ① as the center point (vertex 2 at this point), to traverse Visit[i] to 0 points, if distance[1][2] + Distance[2][i] < Distance[1][i]

The new shorter path is assigned to it, i.e. distance[1][i] = distance[1][2] + distance[2][i],

Vertex 2 can reach at this point only Vertex 3, and distance[1][3] = max, so the value of update distance[1][3] is distance[1][2] + distance[2][3] = 60

After completing the two steps above, go back to step ①, which is a loop, each time the loop can find a minimum distance point and update other points, so the loop will traverse

The shortest distance of all points can be found N-1 times, the approximate process is as follows:

for (int i = 2; I <= N; i++) {

Step ① (Find the shortest distance within a loop)

Step ② (centered on the point found by ①, updating all Visit[i] points to the source point through a loop)

}

The complete code is as follows:

 Packagealgorithm;ImportJava.util.Scanner; Public classDijkstra__single_source_shortest_path {Private Static intN; Private Static intM; Private Static intMax; Private Static int[] visit; Private Static int[] distance; Private Static int[] bestmin; Private Staticstring[] path;  Public Static voidDijkstra () {visit[1] = 1; bestmin[1] = 0; //The Big cycle (this is done here even if the algorithm, the back of the output what can not see)         for(intL = 2; L <= N; l++) {            intDtemp =Max; intK =-1; //Step ①             for(inti = 2; I <= N; i++) {                if(Visit[i] = = 0 && Distance[1][i] <dtemp) {Dtemp= Distance[1][i]; K=i; }} Visit[k]= 1; BESTMIN[K]=dtemp; //Step ②             for(inti = 2; I <= N; i++) {                if(Visit[i] = = 0 && (distance[1][k] + distance[k][i]) < distance[1][i]) {distance[1][i] = Distance[1][k] +Distance[k][i]; Path[i]= Path[k] + "--" +i; }            }        }                //Output Path         for(inti=1;i<=n;i++) {System.out.println (The shortest path from "+1+" to "+i+" is: "+Path[i]); } System.out.println ("=====================================");  for(inti = 1; I <= N; i++) {System.out.println (The shortest distance from 1 to "+ i +" points is: "+Bestmin[i]); }    }     Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner Input=NewScanner (system.in); System.out.print ("Please enter the number of nodes N, total number of paths m:"); N=Input.nextint (); M=Input.nextint (); Max= 10000; Bestmin=New int[N+1]; Distance=New int[N+1] [N+1]; Visit=New int[N+1]; Path=NewString[n+1];  for(inti = 1; I <= N; i++) {             for(intj = 1; J <= N; J + +) {                if(i = =j) {Distance[i][j]= 0; }Else{Distance[i][j]=Max; }} Bestmin[i]=Max; Path[i]=NewString ("1-->" +i); } System.out.println ("Please enter the" + M + "strip data x, Y, Z (the distance to the Y point from the point of origin is *):");  for(inti = 1; I <= M; i++) {            intx =Input.nextint (); inty =Input.nextint (); intz =Input.nextint (); Distance[x][y]=Z;                } input.close ();    Dijkstra (); }}

The results of the operation are as follows:

The above content is I in the university to learn the content of a summary, if there is anything wrong, please correct me!

Java single source shortest path (shortest path,sssp problem) Dijkstra algorithm solution

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.