-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement PageRank on VertexProgram API
- Loading branch information
Joseph Poremba
authored and
Joseph Poremba
committed
Nov 26, 2021
1 parent
225583a
commit 5f558c8
Showing
3 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.algorithm | ||
|
||
// Non-normalized PageRank | ||
// Assumes no sink vertices | ||
class PageRank(iters: Int) extends VertexProgram[Int, Double, Double, Double] { | ||
|
||
require(iters >= 1) | ||
|
||
override val mode: VertexProgram.Mode = VertexProgram.Outwards | ||
|
||
override def gather(edgeVal: Int, message: Double): Double = message | ||
|
||
override def sum(a: Double, b: Double): Double = a + b | ||
|
||
override def apply(superStepNumber: Int, thisVertex: VertexInfo, oldVal: Double, total: Option[Double]): Double = { | ||
if (superStepNumber == 0) { | ||
// Do nothing on first superstep except send PR to neighbours | ||
oldVal | ||
} else { | ||
// println(s"StepNum: ${superStepNumber}") | ||
val sum = total.getOrElse(0.0) // Safe: should have received something from every neighbour | ||
0.15 + (0.85 * sum) | ||
} | ||
} | ||
|
||
override def scatter(superStepNumber: Int, thisVertex: VertexInfo, oldVal: Double, newVal: Double): Option[Double] = { | ||
if(superStepNumber >= iters) { | ||
None | ||
} else { | ||
Some(newVal / thisVertex.degree) | ||
} | ||
} | ||
|
||
override def voteToHalt(superStepNumber: Int, oldVal: Double, newVal: Double): Boolean = { | ||
superStepNumber >= iters | ||
} | ||
|
||
override val defaultVertexValue: Double = 1.0 | ||
|
||
override val defaultActivationStatus: Boolean = true | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.algorithm | ||
|
||
import org.scalatest.matchers.MatchResult | ||
import org.scalatest.matchers.Matcher | ||
|
||
object EpsilonCloseMatching { | ||
|
||
class EpsilonCloseCheck[T](epsilon: Double, expected: Map[T, Double]) extends Matcher[Map[T, Double]] { | ||
|
||
def apply(left: Map[T, Double]): MatchResult = { | ||
|
||
val bad = expected.filter { case (k, v) => { | ||
left.get(k) match { | ||
case None => false | ||
case Some(v_prime) => Math.abs(v - v_prime) > epsilon | ||
} | ||
}} | ||
|
||
MatchResult( | ||
left.keySet == expected.keySet && bad.isEmpty, | ||
s"Map ${left} not sufficiently close to Map ${expected}", | ||
"Maps sufficiently close" | ||
) | ||
} | ||
|
||
} | ||
|
||
def BeEpsilonClose[T](epsilon: Double)(expected: Map[T, Double]): Matcher[Map[T, Double]] = new EpsilonCloseCheck(epsilon, expected) | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters