-
Notifications
You must be signed in to change notification settings - Fork 32
/
PageRank.scala
93 lines (78 loc) · 2.61 KB
/
PageRank.scala
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
/*
* @author Philip Stutz
*
* Copyright 2010 University of Zurich
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.signalcollect.examples
import com.signalcollect._
import com.signalcollect.configuration.ExecutionMode
/**
* Represents an edge in a PageRank compute graph
*
* @param s: the identifier of the source vertex
* @param t: the identifier of the target vertex
*/
class PageRankEdge[Id](t: Id) extends DefaultEdge(t) {
type Source = PageRankVertex[Id]
/**
* The signal function calculates how much rank the source vertex
* transfers to the target vertex.
*/
def signal = source.state * weight / source.sumOfOutWeights
}
/**
* Represents a page in a PageRank compute graph
*
* @param id: the identifier of this vertex
* @param dampingFactor: @see <a href="http://en.wikipedia.org/wiki/PageRank">PageRank algorithm</a>
*/
class PageRankVertex[Id](id: Id, dampingFactor: Double = 0.85) extends DataGraphVertex[Id, Double](id, 1 - dampingFactor) {
type Signal = Double
/**
* The collect function calculates the rank of this vertex based on the rank
* received from neighbors and the damping factor.
*/
def collect: Double = 1 - dampingFactor + dampingFactor * signals.sum
override def scoreSignal: Double = {
if (edgesModifiedSinceSignalOperation) {
1
} else {
lastSignalState match {
case None => 1
case Some(oldState) => (state - oldState).abs
}
}
}
}
/** Builds a PageRank compute graph and executes the computation */
object PageRank extends App {
val graph = new GraphBuilder[Any, Any]().
withConsole(true).
build
graph.awaitIdle
graph.addVertex(new PageRankVertex(1))
graph.addVertex(new PageRankVertex(2))
graph.addVertex(new PageRankVertex(3))
graph.addEdge(1, new PageRankEdge(2))
graph.addEdge(2, new PageRankEdge(1))
graph.addEdge(2, new PageRankEdge(3))
graph.addEdge(3, new PageRankEdge(2))
graph.awaitIdle
val stats = graph.execute(ExecutionConfiguration.withExecutionMode(ExecutionMode.Interactive))
println(stats)
graph.foreachVertex(println(_))
graph.shutdown
}