-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphReader.java
78 lines (66 loc) · 2.31 KB
/
GraphReader.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* File Reader.
*/
class GraphReader {
/**
* Reads graph from file.
*
* @param graph Graph to pass edges to.
* @param fileName Filename to read.
* @throws IOException If error reading file.
*/
void readGraph(Graph graph, String fileName) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = bufferedReader.readLine()) != null) {
// Ignore lines starting with #
if (line.charAt(0) == '#') {
continue;
}
String[] nodesString = line.split("\\s+", 2);
graph.addEdge(nodesString[0], nodesString[1]);
}
bufferedReader.close();
}
/**
* Reads hospitals from file.
*
* @param graph Graph to pass hospitals to.
* @param fileName Filename to read.
* @throws IOException If error reading file.
*/
void readHospitals(Graph graph, String fileName) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
int num = Integer.parseInt(bufferedReader.readLine().split(" ")[1]);
for (int i = 0; i < num; i++) {
graph.addHospital(Integer.parseInt(bufferedReader.readLine()));
}
bufferedReader.close();
}
/**
* Reads nodes from file.
* Nodes to show path are stored identically as hospitals.
*
* @param graph Graph to pass hospitals to.
* @param fileName Filename to read.
* @throws IOException If error reading file.
*/
void readNodes(Graph graph, String fileName) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
String numString = bufferedReader.readLine();
if (numString.trim().equalsIgnoreCase("all")) {
for (int node : graph.getAdjacencyList().keySet()) {
graph.addNodes(node);
}
} else {
int num = Integer.parseInt(numString.split(" ")[1]);
for (int i = 0; i < num; i++) {
graph.addNodes(Integer.parseInt(bufferedReader.readLine()));
}
}
bufferedReader.close();
}
}