-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFollowsAnalysis.java
More file actions
294 lines (279 loc) · 9.09 KB
/
FollowsAnalysis.java
File metadata and controls
294 lines (279 loc) · 9.09 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
///////////////////////////////////////////////////////////////////////////////
// ALL STUDENTS COMPLETE THESE SECTIONS
// Title: Program 5, Twitter Graph
// Files: FollowsAnalysis.java, Graphnode.java, Graph.java
// Semester: Spring 2011
//
// Author: Erin Rasmussen ejrasmussen2@wisc.edu
// CS Login: rasmusse
// Lecturer's Name: Beck Hasti
// Lab Section: Lecture 2
//
//
// STUDENTS WHO GET HELP FROM ANYONE OTHER THAN THEIR PARTNER
// Credits: Some code taken from online readings
//////////////////////////// 80 columns wide //////////////////////////////////
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class FollowsAnalysis {
/**
* The application part of the graph which calculates certain statistics
* about the graph.
*
* <p>Bugs: large files take a very long time on certain computers for
* the "minutes" (Question 7) part of the calculations.
*
* @author Erin Rasmussen
*/
public static void main(String[] args) {
Graph twitter = new Graph();
int position = 0;
if (args.length != 1){
System.out.println("Usage: java FollowsAnalysis fileName");
System.exit(0);
}
if (!(new File(args[0]).exists())){
System.out.println("Error: Cannot access input file");
System.exit(0);
}
try {
File infile = new File(args[0]);
Scanner in = new Scanner(infile);
while (in.hasNextLine()){
position = 0;
String a = in.nextLine();
while(a.charAt(position) != ':'){
position++;
}
String user = a.substring(0,position).toLowerCase();
twitter.addNode(user);
int j = position + 1;
for (int i = position + 1; i < a.length(); i++){
if (a.charAt(i) == ','){
twitter.addEdge(user, a.substring(j, i).toLowerCase());
j = i + 1;
}
if (i == a.length()-1){
if (a.charAt(i) == ','){
if (!a.substring(j, i + 1).toLowerCase().equals(""))
twitter.addEdge(user, a.substring(j, i + 1)
.toLowerCase());
}
else{
if (!a.substring(j).toLowerCase().equals(""))
twitter.addEdge(user, a.substring(j).
toLowerCase());
}
}
}
}
} catch(FileNotFoundException e){
System.out.println("Error: Cannot access input file");
System.exit(0);
}
System.out.println("Number of users: " + twitter.size());
System.out.println("Number of follows links: " + twitter.numEdges());
System.out.print("DFS visit order: ");
List<String> dfs = twitter.dfs(twitter.allNodes.first().getName());
for (int i = 0; i < dfs.size() - 1; i++){
System.out.print(dfs.get(i) + ",");
}
System.out.print(dfs.get(dfs.size() - 1));
System.out.println();
System.out.print("BFS visit order: ");
List<String> bfs = twitter.bfs(twitter.allNodes.first().getName());
for (int i = 0; i < bfs.size() - 1; i++){
System.out.print(bfs.get(i) + ",");
}
System.out.print(bfs.get(bfs.size() - 1));
System.out.println();
System.out.print("Most followers: ");
TreeSet<Graphnode> mostFollowers = mostFollowers(twitter.allNodes);
Iterator<Graphnode> iter = mostFollowers.iterator();
while (iter.hasNext()){
Graphnode tmp = iter.next();
if (!iter.hasNext())System.out.print(tmp.getName());
else System.out.print(tmp.getName() + ",");
}
System.out.println();
System.out.println("No followers: " + noFollowers(twitter.allNodes));
System.out.println("Don't follow anyone: " + doesNotFollow
(twitter.allNodes));
System.out.println("Mutual followers: " + mutualFollowers
(twitter.allNodes));
System.out.print("Receive most tweets: ");
TreeSet<Graphnode> mostSubscribed = mostSubscribed(twitter.allNodes);
Iterator<Graphnode> iter2 = mostSubscribed.iterator();
while (iter2.hasNext()){
Graphnode tmp = iter2.next();
if (!iter2.hasNext())System.out.print(tmp.getName());
else System.out.print(tmp.getName() + ",");
}
System.out.println();
System.out.print("Reach everyone: ");
TreeSet<Graphnode> reachAll = reachAll(twitter.allNodes, twitter);
if(reachAll.size() == 0)System.out.println("none");
else {
Iterator<Graphnode> iter3 = reachAll.iterator();
while (iter3.hasNext()){
Graphnode tmp = iter3.next();
if (!iter3.hasNext())System.out.print(tmp.getName());
else System.out.print(tmp.getName() + ",");
}
System.out.println();
}
System.out.println("Minutes to get tweet: " + minutes(twitter.allNodes,
twitter));
}
/**
* Returns a TreeSet of the Graphnodes with the most followers.
*
* @param allNodes a list of all nodes in the graph
* @return a TreeSet of all nodes tied with the most followers
*/
public static TreeSet<Graphnode> mostFollowers(TreeSet<Graphnode>
allNodes){
TreeSet<Graphnode> mostFollowers = new TreeSet<Graphnode>();
mostFollowers.add(allNodes.first());
int most = allNodes.first().getOutDegree();
Iterator<Graphnode> iter = allNodes.iterator();
Graphnode tmp;
while (iter.hasNext()){
tmp = iter.next();
if (tmp.getOutDegree() > most){
most = tmp.getOutDegree();
mostFollowers.clear();
mostFollowers.add(tmp);
}
if (tmp.getOutDegree() == most) mostFollowers.add(tmp);
}
return mostFollowers;
}
/**
* Returns the amount of users who don't have any followers, i.e. they
* are just a follower of others.
* @param allNodes all nodes in the graph
* @return amount of users who have no followers
*/
public static int noFollowers(TreeSet<Graphnode> allNodes){
Iterator<Graphnode> iter = allNodes.iterator();
int count = 0;
while (iter.hasNext()){
if (iter.next().getOutDegree() == 0)count++;
}
return count;
}
/**
* Returns the amount of users who don't follow anyone
*
* @param allNodes all nodes in the graph
* @return # of users who don't follow anyone
*/
public static int doesNotFollow(TreeSet<Graphnode> allNodes){
Iterator<Graphnode> iter = allNodes.iterator();
int count = 0;
while (iter.hasNext()){
if (iter.next().getInDegree() == 0)count++;
}
return count;
}
/**
* Returns amount of pairs where the users are mutual followers of
* each other.
*
* @param allNodes all nodes in the graph
* @return amount of mutual followers
*/
public static int mutualFollowers(TreeSet<Graphnode> allNodes){
Iterator<Graphnode> iter = allNodes.iterator();
int count = 0;
while (iter.hasNext()){
Graphnode tmp = iter.next();
Graphnode tmp4 = null;
Iterator<String> iter2 = tmp.getSuccessors().iterator();
while (iter2.hasNext()){
String tmp2 = iter2.next();
Iterator<Graphnode> iter3 = allNodes.iterator();
while (iter3.hasNext()){
Graphnode tmp3 = iter3.next();
if (tmp2.equals(tmp3.getName()))tmp4 = tmp3;
}
Iterator<String> iter4 = tmp4.getSuccessors().iterator();
while (iter4.hasNext()){
if (iter4.next().equals(tmp.getName())) count++;
}
}
}
return count / 2; //because everything gets counted twice
}
/**
* Returns a TreeSet of the users who are the followers of the most
* users.
* @param allNodes all nodes in the graph
* @return a TreeSet of users who follow the most users
*/
public static TreeSet<Graphnode> mostSubscribed(TreeSet<Graphnode>
allNodes){
TreeSet<Graphnode> mostFollowers = new TreeSet<Graphnode>();
mostFollowers.add(allNodes.first());
int most = allNodes.first().getInDegree();
Iterator<Graphnode> iter = allNodes.iterator();
Graphnode tmp;
while (iter.hasNext()){
tmp = iter.next();
if (tmp.getInDegree() > most){
most = tmp.getInDegree();
mostFollowers.clear();
mostFollowers.add(tmp);
}
if (tmp.getInDegree() == most)mostFollowers.add(tmp);
}
return mostFollowers;
}
/**
* Returns a TreeSet of the users capable of reaching all users
* through retweeting. None is displayed if noone can do this.
*
* @param allNodes all nodes in the graph
* @param twitter the graph to be examines
* @return all users who can reach everyone
*/
public static TreeSet<Graphnode> reachAll(TreeSet<Graphnode> allNodes,
Graph twitter){
TreeSet<Graphnode> reachAll = new TreeSet<Graphnode>();
Iterator<Graphnode> iter = allNodes.iterator();
while (iter.hasNext()){
Graphnode tmp = iter.next();
List<String> tmpList = twitter.bfs(tmp.getName());
if (tmpList.size() == allNodes.size())reachAll.add(tmp);
}
return reachAll;
}
/**
* Returns the amount of minutes it would take for the user with the
* most followers to reach all of their followers through the retweets.
*
* @param allNodes all nodes in the graph
* @param twitter the graph to be examined
* @return minutes for the tweet to reach all followers
*/
public static int minutes(TreeSet<Graphnode> allNodes, Graph twitter){
Graphnode most = mostFollowers(allNodes).first();
@SuppressWarnings("unused")
List<String> shortestPath;
Iterator<Graphnode> iter = allNodes.iterator();
int max = 0;
while (iter.hasNext()){
Graphnode tmp = iter.next();
shortestPath = twitter.shortestPath(most.getName(), tmp.getName());
int intermediate = tmp.getDistance();
if (intermediate > max) max = intermediate;
}
iter = allNodes.iterator();
while (iter.hasNext()){
iter.next().resetDistance();
}
return max;
}
}