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
- 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 used to initialize the data source, returning the starting node of the graph
[Java]View PlainCopy
- Public class Mapbuilder {
- Public Node Build (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 as follows:
The Dijkstra object is used to calculate the shortest path from the starting node to all other nodes
[Java]View PlainCopy
- 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 the open
- Node start=New Mapbuilder (). Build (Open,close);
- return start;
- }
- public void Computepath (Node start) {
- Node Nearest=getshortestpath (start); //Take the nearest child node of the start node and put the close
- if (nearest==null) {
- return;
- }
- Close.add (nearest);
- Open.remove (nearest);
- Map<node,integer> Childs=nearest.getchild ();
- For (Node Child:childs.keySet ()) {
- if (open.contains (child)) {//If sub-nodes are in open
- Integer Newcompute=path.get (Nearest.getname ()) +childs.get (child);
- if (Path.get (Child.getname ()) >newcompute) {//previously set distance is greater than the newly calculated distance
- Path.put (Child.getname (), newcompute);
- Pathinfo.put (Child.getname (), Pathinfo.get (Nearest.getname ()) +"--" +child.getname ());
- }
- }
- }
- Computepath (start); //Repeat yourself to ensure that all child nodes are traversed
- Computepath (nearest); //outward layer recursively until all vertices are traversed
- }
- public void Printpathinfo () {
- Set<map.entry<string, string>> pathinfos=pathinfo.entryset ();
- for (map.entry<string, string> Pathinfo:pathinfos) {
- System.out.println (Pathinfo.getkey () +":" +pathinfo.getvalue ());
- }
- }
- /**
- * Get the nearest child node with 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 (child)) {
- int Distance=childs.get (child);
- if (Distance<mindis) {
- Mindis=distance;
- Res=child;
- }
- }
- }
- return res;
- }
- }
Main for testing Dijkstra objects
[Java]View PlainCopy
- Public class Main {
- public static void Main (string[] args) {
- Dijkstra test=New Dijkstra ();
- Node Start=test.init ();
- Test.computepath (start);
- Test.printpathinfo ();
- }
- }
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)