An introduction to algorithms--all the shortest paths to the points: Sparse graph Johnson algorithm

Source: Internet
Author: User

Package Org.loda.graph;import org.loda.structure.stack;import org.loda.util.in;/** * * @ClassName: Johnson time Complexity: EVLGV * @Description: The Johnson algorithm on the sparse graph, because the sparse graph data structure is recommended to use adjacent linked list, so there is also the adjacency linked list, the algorithm is used for sparse graph, if it is dense graph, it is recommended to implement a simpler floydwashall algorithm, The time complexity of the v^3 can be guaranteed * * The Johnson algorithm uses a weight equal to each edge, so that all edges are non-negative, so that a more efficient Dijkstra algorithm can be used for each edge. * Note that it is not easy to add the same value to each side and then make all the edges become non-negative, the reason is that there are two paths from the a->b, a weight of 2, one for the weight and equal, and if all add 1, it becomes the 3 and the same, inconsistent, will cause the update of the edge * * Johnson's clever introduction of the H function to solve this problem, through this function for each side of the re-assignment weight * * @author Minjun * @date June 2, 2015 afternoon 5:49:13 * */public class Johnson {/* * * Weight function h is used to re-assign weight function, use array to store each I corresponding weight function value */private double[] h;/** * distance */private double[][] dist;/** * precursor node */private int[] [] prev;/** * contains negative ring */private Boolean negativecycle; @SuppressWarnings ("Unchecked") public Johnson (Weightdigraph g) {int v = G.V (); h = new Double[v];d ist = new Double[v][v];p rev = new int[v][v];//First Bellmanford algorithm to determine if there is a negative ring, if present, stop calculating the shortest path Bel Lmanford bf = new Bellmanford (g, 0), if (Bf.hasnegcycle ()) {negativecycle = true; SysteM.out.println ("There is a negative ring, there is no shortest path ..."); return;} /** * Use Johnson algorithm to calculate shortest path */computeshortestpath (g);} public void Computeshortestpath (Weightdigraph g) {int v = G.V ();//Create a new diagram G,g contains an extra vertex s ' as the origin of the H function (currently will s ' Set to vertex with index V) weightdigraph G = new Weightdigraph (v + 1), for (int i = 0; i < V; i++) {//To connect new vertex v to other nodes and set their distance to 0g.add (V, I , 0); for (Edge E:g.adj (i)) {int J = e.otherside (i); G.add (i, J, E.weight ());}} int V = G.V (); Bellmanford bf = new Bellmanford (G, V-1);//Assign a value for the weight function for (int i = 0; i < V; i++) {H[i] = Bf.distto (i);}  Weightdigraph WG = new Weightdigraph (v);//Create a new graph, add all the updated weighted nodes and edges to the graph, when the graph is a non-negative weight graph, you can use the Dijkstra algorithm for (int i = 0; i < V; i++) {for (Edge E:g.adj (i)) {int J = e.otherside (i); Wg.add (i, J, E.weight () + h[i]-h[j]);}} for (int i = 0; i < V; i++) {//Use Dijkstra algorithm for each node Dijkstra d = new Dijkstra (WG, I); for (int j = 0; J < V; j + +) {//due to each A certain weight is added to the edges, where the added weight minus dist[i][j] = D.distto (j) + H[j]-h[i];d in order to revert to the original distance. Pathto (i);p Rev[i]=d.prev;}} /** * * @Title: Hasnegtivecycle * @Description: Contains negative ring * @param @return Set File * @return Boolean return type * @throws */public boolean hasnegtivecycle () {return negativecycle;} /** * * @Title: Distto * @Description: i->j distance * @param @param i * @param @param j * @param @return Setting file * @return Dou ble return type * @throws */public double distto (int i, int j) {return dist[i][j];} public iterable<integer> pathto (int i,i  NT J) {int[] p=prev[i]; Stack<integer> path=new stack<integer> (); for (int m=j;m!=i;m=p[m]) {Path.push (M);} path.push (i); return path; }public static void Main (string[] args) {//Text data with no negative weight ring String Text1 = "f:\\ algorithm \\attach\\tinyEWDn.txt";// Text data containing a negative weight ring string text2 = "f:\\ algorithm \\attach\\tinyEWDnc.txt"; Weightdigraph g = new Weightdigraph (New in (Text1));  Johnson d = new Johnson (g), if (D.hasnegtivecycle ()) {System.out.println ("The weighted graph contains a negative weight ring, there is no shortest path");} else {int s = 0;for (int i = 0; I < G.V (); i++) {System.out.println ("from Origin" + S + "to" + i + "distance is" + d.distto (s, i) + ", Path is:"), for (int M:d.pathto (S, i)) {System.out.print ( m+ "->");} System.out.println ();}}}

If a negative loop diagram is used, the result is:

There is a negative ring, there is no shortest path ... The weighted graph contains a negative weight ring, and no shortest path exists.

If a non-negative ring graph is used, the result is:

From Origin 0 to 0 distance is 0.0, path is:0-> from Origin 0 to 1 distance is 0.93, path is:0->2->7->3->6->4->5->1-> from Origin 0 to 2 distance is 0.26, path is: 0- >2-> from Origin 0 to 3 distance is 0.9900000000000001, path is:0->2->7->3-> from Origin 0 to 4 distance is 0.26000000000000023, path is: 0->2- >7->3->6->4-> distance from Origin 0 to 5 is 0.6100000000000001, path is:0->2->7->3->6->4->5-> From Origin 0 to 6 distance is 1.5100000000000002, path is:0->2->7->3->6-> from Origin 0 to 7 distance is 0.6000000000000001, path is: 0->2->7- >

An introduction to algorithms--all the shortest paths to the points: Sparse graph Johnson algorithm

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.