-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_3.java
More file actions
executable file
·153 lines (135 loc) · 5.81 KB
/
Exercise_3.java
File metadata and controls
executable file
·153 lines (135 loc) · 5.81 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
package exercise_3;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.graphx.*;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.storage.StorageLevel;
import scala.Tuple2;
import scala.collection.Iterator;
import scala.collection.JavaConverters;
import scala.reflect.ClassTag$;
import scala.runtime.AbstractFunction1;
import scala.runtime.AbstractFunction2;
import scala.runtime.AbstractFunction3;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Exercise_3 {
private static class Data implements Serializable{
private String name = "";
private String path = "";
private int dist = 0;
Data(String n, String p, int d){
this.name = n;
this.path = p;
this.dist = d;
}
void setName(String n) {
this.name = n;
}
void setPath(String p) {
this.path = p;
}
void setDist(int d) {
this.dist = d;
}
String getName() {
return this.name;
}
String getPath(){
return this.path;
}
int getDist() {
return this.dist;
}
}
private static class VProg extends AbstractFunction3<Long,Data,Data,Data> implements Serializable {
@Override
public Data apply(Long vertexID, Data vertexValue, Data message) {
if (message.getDist() == Integer.MAX_VALUE) { // superstep 0
return vertexValue;
} else { // superstep > 0
if(vertexValue.getDist()<message.getDist()) {
return vertexValue;
} else {
return message;
}
}
}
}
private static class sendMsg extends AbstractFunction1<EdgeTriplet<Data,Integer>, Iterator<Tuple2<Object,Data>>> implements Serializable {
@Override
public Iterator<Tuple2<Object, Data>> apply(EdgeTriplet<Data, Integer> triplet) {
Tuple2<Object,Data> sourceVertex = triplet.toTuple()._1();
Tuple2<Object,Data> dstVertex = triplet.toTuple()._2();
int dist = triplet.toTuple()._3();
if(sourceVertex._2.getDist() == Integer.MAX_VALUE)
return JavaConverters.asScalaIteratorConverter(Arrays.asList(new Tuple2<Object,Data>(triplet.dstId(),sourceVertex._2)).iterator()).asScala();
if(sourceVertex._2.getDist() + dist < dstVertex._2.getDist()){
Data updatedData = new Data(dstVertex._2.getName(),sourceVertex._2.getPath()+","+dstVertex._2.getName(), sourceVertex._2.getDist()+dist);
ArrayList<Tuple2<Object,Data>> myarray = new ArrayList();
myarray.add(new Tuple2<Object,Data>(triplet.dstId(),updatedData));
return JavaConverters.asScalaIteratorConverter(myarray.iterator()).asScala();
}
return JavaConverters.asScalaIteratorConverter(new ArrayList<Tuple2<Object,Data>>().iterator()).asScala();
}
}
private static class merge extends AbstractFunction2<Data,Data,Data> implements Serializable {
@Override
public Data apply(Data o, Data o2) {
if(o.getDist()<o2.getDist())
return o;
else
return o2;
}
}
public static void shortestPathsExt(JavaSparkContext ctx) {
Map<Long, String> labels = ImmutableMap.<Long, String>builder()
.put(1l, "A")
.put(2l, "B")
.put(3l, "C")
.put(4l, "D")
.put(5l, "E")
.put(6l, "F")
.build();
List<Tuple2<Object,Data>> vertices = Lists.newArrayList(
new Tuple2<Object,Data>(1l, new Data("A","A",0)),
new Tuple2<Object,Data>(2l, new Data("B","",Integer.MAX_VALUE)),
new Tuple2<Object,Data>(3l, new Data("C","",Integer.MAX_VALUE)),
new Tuple2<Object,Data>(4l, new Data("D","",Integer.MAX_VALUE)),
new Tuple2<Object,Data>(5l, new Data("E","",Integer.MAX_VALUE)),
new Tuple2<Object,Data>(6l, new Data("F","",Integer.MAX_VALUE))
);
List<Edge<Integer>> edges = Lists.newArrayList(
new Edge<Integer>(1l, 2l, 4), // A --> B (4)
new Edge<Integer>(1l, 3l, 2), // A --> C (2)
new Edge<Integer>(2l, 3l, 5), // B --> C (5)
new Edge<Integer>(2l, 4l, 10), // B --> D (10)
new Edge<Integer>(3l, 5l, 3), // C --> E (3)
new Edge<Integer>(5l, 4l, 4), // E --> D (4)
new Edge<Integer>(4l, 6l, 11) // D --> F (11)
);
JavaRDD<Tuple2<Object,Data>> verticesRDD = ctx.parallelize(vertices);
JavaRDD<Edge<Integer>> edgesRDD = ctx.parallelize(edges);
Graph<Data,Integer> G = Graph.apply(verticesRDD.rdd(),edgesRDD.rdd(),new Data("","",0), StorageLevel.MEMORY_ONLY(), StorageLevel.MEMORY_ONLY(),
scala.reflect.ClassTag$.MODULE$.apply(Data.class),scala.reflect.ClassTag$.MODULE$.apply(Integer.class));
GraphOps ops = new GraphOps(G, scala.reflect.ClassTag$.MODULE$.apply(Data.class),scala.reflect.ClassTag$.MODULE$.apply(Integer.class));
ops.pregel(new Data("","",Integer.MAX_VALUE),
Integer.MAX_VALUE,
EdgeDirection.Out(),
new VProg(),
new sendMsg(),
new merge(),
ClassTag$.MODULE$.apply(Data.class))
.vertices()
.toJavaRDD()
.foreach(v -> {
Tuple2<Object,Data> vertex = (Tuple2<Object,Data>)v;
System.out.println("Minimum cost to get from "+labels.get(1l)+" to "+labels.get(vertex._1)+" is ["+vertex._2.getPath()+"] with cost "+vertex._2.getDist());
});
}
}