-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriendNet.java
More file actions
358 lines (330 loc) · 14.7 KB
/
FriendNet.java
File metadata and controls
358 lines (330 loc) · 14.7 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
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
// Kevin Mattappally
// Davis Fairchild
// Ryan Hays
// How we are going to implement the best freind chain
// 1. We will implement the best friend chain by utilizing the
// Dijkstra's algorith
// Here are our two killer features:
// 1. Finding Mutual Friends between two users
// a. To find the mutual friends, we would be using
// the brute force algorithm.
// 2. Finding the most popular / best friend
// a. To find the most popular friend, we would find the friend
// in the graph with the highest overall average friend rating.
import java.util.*;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.io.*;
import java.lang.*;
public class FriendNet {
private static Map<String, List<Friend>> map = new HashMap<>();
public static void main(String[] args) throws IOException {
File file = new File("Friends.txt");
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext()){
String user = inputFile.next();
String friend = inputFile.next();
int rank = inputFile.nextInt();
Friend f = new Friend(friend, rank);
if (map.containsKey(user)) {
List<Friend> temp = map.get(user);
temp.add(f);
map.put(user, temp);
} else {
List<Friend> newFriend = new ArrayList<>();
newFriend.add(f);
map.put(user, newFriend);
}
if (!map.containsKey(friend)) {
map.put(friend, new ArrayList<>());
}
}
System.out.println(map);
inputFile.close();
do {
showMenu();
} while (true);
}
public static void showMenu() {
Scanner sc = new Scanner(System.in);
System.out.println("\nWhat do you want to do?");
System.out.println("1) Check if user exists");
System.out.println("2) Check connection between users");
System.out.println("3) Quit");
System.out.println("4) Best Friend Chain");
System.out.println("5) Find Mutual Friends");
System.out.println("6) Find Best Quality Friend");
char choice = sc.next().charAt(0);
switch (choice) {
case '1':
checkUserExists();
break;
case '2':
checkConnection();
break;
case '3':
// Quit
System.out.println("Exiting...");
System.exit(0);
break;
case '4':
bestFriendChain();
break;
case '5':
mutualFriends();
break;
case '6':
bestQualityFriend();
break;
default:
System.out.println("Invalid Choice");
}
}
public static void checkUserExists() {
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter a user to check: ");
String enteredUser = sc.next();
if (map.containsKey(enteredUser)) {
System.out.println(enteredUser + " is in this network\n");
} else {
System.out.println(enteredUser + " is not in this network\n");
}
}
public static void checkConnection() {
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter the first user: ");
String firstUser = sc.next();
System.out.print("Enter the second user: ");
String secondUser = sc.next();
if (map.containsKey(firstUser)) {
List<Friend> firstUserFriends = map.get(firstUser);
for (int i = 0; i < firstUserFriends.size(); i++) {
if (firstUserFriends.get(i).getName().equals(secondUser)) {
System.out.println("The weight of " + firstUser + " to " + secondUser + " = " + firstUserFriends.get(i).getRank());
return;
}
}
System.out.println(secondUser + " is not a friend of " + firstUser + "\n");
} else {
System.out.println(firstUser + " is not in this network" + "\n");
}
}
public static void bestFriendChain() {
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter the first user: ");
String firstUser = sc.next();
System.out.print("Enter the second user: ");
String secondUser = sc.next();
System.out.println("Now calculating the best friend chain:");
// starting main portion of algorithm
// based of Dikstra's algorithm
// uses ArrayLists rather than PriorityQueue
ArrayList<String> nodes = new ArrayList<>();
ArrayList<Integer> distances = new ArrayList<>();
ArrayList<String> prevs = new ArrayList<>();
ArrayList<Boolean> visited = new ArrayList<>();
for (String u : map.keySet()) {
nodes.add(u);
prevs.add("_");
visited.add(false);
if (u.equals(firstUser)) {
distances.add(0);
} else {
distances.add(Integer.MAX_VALUE);
}
}
// initial contents of lists
printLists("Initial", nodes, distances, prevs, visited);
for (int i = 0; i < nodes.size(); i++) {
// calculate min distance index
int minIndex = 0;
for (int j = 1; j < nodes.size(); j++) {
if (distances.get(j) < distances.get(minIndex) && visited.get(j) == false) {
minIndex = j;
}
}
visited.set(minIndex, true);
// for each adjacent vertex calculate min distance
int numFriends = map.get(nodes.get(minIndex)).size();
for (int j = 0; j < numFriends; j++) {
String currentFriend = map.get(nodes.get(minIndex)).get(j).getName();
int unfriendlinessRank = 10 - map.get(nodes.get(minIndex)).get(j).getRank();
// see which index that friend is located at
for (int k = 0; k < nodes.size(); k++) {
if (nodes.get(k).equals(currentFriend)) {
// update current min distance
if (distances.get(k) > (unfriendlinessRank + distances.get(minIndex))) {
// found a faster route
distances.set(k, unfriendlinessRank + distances.get(minIndex));
prevs.set(k, nodes.get(minIndex));
}
}
}
}
}
// final contents of lists
printLists("Final", nodes, distances, prevs, visited);
// showing the user the total unfriendliness value
int unfriendlinestValue = distances.get(nodes.indexOf(secondUser));
// show best friend chain
if (unfriendlinestValue >= 2000000 || unfriendlinestValue <= -2000000) {
System.out.println("\nUnable to Obtain Friend Chain :(");
} else {
// friend chain is possible
System.out.printf("\nTotal Unfriendlinest Value = %d\n", unfriendlinestValue);
// representing the chain in an arraylist
ArrayList<String> chain = new ArrayList<>();
chain.add(nodes.get(distances.indexOf(unfriendlinestValue)));
while (!(prevs.get(nodes.indexOf(chain.get(0))).equals("_"))) {
chain.add(0, prevs.get(nodes.indexOf(chain.get(0))));
}
// printing out the best friend chain
System.out.print("Best Friend Chain: ");
System.out.print(chain.get(0));
for (int i = 1; i < chain.size(); i++) {
System.out.print(" --> " + chain.get(i));
}
System.out.println();
}
}
// prints out the lists in a pretty fashion
public static void printLists(String time, ArrayList a, ArrayList b, ArrayList c, ArrayList d) {
System.out.println("\n" + time + " list contents");
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
public static void mutualFriends() {
//get user1's friends, check user 2's friends, find commonalities
ArrayList<String> mutualFriends = new ArrayList<String>();
//establish people to search between
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter the first user: ");
String firstUser = sc.next();
System.out.print("Enter the second user: ");
String secondUser = sc.next();
//search through both friends lists getting common ones
if (map.containsKey(firstUser)) {
if (map.containsKey(secondUser)) {
List<Friend> firstUserFriends = map.get(firstUser); //get user1 friends
List<Friend> secondUserFriends = map.get(secondUser); //get user2 friends
for (int i = 0; i < firstUserFriends.size(); i++) {
for (int j = 0; j < secondUserFriends.size(); j++){
if (firstUserFriends.get(i).getName().equals(secondUserFriends.get(j).getName())) { //check if equal
String tempString = firstUserFriends.get(i).getName(); //if equal make a tempString
mutualFriends.add(tempString); //add that tempString to an array list
}
}
}
if (!mutualFriends.isEmpty()){
System.out.println("These Mutual Friends were found:"); //print array list
System.out.println(mutualFriends);
}
else {
System.out.println("No mutual friends were found."); //no mutual friends found
}
}
else {
System.out.println(secondUser + " is not in this network" + "\n");
}
} else {
System.out.println(firstUser + " is not in this network" + "\n");
}
}
public static void bestQualityFriend() {
ArrayList<String> friendsList = new ArrayList<String>();
ArrayList<String> usersList = new ArrayList<String>();
ArrayList<String> maxFriendshipList = new ArrayList<String>();
int friendCounter = 0;
int runningFriendshipTotal = 0;
double averageFriendshipLevel = 0;
double valueOfBestFriend = 0;
int indexOfBestFriend = -1;
System.out.println("");
System.out.println("Users: Friends and Ranks");
for (String keys : map.keySet())
{
System.out.println(keys + ":"+ map.get(keys));
}
//System.out.println("\n");
//System.out.println(map.keySet());
for (String keys : map.keySet())
{
String tempString = map.get(keys).toString();
//System.out.println(tempString);
//tempString = tempString.replaceAll("\\D", "");
friendsList.add(tempString);
usersList.add(keys);
}
//loop iterates over every key in the map (users with associate friend lists)
for (int i = 0; i < usersList.size(); i++)
{
String currentUser = usersList.get(i);
//System.out.println(currentUser);
friendCounter = 0;
runningFriendshipTotal = 0;
averageFriendshipLevel = 0;
//iterates over the vlaues in the map (friends list with ranks)
for (int j = 0; j < friendsList.size(); j++)
{
//if index i is the same as index j, this is the user's own list of friends
//exclude the list of the user's own friends
if (j != i)
{
//iterates over each character of the values (friends and ranks)
//System.out.println(friendsList.get(j));
boolean isFriend = friendsList.get(j).indexOf(currentUser) != -1? true: false;
//System.out.println(isFriend);
if (isFriend)
{
int firstIndexOfName = friendsList.get(j).indexOf(currentUser);
int lengthOfName = currentUser.length();
friendCounter++;
//System.out.println(firstIndexOfName);
//System.out.println("Name lenght: " + lengthOfName);
int friendshipRank = Character.getNumericValue(friendsList.get(j).charAt(firstIndexOfName + lengthOfName + 2));
//System.out.println(friendshipRank);
//System.out.print("Friendshiplevel: ");
if (friendshipRank == 1)
{
if(friendsList.get(j).charAt(firstIndexOfName + lengthOfName + 3) == '0')
{
friendshipRank = 10;
//System.out.println(friendshipRank);
}
else
{
friendshipRank = 1;
//System.out.println(friendshipRank);
}
}
else
{
//System.out.println(friendshipRank);
}
//System.out.println(friendsList.get(j).charAt(firstIndexOfName + lengthOfName + 2));
//System.out.print(friendsList.get(j).charAt(firstIndexOfName + lengthOfName + 3));
runningFriendshipTotal += friendshipRank;
}
}
}
//System.out.println("Number of friends: " + friendCounter);
//System.out.println("Total friendshiplevel: " + runningFriendshipTotal);
averageFriendshipLevel = runningFriendshipTotal / (double) friendCounter;
//System.out.println("Average friendship total: " + averageFriendshipLevel);
if (averageFriendshipLevel > valueOfBestFriend)
{
indexOfBestFriend = i;
valueOfBestFriend = averageFriendshipLevel;
}
}
System.out.println("");
//System.out.println("The index of the best quality friend is: " + indexOfBestFriend);
System.out.println("The user who is the best quality friend is " + usersList.get(indexOfBestFriend) + " with a level of: " + valueOfBestFriend);
// System.out.println(usersList.get(0));
// System.out.println(friendsList.get(0));
// System.out.println(friendsList.get(0).charAt(0));
// System.out.println(friendsList.get(0).length());
}
}