-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResultWriter.java
131 lines (105 loc) · 4.51 KB
/
ResultWriter.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
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
/**
* File Writer.
*/
class ResultWriter {
private final String fileName;
ResultWriter(String fileName) {
this.fileName = fileName;
}
/**
* Writes k-shortest paths results to file.
*
* @param nodes Nodes to show path.
* @param visitedOrder Hashmap of the node and its visited order from hospital.
* @param partialMinDistanceSpanningTrees Hashmap of hospital and its MDST.
* @throws IOException If there is error writing file.
*/
void writeKShortestPaths(ArrayList<Integer> nodes, HashMap<Integer, LinkedList<Integer>> visitedOrder,
HashMap<Integer, HashMap<Integer, Integer>> partialMinDistanceSpanningTrees)
throws IOException {
int counter = 0;
Instant start = Instant.now();
BufferedWriter myWriter = new BufferedWriter(new FileWriter(fileName));
for (int node : nodes) {
myWriter.write("Node: " + node);
if (!visitedOrder.containsKey(node)) {
myWriter.write("\nNo paths!\n\n");
continue;
}
int initialSize = visitedOrder.get(node).size();
// Find path for each hospitals
for (int i = 0; i < initialSize; i++) {
Integer currentHospital = visitedOrder.get(node).poll();
int currentNode = node;
int previousNode = partialMinDistanceSpanningTrees.get(currentHospital).get(currentNode);
int distance = 0;
StringBuilder path = new StringBuilder();
path.append(currentNode);
// Backtrack from node to hospital
while (currentNode != previousNode) {
distance++;
path.append("-").append(previousNode);
currentNode = previousNode;
previousNode = partialMinDistanceSpanningTrees.get(currentHospital).get(previousNode);
}
myWriter.write("\nTop " + (i + 1) + " Shortest Distance: " + distance + "\nPath: "
+ path.toString());
counter += distance;
}
myWriter.write("\n\n");
}
myWriter.close();
Instant end = Instant.now();
Duration timeTaken = Duration.between(start, end);
System.out.println("Number of iterations to write modified BFS paths: " + counter);
System.out.println("Time taken to write modified BFS paths: " + timeTaken.toNanos() + "ns ("
+ timeTaken.toSeconds() + "s)");
}
/**
* Writes shortest path to file.
*
* @param nodes Nodes to show path.
* @param previousNodes Hashmap of the node and its previous node.
* @throws IOException If there is error writing file.
*/
void writeShortestPath(ArrayList<Integer> nodes, HashMap<Integer, Integer> previousNodes) throws IOException {
int counter = 0;
Instant start = Instant.now();
BufferedWriter myWriter = new BufferedWriter(new FileWriter(fileName));
for (int node : nodes) {
myWriter.write("Node: " + node);
if (!previousNodes.containsKey(node)) {
myWriter.write("\nNo paths!\n\n");
continue;
}
int currentNode = node;
int previousNode = previousNodes.get(node);
int distance = 0;
StringBuilder path = new StringBuilder();
path.append(currentNode);
// Backtrack from node to hospital
while (currentNode != previousNode) {
distance++;
path.append("-").append(previousNode);
currentNode = previousNode;
previousNode = previousNodes.get(previousNode);
}
myWriter.write("\nShortest Distance: " + distance + "\nPath: " + path.toString() + "\n\n");
counter += distance;
}
myWriter.close();
Instant end = Instant.now();
Duration timeTaken = Duration.between(start, end);
System.out.println("Number of iterations to write BFS paths: " + counter);
System.out.println("Time taken to write BFS paths: " + timeTaken.toNanos() + "ns (" + timeTaken.toSeconds()
+ "s)");
}
}