1 PackageiYou.neugle.graph;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 //The code to create the diagram procedure is in the blog post of the diagram, which is used directly here7 Public classDijkstra {8 PrivateMYGRAPH1 graph;9 Private intstart;Ten Private intMaxnum; One Private int[] distance;//starting point to end distance A Private int[] point;//collection of other points except the starting point - Privatestring[] path;//the path from the starting point to the endpoint - Privatelist<integer> s =NewArraylist<integer> ();//the collection of points the - PublicDijkstra (MyGraph1 graph,intstart) { - This. Graph =graph; - This. Start = Start-1; + This. Maxnum = This. Graph.getgraph (). Maxnum; -Distance =New int[ This. maxNum-1]; +Point =New int[ This. maxNum-1]; APath =Newstring[ This. maxNum-1]; at } - - //Initialize minimum distance array - Private voidInit () { - for(inti = 0; I < This. maxNum-1; i++) { - This. distance[i] =Integer.max_value; in if(I >= This. Start) { - This. point[i] = i + 1; to}Else { + This. point[i] =i; - } the } * } $ Panax Notoginseng Public voidDijkstracore () { - This. Init (); the //first add the starting node to the set S + This. S.add ( This. Start); A //Initialize Intermediate node u the intU = This. Start; + //Terminate if s set reaches Maxnum - while(S.size () < This. Maxnum) { $ int[] edges = This. Graph.getgraph (). Edge; $ Booleanb =false; - for(inti = 0; i < edges[u].length; i++) { - //if the start node and the middle node are not connected to the general rule no action is made (excluding the start node) the if(edges[ This. start][u] = = 0 && u! = This. Start) { - Break;Wuyi } the //The distance from the node to the starting point is not to be asked. - if(i = = This. Start) { Wub =true; - Continue; About } $ intx; - //If the node after the starting node requires i-- - if(b = =false) { -x =i; A}Else { +x = i-1; the } - //If there is a path, the calculation $ if(edges[u][i]! = 0) { the inttemp = edges[ This. Start][u] +Edges[u][i]; the if(Temp < This. Distance[x]) { the This. distance[x] =temp; the if( This. Start = =u) { - This. path[x] = ( This. Start + 1) + "+" + (i + 1); in}Else { the This. path[x] = ( This. Start + 1) + "+" + (U + 1) the+ "+" + (i + 1); About } the } the } the } + //find the next intermediate node -U = This. Function (); the //add an intermediate point to the set SBayi This. S.add (u); the } the This. Print (); - } - the //function function: Find the minimum value in the distance array at this time (the condition of the minimum value is not the minimum value in s) the Private intFunction () { the intU =Integer.max_value; the intK =-1; - for(inti = 0; I < This. distance.length; i++) { the //If the node exists in S, continue to find other minor nodes the if( This. S.contains ( This. Point[i])) { the Continue;94}Else { the if( This. Distance[i] <u) { theU = This. Distance[i]; theK = This. Point[i];98 } About } - }101 returnK;102 }103 104 //Print Results the Private voidPrint () {106 for(inti = 0; I < This. distance.length; i++) {107System.out.println ( This. Path[i] + ":" + This. Distance[i]);108 }109 } the 111 Public Static voidMain (string[] args) { theMYGRAPH1 graph =NewMYGRAPH1 (5, 0);113Graph. Createmaxtrixgraph (1, 2, 2); theGraph. Createmaxtrixgraph (1, 3, 5); theGraph. Createmaxtrixgraph (1, 5, 3); theGraph. Createmaxtrixgraph (2, 4, 4);117Graph. Createmaxtrixgraph (3, 5, 5);118Graph. Createmaxtrixgraph (4, 5, 2);119 graph. Outputmaxtrixgraph (); -Dijkstra Dijkstra =NewDijkstra (graph, 2);121 Dijkstra. Dijkstracore ();122 }123}
1 2 3 4 5 1 0 2 5 0 3 2 2 0 0 4 0 3 5 0 0 0 5 4 0 4 0 0 2 5 3 0 5 2 0 2->1:22->1->3:72->4:42->1->5:5
Java data structure and algorithm------diagram (Shortest path Dijkstra)