Dijkstra algorithm to find the shortest path (Java)

Source: Internet
Author: User

SOURCE Link: Dijkstra Algorithm for Shortest path (Java) Task description: Gets the shortest path description of the starting node to all other nodes in an no-map diagram

The Dijkstra (Dijkstra) algorithm is a typical shortest path routing algorithm used to calculate the shortest path of a node to all other nodes. The main feature is to extend from the center of the starting point to the outer layer until it expands to the end point.

Dijkstra general expressions usually have two ways, a permanent and temporary marking method, one is open, close table
Using the method of Open,close table, it uses the greedy algorithm strategy, the approximate process is as follows:
1. Declare two collections, open and Close,open to store the nodes that are not traversed, and close to store the traversed nodes
2. Initial phase, put the initial node in close, all other nodes into the open
3. Traverse the outer layer at the center of the initial node, get the child node closest to the specified node and place it in close and calculate the new path until close contains all child nodes

The code example is as follows:
The node object is used to encapsulate the nodes ' information, including the name and child nodes
[Java]View PlainCopy
  1. Public class Node {
  2. private String name;
  3. Private map<node,integer> child=new hashmap<node,integer> ();
  4. Public Node (String name) {
  5. This.name=name;
  6. }
  7. Public String GetName () {
  8. return name;
  9. }
  10. public void SetName (String name) {
  11. this.name = name;
  12. }
  13. Public Map<node, integer> getchild () {
  14. return child;
  15. }
  16. public void SetChild (Map<node, integer> child) {
  17. this.child = child;
  18. }
  19. }

Mapbuilder used to initialize the data source, returning the starting node of the graph
[Java]View PlainCopy
  1. Public class Mapbuilder {
  2. Public Node Build (set<node> Open, set<node> close) {
  3. Node nodea=new node ("A");
  4. Node nodeb=new node ("B");
  5. Node nodec=new node ("C");
  6. Node noded=new node ("D");
  7. Node nodee=new node ("E");
  8. Node nodef=new node ("F");
  9. Node nodeg=new node ("G");
  10. Node nodeh=new node ("H");
  11. Nodea.getchild (). Put (NodeB, 1);
  12. Nodea.getchild (). Put (NodeC, 1);
  13. Nodea.getchild (). Put (noded, 4);
  14. Nodea.getchild (). Put (Nodeg, 5);
  15. Nodea.getchild (). Put (Nodef, 2);
  16. Nodeb.getchild (). Put (NodeA, 1);
  17. Nodeb.getchild (). Put (Nodef, 2);
  18. Nodeb.getchild (). Put (Nodeh, 4);
  19. Nodec.getchild (). Put (NodeA, 1);
  20. Nodec.getchild (). Put (Nodeg, 3);
  21. Noded.getchild (). Put (NodeA, 4);
  22. Noded.getchild (). Put (NodeE, 1);
  23. Nodee.getchild (). Put (noded, 1);
  24. Nodee.getchild (). Put (Nodef, 1);
  25. Nodef.getchild (). Put (NodeE, 1);
  26. Nodef.getchild (). Put (NodeB, 2);
  27. Nodef.getchild (). Put (NodeA, 2);
  28. Nodeg.getchild (). Put (NodeC, 3);
  29. Nodeg.getchild (). Put (NodeA, 5);
  30. Nodeg.getchild (). Put (Nodeh, 1);
  31. Nodeh.getchild (). Put (NodeB, 4);
  32. Nodeh.getchild (). Put (Nodeg, 1);
  33. Open.add (NodeB);
  34. Open.add (NODEC);
  35. Open.add (noded);
  36. Open.add (NodeE);
  37. Open.add (NODEF);
  38. Open.add (NODEG);
  39. Open.add (Nodeh);
  40. Close.add (NodeA);
  41. return NodeA;
  42. }
  43. }
The structure of the diagram is as follows:


