Shortest path (adjacency table)-dijkstra algorithm: The generated graph uses the adjacency table storage way.
The specific implementation code is as follows:
Package com.threeTop.www;
Import java.util.Hashtable;
Import Java.util.Stack; Dijkstra algorithm for/** * adjacency table storage mode * @author WJGS */public class Dijkstra2 {//graph vertex array private listgraphnode []
Nodes
With the help of a hash table private hashtable<integer,integer> valueindex=new hashtable<integer,integer> (); /** * Initialization graph vertex * @param vertexes/public Dijkstra2 (int []vetexes) {nodes=new listgraphnode[vetexes.lengt
h];
for (int i=0;i<vetexes.length;i++) {//Initialize each node nodes[i]=new listgraphnode (I,vetexes[i],null);
Hash records each position valueindex.put (Vetexes[i], i); }/** * Add start attainable edge * @param start * @param end * @param weights corresponding weights array/public void Adde
Dge (int start, int []end,int[]weights) {int index=valueindex.get (start);
if (index<0) {throw new RuntimeException ("The specified starting vertex is not found!");
} Listgraphnode Node=nodes[index]; for (int j=0;j<end.length;j++) {int K=valueindex. Get (End[j]);
if (k<0) {throw new RuntimeException ("failed to find specified reach vertex!");
} node.next=new Listgraphnode (k,end[j],weights[j],null); Node=node.next; Link Next}/** * Dijkstra algorithm implements the shortest path to each point * @param start/public void Dijkstra (int star
t) {int length =nodes.length; int X=valueindex.get (start);
Record starting point 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];
The shortest distance of storage v to u int [] [] distance=new int[length][length];
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++) {//Initialize path to-1 path[i]=-1;
for (int i=0;i<length;i++) {Listgraphnode node=nodes[i];
Node=node.next;
while (node!=null) {distance[i][node.index]=node.weight;
if (x==i) {//If it is a linked list of x vertices, initialize the previous vertex of all reachable vertices to x path[node.index]=x; } Node=node.next;
}/////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 for (int j=0;j<length;j++) {//s[j]==1 Description has found the shortest path if (S[j]!=1&&x!=j&&dis
Tance[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 (nodes[x].value+ "-->" +node S[i]. value+ "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 (nodes[index].value+ "");
Stack.push (Nodes[index].value);
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};
Dijkstra2 listgraph=new Dijkstra2 (vetexes);
Listgraph.addedge (1, New Int[]{2,3,5,6},new int[]{16,1,12,15});
Listgraph.addedge (2, New Int[]{1,4,6},new int[]{16,2,8});
Listgraph.addedge (3, New Int[]{1,5},new int[]{1,5});
Listgraph.addedge (4, New Int[]{2,5,6},new int[]{2,9,3});
Listgraph.addedge (5, New Int[]{1,3,4,6},new int[]{12,5,9,8});
Listgraph.addedge (6, New Int[]{1,2,4,5},new int[]{15,8,3,8});
Listgraph.dijkstra (1);
}
}