Dijkstra Shortest Path algorithm based on Java implementation __ algorithm

Source: Internet
Author: User
Package graph;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.LinkedList;
Import Java.util.Map;

Import Java.util.Map.Entry; /** * @author Wyhong * * */public class Dijkstra {public class node{/** * node identifier/private Integer Identi
		Fier;
		/** * Whether the node has been visited/private Boolean visited = false;
		
		/** * The mapping relationship between this node and other nodes * * private map<node,integer> mapping = new hashmap<node,integer> ();
		Public Integer Getidentifier () {return identifier;
		The public void Setidentifier (Integer identifier) {this.identifier = identifier;
		public Boolean isvisited () {return visited;
		} public void Setvisited (Boolean visited) {this.visited = visited;
		Public Map<node, Integer> getmapping () {return mapping; }/** * Getting optimal path by Dijkstra algorithm * @param src * @param dest * @return/public static Linkedlist<node> Getoptimalpath (node src, node dest) {return Dijkstra (sRC, dest, 0, 0, New linkedlist<node> ()); /** * Dijkstra algorithm * @param src start node * @param dest Target node * @param the distance between the starting node and the target node * @para The sum of the shortest path weights of M optimaldist * @param optimalpath Shortest Path * @return/private static linkedlist<node> Dijkstra (Node s RC, Node dest, int adjacentdist, int optimaldist, linkedlist<node> optimalpath) {if (Optimalpath.size () ==0) {opt
		Imalpath.add (SRC);
		}//The mapping relationship between the current node and other nodes map<node,integer> mapping = src.getmapping ();
		The minimum distance int partialmindist = 0 for the current node and its neighboring nodes;
		The next optimal node Partialoptimalnode = null for the current preselection;
		Iterator<entry<node, integer>> entryiterator = Mapping.entryset (). iterator (); 
			while (Entryiterator.hasnext ()) {entry<node, integer> Entry = (Entry<node, integer>) Entryiterator.next ();
			Node NextNode = Entry.getkey ();
			Determines whether the adjacent node is accessed by the IF (nextnode.isvisited ()) continue; 
			int dist = Entry.getvalue (); The end point is adjacent to the starting point, but not necessarily the shortest path if (nextnode.getidentifier) = = Dest.getidentifier () && optimalpath.size () = = 1) adjacentdist = dist;
				if (partialmindist!= 0) {Boolean currentisoptimal = partialmindist > dist? false:true;
					if (!currentisoptimal) {partialmindist = dist;
				Partialoptimalnode = NextNode;
				}}else{partialmindist = dist;
			Partialoptimalnode = NextNode;
		}//Set the current node has been accessed src.setvisited (true);
		Add list Optimalpath.add (Partialoptimalnode);
		Optimaldist + = Partialmindist; if (Partialoptimalnode.getidentifier ()!=dest.getidentifier ()) {//If the current optimal path length is greater than the distance between the endpoint and the start point, the shortest path is the starting point---> Endpoint if (adjacentdist!= 0 && optimaldist > Adjacentdist)
				{src = Optimalpath.removefirst ();
				Optimalpath.clear ();
				Optimalpath.add (SRC);
				Optimalpath.add (dest);
				Optimaldist = adjacentdist;
			return optimalpath;
		//Otherwise the current node is recursive return Dijkstra (Partialoptimalnode, dest, Adjacentdist, Optimaldist, Optimalpath);
		}else{return optimalpath;
 }
	}
}

Test Cases:

Package graph.test; Import graph.
Dijkstra; Import graph.

Dijkstra.node;
Import Java.util.Iterator;

Import java.util.LinkedList; public class Dijkstratest {/** * @param args */public static void main (string[] args) {//TODO auto-generated
		Method stub Dijkstra Dijkstra = new Dijkstra ();
		Dijkstra.node node_1 = Dijkstra.new Node ();
		Dijkstra.node node_2 = Dijkstra.new Node ();
		Dijkstra.node node_3 = Dijkstra.new Node ();
		Dijkstra.node node_4 = Dijkstra.new Node ();
		Dijkstra.node node_5 = Dijkstra.new Node ();
		
		Dijkstra.node node_6 = Dijkstra.new Node ();
		Node_1.setidentifier (1);
		Node_1.getmapping (). Put (node_2, 7);
		Node_1.getmapping (). Put (Node_3, 9);
		
		Node_1.getmapping (). Put (Node_6, 14);
		Node_2.setidentifier (2);
		Node_2.getmapping (). Put (Node_1, 7);
		Node_2.getmapping (). Put (Node_3, 10);
		
		Node_2.getmapping (). Put (Node_4, 15);
		Node_3.setidentifier (3);
		Node_3.getmapping (). put (node_1,7);
		Node_3.getmapping (). put (node_2,10); Node_3.getmapping (). Put (node_4,11);
		
		Node_3.getmapping (). put (node_6,2);
		Node_4.setidentifier (4);
		Node_4.getmapping (). Put (Node_3, 11);
		Node_4.getmapping (). Put (node_2, 15);
		
		Node_4.getmapping (). Put (node_5, 6);
		Node_5.setidentifier (5);
		Node_5.getmapping (). Put (Node_4, 6);
		
		Node_5.getmapping (). Put (Node_6, 9);
		Node_6.setidentifier (6);
		Node_6.getmapping (). Put (node_5, 9);
		
		Node_6.getmapping (). Put (Node_1, 14);
		linkedlist<node> Optimalpath = Dijkstra.getoptimalpath (Node_1, node_5);
		System.out.println ("-------The Optimal Path--------"); for (iterator<node> iterator = Optimalpath.iterator (); Iterator.hasnext ();)
			{Node node = (node) iterator.next ();
			if (Iterator.hasnext ()) {System.out.print (Node.getidentifier () + "-->");
			}else{System.out.print (Node.getidentifier ());
 }
		}
	}

}
Results:


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.