The Dijkstra object is used to calculate the shortest path from the starting node to all other nodes
[Java]View PlainCopy
  1. Public class Dijkstra {
  2. set<node> open=New hashset<node> ();
  3. set<node> close=New hashset<node> ();
  4. map<string,integer> path=New hashmap<string,integer> (); Package path Distance
  5. map<string,string> pathinfo=New hashmap<string,string> (); Package path Information
  6. Public Node Init () {
  7. //initial path, because there is no a->e this path, so path (E) is set to Integer.max_value
  8. Path.put ("B", 1);
  9. Pathinfo.put ("B", "a->b");
  10. Path.put ("C", 1);
  11. Pathinfo.put ("C", "a->c");
  12. Path.put ("D", 4);
  13. Pathinfo.put ("D", "a->d");
  14. Path.put ("E", Integer.max_value);
  15. Pathinfo.put ("E", "A");
  16. Path.put ("F", 2);
  17. Pathinfo.put ("F", "a->f");
  18. Path.put ("G", 5);
  19. Pathinfo.put ("G", "a->g");
  20. Path.put ("H", Integer.max_value);
  21. Pathinfo.put ("H", "A");
  22. //Put the initial node in close and the other nodes into the open
  23. Node start=New Mapbuilder (). Build (Open,close);
  24. return start;
  25. }
  26. public void Computepath (Node start) {
  27. Node Nearest=getshortestpath (start); //Take the nearest child node of the start node and put the close
  28. if (nearest==null) {
  29. return;
  30. }
  31. Close.add (nearest);
  32. Open.remove (nearest);
  33. Map<node,integer> Childs=nearest.getchild ();
  34. For (Node Child:childs.keySet ()) {
  35. if (open.contains (child)) {//If sub-nodes are in open
  36. Integer Newcompute=path.get (Nearest.getname ()) +childs.get (child);
  37. if (Path.get (Child.getname ()) >newcompute) {//previously set distance is greater than the newly calculated distance
  38. Path.put (Child.getname (), newcompute);
  39. Pathinfo.put (Child.getname (), Pathinfo.get (Nearest.getname ()) +"--" +child.getname ());
  40. }
  41. }
  42. }
  43. Computepath (start); //Repeat yourself to ensure that all child nodes are traversed
  44. Computepath (nearest); //outward layer recursively until all vertices are traversed
  45. }
  46. public void Printpathinfo () {
  47. Set<map.entry<string, string>> pathinfos=pathinfo.entryset ();
  48. for (map.entry<string, string> Pathinfo:pathinfos) {
  49. System.out.println (Pathinfo.getkey () +":" +pathinfo.getvalue ());
  50. }
  51. }
  52. /** 
  53. * Get the nearest child node with node
  54. */
  55. Private node Getshortestpath (node node) {
  56. Node res=null;
  57. int mindis=integer.max_value;
  58. Map<node,integer> Childs=node.getchild ();
  59. For (Node Child:childs.keySet ()) {
  60. if (open.contains (child)) {
  61. int Distance=childs.get (child);
  62. if (Distance<mindis) {
  63. Mindis=distance;
  64. Res=child;
  65. }
  66. }
  67. }
  68. return res;
  69. }
  70. }

Main for testing Dijkstra objects
[Java]View PlainCopy
    1. Public class Main {
    2. public static void Main (string[] args) {
    3. Dijkstra test=New Dijkstra ();
    4. Node Start=test.init ();
    5. Test.computepath (start);
    6. Test.printpathinfo ();
    7. }
    8. }

Print out the following:
D:a->d
E:a->f->e
F:a->f
G:a->c->g
B:a->b
C:a->c
H:a->b->h Reference Link: Java implementation Dijkstra algorithm to find the shortest path in Java to write a Dijkstra algorithm (single source shortest path algorithm, Dijkstra algorithm) shortest path-dijkstra algorithm and Floyd algorithm

Dijkstra algorithm for Shortest Path (Java) (RPM)

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.