Pregel single source point shortest path in GRAPHX

Source: Internet
Author: User

Original link: Graphx in Pregel single source point shortest path

Example of a single source point shortest path in Graphx, using the method of class Pregel.

The core part is three functions:

1. Node processing message function Vprog: (Vertexid, VD, A) = VD (Node ID, node attribute, message) = = Node Property

2. Node Send Message function SENDMSG:EDGETRIPLET[VD, ED] = iterator[(vertexid,a)] (side tuple) + iterator[(target Node ID, message)]

3. Message merge function Mergemsg: (A, a) + a) (message, message) = = Message

 PackageMyClass. GraphXImportorg.apache.spark.graphx._ImportOrg.apache.spark.SparkContext//Import random graph Generation Library  Importorg.apache.spark.graphx.util.GraphGenerators/*** Created by Jack on 3/4/14. */Object Pregel {def main (args:array[string]) {val sc=NewSparkcontext ("local", "Pregel test", System.getenv ("Spark_home"), Sparkcontext.jarofclass ( This. GetClass)) //A graph with edge attributes containing distances//Initializes a random graph in which the degree of the node conforms to the logarithmic normal distribution and the Edge property is initialized to 1Val graph:graph[int, Double] =Graphgenerators.lognormalgraph (SC, numvertices= ten). Mapedges (E =e.attr.todouble) Graph.edges.foreach (println) Val Sourceid:vertexid= 4//The Ultimate Source//Initialize The graph such that all vertices except the root has distance infinity. //initialize the distance from each node to the origin pointVal initialgraph = graph.mapvertices (id, _) = =if(id = = SourceID) 0.0Elsedouble.positiveinfinity) Val sssp=Initialgraph.pregel (double.positiveinfinity) (//Vertex Program, a function of node processing messages, dist as the original Node property (double), newdist as the message type (double)(ID, dist, newdist) =math.min (Dist, newdist),//send Message, which returns the result as (target Node ID, message (i.e. shortest distance))triplet = {                  if(Triplet.srcattr + triplet.attr <triplet.dstattr) {Iterator (Triplet.dstid, triplet.srcattr+triplet.attr))}Else{Iterator.empty}},//Merge message, merging messages, similar to combiner in Hadoop(A, b) = =Math.min (A, B)) println (sssp.vertices.collect.mkString ("\ n"))      }  }  
Additional Reference Links:

http://bbs.csdn.net/topics/390985424

Graphx Pregel Single source point shortest path (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.