-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphExplorer.java
More file actions
149 lines (130 loc) · 4.84 KB
/
GraphExplorer.java
File metadata and controls
149 lines (130 loc) · 4.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
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
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.io.FileWriter;
public class GraphExplorer {
private final PCRParser parser;
private final HashMap<String, String[]> parents;
public GraphExplorer() {
parser = new PCRParser();
parents = new HashMap<>();
}
public void exploreGraph(String startInstructorCode) {
exploreGraph(startInstructorCode, Integer.MAX_VALUE, false);
}
public void exploreGraph(String startInstructorCode, boolean save) {
exploreGraph(startInstructorCode, Integer.MAX_VALUE, save);
}
public void exploreGraph(String startInstructorCode, int maxNodes) {
exploreGraph(startInstructorCode, maxNodes, false);
}
public void exploreGraph(String startInstructorCode, int maxNodes, boolean save) {
int saved = 0;
parents.put(startInstructorCode, new String[] {null, null});
LinkedList<String> queue = new LinkedList<>();
queue.add(startInstructorCode);
while (!queue.isEmpty()) {
int foundSoFar = parents.size();
System.out.println(foundSoFar + " instructors found so far");
// auto-save every 500 instructors
if (save && foundSoFar - saved > 500) {
saved = foundSoFar;
writeParentsToCSV();
}
if (parents.size() > maxNodes) {
if (save) {
writeParentsToCSV();
}
return;
}
String curr = queue.poll();
Set<String> currCourses = new HashSet<>();
try {
currCourses = parser.getInstructorCourses(curr);
} catch (IllegalArgumentException ignored) {
}
for (String course : currCourses) {
Set<String> courseInstructors = new HashSet<>();
try {
courseInstructors = parser.getCourseInstructors(course);
} catch (IllegalArgumentException ignored) {
}
for (String instructor : courseInstructors) {
if (parents.containsKey(instructor)) {
continue;
}
queue.add(instructor);
parents.put(instructor, new String[] {curr, course});
}
}
}
if (save) {
writeParentsToCSV();
}
}
public List<String> findShortestPath(String instructorName) {
List<String> path = new ArrayList<>();
String professorName;
String curr = parser.getInstructorCode(instructorName);
if (!parents.containsKey(curr)) {
throw new IllegalArgumentException("instructor not found or not connected");
}
while (curr != null) {
String[] parentArray = parents.get(curr);
if (parentArray[0] != null) {
professorName = parser.getInstructorName(parentArray[0]);
path.add("(" + professorName + ", " + parentArray[1] + ")");
}
curr = parentArray[0];
}
return path;
}
private void writeParentsToCSV() {
try {
FileWriter writer = new FileWriter("parentPointers.csv");
for (Map.Entry<String, String[]> entry : parents.entrySet()) {
String[] value = entry.getValue();
writer.write(entry.getKey() + "," + value[0] + "," + value[1] + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void importFromCSV(String filename) {
try {
FileReader reader = new FileReader(filename);
Scanner scanner = new Scanner(reader);
while (scanner.hasNextLine()) {
String[] elements = scanner.nextLine().split(",");
if (elements[1].equals("null")) {
elements[1] = null;
}
if (elements[2].equals("null")) {
elements[2] = null;
}
parents.put(elements[0], new String[] {elements[1], elements[2]});
}
} catch (IOException e) {
e.printStackTrace();
}
}
public float getAveragePathLength() {
float totalPathLength = 0;
float count = 0;
for (Map.Entry<String, String[]> entry : parents.entrySet()) {
count++;
String curr = entry.getKey();
int pathLength = 0;
while (curr != null) {
String[] parentArray = parents.get(curr);
if (parentArray[0] != null) {
pathLength++;
}
curr = parentArray[0];
}
totalPathLength += pathLength;
}
return totalPathLength / count;
}
}