Data Science with Apache Spark

Searchβ¦

Graphx Graph Traversal with Pregel Explained

Breadth First Graph Traversal with Graphx using Pregel

To demonstrate Graph Traversal using Pregel from Graphx, construct the graph first, called graph:

1

val vertices=sc.parallelize(Seq((1L,1.0),(2L,2.0),(3L,3.0),(4L,4.0),(5L,5.0),(6L,6.0),(7L,7.0)))

2

val edges=sc.parallelize(Seq(Edge(1L,2L,1),Edge(1L,3L,2), Edge(2L,4L,3), Edge(2L,5L,4), Edge(3L,6L, 5), Edge(3L,7L, 6)))

3

val graph=Graph(vertices,edges)

Copied!

Create a new graph called markedGraph by transforming original graph:

1

// travers starts from root of the tree, which is id 1

2

val start: VertexId = 1

3

//set start vertex attributes to 0.0, others to Double.inifinity

4

val markedGraph = graph.mapVertices((id, _) => if (id == start) 0.0 else Double.PositiveInfinity)

Copied!

Thought Process:

markedGraph is the same as original graph except vertex attributes other than the VertexId=1 is Infinity. Vertex attribute for VertexId=1 is 0.0.

We will visit all vertices as long as vertex attributes are Infinity. During visit, set the visited Vertex attribute to non Infinity.

We will not visit vertices where vertex attributes are not Infinity.

When each vertex attribute is no longer Infinity in the end, visit (traverse) ends, thus complete the graph traversal.

Then print all vertex id out after traversal ends where no more vertex attributes is Infinity.

Implementation of Pregel:

Calling < graph >.pregel will return a new graph. We will use Pregel to traverse markedGraph that has starting vertex (1, 0.0), rest of vertices are (VertexId, Double.Infinity).

Internally, pregel will run in loop, each step of the loop is called SuperStep. During each SuperStep, the vertex where its vertex attribute not Double.Infinity will send its vertex attribute to connected neighboring vertices whose vertex attribites are Double.Infinity. The receiving vertices whose vertex attributes will be set to the smallest value of messages (if it receives more than one messages from different connected beighboring sending vertices), overwriting its original vertex value of Double.Infinity.

Vertices whose vertex attributes are Double.Infinity do not send message, just stay put.

Also, vertices do not send messages to connected neighboring vertices whose vertex attributes are NOT Double.Infinity, also stay put.

Loop or iteration will end when all vertices whose vertex attributes are NOT Double.Infinity. Hence, traverse completes.

1

//vprog is Vertex Program, take action with message received, to set the Vertex attribute with smaller value message, if it was β, it will be replaced // by a value which is smaller than β

2

β

3

val vprog = { (id: VertexId, attr: Double, msg: Double) => math.min(attr,msg) }

4

β

5

val sendMessage = { (triplet: EdgeTriplet[Double, Int]) =>

6

//define the return variable βrunβ as an iterator, initialized during each superstep

7

var run:Iterator[(VertexId, Double)] = Iterator.empty

8

//Only the vertex that has Double.PositiveInfinity has not been visited, to avoid endless loop

9

if(!(triplet.srcAttr != Double.PositiveInfinity && triplet.dstAttr != Double.PositiveInfinity)){

10

if(triplet.srcAttr != Double.PositiveInfinity){

11

//If source Vertex attribute is not infinity, dest vertex attribute is infinity, send source attribute+1,

12

// as new dest vertex attribute, overwrite its original infinity value

13

run = Iterator((triplet.dstId,triplet.srcAttr+1))

14

}else if(triplet.dstAttr != Double.PositiveInfinity) {

15

//If dest Vertex attribute is not infinity, source vertex attribute is infinity, send dest attribute+1,

16

// as new source vertex attribute, overwrite its original infinity value

17

run = Iterator((triplet.srcId,triplet.dstAttr+1))

18

}

19

}

20

//return the the run that has the Iterator

21

run

22

}

23

β

24

//MergeMessage is a reduce function, the set the vertex attribute that has received more than one message in the Iterator

25

//if the receiving vertex has connect to more than one sending vertex and received more than one messages, choose the smallest message by reduce

26

//val mergeMessage = { (a: Double, b: Double) => math.min(a,b) }

27

β

28

val graphTraverse = markedGraph.pregel(Double.PositiveInfinity, 20)(vprog, sendMessage, mergeMessage)

Copied!

How Pregel works

Pregel method take 2 argument lists

pregel(< argument list 1 >)(< argument list 2 >)

argument list 1 is a list of value arguments, argument list 1 is to set initial value

argument list 2 is a list of functional arguments, which is an example of Scala higher order function, meaning each function argument is free to have its own execution algorithm, its own logic.

In this example

1

val graphTraverse = markedGraph.pregel`(Double.PositiveInfinity, 20)`(vprog, sendMessage, mergeMessage)

Copied!

first argument list is

(Double.PositiveInfinity, 20)

It defines:

- 1.Initial message is Double.PositiveInfinity. The initial message is to start the computation. This message is passed to all the vertices in the vertexRDD to do the 1st iteration. Including the starting vertex whose vertex attribute is 0.0, but Double.PositiveInfinity is greater than 0.0, therefore, it will not replace vertex attribute that is not greater than the message which is Double.PositiveInfinity.
- 2.Maximum iteration 20, loop will end after 20 SuperSteps. In this example, 20 will be sufficient as a mechanism to break endless loop if it happens.

second argument list is

(vprog, sendMessage, mergeMessage)

that contains 3 function arguments:

//vprog is Vertex Program, take action with message received, to set the Vertex attribute with smaller value message, if it was β, it will be replaced // by a value which is smaller than β

val vprog = { (id: VertexId, attr: Double, msg: Double) => math.min(attr,msg) }

1

//vprog is Vertex Program, take action with message received, to set the Vertex attribute with smaller value message, if it was β, it will be replaced

2

// by a value which is smaller than β

3

β

4

val vprog = { (id: VertexId, attr: Double, msg: Double) => math.min(attr,msg) }

Copied!

sendMessage During each SuperStep in the loop once the computation starts, each triplet that has the connected neighboring vertices to examine whether sending message can proceed, *if and only if a vertex whose vertex attribute is NOT Double.PositiveInfinity can send message ONLY to directly connected vertex whose vertex attribute is Double.PositiveInfinity*

The message if send is in the form of sending vertex attribute plus 1.

Logic of this code is:

For each SuperStep, sendMessage returns an Iterator of VertexId and message received. (Again, message is in the form of sending vertex attribute plus 1)

1

val sendMessage = { (triplet: EdgeTriplet[Double, Int]) =>

2

//define the return variable βrunβ as an iterator, initialized during each superstep

3

var run:Iterator[(VertexId, Double)] = Iterator.empty

4

//Only the vertex that has Double.PositiveInfinity has not been visited, to avoid endless loop

5

if(!(triplet.srcAttr != Double.PositiveInfinity && triplet.dstAttr != Double.PositiveInfinity)){

6

if(triplet.srcAttr != Double.PositiveInfinity){

7

//If source Vertex attribute is not infinity, dest vertex attribute is infinity, send source attribute+1,

8

// as new dest vertex attribute, overwrite its original infinity value

9

run = Iterator((triplet.dstId,triplet.srcAttr+1))

10

}else if(triplet.dstAttr != Double.PositiveInfinity) {

11

//If dest Vertex attribute is not infinity, source vertex attribute is infinity, send dest attribute+1,

12

// as new source vertex attribute, overwrite its original infinity value

13

run = Iterator((triplet.srcId,triplet.dstAttr+1))

14

}

15

}

16

//return the the run that has the Iterator

17

run

18

}

Copied!

mergeMessage

For reciving Vertex, if multiple messages are received, return the smallest message value.

1

//MergeMessage is a reduce function, the set the vertex attribute that has received more than one message in the Iterator

2

//if the receiving vertex has connect to more than one sending vertex and received more than one messages, choose the smallest message by reduce

3

//val mergeMessage = { (a: Double, b: Double) => math.min(a,b) }

Copied!

Following is visualization of each SuperStep in the loop:

Super Step 1: root vertex (1,0.0) sends message which its vertex attribute 0.0+1=1.0 to its connected neighboring vertices (vertexId=2) and (vertexId=3) which had Double.PositiveInfinity prior, setting vertex (vertexId=2) to (2,1.0), (vertexId=3) to (3,2.0)

Super Step 2: In this step, vertices (vertexId=2) and (vertexId=3) sends message which its vertex attribute 1.0+1=2.0 to its connected neighboring vertices, sent the vertex attributes of these receiving vertices to 2.0

Super Step 3: No vertex attributes are Double.PositiveInfinity, loop ends. Here is the end state of the Graph that is returned by pregel call

Summary:

Before Super Loop starts

markedGraph.vertices.collect

res2: Array[(org.apache.spark.graphx.VertexId, Double)] = Array((4,Infinity), (1,0.0), (5,Infinity), (6,Infinity), (2,Infinity), (3,Infinity), (7,Infinity))

After Super Loop ends, all vertices with Infinity attributes have been changed to a non infinity value during visiting the vertex by message

graphTraverse.vertices.collect

res3: Array((4,2.0), (1,0.0), (5,2.0), (6,2.0), (2,1.0), (3,1.0), (7,2.0))

If you print out based on traverse, it should be starting from where the vertex attribute to be 0.0, then propagating to 1.0, then 2.0

Here is the entire Graph traverse code and the result of running it is the path of graph traversal

1

import org.apache.spark._

2

import org.apache.spark.graphx._

3

import org.apache.spark.rdd.RDD

4

β

5

β

6

β

7

def graphTraversal(startVertexId: VertexId, graph: org.apache.spark.graphx.Graph[Double,Int]):

8

org.apache.spark.graphx.Graph[Double,Int]={

9

β

10

//graph.triangleCount.vertices.collect.foreach(println)

11

/*

12

Output tuple pair, 1st element is node id, 2nd element is triangle count

13

All zeros

14

(4,0)

15

(6,0)

16

(2,0)

17

(1,0)

18

(3,0)

19

(7,0)

20

(5,0)

21

*/

22

//Following codes travers the tree graph:

23

// travers starts from root of the tree, which is id 1

24

var traverseList: List[Long]=List()

25

val start: VertexId = startVertexId

26

//set start vertex attributes to 0.0, others to Double.inifinity

27

val markedGraph = graph.mapVertices((id, _) => if (id == start) 0.0 else

28

Double.PositiveInfinity)

29

β

30

β

31

//val vprog = { (id: VertexId, attr: Double, msg: Double) => math.min(attr,msg)}

32

//vprog receiving messages in parallel for messages in the same SuperStep, sendMessage always returns Iterator

33

def vprog (id: org.apache.spark.graphx.VertexId, attr: Double, msg: Double): Double =

34

math.min(attr,msg)

35

val sendMessage = { (triplet: EdgeTriplet[Double, Int]) => {

36

var run:Iterator[(VertexId, Double)] = Iterator.empty

37

//Only the vertex that has Double.PositiveInfinity has not been visited, to avoid endless loop

38

if(!(triplet.srcAttr != Double.PositiveInfinity && triplet.dstAttr != Double.PositiveInfinity)){

39

if(triplet.srcAttr != Double.PositiveInfinity && triplet.dstAttr == Double.PositiveInfinity){

40

// println(" ")

41

run = Iterator((triplet.dstId,triplet.srcAttr+1))

42

}else if(triplet.dstAttr != Double.PositiveInfinity && triplet.srcAttr == Double.PositiveInfinity){

43

// println(" ")

44

run = Iterator((triplet.srcId,triplet.dstAttr+1))

45

}

46

}

47

run

48

}

49

}

50

val mergeMessage = { (a: Double, b: Double) => math.min(a,b) }

51

return markedGraph.pregel(Double.PositiveInfinity, 20)(vprog, sendMessage, mergeMessage)

52

β

53

}

54

β

55

//Now call the function graphTraversal

56

β

57

val vertices=sc.parallelize(Seq((1L,1.0),(2L,2.0),(3L,3.0),(4L,4.0),(5L,5.0),(6L,6.0),(7L,7.0)))

58

val edges=sc.parallelize(Seq(Edge(1L,2L,1),Edge(1L,3L,2), Edge(2L,4L,3), Edge(2L,5L,4), Edge(3L,6L, 5), Edge(3L,7L, 6)))

59

β

60

graphTraversal(1L, Graph(vertices,edges)).vertices.sortBy(_._2).map(x=>x._1).collect.foreach(println)

61

β

62

/*

63

Output the traverse sequence:

64

β

65

1

66

2

67

3

68

4

69

5

70

6

71

7

72

*/

Copied!

Summary

Clearly, as you can see, Graph traversal by sending message from the starting Vertex to its directly connected vertices then propagate to next layer of vertices is accomplished by breadth first traversal.

Last modified 1yr ago

Copy link