Advertisement
osipyonok

PageRank JAVA

Dec 7th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class Main {
  5.     static final int MAXN = 505;
  6.     static final double eps = 1e-7;
  7.     static final double alpha = 0.85;
  8.    
  9.     static int n , m;
  10.     static Map<Integer , ArrayList<Integer>> g = new HashMap<Integer , ArrayList<Integer>>();
  11.    
  12.    
  13.     static double H[][] = new double[MAXN][MAXN];
  14.     static double G[][] = new double[MAXN][MAXN];
  15.     static ArrayList<Boolean> a = new ArrayList<Boolean>(MAXN);
  16.    
  17.     static double norm(ArrayList<Double> v){
  18.         double sum = 0;
  19.         for(double d : v){
  20.             sum += d;
  21.         }
  22.         return sum;
  23.     }
  24.    
  25.     static ArrayList<Double> Substract(ArrayList<Double> a , ArrayList<Double> b){
  26.         ArrayList<Double> res = new ArrayList<Double>(MAXN);
  27.         for(int i = 0 ; i < n ; ++i){
  28.             res.add(i, a.get(i) - b.get(i));
  29.         }
  30.         return res;
  31.     }
  32.    
  33.     static void Build_H(){
  34.         for(int i = 0 ; i < n ; ++i){
  35.             a.add(i , g.get(i).size() == 0);
  36.             double cur = 1.0 / (double)g.get(i).size();
  37.             for(int j : g.get(i)){
  38.                 H[i][j] = cur;
  39.             }
  40.         }
  41.     }
  42.    
  43.     static void Build_G(){
  44.         for(int i = 0 ; i < n ; ++i){
  45.             double t = (alpha * (a.get(i)?1:0) + (1 - alpha)) / (double)n;
  46.             for(int j = 0 ; j < n ; ++j){
  47.                 G[i][j] = H[i][j] * alpha;
  48.                 G[i][j] += t;
  49.             }
  50.         }
  51.     }
  52.    
  53.     static void Transpose_G(){
  54.         for(int i = 0 ; i < n ; ++i){
  55.             for(int j = i + 1 ; j < n ; ++j){
  56.                 double tmp = G[i][j];
  57.                 G[i][j] = G[j][i];
  58.                 G[j][i] = tmp;
  59.             }
  60.         }
  61.     }
  62.    
  63.     static ArrayList<Double> MultG(ArrayList<Double> v){
  64.         ArrayList<Double> ans = new ArrayList<Double>();
  65.         for(int i = 0 ; i < n ; ++i)ans.add(0.0);
  66.         for(int i = 0 ; i < n ; ++i){
  67.             double aaa = 0.0;
  68.             for(int j = 0 ; j < n ; ++j){
  69.                 aaa += (G[i][j] * v.get(j));
  70.             }
  71.             ans.add(i , aaa);
  72.         }
  73.         return ans;
  74.     }
  75.    
  76.     static ArrayList<Double> PowMethod(){
  77.         ArrayList<Double> X = new ArrayList<Double>();
  78.         ArrayList<Double> Y = new ArrayList<Double>();
  79.         for(int i = 0 ; i < n ; ++i){
  80.             X.add(i , 1.0);
  81.             Y.add(i , 1.0);
  82.         }
  83.         double lambda1 , lambda2 = Double.MAX_VALUE;
  84.         do{
  85.            
  86.             X = Y;
  87.             lambda1 = lambda2;
  88.             Y = MultG(Y);
  89.             int pos = 0;
  90.             while(Math.abs(X.get(pos)) < eps)++pos;
  91.             lambda2 = Y.get(pos) / X.get(pos);
  92.             double norma = norm(Y);
  93.             ArrayList<Double> tmp = new ArrayList<Double>();
  94.             for(int i = 0 ; i < n ; ++i){
  95.                 tmp.add(i , Y.get(i)/norma);
  96.             }
  97.             Y = tmp;
  98.         }while(Math.abs(lambda1 - lambda2) > eps);
  99.         return X;
  100.     }
  101.    
  102.     static ArrayList<Double> PageRank(){
  103.         Transpose_G();
  104.         ArrayList<Double> v = PowMethod();
  105.         return v;
  106.     }
  107.    
  108.     public static void main(String[] args) {
  109.         Scanner sc = new Scanner(System.in);
  110.         n = sc.nextInt();
  111.         for(int i = 0 ; i < n ; ++i){
  112.             g.put(i , new ArrayList<Integer>());
  113.         }
  114.         m = sc.nextInt();
  115.         for(int i = 0 ; i < m ; ++i){
  116.             int u = sc.nextInt();
  117.             int v = sc.nextInt();
  118.             --u;
  119.             --v;
  120.             ArrayList<Integer> tmp = g.get(u);
  121.             tmp.add(v);
  122.             g.put(u , tmp);
  123.         }
  124.         Build_H();
  125.         Build_G();
  126.         ArrayList<Double> v = PageRank();
  127.         for(int i = 0 ; i < n ; ++i){
  128.             System.out.print(v.get(i) + " ");
  129.         }
  130.     }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement