Data structure-single source shortest path Dijkstra (Dijkstra) algorithm detailed (Java)

Source: Internet
Author: User

Given a graph to find the shortest path of an endpoint (goal) to the rest of the endpoint or an endpoint, the easiest way to think of it is to take advantage of DFS, assuming that the average path from the beginning to an endpoint is N, the average adjacency endpoint for each endpoint is m, and the time complexity of this shortest path using the DFS algorithm is O ( MN), which is obviously very unsatisfactory.

Therefore, Dijkstra (Dijkstra) algorithm, in order to solve the direction of an endpoint to the other end of the shortest path, the number of points in M, the time complexity of O (M2) to find the results, the precondition is that the weight of each edge can not be negative.

Next, the algorithm details are described:

1. Divide all endpoints into two sets, all endpoint collections that have found the shortest path, and endpoint collections where the shortest paths are not found (initially only the starting point is in the shortest route endpoint collection found).

2. Maintain an array of distances of length m (this is the expression below), storing the shortest distance of all current endpoints. When a new endpoint finds the shortest path, it updates the current shortest distance (the initial value is the distance from the starting point to the endpoint that can be reached) for the rest of the endpoints that the endpoint can reach without finding the shortest path.

3, the Loop m-1 times, each time from the distance array to find the smallest value, the value is the shortest path of the endpoint (such as the starting point is a, the time to find the minimum value is B, then this value is the shortest distance B, because it is not possible to go out from a other path through the other path and then to B distance is smaller , unless there is a negative value for the weight of the path, but this is contrary to the Dijkstra precondition).

4, the end of the current find the shortest path, if starting from that point, can reach the end of the shortest path is less than the value of the current distance array, then update this value.

5, the Loop m-1 to find the shortest path of all the endpoints, or in a loop can not find the required endpoint, then the remaining endpoints are unreachable, all end this calculation.

The process was a little obscure to my expression ... The following can be understood by code.

1 //calculate the shortest distance from start point to end point2      Public voidCalcdistance (intStartint[] matrix) {3          This. Matrix =Matrix;4          This. Start =start;5         intCNT = matrix.length;//Point Count6          This. goalcnt =CNT;7         Boolean[] found =New Boolean[CNT];//true value indicate the shortest distance from start to N has been found8         int[] dis =New int[CNT];//store the shortest distance from start to N, initial as Int_max9         int[] Preroute =New int[CNT];//Preroute[u] = V represent the previous goal of shortest path to u are vTen          for(inti = 0; I < CNT; i++) { OneDis[i] = start = = I? 0:matrix[start][i]! = 0?Matrix[start][i]: integer.max_value; APreroute[i] = 1; -         } -Found[start] =true; the         //find out one shortest path in one iteration -          for(inti = 0; i < cnt-1; i++) { -             //The shortest distance of the index of goal is found in current iteration -             intTDis =Integer.max_value, +Tind =-1; -              for(intj = 0; J < CNT; J + +) { +                 if(!found[j] && Dis[j] <TDis) { ATind =J; atTDis =Dis[j]; -                 } -             } -             if(-1 = =Tind) { -                 //The left goal is unreachable -                  Break; in             } -Found[tind] =true; to distance.put (Tind, Dis[tind]); +             //Find out the route of the Tind goal -             intE =Tind; thestack<integer> stack =NewStack<>(); *              while(-1! =e) { $ Stack.push (e);Panax NotoginsengE =Preroute[e]; -             } the route.put (tind, stack); +             //Update the shorter path A              for(intj = 0; J < CNT; J + +) { the                 if(matrix[tind][j]! = 0) { +                     intNewdis = Dis[tind] +Matrix[tind][j]; -                     if(!found[j] && Newdis <Dis[j]) { $DIS[J] =Newdis; $PREROUTE[J] = Tind;//Tind is the previous goal of the shortest path to J goal -                     } -                 } the             } -         }Wuyi}

Above is the core algorithm of the Dijkstra, pass in a graph a starting point, find the shortest path to the rest of the endpoint, Found[n] True indicates that the shortest path to the end of the subscript n is found, the DIS array is the distance array mentioned above, 32~ 39 rows and Preroute arrays are just to find the path through the node, not the algorithm content.

Let's look at a graph to test the results of the algorithm, S (N0) as the starting point.

into matrix form as follows:

 Public Static voidMain (string[] args) {//test Data        int[] Matrix = {                {0,3,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},                {3,0,1,0,1,0,0,0,0,4,0,0,0,0,0,0,0,0},                {1,1,0,1,2,1,0,0,0,0,0,0,0,0,0,0,0,0},                {1,0,1,0,0,2,2,1,0,0,0,0,0,0,0,0,0,0},                {0,1,2,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0},                {0,0,1,2,1,0,1,0,0,3,1,0,3,0,0,0,0,0},                {0,0,0,2,0,1,0,1,2,0,0,0,2,4,3,0,0,0},                {0,0,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0},                {0,0,0,0,0,0,2,1,0,0,0,0,0,0,1,3,0,0},                {0,4,0,0,1,3,0,0,0,0,1,1,0,0,0,0,0,0},                {0,0,0,0,0,1,0,0,0,1,0,1,2,0,0,0,0,0},                {0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,1,0},                {0,0,0,0,0,3,2,0,0,0,2,1,0,2,0,0,1,0},                {0,0,0,0,0,0,4,0,0,0,0,0,2,0,1,2,2,1},                {0,0,0,0,0,0,3,0,1,0,0,0,0,1,0,1,0,0},                {0,0,0,0,0,0,0,0,3,0,0,0,0,2,1,0,0,4},                {0,0,0,0,0,0,0,0,0,0,0,1,1,2,0,0,0,1},                {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,4,1,0}}; Dijkstra Dijkstra=NewDijkstra (); Dijkstra.calcdistance (0, Matrix);  for(inti = 1; I < dijkstra.getgoalcnt (); i++) {System.out.println ("Destination:" +i); System.out.println ("Distance:" +dijkstra.getdistance (i)); System.out.println ("Path:" +Dijkstra.getroutemap (i));        System.out.println (); }    }

Result of Operation:

Destination:1Distance:2Path:0->2->1Destination:2Distance:1Path:0->2Destination:3Distance:1Path:0->3Destination:4Distance:3Path:0->2->4Destination:5Distance:2Path:0->2->5Destination:6Distance:3Path:0->3->6Destination:7Distance:2Path:0->3->7Destination:8Distance:3Path:0->3->7->8Destination:9Distance:4Path:0->2->4->9Destination:10Distance:3Path:0->2->5->10Destination:11Distance:4Path:0->2->5->10->11Destination:12Distance:5Path:0->2->5->12Destination:13Distance:5Path:0->3->7->8->14->13Destination:14Distance:4Path:0->3->7->8->14Destination:15Distance:5Path:0->3->7->8->14->15Destination:16Distance:5Path:0->2->5->10->11->16Destination:17Distance:6Path:0->3->7->8->14->13->17

The preliminary test results are correct.

At this point, the above is a personal understanding of the Dijkstra algorithm, if there is a problem, please indicate treatise.

Respect intellectual property rights, reprint quoted please inform the author and indicate the source!

Data structure-single source shortest path Dijkstra (Dijkstra) algorithm detailed (Java)

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.