Download code
From LiteratePrograms
Back to Dijkstra's_algorithm_(Java)
Download for Windows: single file, zip
Download for UNIX: single file, zip, tar.gz, tar.bz2
Dijkstra.java
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Dijkstra's_algorithm_(Java)?action=history&offset=20081113161332 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Dijkstra's_algorithm_(Java)?oldid=15444 25 */ 26 27 import java.util.PriorityQueue; 28 29 import java.util.List; 30 import java.util.ArrayList; 31 import java.util.Collections; 32 33 34 class Vertex implements Comparable<Vertex> 35 { 36 public final String name; 37 public Edge[] adjacencies; 38 public double minDistance = Double.POSITIVE_INFINITY; 39 public Vertex previous; 40 public Vertex(String argName) { name = argName; } 41 public String toString() { return name; } 42 public int compareTo(Vertex other) 43 { 44 return Double.compare(minDistance, other.minDistance); 45 } 46 47 } 48 49 50 class Edge 51 { 52 public final Vertex target; 53 public final double weight; 54 public Edge(Vertex argTarget, double argWeight) 55 { target = argTarget; weight = argWeight; } 56 } 57 58 59 public class Dijkstra 60 { 61 public static void computePaths(Vertex source) 62 { 63 source.minDistance = 0.; 64 PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>(); 65 vertexQueue.add(source); 66 67 while (!vertexQueue.isEmpty()) { 68 Vertex u = vertexQueue.poll(); 69 70 // Visit each edge exiting u 71 for (Edge e : u.adjacencies) 72 { 73 Vertex v = e.target; 74 double weight = e.weight; 75 double distanceThroughU = u.minDistance + weight; 76 if (distanceThroughU < v.minDistance) { 77 vertexQueue.remove(v); 78 79 v.minDistance = distanceThroughU ; 80 v.previous = u; 81 vertexQueue.add(v); 82 83 } 84 85 } 86 } 87 } 88 89 90 public static List<Vertex> getShortestPathTo(Vertex target) 91 { 92 List<Vertex> path = new ArrayList<Vertex>(); 93 for (Vertex vertex = target; vertex != null; vertex = vertex.previous) 94 path.add(vertex); 95 96 Collections.reverse(path); 97 return path; 98 } 99 100 public static void main(String[] args) 101 { 102 Vertex v0 = new Vertex("Harrisburg"); 103 Vertex v1 = new Vertex("Baltimore"); 104 Vertex v2 = new Vertex("Washington"); 105 Vertex v3 = new Vertex("Philadelphia"); 106 Vertex v4 = new Vertex("Binghamton"); 107 Vertex v5 = new Vertex("Allentown"); 108 Vertex v6 = new Vertex("New York"); 109 v0.adjacencies = new Edge[]{ new Edge(v1, 79.83), 110 new Edge(v5, 81.15) }; 111 v1.adjacencies = new Edge[]{ new Edge(v0, 79.75), 112 new Edge(v2, 39.42), 113 new Edge(v3, 103.00) }; 114 v2.adjacencies = new Edge[]{ new Edge(v1, 38.65) }; 115 v3.adjacencies = new Edge[]{ new Edge(v1, 102.53), 116 new Edge(v5, 61.44), 117 new Edge(v6, 96.79) }; 118 v4.adjacencies = new Edge[]{ new Edge(v5, 133.04) }; 119 v5.adjacencies = new Edge[]{ new Edge(v0, 81.77), 120 new Edge(v3, 62.05), 121 new Edge(v4, 134.47), 122 new Edge(v6, 91.63) }; 123 v6.adjacencies = new Edge[]{ new Edge(v3, 97.24), 124 new Edge(v5, 87.94) }; 125 Vertex[] vertices = { v0, v1, v2, v3, v4, v5, v6 }; 126 127 128 computePaths(v0); 129 for (Vertex v : vertices) 130 { 131 System.out.println("Distance to " + v + ": " + v.minDistance); 132 List<Vertex> path = getShortestPathTo(v); 133 System.out.println("Path: " + path); 134 } 135 136 } 137 } 138
