Java implementation of Dijkstra algorithm

Source: Internet
Author: User

Tag: Shortest path Dijkstar greedy algorithm Java

Learn Dijstar algorithm starting from Shortest path

The following is a description of the shortest path problem:


The following is a description of the Dijkstar algorithm:


Here's how to solve the problem in the right graph using the Dijkstar algorithm:


Here is the adjacency matrix of the graph:


Here is the calculation process:




Here's how to solve the shortest path:



Here is the Java code implementation, which need to pay attention to the problem I have marked (must be careful!!!!!) ):

Package Greedy_algorithm;import Java.util.arraylist;import Java.util.hashmap;import java.util.hashset;import Java.util.map;import Java.util.set;public class Dijkstra {private set<integer> permanent=new hashset<> (); Private set<integer> temporary=new hashset<> ();p rivate map<integer, integer> nodeMap=new HashMap <integer, integer> (); key= permanent node, value= shortest path before a node, improves backtracking efficiency private int [] [] m;private Mark [] d;private int n;private final static int max=65535;c Lass mark{private int Length=max;private int Previous=-1;private boolean ismarked=false;public mark () {}public mark (int Length,int Previous,boolean ismarked) {this.length=length;this.previous=previous;this.ismarked=ismarked;}} Public Dijkstra () {n=6;m=new int[][]{{0, 1, 4,-1,-1,-1}, {1, 0, 2, 7, 5,-1}, {4, 2, 0,-1, 1,-1}, {-1, 7,- 1, 0, 3, 2}, {-1, 5, 1, 3, 0, 6}, { -1,-1,-1, 2, 6, 0}};d =new mark[n][n];for (int i=0;i<n;i++) {for (int j=0;j&lt ; n;j++) {d[i][j]=new Mark ();//system.out.prIntln (d[i][j].length+ "" +d[i][j].previous+ "" +d[i][j].ismarked);}}} Public Dijkstra (int n,int [][]m,mark [][]d) {this.n=n;this.m=m;this.d=d;if (n!=m.length && n!=m[0].length & & N!=d.length && n!=d[0].length) {System.out.println ("Incorrect input format"); System.exit (1);}} public void Shortestpath () {arraylist<mark> vlist;permanent.add (0), for (int i=1;i<n;i++) {temporary.add (i);} D[0][0]=new Mark (0,-1,true), Nodemap.put (0,-1),/*for (int q=0;q<n;q++) System.out.print ("(" +d[0][q].length+ "," +d [0] [q].previous+ "," +d[0][q].ismarked+ "); System.out.println (); */for (int i=1;i<n;i++) {for (int t:temporary) {vlist=new arraylist<dijkstra.mark> (); for (int p:permanent) {if (m[t][p]!=-1) {Vlist.add (New Mark (GetLength (t, p), P,false));}} if (Vlist.size ()!=0) {d[i][t]=getmin (vList);}} Marknode (i);/*for (int r=0;r<n;r++) System.out.print ("(" +d[i][r].length+ "," +d[i][r].previous+ "," +d[i][r "). Ismarked+ ")"); System.out.println (); */}}public void traceBack (int end) {int node=end;for (int i=0;i<n;i+ +) {if (d[i][node].ismarked==true) {System.out.println ("the shortest lenth from V0 to V" +node+ "is:" +d[i][node].length) ;}} System.out.println ("The Shortest Path is:"); Trace (end);} public void trace (int end) {int node =end;if (node>=0) {Trace (Nodemap.get (node)), if (node==5) System.out.print ("V" + node); ElseSystem.out.print ("V" +node+ ");}} public int getlength (int t,int p) {int length=m[t][p];for (int i=0;i<n;i++) {if (d[i][p].ismarked==true) {Length+=d[i] [P].length;break;}} return length;}            Public Mark getmin (arraylist<mark> vList) {Mark mark=new mark (); int Min=max; Notice here must be Min=max, this place of problem looked for a long time of mistake!!! System.out.println ("min=" +min); for (Mark c:vlist) {//system.out.println ("+c.length+", "+c.previous+", "+ C.ismarked+ ")"), if (min>c.length) {min=c.length;mark=c;//system.out.println ("break");}} System.out.println ("+mark.length+", "+mark.previous+", "+mark.ismarked+"); return mark==null?new mark ( -1,-1, false): Mark; public void Marknode (int row) {int min=max;int position=-1;for (int j=0;j<n;j++) {if (D[row][j].length!=max) {if (min>d[row][j].length) {min=d[row][j].length;position=j;}}} if (position!=-1) {d[row][position].ismarked=true;permanent.add (position); Temporary.remove (position); nodemap.put (position, d[row][position].previous);}} public static void Main (string[] args) {Dijkstra dijkstra=new Dijkstra ();d Ijkstra.shortestpath ();/*for (map.entry< Integer, integer> Entry:dijkstra.nodeMap.entrySet ()) System.out.println (entry); */dijkstra.traceback (5);}}


Java implementation of Dijkstra algorithm

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.