Dijkstra algorithm to find shortest path (Java) __ algorithm

Source: Internet
Author: User
Task Description: Gets the shortest path description of the starting node to all other nodes in a non-direction graph

Dijkstra (Dijkstra) algorithm is a typical shortest path routing algorithm, which is used to compute the shortest path of a node to all other nodes. The main feature is to extend the starting point to the outer layer until the end is extended.

There are usually two ways to Dijkstra general expressions, one in the form of permanent and temporary labeling, and one with open, close table.
Using the Open,close table method, it uses the greedy algorithm strategy, the process is as follows:
1. Declare two sets, open and Close,open are used to store the traversed nodes, and close is used to store the traversed nodes
2. Initial phase, put the initial node in close, all other nodes into open
3. Take the initial node as the center to the outer layer, get the child node closest to the specified node into the close and recalculate the path until it contains all the child nodes

The code example is as follows:
Node objects are used to encapsulate information about nodes, including names and child nodes
public class Node {
	private String name;
	Private map<node,integer> child=new hashmap<node,integer> ();
	Public Node (String name) {
		this.name=name;
	}
	Public String GetName () {return
		name;
	}
	public void SetName (String name) {
		this.name = name;
	}
	Public Map<node, Integer> Getchild () {return child
		;
	}
	public void SetChild (Map<node, integer> child) {
		this.child = child;
	}
}

Mapbuilder is used to initialize the data source and return the starting node of the diagram
public class Mapbuilder {public node builds (set<node> open, set<node> close) {Node nodea=new node ("A");
		Node nodeb=new node ("B");
		Node nodec=new node ("C");
		Node noded=new node ("D");
		Node nodee=new node ("E");
		Node nodef=new node ("F");
		Node nodeg=new node ("G");
		Node nodeh=new node ("H");
		Nodea.getchild (). Put (NodeB, 1);
		Nodea.getchild (). Put (NodeC, 1);
		Nodea.getchild (). Put (noded, 4);
		Nodea.getchild (). Put (nodeg, 5);
		Nodea.getchild (). Put (Nodef, 2);
		Nodeb.getchild (). Put (NodeA, 1);
		Nodeb.getchild (). Put (Nodef, 2);
		Nodeb.getchild (). Put (Nodeh, 4);
		Nodec.getchild (). Put (NodeA, 1);
		Nodec.getchild (). Put (nodeg, 3);
		Noded.getchild (). Put (NodeA, 4);
		Noded.getchild (). Put (NodeE, 1);
		Nodee.getchild (). Put (noded, 1);
		Nodee.getchild (). Put (nodef, 1);
		Nodef.getchild (). Put (NodeE, 1);
		Nodef.getchild (). Put (NodeB, 2);
		Nodef.getchild (). Put (NodeA, 2);
		Nodeg.getchild (). Put (NodeC, 3);
		Nodeg.getchild (). Put (NodeA, 5);
		Nodeg.getchild (). Put (Nodeh, 1); NodEh.getchild (). Put (NodeB, 4);
		Nodeh.getchild (). Put (nodeg, 1);
		Open.add (NodeB);
		Open.add (NODEC);
		Open.add (noded);
		Open.add (NodeE);
		Open.add (NODEF);
		Open.add (NODEG);
		Open.add (Nodeh);
		Close.add (NodeA);
	return NodeA; }
}
The structure of the diagram is shown in the following illustration:


The Dijkstra object is used to calculate the shortest path from the starting node to all other nodes
public class Dijkstra {set<node> open=new hashset<node> ();
	Set<node> close=new hashset<node> (); Map<string,integer> path=new hashmap<string,integer> ();//package path distance map<string,string> pathInfo=new
		Hashmap<string,string> ()//package path information public Node init () {//initial path, because there is no a->e this path, so path (E) is set to Integer.max_value
		Path.put ("B", 1);
		Pathinfo.put ("B", "a->b");
		Path.put ("C", 1);
		Pathinfo.put ("C", "a->c");
		Path.put ("D", 4);
		Pathinfo.put ("D", "a->d");
		Path.put ("E", Integer.max_value);
		Pathinfo.put ("E", "A");
		Path.put ("F", 2);
		Pathinfo.put ("F", "a->f");
		Path.put ("G", 5);
		Pathinfo.put ("G", "a->g");
		Path.put ("H", Integer.max_value);
		Pathinfo.put ("H", "A");
		Put the initial node in close and the other nodes into open node Start=new mapbuilder (). Build (Open,close);
	return start; The public void Computepath (node start) {node Nearest=getshortestpath (start), or the child node closest to the start node, is placed in the close if (Nearest==nul
		L) {return;
		} close.add (nearest); OpeN.remove (nearest);
		Map<node,integer> Childs=nearest.getchild (); For (node Child:childs.keySet ()) {if (open.contains) {//If the child node is an Integer newcompute=path.get in open (Nearest.getnam
				E ()) +childs.get (child);
					if (Path.get (Child.getname ()) >newcompute) {//The distance previously set is greater than the newly computed distance path.put (Child.getname (), newcompute);
				Pathinfo.put (Child.getname (), Pathinfo.get (Nearest.getname ()) + "->" +child.getname ()); }} computepath (start);//repeat itself to ensure that all child nodes are traversed computepath (nearest);//to the outer layer recursively until all vertices are traversed} public void Printpathin
		Fo () {set<map.entry<string, string>> pathinfos=pathinfo.entryset (); For (map.entry<string, string> Pathinfo:pathinfos) {System.out.println (Pathinfo.getkey () + ":" +
		Pathinfo.getvalue ());
		}/** * Gets the child node closest to node/private node Getshortestpath (node node) {node res=null;
		int mindis=integer.max_value;
		Map<node,integer> Childs=node.getchild ();
			For (Node Child:childs.keySet ()) {if (open.contains) {	int Distance=childs.get (child);
					if (Distance<mindis) {mindis=distance;
				Res=child;
	}} return res; }
}

Main is used to test Dijkstra objects
public class Main {public
	static void Main (string[] args) {
		Dijkstra test=new Dijkstra ();
		Node start=test.init ();
		Test.computepath (start);
		Test.printpathinfo ();
	}

The printout is as follows:
D:a->d
E:a->f->e
F:a->f
G:a->c->g
B:a->b
C:a->c
H:a->b->h
Related Article

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.