The dijela algorithm processes the (dijkstra) Java Implementation of the shortest path in an undirected graph (specifying two points and finding the Shortest Path)

Source: Internet
Author: User

 

 

 

How can we find the shortest path from V0 to V5 in the figure?
The java implementation method is as follows:
Step 1: Create a weight matrix based on the graph:
Int [] [] W = {
{0, 1, 4,-1,-1,-1 },
{1, 0, 2, 7, 5,-1 },
{4, 2, 0,-1, 1,-1 },
{-1, 7,-1, 0, 3, 2 },
{-1, 5, 1, 3, 0, 6 },
{-1,-1,-1, 2, 6, 0}; (-1 indicates that the two sides are not adjacent and the weight is infinitely large)
For example, W [0] [2] = 4 indicates that the weight of point V0 to point V2 is 4.
W [0] [3] =-1 indicates that vertex V0 is not adjacent to V3, so the weight is infinitely large.
Step 2: V0 labels; distance: {, 4,-1,-1,-1} is obtained from the path from V0 to other points }; find the vertex with the smallest weight from V0 to each point (except for the vertex of the label,-1 indicates an infinite number). Therefore, 1 is the corresponding subscript 1, and V1 is obtained, then, change the V0 path from V1 to another vertex to get distance: {0, 1, 3, 8, 6,-1 };
Step 3: Find the vertex with the smallest weight in distance, (except the vertex of the label) Get V2, label V2, and change V0 to get distance through the path from V1-> V2 to other vertices: {0, 1, 3, 8, 4,-1 };
Step 4: Find the vertex with the smallest weight in distance, (except the vertex of the label) Get V4, label V4, and change V0 to get distance through the path from V1-> V2 to other vertices: {0, 1, 3, 7, 4, 10 };
Step 4: Find the vertex with the smallest weight in distance, (except the vertex of the label) Get V3, label V3, and change V0 to get distance through the path from V1-> V2 to other vertices: {0, 1, 3, 7, 4, 9 };
In the end, only V5 is left unlabeled and V5 is found. End!
The source code is as follows:
Package com. xh. Dijkstra;
 
