Class PageRank
- All Implemented Interfaces:
Algorithm,DynamicAlgorithm,org.graphstream.stream.ElementSink
public class PageRank extends Object implements DynamicAlgorithm, org.graphstream.stream.ElementSink
The PageRank is an algorithm that measures the "importance" of the nodes in a
graph. It assigns to each node a rank. This rank corresponds to the
probability that a "random surfer" visits the node. The surfer goes from node
to node in the following way: with probability setDampingFactor(double)). The ranks are real
numbers between 0 and 1 and sum up to one.
Usage
This implementation uses a variant of the power iteration algorithm to
compute the node ranks. It computes the approximate ranks iteratively going
closer to the exact values at each iteration. The accuracy can be controlled
by a precision parameter (see setPrecision(double)). When the L1
norm of the difference between two consecutive rank vectors becomes less than
this parameter, the result is considered precise enough and the computation
stops.
This implementation works with both directed and undirected edges. An undirected edge acts as two directed arcs.
The graph dynamics is taken into account and the ranks are not computed from
scratch at each modification in the structure of the graph. However, the
ranks become less and less accurate after each modification. To establish the
desired precision, one must either explicitly call compute() or ask
for a rank of a node by calling getRank(Node).
The computed ranks are stored in node attribute. The name of this attribute
can be changed by a call to setRankAttribute(String) but only before
the call to init(Graph). Another way to obtain the ranks is to call
getRank(Node). The second method is preferable because it will
update the ranks if needed and will always return values within the desired
precision.
Example
Graph graph = new SingleGraph("test");
graph.addAttribute("ui.antialias", true);
graph.addAttribute("ui.stylesheet",
"node {fill-color: red; size-mode: dyn-size;} edge {fill-color:grey;}");
graph.display();
DorogovtsevMendesGenerator generator = new DorogovtsevMendesGenerator();
generator.setDirectedEdges(true, true);
generator.addSink(graph);
PageRank pageRank = new PageRank();
pageRank.init(graph);
generator.begin();
while (graph.getNodeCount() < 100) {
generator.nextEvents();
for (Node node : graph) {
double rank = pageRank.getRank(node);
node.addAttribute("ui.size",
5 + Math.sqrt(graph.getNodeCount() * rank * 20));
node.addAttribute("ui.label", String.format("%.2f%%", rank * 100));
}
Thread.sleep(1000);
}
- Computational Complexity :
- Each iteration takes O(m + n) time, where n is the number of nodes and m is the number of edges. The number of iterations needed to converge depends on the desired precision.
- Scientific Reference :
- Lawrence Page, Sergey Brin, Rajeev Motwani and Terry Winograd. The PageRank citation ranking: Bringing order to the Web. 1999
-
Field Summary
Fields Modifier and Type Field Description static doubleDEFAULT_DAMPING_FACTORDefault damping factorstatic doubleDEFAULT_PRECISIONDefault precisionstatic StringDEFAULT_RANK_ATTRIBUTEDefault rank attribute -
Constructor Summary
-
Method Summary
Modifier and Type Method Description voidcompute()Run the algorithm.StringdefaultResult()voidedgeAdded(String sourceId, long timeId, String edgeId, String fromNodeId, String toNodeId, boolean directed)voidedgeRemoved(String sourceId, long timeId, String edgeId)doublegetDampingFactor()Returns the current damping factor.intgetIterationCount()Returns the total number of iterations.doublegetPrecision()Returns the currently used numeric precisiondoublegetRank(org.graphstream.graph.Node node)Returns the rank of a node.StringgetRankAttribute()Returns the current rank attributevoidgraphCleared(String sourceId, long timeId)voidinit(org.graphstream.graph.Graph graph)Initialization of the algorithm.voidnodeAdded(String sourceId, long timeId, String nodeId)voidnodeRemoved(String sourceId, long timeId, String nodeId)voidsetDampingFactor(double dampingFactor)Sets the damping factor.voidsetPrecision(double precision)Sets the numeric precision.voidsetRankAttribute(String rankAttribute)Sets the rank attribute.voidsetVerbose(boolean verbose)Switches on or off the verbose mode.voidstepBegins(String sourceId, long timeId, double step)voidterminate()Terminate the dynamic algorithm.
-
Field Details
-
DEFAULT_DAMPING_FACTOR
public static final double DEFAULT_DAMPING_FACTORDefault damping factor- See Also:
- Constant Field Values
-
DEFAULT_PRECISION
public static final double DEFAULT_PRECISIONDefault precision- See Also:
- Constant Field Values
-
DEFAULT_RANK_ATTRIBUTE
Default rank attribute- See Also:
- Constant Field Values
-
-
Constructor Details
-
Method Details
-
getDampingFactor
public double getDampingFactor()Returns the current damping factor.- Returns:
- The current damping factor
-
setDampingFactor
Sets the damping factor.- Parameters:
dampingFactor- The new damping factor- Throws:
IllegalArgumentException- If the damping factor is less than 0.01 or greater than 0.99
-
getPrecision
public double getPrecision()Returns the currently used numeric precision- Returns:
- The precision
-
setPrecision
Sets the numeric precision. Precision values close to zero lead to more accurate results, but slower convergence- Parameters:
precision- The new precision- Throws:
IllegalArgumentException- if the precision is less than 1.0e-7
-
getRankAttribute
Returns the current rank attribute- Returns:
- The current rank attribute
-
setRankAttribute
Sets the rank attribute. The computed ranks of each node are stored as values of this attribute.- Parameters:
rankAttribute- The node attribute used to store the computed ranks- Throws:
IllegalStateException- if the algorithm is already initialized
-
setVerbose
public void setVerbose(boolean verbose)Switches on or off the verbose mode. In verbose mode the algorithm prints at each iteration the number of iterations and the L1 norm of the difference between the current and the previous rank vectors.- Parameters:
verbose- Verbose mode
-
init
public void init(org.graphstream.graph.Graph graph)Description copied from interface:AlgorithmInitialization of the algorithm. This method has to be called before theAlgorithm.compute()method to initialize or reset the algorithm according to the new given graph. -
compute
public void compute()Description copied from interface:AlgorithmRun the algorithm. TheAlgorithm.init(Graph)method has to be called before computing.- Specified by:
computein interfaceAlgorithm- See Also:
Algorithm.init(Graph)
-
terminate
public void terminate()Description copied from interface:DynamicAlgorithmTerminate the dynamic algorithm.- Specified by:
terminatein interfaceDynamicAlgorithm- See Also:
Algorithm.init(org.graphstream.graph.Graph)
-
defaultResult
-
nodeAdded
- Specified by:
nodeAddedin interfaceorg.graphstream.stream.ElementSink
-
nodeRemoved
- Specified by:
nodeRemovedin interfaceorg.graphstream.stream.ElementSink
-
edgeAdded
public void edgeAdded(String sourceId, long timeId, String edgeId, String fromNodeId, String toNodeId, boolean directed)- Specified by:
edgeAddedin interfaceorg.graphstream.stream.ElementSink
-
edgeRemoved
- Specified by:
edgeRemovedin interfaceorg.graphstream.stream.ElementSink
-
graphCleared
- Specified by:
graphClearedin interfaceorg.graphstream.stream.ElementSink
-
stepBegins
- Specified by:
stepBeginsin interfaceorg.graphstream.stream.ElementSink
-
getRank
public double getRank(org.graphstream.graph.Node node)Returns the rank of a node. If the ranks are not up to date, recomputes them- Parameters:
node- A node- Returns:
- The rank of the node
-
getIterationCount
public int getIterationCount()Returns the total number of iterations. This number accumulates the number of iterations performed by each call tocompute(). It is reset to zero in the calls toinit(Graph).- Returns:
- The number of iterations
-