Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- static final int MAXN = 505;
- static final double eps = 1e-7;
- static final double alpha = 0.85;
- static int n , m;
- static Map<Integer , ArrayList<Integer>> g = new HashMap<Integer , ArrayList<Integer>>();
- static double H[][] = new double[MAXN][MAXN];
- static double G[][] = new double[MAXN][MAXN];
- static ArrayList<Boolean> a = new ArrayList<Boolean>(MAXN);
- static double norm(ArrayList<Double> v){
- double sum = 0;
- for(double d : v){
- sum += d;
- }
- return sum;
- }
- static ArrayList<Double> Substract(ArrayList<Double> a , ArrayList<Double> b){
- ArrayList<Double> res = new ArrayList<Double>(MAXN);
- for(int i = 0 ; i < n ; ++i){
- res.add(i, a.get(i) - b.get(i));
- }
- return res;
- }
- static void Build_H(){
- for(int i = 0 ; i < n ; ++i){
- a.add(i , g.get(i).size() == 0);
- double cur = 1.0 / (double)g.get(i).size();
- for(int j : g.get(i)){
- H[i][j] = cur;
- }
- }
- }
- static void Build_G(){
- for(int i = 0 ; i < n ; ++i){
- double t = (alpha * (a.get(i)?1:0) + (1 - alpha)) / (double)n;
- for(int j = 0 ; j < n ; ++j){
- G[i][j] = H[i][j] * alpha;
- G[i][j] += t;
- }
- }
- }
- static void Transpose_G(){
- for(int i = 0 ; i < n ; ++i){
- for(int j = i + 1 ; j < n ; ++j){
- double tmp = G[i][j];
- G[i][j] = G[j][i];
- G[j][i] = tmp;
- }
- }
- }
- static ArrayList<Double> MultG(ArrayList<Double> v){
- ArrayList<Double> ans = new ArrayList<Double>();
- for(int i = 0 ; i < n ; ++i)ans.add(0.0);
- for(int i = 0 ; i < n ; ++i){
- double aaa = 0.0;
- for(int j = 0 ; j < n ; ++j){
- aaa += (G[i][j] * v.get(j));
- }
- ans.add(i , aaa);
- }
- return ans;
- }
- static ArrayList<Double> PowMethod(){
- ArrayList<Double> X = new ArrayList<Double>();
- ArrayList<Double> Y = new ArrayList<Double>();
- for(int i = 0 ; i < n ; ++i){
- X.add(i , 1.0);
- Y.add(i , 1.0);
- }
- double lambda1 , lambda2 = Double.MAX_VALUE;
- do{
- X = Y;
- lambda1 = lambda2;
- Y = MultG(Y);
- int pos = 0;
- while(Math.abs(X.get(pos)) < eps)++pos;
- lambda2 = Y.get(pos) / X.get(pos);
- double norma = norm(Y);
- ArrayList<Double> tmp = new ArrayList<Double>();
- for(int i = 0 ; i < n ; ++i){
- tmp.add(i , Y.get(i)/norma);
- }
- Y = tmp;
- }while(Math.abs(lambda1 - lambda2) > eps);
- return X;
- }
- static ArrayList<Double> PageRank(){
- Transpose_G();
- ArrayList<Double> v = PowMethod();
- return v;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- n = sc.nextInt();
- for(int i = 0 ; i < n ; ++i){
- g.put(i , new ArrayList<Integer>());
- }
- m = sc.nextInt();
- for(int i = 0 ; i < m ; ++i){
- int u = sc.nextInt();
- int v = sc.nextInt();
- --u;
- --v;
- ArrayList<Integer> tmp = g.get(u);
- tmp.add(v);
- g.put(u , tmp);
- }
- Build_H();
- Build_G();
- ArrayList<Double> v = PageRank();
- for(int i = 0 ; i < n ; ++i){
- System.out.print(v.get(i) + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement