Directory
1 Problem Description
2 Solutions
Introduction to the principle of 2.1 dynamic programming Method
2.2 Specific Code
2.3 Running Results
1 problem description
(1) experimental topics
given a weighted connected graph ( undirected or directed ), it is required to find the shortest path from each point to all other fixed points and the length of the shortest path.
(2) Purpose of the experiment
1) deeply grasp the design idea of dynamic programming method and be proficient in it, and understand the difference between it and the method of division and treatment;
2) Mastering the optimality principle and the optimal substructure property;
3) Understand that the key to solving the problem with dynamic programming method is to determine the recurrence of dynamic programming function.
(3) experimental requirements
1) Implement Floyd algorithm;
2) The input of the algorithm can be entered manually, or it can be generated automatically;(PS: here for the weighted matrix of the graph of the manual input is a bit cumbersome, so this article only to achieve the given weight matrix value, calculate the final result )
3) The algorithm not only outputs the shortest path from each vertex to all other vertices, but also the length of the output shortest path;
4) Design An example of a graph or a graph with a negative weight, for whichtheFloyd algorithm cannot output the correct results. (PS: The problem here is that the weight becomes negative, instead the result is the longest path result)
2 Solutions
Introduction to the principle of 2.1 Dynamic programming method
Dynamic programming algorithms are often used to solve problems with some kind of optimal properties. There may be many possible solutions to this type of problem. Each solution corresponds to a value, and we want to find the solution with the best value. The dynamic programming algorithm is similar to the partition method, and its basic idea is to decompose the problem into several sub-problems, solve the problem first, then get the solution of the original problem from the solution of these sub-problems. Unlike the divide-and-conquer method, the If you use divide-and-conquer method to solve this kind of problem, the number of sub-problems is too many, some sub-problems have been repeated calculation many times. If we can fill form
The biggest difference from the divide-and-conquer approach is : It is suitable for solving problems by dynamic programming method, which is obtained by decomposition. sub-problems are often not independent of each other (i.e. the solution of the next sub-stage is based on the solution of the previous sub-stage and is further solved)
2.2 specific code
PackageCom.liuzhen.floyd; Public classFloyd {//The distance matrix and the middle node path matrix of the final graph are returned according to the weighted matrix of the graph and the initial intermediate node path matrix. Public Static voidGetshortestpath (int[] Chart,int[] path) { intLen =chart.length; //int k = 0; for(intK = 0;k < len;k++) {//k = 0 means, increase k = 0 that one middle Vertex, k self-increment, indicating subsequent increase of the K middle vertex for(inti = 0;i < len;i++){ for(intj = 0;j < len;j++){ inttemp = 0;//when a new intermediate vertex is added, the path length from I to K to J is stored with this variable if(Chart[i][k]! = && Chart[k][j]! = 100)//when A[i][k] and a[k][j] paths can be walked, 100 means two vertices are not connectedtemp = Chart[i][k] +Chart[k][j];//System.out.println ("Middle vertex k Value:" +k);//System.out.println ("chart[" +i+ "]" + "[" +k+ "] =" +chart[i][k]);//System.out.println ("chart[" +k+ "]" + "[" +j+ "] =" +chart[k][j]); if(Chart[i][j] > Temp && temp! = 0) {//if the current I-to-J path length (including the inability to reach the condition) is greater than the path with K as the middle vertexCHART[I][J] =temp; PATH[I][J]= k+1;//when the two vertices are connected and are the shortest path, the k+1 is stored in the middle node path Matrix Path } } } } } //returns the shortest path between the two vertices, based on the middle node path matrix of the graph and two vertices Public StaticString Getoneshortestpath (int[] Path,intStartintend) { CharStartnode = (Char) (' a ' +start); CharEndnode = (Char) (' a ' +end); String Nodepath= ""; if(Path[start][end] = = 0) {Nodepath+ = startnode+ "+" +Endnode; returnNodepath; } intMiddle = path[start][end]-1; //Solving shortest paths with recursionNodepath + = Getoneshortestpath (path,start,middle) + "," +Getoneshortestpath (path,middle,end); returnNodepath; } //the shortest path and shortest path length between all vertices of the output graph Public Static voidPrintshortestpath (int[] Path,int[] [] result) { intLen =path.length; for(inti = 0;i < len;i++){ CharStartnode = (Char) (' a ' +i); for(intj = 0;j < len;j++){ CharEndnode = (Char) (' a ' +j); String Ijpath= startnode+ "-" +endnode+ "Shortest Path:"; String Nodepath=Getoneshortestpath (PATH,I,J); System.out.println (Ijpath+nodepath+ ". The path length is: "+Result[i][j]); } } } Public Static voidMain (String args[]) {/*in the chart array, the array 0,1,2,3 represents the weight value between two vertices, that is, the distance between the two vertices, and 100 means that the two vertices are not connected .*/ int[] chart = {{0,100,3,100},{2,0,100,100},{100,7,0,1},{6,100,100,0}}; System.out.println (The weighted matrix for a graph chart is (PS: where a value of 100 means infinity, i.e. a path that cannot be communicated): "); System.out.println ("\ T" + "a" + "\ T" + "B" + "\ T" + "C" + "\ T" + "D"); for(inti = 0;i < 4;i++){ CharStartnode = (Char) (' a ' +i); System.out.print (Startnode+ "\ T"); for(intj = 0;j < 4;j++) System.out.print (Chart[i][j]+ "\ T"); System.out.println (); } /*in the path array, 0 means two vertices are connected, and 1 means there is an intermediate node between the two vertices a,2 means there is an intermediate node between the two vertices b,3 two vertices have an intermediate node C, and 42 vertices have an intermediate node D. * 100 means two vertices are not connected*/ int[] path = {{0,100,0,100},{0,0,100,100},{100,0,0,0},{0,100,100,0}}; Getshortestpath (Chart,path); System.out.println (The distance matrix for the graph chart is: "); System.out.println ("\ T" + "a" + "\ T" + "B" + "\ T" + "C" + "\ T" + "D"); for(inti = 0;i < 4;i++){ CharStartnode = (Char) (' a ' +i); System.out.print (Startnode+ "\ T"); for(intj = 0;j < 4;j++) System.out.print (Chart[i][j]+ "\ T"); System.out.println (); } System.out.println (The middle node path matrix for a directed graph chart is (PS: A value of 0 means that two nodes are directly connected, a value of 1 means that two nodes have an intermediate node A, a value of 2 means that the middle node is B, and so on): "); System.out.println ("\ T" + "a" + "\ T" + "B" + "\ T" + "C" + "\ T" + "D"); for(inti = 0;i < 4;i++){ CharStartnode = (Char) (' a ' +i); System.out.print (Startnode+ "\ T"); for(intj = 0;j < 4;j++) System.out.print (Path[i][j]+ "\ T"); System.out.println (); } System.out.println ("The final result is:"); Printshortestpath (Path,chart); }}
2.3 Running results
The weighted matrix of a graph chart is (PS: where a value of 100 means infinity, i.e. a path that cannot be communicated): a B c da0 100 3 100b2 0 100 100C100 7 0 1D6 100 100 0the distance matrix for a graph chart is: a b c da0 10 3 4b2 0 5 6C7 7 0 1D6 16 9 0The middle node path matrix for a directed graph chart is (PS: A value of 0 means that two nodes are directly connected, a value of 1 means that the two node has an intermediate node A, a value of 2 means that the middle node is B, and so on): a b c da0 3 0 3b0 0 1 3C4 0 0 0D0 3 1 0The final result is: a-->a Shortest path is: A->a. Path length is: 0a-->b Shortest path is: A->c, c->b. Path length is: 10a-->c Shortest path is: a->c. Path length is: 3a-->d Shortest path is: A->c, c->d. Path length is: 4b-->a Shortest path is: B->a. Path length is: 2b-->b Shortest path is: b->b. Path length is: 0b-->c Shortest path is: B->a, a->c. Path length is: 5b-->d Shortest path is: B->a, A->c, C->d. Path length is: 6c-->a Shortest path is: C->d, D->a. Path length is: 7c-->b Shortest path is: c->b. Path length is: 7c-->c Shortest path is: c->c. Path length is: 0c-->d Shortest path is: c->d. Path length is: 1d-->a Shortest path is: D->a. Path length is: 6d-->b Shortest path is: D->a, A->c, C->b. Path length is: 16d-->c Shortest path is: D->a, a->c. Path length is: 9d-->d Shortest path is: d->d. Path length is: 0
Resources:
1, http://www.360doc.com/content/13/0601/00/8076359_289597587.shtml
Algorithm Note _006: "Dynamic Programming Method" for the shortest path problem of the whole source