-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmallWorld.java
420 lines (332 loc) · 13.5 KB
/
SmallWorld.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
/*
*
* CS61C Spring 2013 Project 2: Small World
*
* Partner 1 Name: Alec Guertin
* Partner 1 Login: de
*
* Partner 2 Name: Peter Sujan
* Partner 2 Login: cc
*
* REMINDERS:
*
* 1) YOU MUST COMPLETE THIS PROJECT WITH A PARTNER.
*
* 2) DO NOT SHARE CODE WITH ANYONE EXCEPT YOUR PARTNER.
* EVEN FOR DEBUGGING. THIS MEANS YOU.
*
*/
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.Math;
import java.util.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class SmallWorld {
// Maximum depth for any breadth-first search
public static final int MAX_ITERATIONS = 20;
// Example writable type
public static class EValue implements Writable {
public int exampleInt; //example integer field
public long[] exampleLongArray; //example array of longs
public EValue(int exampleInt, long[] exampleLongArray) {
this.exampleInt = exampleInt;
this.exampleLongArray = exampleLongArray;
}
public EValue() {
// does nothing
}
// Serializes object - needed for Writable
public void write(DataOutput out) throws IOException {
out.writeInt(exampleInt);
// Example of serializing an array:
// It's a good idea to store the length explicitly
int length = 0;
if (exampleLongArray != null){
length = exampleLongArray.length;
}
// always write the length, since we need to know
// even when it's zero
out.writeInt(length);
// now write each long in the array
for (int i = 0; i < length; i++){
out.writeLong(exampleLongArray[i]);
}
}
// Deserializes object - needed for Writable
public void readFields(DataInput in) throws IOException {
// example reading an int from the serialized object
exampleInt = in.readInt();
// example reading length from the serialized object
int length = in.readInt();
// Example of rebuilding the array from the serialized object
exampleLongArray = new long[length];
for(int i = 0; i < length; i++){
exampleLongArray[i] = in.readLong();
}
}
public String toString() {
// We highly recommend implementing this for easy testing and
// debugging. This version just returns an empty string.
return new String();
}
}
/* Represents a node, as well as its neighbors and whether it
* has been visited.
*/
public static class Vertex implements Writable {
int dist;
int visited;
ArrayList<LongWritable> neighbors;
LongWritable origin;
public Vertex() {
}
public Vertex(int d, int v, ArrayList<LongWritable> n, LongWritable orig) {
dist = d;
visited = v;
neighbors = n;
origin = orig;
}
public void write(DataOutput out) throws IOException {
out.writeInt(dist);
out.writeInt(visited);
int length = 0;
if (neighbors != null){
length = neighbors.size();
}
out.writeInt(length);
// now write each long in the array
for (int i = 0; i < length; i += 1){
out.writeLong(neighbors.get(i).get());
}
out.writeLong(origin.get());
}
public void readFields(DataInput in) throws IOException {
dist = in.readInt();
visited = in.readInt();
int length = in.readInt();
LongWritable x = new LongWritable();
neighbors = new ArrayList<LongWritable>();
for (int i = 0; i < length; i++) {
x.readFields(in);
neighbors.add(new LongWritable(x.get()));
}
origin = new LongWritable(in.readLong());
}
public String toString() {
String output = "";
for (int i = 0; i < neighbors.size(); i++) {
output += " " + neighbors.get(i);
}
output += " ; " + dist + " ; " + visited + " ; " + origin;
return output;
}
}
/* The first mapper. Part of the graph loading process, currently just an
* identity function. Modify as you wish. */
public static class LoaderMap extends Mapper<LongWritable, LongWritable,
LongWritable, LongWritable> {
@Override
public void map(LongWritable key, LongWritable value, Context context)
throws IOException, InterruptedException {
int inputValue = Integer.parseInt(context.getConfiguration().get("inputValue"));
context.write(key, value);
context.write(value, new LongWritable(-1));
}
}
/* The first reducer. This is also currently an identity function (although it
* does break the input Iterable back into individual values). Modify it
* as you wish. In this reducer, you'll also find an example of loading
* and using the denom field.
*/
public static class LoaderReduce extends Reducer<LongWritable, LongWritable,
LongWritable, Vertex> {
public long denom;
public void reduce(LongWritable key, Iterable<LongWritable> values,
Context context) throws IOException, InterruptedException {
int denom = Integer.parseInt(context.getConfiguration().get("denom"));
// You can print it out by uncommenting the following line:
//System.out.println(denom);
ArrayList<LongWritable> armenians = new ArrayList<LongWritable>();
for (LongWritable value : values){
if (value.get() != -1) {
armenians.add(new LongWritable(value.get()));
}
}
int distance = Integer.MAX_VALUE;
int visisted = -1;
double chance = (new Random()).nextDouble();
if (chance <= (1.0 / denom)) {
distance = 0;
visisted = 0;
}
if (armenians.size() == 0) {
context.write(key, new Vertex(0, 1, armenians, key));
} else {
context.write(key, new Vertex(distance, visisted, armenians, key));
}
}
}
// ------- Add your additional Mappers and Reducers Here ------- //
/* The BFS mapper.*/
public static class BFSMap extends Mapper<LongWritable, Vertex,
LongWritable, Vertex> {
@Override
public void map(LongWritable key, Vertex value, Context context)
throws IOException, InterruptedException {
int inputValue = Integer.parseInt(context.getConfiguration().get("inputValue"));
if (value.visited == 0) {
for (int i = 0; i < value.neighbors.size(); i += 1) {
Vertex distPlus = new Vertex(value.dist + 1, value.visited, new ArrayList<LongWritable>(), value.origin);
context.write(value.neighbors.get(i), distPlus);
}
Vertex visPlus = new Vertex(value.dist, value.visited + 1, value.neighbors, value.origin);
context.write(key, new Vertex(value.dist, value.visited + 1, value.neighbors, value.origin));
} else {
context.write(key, value);
}
}
}
/* The BFS reducer. */
public static class BFSReduce extends Reducer<LongWritable, Vertex,
LongWritable, Vertex> {
public long denom;
public void reduce(LongWritable key, Iterable<Vertex> values,
Context context) throws IOException, InterruptedException {
int minDist = Integer.MAX_VALUE;
ArrayList<LongWritable> serbians = null;
HashSet<Long> blackSet = new HashSet<Long>();
HashMap<Long, Integer> minDistances = new HashMap<Long, Integer>();
for (Vertex value : values){
if (value.visited == 0 && key.get() == value.origin.get() && value.dist != 0) {
continue;
}
if (value.visited != 0) {;
blackSet.add(value.origin.get());
if (minDistances.containsKey(value.origin.get())) {
minDistances.remove(value.origin.get());
}
context.write(key, value);
}
if (!blackSet.contains(value.origin.get()) && (!minDistances.containsKey(value.origin.get()) || (minDistances.get(value.origin.get()) > value.dist))) {
minDistances.put(value.origin.get(), value.dist);
}
if (value.neighbors.size() > 0) {
serbians = value.neighbors;
}
}
Iterator<Long> keys = minDistances.keySet().iterator();
while (keys.hasNext()) {
Long org = keys.next();
Vertex temp = new Vertex(minDistances.get(org), 0, serbians, new LongWritable(org));
context.write(key, temp);
}
}
}
/* The Histogram mapper. */
public static class HistoMap extends Mapper<LongWritable, Vertex, LongWritable, LongWritable> {
static LongWritable one = new LongWritable(1);
@Override
public void map(LongWritable key, Vertex value, Context context)
throws IOException, InterruptedException {
if (value.visited == 1) {
context.write(new LongWritable((long) value.dist), one);
}
}
}
/* The Histogram reducer. */
public static class HistoReduce extends Reducer<LongWritable, LongWritable,
LongWritable, LongWritable> {
public long denom;
public void reduce(LongWritable key, Iterable<LongWritable> values,
Context context) throws IOException, InterruptedException {
long total = 0;
for (LongWritable value : values){
total += value.get();
}
context.write(key, new LongWritable(total));
}
}
public static void main(String[] rawArgs) throws Exception {
GenericOptionsParser parser = new GenericOptionsParser(rawArgs);
Configuration conf = parser.getConfiguration();
String[] args = parser.getRemainingArgs();
// Pass in denom command line arg:
conf.set("denom", args[2]);
// Sample of passing value from main into Mappers/Reducers using
// conf. You might want to use something like this in the BFS phase:
// See LoaderMap for an example of how to access this value
conf.set("inputValue", (new Integer(5)).toString());
// Setting up mapreduce job to load in graph
Job job = new Job(conf, "load graph");
job.setJarByClass(SmallWorld.class);
job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(LongWritable.class);
job.setOutputKeyClass(LongWritable.class);
job.setOutputValueClass(Vertex.class);
job.setMapperClass(LoaderMap.class);
job.setReducerClass(LoaderReduce.class);
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
// Input from command-line argument, output to predictable place
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path("bfs-0-out"));
// Actually starts job, and waits for it to finish
job.waitForCompletion(true);
// Repeats your BFS mapreduce
int i = 0;
while (i < MAX_ITERATIONS) {
job = new Job(conf, "bfs" + i);
job.setJarByClass(SmallWorld.class);
// Feel free to modify these four lines as necessary:
job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(Vertex.class);
job.setOutputKeyClass(LongWritable.class);
job.setOutputValueClass(Vertex.class);
job.setMapperClass(BFSMap.class);
job.setReducerClass(BFSReduce.class);
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
// Notice how each mapreduce job gets gets its own output dir
FileInputFormat.addInputPath(job, new Path("bfs-" + i + "-out"));
FileOutputFormat.setOutputPath(job, new Path("bfs-"+ (i+1) +"-out"));
job.waitForCompletion(true);
i++;
}
// Mapreduce config for histogram computation
job = new Job(conf, "hist");
job.setJarByClass(SmallWorld.class);
// Feel free to modify these two lines as necessary:
job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(LongWritable.class);
// DO NOT MODIFY THE FOLLOWING TWO LINES OF CODE:
job.setOutputKeyClass(LongWritable.class);
job.setOutputValueClass(LongWritable.class);
// You'll want to modify the following based on what you call your
// mapper and reducer classes for the Histogram Phase
job.setMapperClass(HistoMap.class);
job.setReducerClass(HistoReduce.class);
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
// By declaring i above outside of loop conditions, can use it
// here to get last bfs output to be input to histogram
FileInputFormat.addInputPath(job, new Path("bfs-"+ i +"-out"));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
}
}