Dijkstra algorithm Java Reality

Source: Internet
Author: User

First, a path map.


A total of 8 points, the number of lines between each point, indicating the distance between the two points, if there is no direct connection between the two points, then can only be reached through other points.

Dijkstra algorithm, the internet has a high definition, the description of the text is not too popular, and regardless of it.

The following is an example of a to G Shortest path, to talk about the specific algorithm implementation ideas:

Prerequisites: Define a close collection, the parsed points are put into this collection, A is the starting point, and first put into the collection.

1. Starting with a, first look for a link to a nearest (c point)

2. After finding this c point, loop C all the points connected (in fact B and D), recalculate the temporary shortest distance from a to B,a to D (initially a to D is not connected, the distance value between ad is set to ∞, with integer.max_ Value ) and save. Note that this shortest distance is temporary, and there may be a shorter distance before the trailing path is not fully searched. After analyzing the C point, put the C point into the close collection.

3. Look again at a point closest to a (not the point in the close set, that is, the point that has not been analyzed, af=25,ab=13,ad=15 is actually B point)

4. Then perform the Step2 action at point B, and find the temporary shortest distance for all child nodes of a distance B, so repeatedly, until the close collection includes all points.

The specific Java code is as follows:

/** * */package com.test.dijkstra;import java.util.arraylist;import java.util.hashmap;import java.util.LinkedHashSet  Import Java.util.list;import java.util.map;/** * @author Chaisson * @since 2015-5-30 Morning 11:51:59 * */public class Dijkstra {list<node> openlist = new arraylist<node> ()//not visited list<node> closelist = new Arraylist<node> ( );//has accessed Node A = new node ("a"); Node B = new node ("B"); Node C = new node ("C"); Node D = new node ("D"); Node E = new node ("E"); Node F = new node ("F"); Node G = new Node ("G"); Node H = new node ("H");//Initialize the relationship between data nodes private void init () {a.linkednode.add (B); A.linkednode.add (C); A.linkednode.add (F); A.setvalue (b,14); A.setvalue (c,4); A.setvalue (f,25); B.linkednode.add (A); B.linkednode.add (C); B.linkednode.add (E); B.setvalue (A, 14); B.setvalue (C, 9); B.setvalue (E, 7); C.linkednode.add (A); C.linkednode.add (B); C.linkednode.add (D); C.setvalue (A, 4); C.setvalue (B, 9); C.setvalue (D, one);D. Linkednode.add (c);D. Linkednode.add (E);D. Linkednode.add (H);D. SetValue (c, one);D. SETValue (E, N);D. SetValue (H, 5); E.linkednode.add (B); E.linkednode.add (D); E.linkednode.add (F); E.linkednode.add (H); E.setvalue (B, 7); E.setvalue (D, 12); E.setvalue (F, 3); E.setvalue (H, 9); F.linkednode.add (A); F.linkednode.add (E); F.linkednode.add (G); F.setvalue (A, 25); F.setvalue (E, 3); F.setvalue (G, 8); G.linkednode.add (F); G.linkednode.add (H); G.setvalue (F, 8); G.setvalue (H, 17); H.linkednode.add (D); H.linkednode.add (E); H.linkednode.add (G); H.setvalue (D, 5); H.setvalue (E, 9); H.setvalue (G, +); Openlist.add (A); Openlist.add (B); Openlist.add (C); Openlist.add (D); Openlist.add (E); Openlist.add ( F); Openlist.add (G); Openlist.add (H);} Calculates the path that passes from start to end, public void calculate (Node Start,node end) {if (closelist.size () = = Openlist.size ()) { System.out.println (Start.getname () + "-" +end.getname () + "Min.length.length:" +start.getvalue (end); return;} Node Childnode = Getminvaluenode (start);//finds the node Start.getallpassnodes (Childnode) closest to the start node except for the node that has been analyzed. Add ( Childnode);//record expands to the current node of all past nodes if (Childnode = = end) {System.out.println (sTart.getname () + "+" +end.getname () + "Min.length:" +start.getvalue (end)); return;} System.out.println ("Current Distance" +start.getname () + "Nearest node:" +childnode.getname ()); for (node CcNode:childNode.linkedNode) {if (Closelist.contains (Ccnode)) {continue;} /** * Start node distance from one of its nearest nodes (assuming there are 1 or more child nodes) * that is, the distance from the start node to the child sub-node * Recalculate A (assuming start is a, the same below) to all points distance, compared to the original distance */int Ccnodevalue = Start.getvalue (childnode) +childnode.getvalue (Ccnode);//After the maximum value, it becomes negative if (Math.Abs (Ccnodevalue) < Start.getvalue (Ccnode)) {start.setvalue (ccnode,ccnodevalue); System.out.println (Start.getname () + "," and "+ccnode.getname () +" The shortest distance is: "+ccnodevalue);//The shortest distance is only temporary, as long as the analysis is not over, The shortest distance may further reduce start.getallpassnodes (Ccnode). Clear ();//temporary shortest distance reduction, the path passed also cleared to re-add start.getallpassnodes (Ccnode). AddAll ( Start.getallpassnodes (Childnode)); Start.getallpassnodes (Ccnode). Add (Ccnode);} }closelist.add (Childnode); calculate (start,end);//Repeat calculates a to all points of the shortest distance, then take the shortest distance a node, sub-node analysis of it "to the outside node expansion analysis"}// Take the node closest to the entry node, and if there are multiple nodes of the same distance, pick one of the private node Getminvaluenode (node node) {node reTnode = Null;int MinValue = integer.max_value;for (Node n:node.getvaluemap (). KeySet ()) {if (Closelist.contains (n)) { Continue;} if (Node.getvalue (n) < MinValue) {minValue = Node.getvalue (n); retnode = n;}} return retnode;} public static void Main (string[] args) {Dijkstra d = new Dijkstra ();d. Init ();d. Closelist.add (D.A);d. Calculate (D.A, D.G) ;//print path for (Node node:d.a.getallpassnodes (D.G)) {System.out.print (Node.getname () + ");}}} Class Node {Private String name;//log node all connected nodepublic list<node> Linkednode = new arraylist<node> ();// Log the shortest distance between node and other node private map<node,integer> ValueMap = new hashmap<node,integer> ();// Records all the nodes that pass from this node to the other node, and maintains the order, in fact, corresponding to the ValueMap private map<node,linkedhashset<node>> Ordersetmap = new Hashmap<node,linkedhashset<node>> ();p ublic Node (String name) {this.name = name;} public void SetValue (node Node,integer value) {Valuemap.put (node, value);} If there is no value for this node to the parameter node, the default maximum public Integer GetValue (node node) {return ValuemaP.get (node) = = null? Integer.MAX_VALUE:valueMap.get (node);} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Map<node, Integer> Getvaluemap () {return valueMap;} Take this node to the parameter node all of the node sets passed by public linkedhashset<node> getallpassnodes (node node) {if (ordersetmap.get (node) = = null) { linkedhashset<node> set = new linkedhashset<node> (); Set.add (this); Ordersetmap.put (node, set);} return Ordersetmap.get (node);}}
The main method performs a calculation from A to G, with the following result:

Current minimum distance for a->b is: 13
Current minimum distance for a->d is: 15
Current minimum distance for a->e is: 20
Current minimum distance for a->h is: 20
Current minimum distance for A->g is: 37
Current minimum distance for a->f is: 23
Current minimum distance for a->g is: 31
A->g min.length:31
A->c->b->e->f->g->




Dijkstra algorithm Java Reality

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.