using System.Collections.Generic;
using System.Linq;
using Microsoft.Msagl.Core.Layout;
namespace Microsoft.Msagl.Prototype.Ranking {
///
/// Class for centrality computation.
///
sealed internal class Centrality {
private Centrality() { } //to avoid creating the public constructor
///
/// Computes the PageRank in a directed graph.
///
/// A directed graph.
/// Probability of a jump
/// (with uniform probability) to some other node.
/// false=PageRank, true=TrustRank.
/// PageRank scores.
public static double[] PageRank(GeometryGraph graph, double omega, bool inverse) {
Dictionary p=new Dictionary();
int n = graph.Nodes.Count;
foreach(Node v in graph.Nodes) {
p[v] = 1d / (graph.Nodes.Count);
}
for(int c=0; c<50; c++) {
Dictionary q = new Dictionary();
foreach (Node v in graph.Nodes) {
q[v] = (1 - omega) / (graph.Nodes.Count);
}
if(inverse) { // backward propagation
foreach(Node v in graph.Nodes) {
foreach(Edge edge in v.OutEdges) {
Node u=edge.Target;
q[v]+=omega*p[u]/u.InEdges.Count();
}
}
} else { // forward propagation
foreach(Node v in graph.Nodes) {
foreach(Edge edge in v.InEdges) {
Node u=edge.Source;
q[v]+=omega*p[u]/u.OutEdges.Count();
}
}
}
p=q;
}
double[] result=new double[n];
int counter=0;
foreach(Node v in graph.Nodes) {
result[counter]=p[v];
counter++;
}
return result;
}
}
}