The Dijkstra algorithm, called the Dijkstra algorithm, is a successful example of the design algorithm using the "greedy method" (always making the best selection strategy at the moment when solving the problem).
Applicable conditions: With no ring and no negative weight value
Give me a chestnut:
The code for the Dijkstra algorithm is implemented as follows:
Package com.threeTop.www;
Import Java.util.Stack;
Dijkstra algorithm of/** * adjacency Matrix storage mode * @author WJGS */public class Dijkstra {//by subscript Map element value private int[] mapping;
The two-dimensional array of graphs private int[][] matrix; /** * Initialization graph vertex * @param vertexes vertex array/public Dijkstra (int []vertexes) {int Length=vertexes.leng
Th
Mapping=new Int[length]; Matrix=new Int[length][length];
The two-dimensional matrix of the graph for (int i=0;i<length;i++) {mapping[i]=vertexes[i]; }/** * Add a weighted edge * @param start * @param end * @param value */public VO
ID addedge (int start,int end,int value) {int x=-1;
int y=-1;
Look for coordinates for (int i=0;i<mapping.length;i++) {if (x!=-1&&y!=-1) {break;
} if (Start==mapping[i]) {x=i;
} if (End==mapping[i]) {y=i; }//Determine if the vertex exists if (x==-1| | y==-1| | x>mapping.length-1| | Y>mapping.length-1{throw new Indexoutofboundsexception ("The vertex of the edge does not exist!");
///Add edge weights matrix[x][y]=value; /** * Dijkstra algorithm implements the shortest path to each point * @param start/public void Dijkstra (int start) {int Leng
Th =mapping.length; int x=-1;
Record starting point for (int i=0;i<length;i++) {if (Mapping[i]==start) {x=i;
Break
} if (X==-1) {throw new RuntimeException ("No starting vertex found");
}///automatically initialized to 0, all belong to the vertex of the shortest path int[]s=new int[length];
To store the shortest distance of V to u int [] Distance=matrix;
The first vertex int []path=new int[length] of u when storing the shortest path of x to u;
Initializes the path array for (int i=0;i<length;i++) {//if reachable on the assignment if (matrix[x][i]>0) {path[i]=x;
else {//unreachable, then assign the previous vertex subscript 1 path[i]=-1;
}/////First add the starting vertex to S s[x]=1;
for (int i=0;i<length;i++) {///First you need to find the shortest path int min=integer.max_value for the start vertex to each vertex; int v=0;
Record x to the shortest of each vertex for (int j=0;j<length;j++) {if (s[j]!=1&&x!=j&&distance[x][j]!=0&&distance[x][j
]<min) {min=distance[x][j];
V=j;
}//v is the shortest s[v]=1 of the current x to each vertex; Fixed shortest path distance and shortest distance path for (int j=0;j<length;j++) {if s[j]!=1&&distance[v][j]!=0&& amp; (min+distance[v][j]<distance[x][j]| |
distance[x][j]==0)) {//Note a shorter path is found after adding the middle vertex distance[x][j]=min+distance[v][j];
Path[j]=v;
}///Print Shortest Path value Stack <integer>stack=new stack<integer> (); for (int i=0;i<length;i++) {if (distance[x][i]!=0) {System.out.println (mapping[x]+ "-->" +mapping[
i]+ "Shortest path length:" +distance[x][i]);
Path storage paths, can be output in reverse order, can use the stack to achieve positive output System.out.print ("reverse the Shortest Path output:");
int index=i;
while (index!=-1) {System.out.print (mapping[index]+ ""); Stack.push (Mapping[index]);
Index=path[index];
System.out.print ("Positive sequence Shortest path output:");
while (!stack.isempty ()) {System.out.print (Stack.pop () + "");
} System.out.println ();
}} public static void Main (string[] args) {int[] vetexes={1,2,3,4,5,6};
Dijkstra graph=new Dijkstra (vetexes);
Graph.addedge (1, 2,16); Graph.addedge (2, 1,16);
Graph.addedge (1, 3,1); Graph.addedge (3, 1,1);
Graph.addedge (1, 5,12); Graph.addedge (5, 1,12);
Graph.addedge (1, 6,15); Graph.addedge (6, 1,15);
Graph.addedge (2, 4,2); Graph.addedge (4, 2,2);
Graph.addedge (2, 6,8); Graph.addedge (6, 2,8);
Graph.addedge (3, 5,5); Graph.addedge (5, 3,5);
Graph.addedge (4, 6,3); Graph.addedge (6, 4,3);
Graph.addedge (5, 6,8); Graph.addedge (6, 5,8);
Graph.addedge (4, 5,9); Graph.addedge (5, 4,9);
Graph.dijkstra (1);
}
}