-
Notifications
You must be signed in to change notification settings - Fork 201
/
Dijkstras.java
166 lines (139 loc) · 4.14 KB
/
Dijkstras.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
public class Dijkstras {
public static void main(String[] args) {
Graph g = new Graph();
g.addVertex('A', Arrays.asList(new Vertex('B', 7), new Vertex('C', 8)));
g.addVertex('B', Arrays.asList(new Vertex('A', 7), new Vertex('F', 2)));
g.addVertex('C', Arrays.asList(new Vertex('A', 8), new Vertex('F', 6), new Vertex('G', 4)));
g.addVertex('D', Arrays.asList(new Vertex('F', 8)));
g.addVertex('E', Arrays.asList(new Vertex('H', 1)));
g.addVertex('F', Arrays.asList(new Vertex('B', 2), new Vertex('C', 6), new Vertex('D', 8), new Vertex('G', 9), new Vertex('H', 3)));
g.addVertex('G', Arrays.asList(new Vertex('C', 4), new Vertex('F', 9)));
g.addVertex('H', Arrays.asList(new Vertex('E', 1), new Vertex('F', 3)));
System.out.println(g.getShortestPath('A', 'H'));
}
}
class Vertex implements Comparable<Vertex> {
private Character id;
private Integer distance;
public Vertex(Character id, Integer distance) {
super();
this.id = id;
this.distance = distance;
}
public Character getId() {
return id;
}
public Integer getDistance() {
return distance;
}
public void setId(Character id) {
this.id = id;
}
public void setDistance(Integer distance) {
this.distance = distance;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((distance == null) ? 0 : distance.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Vertex other = (Vertex) obj;
if (distance == null) {
if (other.distance != null)
return false;
} else if (!distance.equals(other.distance))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public String toString() {
return "Vertex [id=" + id + ", distance=" + distance + "]";
}
@Override
public int compareTo(Vertex o) {
if (this.distance < o.distance)
return -1;
else if (this.distance > o.distance)
return 1;
else
return this.getId().compareTo(o.getId());
}
}
class Graph {
private final Map<Character, List<Vertex>> vertices;
public Graph() {
this.vertices = new HashMap<Character, List<Vertex>>();
}
public void addVertex(Character character, List<Vertex> vertex) {
this.vertices.put(character, vertex);
}
public List<Character> getShortestPath(Character start, Character finish) {
final Map<Character, Integer> distances = new HashMap<Character, Integer>();
final Map<Character, Vertex> previous = new HashMap<Character, Vertex>();
PriorityQueue<Vertex> nodes = new PriorityQueue<Vertex>();
for(Character vertex : vertices.keySet()) {
if (vertex == start) {
distances.put(vertex, 0);
nodes.add(new Vertex(vertex, 0));
} else {
distances.put(vertex, Integer.MAX_VALUE);
nodes.add(new Vertex(vertex, Integer.MAX_VALUE));
}
previous.put(vertex, null);
}
while (!nodes.isEmpty()) {
Vertex smallest = nodes.poll();
if (smallest.getId() == finish) {
final List<Character> path = new ArrayList<Character>();
while (previous.get(smallest.getId()) != null) {
path.add(smallest.getId());
smallest = previous.get(smallest.getId());
}
return path;
}
if (distances.get(smallest.getId()) == Integer.MAX_VALUE) {
break;
}
for (Vertex neighbor : vertices.get(smallest.getId())) {
Integer alt = distances.get(smallest.getId()) + neighbor.getDistance();
if (alt < distances.get(neighbor.getId())) {
distances.put(neighbor.getId(), alt);
previous.put(neighbor.getId(), smallest);
forloop:
for(Vertex n : nodes) {
if (n.getId() == neighbor.getId()) {
nodes.remove(n);
n.setDistance(alt);
nodes.add(n);
break forloop;
}
}
}
}
}
return new ArrayList<Character>(distances.keySet());
}
}