-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocialNetwork.java
460 lines (427 loc) · 14.7 KB
/
SocialNetwork.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.PriorityQueue;
/**
* This class represents the social network that the GUI directly interfaces to.
*
* @author Jake Wesson and Nate Smith
*/
public class SocialNetwork implements SocialNetworkADT {
private Graph graph; // the graph containing all the info
private File log; // the file being written to in order to save commands
PrintStream writer; // the writer to the file
/**
* This is the constructor for the social network class. It just instantiates the fields and not
* much else.
*/
public SocialNetwork() {
graph = new Graph();
log = new File("log.txt");
try {
writer = new PrintStream(log);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* This method adds a friendship between two users. If either user is not in the network, they are
* added and the friendship is created between them.
*
* @param user - one of the people that a friendship will be created between
* @param friend - the other person that a friendship will be created between
* @return true if friendship added, false if not
* @throws FileNotFoundException - thrown if log file disappears
*/
@Override
public boolean addFriends(String user, String friend) throws FileNotFoundException {
// input checking, checks strings not null or empty
if (user == null || friend == null || user.length() == 0 || friend.length() == 0) {
return false;
}
// check if both people in graph
Person per1 = graph.getNode(user);
Person per2 = graph.getNode(friend);
// if either person doesn't exist, create them
if (per1 == null) {
per1 = new Person(user);
}
if (per2 == null) {
per2 = new Person(friend);
}
// adds edge and checks if successfully added
if (graph.addEdge(per1, per2) == 1) {
writer.println("a " + user + " " + friend);
return true;
}
return false;
}
/**
* This method adds a friendship between two users. If either user is not in the network, they are
* added and the friendship is created between them.
*
* @param user - one of the people that a friendship will be created between
* @param friend - the other person that a friendship will be created between
* @return true if friendship added, false if not
* @throws FileNotFoundException - thrown if log file disappears
*/
@Override
public boolean removeFriends(String user, String friend) throws FileNotFoundException {
// input checking, checks if input strings null or empty
if (user == null || friend == null || user.length() == 0 || friend.length() == 0) {
return false;
}
// check both people in graph
Person per1 = graph.getNode(user);
Person per2 = graph.getNode(friend);
if (per1 == null) {
return false;
}
if (per2 == null) {
return false;
}
// remove edge and check if edge removed successfully
if (graph.removeEdge(per1, per2) == 1) {
writer.println("r " + user + " " + friend);
return true;
}
return false;
}
/**
* This method adds a user to the social network.
*
* If the user is added successfully, it returns true, but if the user is already in the graph,
* the empty string, or null, it returns false.
*
* @param user - user to add
* @return true if user added, false if null or already exists
* @throws FileNotFoundException - thrown if log file disappears
*/
@Override
public boolean addUser(String user) throws FileNotFoundException {
// check valid input
if (user == null || user.length() == 0) {
return false;
}
// create person and see if it's added successfully
Person person = new Person(user);
if (graph.addNode(person) == 1) {
writer.println("a " + user);
return true;
}
return false;
}
/**
* This method adds a user to the social network.
*
* If the user is added successfully, it returns true, but if the user is already in the graph,
* the empty string, or null, it returns false.
*
* @param user - user to add
* @return true if user added, false if null or already exists
* @throws FileNotFoundException - thrown if log file disappears
*/
@Override
public boolean removeUser(String user) throws FileNotFoundException {
// check valid input
if (user == null || user.length() == 0) {
return false;
}
// check if person removed from graph successfully
Person person = new Person(user);
if (graph.removeNode(person) == 1) {
writer.println("r " + user);
return true;
}
return false;
}
/**
* This method returns all of the friends of a specific user in a set.
*
* @param user - the user to get the friends of
* @return a set containing the friends of the user
*/
@Override
public Set<Person> getFriends(String user) {
// check for valid input
if (user == null || user.length() == 0) {
return null;
}
// check if node in graph
Person person = graph.getNode(user);
if (person == null) {
return null;
}
// get friends from graph, place in set
List<Person> friendList = graph.getNeighbors(person);
Set<Person> friends = new HashSet<Person>();
for (Person p : friendList) {
friends.add(p);
}
return friends;
}
/**
* Returns all people in the social network.
*
* @return a set containing all people
*/
public Set<Person> getAllUsers() {
return graph.getAllNodes();
}
/**
* This method finds mutual friends between user1 and user2.
*
* @param user1 - first user to compare to
* @param user2 - second user to compare to
* @return set containing mutual friends between these two users
*/
@Override
public Set<Person> getMutualFriends(String user1, String user2) {
Set<Person> mutual = getFriends(user1);// get friends of user1 in a Set
// compare two sets of friends and retain common friends
mutual.retainAll(getFriends(user2));
return mutual;
}
/**
* This private class only exists to keep the distance and person together for the priority queue
* to function correctly. Since the default java priority queue works on "natural ordering"
* according to the API, this class implements Comparable in order to determine priority.
*
* @author Jake Wesson
*
*/
private class inPq implements Comparable<inPq> {
private int dist; // distance from start node
private Person thePerson; // person object associated with distance
/**
* Constructor for this class.
*
* @param p - person object
* @param dist - distance from start node
*/
public inPq(Person p, int dist) {
thePerson = p;
this.dist = dist;
}
/**
* Overridden compareTo, it just compares the distances to determine priority.
*/
@Override
public int compareTo(inPq comp) {
return dist - comp.dist;
}
}
/**
* This method implements Dijkstra's method to find the shortest path between any two connected
* nodes in the graph.
*
* @param user1 - the user to find the path from
* @param user2 - the user to find the path to
* @return the path taken to get between the two nodes
*/
@Override
public List<Person> getShortestPath(String person1, String person2) {
// input checking
if (person1 == null || person2 == null || person1.length() == 0 || person2.length() == 0) {
return null;
}
// check nodes are in graph
Person per1 = graph.getNode(person1);
Person per2 = graph.getNode(person2);
if (per1 == null || per2 == null) {
return null;
}
// create five parallel arrays, the person being checked, its distance from starting person,
// whether it's visited, its predecessor, and an array of priority queue objects
List<Person> people = new LinkedList<Person>();
for (Person p : graph.getAllNodes()) {
people.add(p);
}
int[] distance = new int[people.size()];
boolean[] visited = new boolean[people.size()];
Person[] pred = new Person[people.size()];
// initialize distance to uninitialized value
for (int i = 0; i < distance.length; ++i) {
distance[i] = -1;
}
// create all priority queue nodes, initialize, and add
List<inPq> nodes = new LinkedList<inPq>();
for (Person p : people) {
inPq qAddition = new inPq(p, -1);
nodes.add(qAddition);
}
// create p queue and set up starting node
PriorityQueue<inPq> pq = new PriorityQueue<inPq>();
for (inPq in : nodes) {
if (in.thePerson.getName().equals(person1)) {
pq.add(in);
in.dist = 0;
distance[people.indexOf(in.thePerson)] = 0;
visited[people.indexOf(in.thePerson)] = true;
break;
}
}
// start of actual algorithm (first non-setup thing)F
while (!pq.isEmpty()) {
inPq currNode = pq.poll();
visited[people.indexOf(currNode.thePerson)] = true;
for (Person p : getFriends(currNode.thePerson.getName())) {
int index = people.indexOf(p);
// for each unvisited successor of currNode...
if (!visited[index]) {
if (distance[index] == -1 || distance[index] > (currNode.dist + 1)) {
nodes.get(index).dist = currNode.dist + 1;
distance[index] = currNode.dist + 1;
pred[index] = currNode.thePerson;
pq.add(nodes.get(index));
}
}
}
}
// process the predecessor list into an order of nodes to visit
int start = people.indexOf(per2);
int end = people.indexOf(per1);
// check if both per1 and per2 actually connected
if (pred[start] == null) {
return null;
}
int currIndex = start;
// traverse the array by chaining together predecessor nodes until start of path node reached
LinkedList<Person> theOrder = new LinkedList<Person>();
theOrder.add(per2);
while (currIndex != end) {
theOrder.addFirst(pred[currIndex]);
currIndex = people.indexOf(pred[currIndex]);
}
return theOrder;
}
/**
* This method creates separate graphs out of the disconnected parts in the graphs, and it returns
* them in a set.
*
* @return a set of graphs created from the individual components split out from the main graph
*/
@Override
public Set<Graph> getConnectedComponents() {
// setup stuff
Set<Person> allPeople = graph.getAllNodes();
Set<Graph> connectedGraphs = new HashSet<Graph>();
// loop for adding graphs to connectedGraphs
while (allPeople.size() != 0) {
Graph currGraph = new Graph();
Queue<Person> q = new LinkedList<Person>();
q.add((Person) allPeople.toArray()[0]);
// loop for building connected component into a graph, basically a BFS
while (!q.isEmpty()) {
Person currNode = q.poll();
currGraph.addNode(currNode);
allPeople.remove(currNode);
// for each unvisited successor...
for (Person p : graph.getNeighbors(currNode)) {
if (currGraph.getNode(p.getName()) == null) {
currGraph.addEdge(currNode, p);
q.add(p);
allPeople.remove(p);
}
}
}
connectedGraphs.add(currGraph);
}
return connectedGraphs;
}
/**
* This method loads input commands from a file and executes commands to create the graph.
*
* @param filename - file name to load
* @return - main user of type person if set, otherwise just returns null
* @throws FileNotFoundException - thrown if log file disappears
*/
@Override
public Person loadFromFile(File filename) throws FileNotFoundException, IllegalArgumentException {
Scanner scnr = new Scanner(filename);
Person mainUser = null;
// loop through each line until run out of input
while (scnr.hasNextLine()) {
String input = scnr.nextLine();
// split into seperate string and insert into array
String[] command = input.split(" ");
// interpret input
switch (command[0]) {
case "a":
if (command.length == 3)// add friends on three inputs
addFriends(command[1], command[2]);
else if (command.length == 2)// add user on two inputs
addUser(command[1]);
// throw illegalArgumentException on invalid input
else
throw new IllegalArgumentException();
break;
case "r":
if (command.length == 3)// remove friends on three inputs
removeFriends(command[1], command[2]);
else if (command.length == 2)// remove user on two inputs
removeUser(command[1]);
// throw illegalArgumentException on invalid inupt
else
throw new IllegalArgumentException();
break;
case "s":
if (command.length == 2)// set main user on two inputs
mainUser = graph.getNode(command[1]);
// throw illegalArgumentException on invalid input
else
throw new IllegalArgumentException();
break;
// invalid first argument
default:
throw new IllegalArgumentException();
}
}
scnr.close();
return mainUser;
}
/**
* Returns the total number of users in the social network.
*
* @return the number of users
*/
public int getNumUsers() {
return graph.order();
}
/**
* Returns the total number of friendships in the social network.
*
* @return the number of friendships
*/
public int getNumFriends() {
return graph.size();
}
/**
* This method saves the graph to a file by copying commands from the log file.
*
* @param filename - file to save to
* @throws FileNotFoundException - thrown if log file disappears
* @throws IOException - thrown if file can't be written to
*/
@Override
public void saveToFile(File filename) throws IOException {
FileInputStream log = new FileInputStream(this.log);
FileOutputStream out = new FileOutputStream(filename);
byte[] buffer = new byte[1024];
int length;
while ((length = log.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
out.close();
log.close();
}
}