// This algorithm is used to solve the shortest path of any two points in an undirected graph.
Public class ShortestDistanceOfTwoPoint_V5 {
Public static int dijkstra (int [] [] W1, int start, int end ){
Boolean [] isLabel = new boolean [W1 [0]. length]; // whether to mark
Int [] indexs = new int [W1 [0]. length]; // The subscript set of all vertices of the label, which is stored in the order of the label. It is actually a stack represented by an array.
Int I _count =-1; // stack Vertex
Int [] distance = W1 [start]. clone (); // the initial value of the shortest distance from v0 to each point
Int index = start; // start from the initial point
Int presentShortest = 0; // the current temporary shortest path.
 
Indexs [++ I _count] = index; // saves the labeled subscript to the subscript set.
IsLabel [index] = true;

While (I _count <W1 [0]. length ){
// Step 1: v0, that is, w [0] [0] finds the point closest to v0
 
Int min = Integer. MAX_VALUE;
For (int I = 0; I <distance. length; I ++ ){
If (! IsLabel [I] & distance [I]! =-1 & I! = Index ){
// If there is an edge at this point and it is not labeled
If (distance [I] <min ){
Min = distance [I];
Index = I; // change the subscript to the current subscript.
}
}
}
If (index = end) {// The current vertex has been found, and the program ends.
Break;
}
IsLabel [index] = true; // mark a point
Indexs [++ I _count] = index; // saves the labeled subscript to the subscript set.
If (W1 [indexs [I _count-1] [index] =-1
| PresentShortest + W1 [indexs [I _count-1] [index]> distance [index]) {
// If the two vertices are not directly connected, or the two vertices have a path greater than the Shortest Path
PresentShortest = distance [index];
} Else {
PresentShortest + = W1 [indexs [I _count-1] [index];
}
 
// Step 2: add the distance in distance to vi
For (int I = 0; I <distance. length; I ++ ){
// If there is an edge between vi and that vertex, the distance from v0 to the following vertex is added.
If (distance [I] =-1 & W1 [index] [I]! =-1) {// if it was previously inaccessible, it is now reachable
Distance [I] = presentShortest + W1 [index] [I];
} Else if (W1 [index] [I]! =-1
& PresentShortest + W1 [index] [I] <distance [I]) {
// If it can be reached before, but the current path is shorter than before, change it to a shorter path.
Distance [I] = presentShortest + W1 [index] [I];
}
 
}
}
// If all vertices are traversed, the shortest path from the start point to each vertex is stored in distance.
Return distance [end]-distance [start];
}
Public static void main (String [] args ){
// Create a weight matrix
Int [] [] W1 = {// test data 1
{0, 1, 4,-1,-1,-1 },
{1, 0, 2, 7, 5,-1 },
{4, 2, 0,-1, 1,-1 },
{-1, 7,-1, 0, 3, 2 },
{-1, 5, 1, 3, 0, 6 },
{-1,-1,-1, 2, 6, 0 }};
Int [] [] W = {// Test Data 2
{0, 1, 3, 4 },
{1, 0, 2,-1 },
{3, 2, 0, 5 },
{4,-1, 5, 0 }};
 
System. out. println (dijkstra (W1, 0, 4 ));
 
}
}
If you need to require the Shortest Path matrix of each vertex in an undirected graph, you can use the dijkstra algorithm multiple times. The Code is as follows:
Package com. xh. Dijkstra;
 
// This program is used to obtain the Shortest Path matrix of a graph.
Public class ShortestDistance_V4 {
Public static int dijkstra (int [] [] W1, int start, int end ){
Boolean [] isLabel = new boolean [W1 [0]. length]; // whether to mark
Int min = Integer. MAX_VALUE;
Int [] indexs = new int [W1 [0]. length]; // a subscript set of all vertex numbers
Int I _count =-1;
Int index = start; // start from the initial point
Int presentShortest = 0;
Int [] distance = W1 [start]. clone (); // the initial value of the shortest distance from v0 to each point
Indexs [++ I _count] = index; // saves the labeled subscript to the subscript set.
IsLabel [index] = true;
While (true ){
// Step 1: v0, that is, w [0] [0] finds the point closest to v0
 
Min = Integer. MAX_VALUE;
For (int I = 0; I <distance. length; I ++ ){
If (! IsLabel [I] & distance [I]! =-1 & I! = Index ){
// If there is an edge at this point and it is not labeled
If (distance [I] <min ){
Min = distance [I];
Index = I; // change the subscript to the current subscript.
}
}
}
If (index = end ){
Break;
}
IsLabel [index] = true;
Indexs [++ I _count] = index; // saves the labeled subscript to the subscript set.
If (W1 [indexs [I _count-1] [index] =-1
| PresentShortest + W1 [indexs [I _count-1] [index]> distance [index]) {
PresentShortest = distance [index];
} Else {
PresentShortest + = W1 [indexs [I _count-1] [index];
}
 
// Step 2: add the distance in the prize distance to vi
For (int I = 0; I <distance. length; I ++ ){
// If there is an edge between vi and that vertex, the distance from v0 to the following vertex is added.
// There is a problem with the program here! Haha
If (distance [I] =-1 & W1 [index] [I]! =-1) {// if it was previously inaccessible, it is now reachable
Distance [I] = presentShortest + W1 [index] [I];
} Else if (W1 [index] [I]! =-1
& PresentShortest + W1 [index] [I] <distance [I]) {
// If it can be reached before, but the current path is shorter than before, change it to a shorter path.
Distance [I] = presentShortest + W1 [index] [I];
}
 
}
}
Return distance [end]-distance [start];
}

Public static int [] [] getShortestPathMatrix (int [] [] W ){
Int [] [] SPM = new int [W. length] [W. length];
// Uses dijkstra algorithm multiple times
For (int I = 0; I <W. length; I ++ ){
For (int j = I + 1; j <W. length; j ++ ){
SPM [I] [j] = dijkstra (W, I, j );
SPM [j] [I] = SPM [I] [j];
}
}
Return SPM;
}
 
Public static void main (String [] args ){
/* Vertex set: V = {v1, v2 ,......, Vn }*/
Int [] [] W = {0, 1, 3, 4}, {1, 0, 2,-1}, {3, 2, 0, 5 },
{4,-1, 5, 0 }};
Int [] [] W1 = {0, 1, 4,-1,-1,-1}, {1, 0, 2, 7, 5,-1 },
{4, 2, 0,-1, 1,-1}, {-1, 7,-1, 0, 3, 2 },
{-1, 5, 1, 3, 0, 6}, {-1,-1,-1, 2, 6, 0}; // create a weight matrix.
; // Create a weight matrix
Int [] [] D = getShortestPathMatrix (W1 );
// Output the final result
For (int I = 0; I <D. length; I ++ ){
For (int j = 0; j <D [I]. length; j ++ ){
System. out. print (D [I] [j] + "");
}
System. out. println ();
}
}
}

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.