Johnson method to find all nodes to the shortest path _ algorithm

Source: Internet
Author: User
Tags addall
Package com.data.struct;
Import Java.util.HashSet;
Import Java.util.Iterator;
Import Java.util.Random;

Import Java.util.Set;
	public class Johnson {private node[][] graphic;
	Private node[][] origonal;
	Private Node [][]result;
	Private Node S;
	Private set<node> nextnodes = new hashset<node> ();
	Private set<node> addlist = new hashset<node> ();

	Private set<node> removelist = new hashset<node> ();
		public Johnson (int v, int e) {origonal = new node[v][v];
		Result=new Node[v][v];
			for (int i = 0; i < e; i++) {int v1 = new Random (). Nextint (v);
			int v2 = new Random (). Nextint (v);
			Node node = new node ();
			NODE.W = new Random (). Nextint (v) + 1;
			NODE.D = integer.max_value-1000;
			Node.start = v1;
			Node.end = v2;
		ORIGONAL[V1][V2] = node;
				for (int i = 0; i < v i++) {if (origonal[i][i] = null) {node node = new Node ();
				NODE.D = integer.max_value-1000;
				NODE.W = 0;
				Node.start = i;
Node.end = i;				Origonal[i][i] = node;
			else {origonal[i][i].w = 0;
		} graphic = new Node[v + 1][v + 1]; for (int i = 0; i < origonal.length. i++) {for (int j = 0; J < Origonal.length; J +) {Graphic[i][j] = Origo
			NAL[I][J];
			for (int i = 0; i < v + 1; i++) {node node = new Node ();
			Node.start = Origonal.length;
			NODE.D = integer.max_value-1000;
			Node.end = i;
		Graphic[origonal.length][i] = node;
		} s = graphic[0][0];

	S.D = 0;
			Public node[][] Johnson () {if (!bellmanford ()) {System.out.println ("Matrix contain negtive cycle");
		return null;
			else {printtmp ();
			for (int i = 0; i < graphic.length i++) {graphic[i][i].h = GRAPHIC[GRAPHIC.LENGTH-1][I].D; for (int i = 0; i < graphic.length. i++) {for (int j = 0; J < Graphic.length; J +) {if graphic[i][j
					]!= null) {GRAPHIC[I][J].W = graphic[i][j].w + graphic[i][i].h-graphic[j][j].h; for (int i=0;i<origonal.length;i++) {Dijkstra (origonal[i][i]);
				Printresult (); for (int j = 0; J < Graphic.length; J +) {if (graphic[i][j]!= null) {GRAPHIC[I][J].D = GRAPHIC[I][J].D-
					Graphic[i][i].h + graphic[j][j].h;
	}}} return result;
		Public Node extractmin () {iterator<node> it = nextnodes.iterator ();
		node node = null;
			while (It.hasnext ()) {if (node = = null) {node = It.next ();
				else {Node n = it.next ();
				if (N.D < NODE.D) {node = n;
	}} return node;
				} public void Dijkstra (Node s) {for (int i=0;i<origonal.length;i++) {for (int j=0;j<origonal.length;j++) {
				if (origonal[i][j]!=null) {origonal[i][j].d=integer.max_value-1000;
		}} s.d=0;
		Nextnodes.clear ();
		for (int i = 0; i < origonal.length i++) {nextnodes.add (origonal[i][i]); for (int k = 0; k < origonal.length k++) {if (k!= s.start && Origonal[s.start][k]!= nulL) {Relex (S, Origonal[k][k]);
		} nextnodes.remove (s);
			while (Nextnodes.size () > 0) {node node = extractmin ();
					for (int k = 0; k < origonal.length; k++) {if (k!= node.start && origonal[node.start][k]!= null) {
				Relex (node, origonal[k][k]);
		} nextnodes.remove (node);
			for (int i=0;i<origonal.length;i++) {node node=new node ();
			Node.start=s.start;
			Node.end=i;
			NODE.D=ORIGONAL[I][I].D;
		Result[s.start][i]=node; } public void Relex (node u, node v) {if (Graphic[v.end][v.end].d > u.d + graphic[u.start][v.start].w) {Grap
			HIC[V.END][V.END].D = U.d + graphic[u.start][v.start].w;
			graphic[v.end][v.end].parent = u;
			Addlist.add (Graphic[v.end][v.end]);
		System.out.println (Graphic[v.end][v.end].start + "=>" + U.start + "| |" + GRAPHIC[V.END][V.END].D);
		} public boolean Bellmanford () {s = graphic[graphic.length-1][graphic.length-1];
		S.D = 0;
		Addlist.clear (); Nextnodes.clear ();
		Removelist.clear ();
		Nextnodes.add (s); for (int k = 0; k < graphic.length; k++) {if (k!= s.start && graphic[s.start][k]!= null) {Relex (S, g
			Raphic[k][k]);
		} nextnodes.addall (Addlist);
		Nextnodes.remove (s);
			while (Nextnodes.size () > 0) {addlist.clear ();
			Removelist.clear ();
			Iterator<node> it = Nextnodes.iterator ();
				while (It.hasnext ()) {node node = It.next ();
						for (int k = 0; k < graphic.length; k++) {if (k!= node.start && graphic[node.start][k]!= null) {
					Relex (node, graphic[k][k]);
			} removelist.add (node);
			} nextnodes.removeall (Removelist);

		Nextnodes.addall (addlist);
					for (int j = 0; J < Graphic.length; J +) {for (int k = 0; k < graphic.length; k++) {if (J!= k) { if (Graphic[j][k]!= null && graphic[k][k].d > GRAPHIC[J][J].D + graphic[j][k].w) {Retu
					RN false;

	}}} return true; } public void Printresult () {System.out.println ("Result:");  for (int i = 0; i < result.length. i++) {for (int j = 0; J < Result.length; J +) {if (result[i][j]!= null)
				{System.out.print (result[i][j].d + "");
				else {System.out.print ("" + "");
		} System.out.println ();
		} public void Printtmp () {System.out.println ("tmp:"); for (int i = 0; i < graphic.length. i++) {for (int j = 0; J < Graphic.length; J +) {if (graphic[i][j)!= nu
				ll) {System.out.print (graphic[i][j].d + "");
				else {System.out.print ("" + "");
		} System.out.println ();
		}} public void print () {System.out.println ("origranal:"); for (int i = 0; i < graphic.length. i++) {for (int j = 0; J < Graphic.length; J +) {if (graphic[i][j)!= nu
				ll) {System.out.print (GRAPHIC[I][J].W + "");
				else {System.out.print ("" + "");
		} System.out.println ();
		} node[][] L = Johnson (); System.out.priNTLN ("Result:"); for (int i = 0; i < l.length; i++) {for (int j = 0; J < L.length; J +) {if (l[i][j]!= null) {System.
				Out.print (L[I][J].D + "");
				else {System.out.print ("" + "");
		} System.out.println ();
		} private class Node {private int D;
		private int w;
		private int start;
		private int end;
		Private Node parent;
	private int h;
		public static void Main (string[] args) {Johnson j = new Johnson (5, 20);

	J.print ();
 }

}

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.