-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.java
More file actions
55 lines (49 loc) · 1.84 KB
/
Tree.java
File metadata and controls
55 lines (49 loc) · 1.84 KB
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
import java.util.Comparator;
import java.util.HashSet;
import java.util.PriorityQueue;
public class Tree{
/* create a shortest path tree starting from this City
* uses Dijkstra's shortest paths algorithm
* set the distance of this City to 0 and others to infinity
* consider each found City closest to the start
* update the best known distance to all adjacent cities
* postcondition: every City.distance is the shortest distance from this to that City
* postcondition: every City.parent is the Link before that City in the set of shortest paths
*/
public void makeTree() {
Comparator<City> comparator = new CityComparator();
PriorityQueue<City> pq = new PriorityQueue<>(comparator);
for (City c : City.cities.values()) {
if (c != this) {
c.distance = Integer.MAX_VALUE;
} else {
c.distance = 0;
}
pq.add(c);
c.parent = null;
for (Link l : c.links) {
l.setUsed(false);
}
}
HashSet<City> tree = new HashSet<>();
while (!pq.isEmpty()) {
City city = pq.poll();
if (city.parent != null) {
city.parent.setUsed(true);
}
tree.add(city);
for (Link l : city.links) {
City child = l.getAdj(city);
if (!tree.contains(child)) {
int length = l.getLength();
if (child.distance > city.distance + length) {
pq.remove(child);
child.distance = city.distance + length;
child.parent = l;
pq.add(child);
}
}
}
}
}
}