-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.java
More file actions
250 lines (213 loc) · 6.74 KB
/
World.java
File metadata and controls
250 lines (213 loc) · 6.74 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.HashSet;
/* Singleton Design */
public class World {
/* Self-reference object */
private static World _instance = null;
/* private variables */
/*
* holds the number of infected users
*/
private int numInfections;
/*
* the latest version KA has put out.
* This will determine which users are considered infected
*/
private int latest_version;
/*
* Essentially, a list of users in the world that are
* mapped by their name/username
*/
private Map<String, User> users;
/* Constructor
* Initializes World by allocating lists.
*/
private World() {
users = new HashMap<String, User>();
numInfections = 0;
latest_version = 0;
}
public static World getInstance() {
if(_instance == null)
_instance = new World();
return _instance;
}
public void setUsersList(Map<String, User> users) {
this.users = users;
//initialize numInfections
for(String s : users.keySet()) {
if(users.get(s).getVersion() == getLatestVersion()) {
numInfections++;
for(User coach : users.get(s).getCoaches()) {
coach.incrementInfectedStudents();
}
}
// A user should not be allowed to have a higher version than
// what the world considers to be the highest version. Infect
// this user and reset his/her version
else if(users.get(s).getVersion() > getLatestVersion()) {
users.get(s).setVersion(getLatestVersion());
numInfections++;
for(User coach : users.get(s).getCoaches()) {
coach.incrementInfectedStudents();
}
}
}
}
public User putUser(String name, User user) {
return users.put(name, user);
}
public int getNumberOfInfections() {
return numInfections;
}
/*
* Total infection infects all users adjacent to the passed in User.
* Adjacent, in this case, means is a student or coach.
* For every newly infected user, repeat the same process from the first line.
* At a high level, this is implemented as a Breadth-First Traversal.
*/
public void totalInfection(User user) {
if(user == null)
return;
LinkedList<User> usersInfected = new LinkedList<User>();
usersInfected.add(user);
//Infect user here if not previously infected
tryInfect(user);
// Check for other users that have been infected already,
// and add them to the usersInfected list
for(String s : users.keySet()) {
if(users.get(s).getVersion() == getLatestVersion()) {
if(users.get(s) != user)
usersInfected.add(users.get(s));
}
}
while(!usersInfected.isEmpty()){
User newlyInfected = usersInfected.pop();
// Add students and coaches to the list
// in each case, if the user's version is less than the latest version,
// then we add them to the list. Otherwise, they've already been infected.
HashSet<User> students = (HashSet) newlyInfected.getStudents();
for(User student : students) {
if(tryInfect(student))
usersInfected.addLast(student);
}
HashSet<User> coaches = (HashSet) newlyInfected.getCoaches();
for(User coach : coaches) {
if(tryInfect(coach))
usersInfected.addLast(coach);
}
}
}
public void limitInfection(User user, int maxInfections) {
// Check base cases before attempting expensive iterations
if(user == null)
return;
// return if there are already enough infections in the world that satisfy
// the parameter
if(numInfections >= maxInfections)
return;
// If maxInfections > users.size(), simply infect all users and return.
if(maxInfections >= users.size()) {
for(User u : users.values()) {
tryInfect(u);
}
return;
}
// infect passed-in user if user is not already infected
tryInfect(user);
while(numInfections < maxInfections) {
// Find coach with greatest number of infected students
// infect all students of found coach and infect coach
User coach = getCoachWithMostInfected(); // This gets an UNINFECTED coach, with most INFECTED students
if(coach != null) {
tryInfect(coach);
for(User student : coach.getStudents()) {
tryInfect(student);
}
}
// If no coach is found, break out of loop
else {
break;
}
}
// begin infecting users until max is reached
for(User u : users.values()) {
if(numInfections >= maxInfections)
break;
else
tryInfect(u);
}
}
public User getUser(String name) {
return users.get(name.toLowerCase());
}
public int getNumberOfUsers() {
return users.size();
}
public int getLatestVersion() {
return latest_version;
}
/*
* A version can only be updated. Doing so "uninfects" every user
* because no user should now have the latest version.
* NOTE: a way to break this logic is if we had a user with a greater
* version than the latest_version originally, but I will not account
* for that case here.
*/
public void incrementLatestVersion() {
// increment
latest_version++;
// Reset infections
numInfections = 0;
// Reset number of infected students for every coach
for(User user : users.values()) {
user.setNumInfectedStudents(0);
}
}
public User getCoachWithMostInfected() {
// first, filter out all coaches that are infected - O(n)
// Whenever an uninfected coach is found,
// find what number of students of his/hers are infected - O(m)
User bestCoach = null;
int mostStudentsInfected = 0;
for(User user : users.values()) {
if(user.getNumInfectedStudents() > mostStudentsInfected &&
user.getVersion() < getLatestVersion()) {
bestCoach = user;
mostStudentsInfected = user.getNumInfectedStudents();
}
}
return bestCoach;
}
/*
* Whenever we infect a user, we also want to increment numInfections.
* this function is to maintain that the two go hand in hand.
* Returns true if infection was successful. Otherwise, false
* On top of that, we increment the this user's coaches' number of
* infected students. This is useful when we perform limited infections,
*/
private boolean tryInfect(User user) {
if(user.getVersion() != latest_version) {
user.setVersion(latest_version);
numInfections++;
for(User coach : user.getCoaches()) {
coach.incrementInfectedStudents();
}
return true;
}
return false;
}
/*
* Simply prints all users that are infected in this world.
*/
public void printInfected() {
System.out.print("Users infected: ");
for(User user : users.values()) {
if(user.getVersion() == latest_version)
System.out.print(user.getName() + " ");
}
System.out.println();
}
}