Advertisement
Guest User

Page Rank Power Iteration

a guest
Jan 22nd, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.Math;
  3.  
  4.  
  5. class PowerIteration{
  6.    
  7.     public static void main(String args[]){
  8.         Scanner scanner = new Scanner(System.in);
  9.         int N,M;
  10.         System.out.println("Enter Number of Nodes and Edges : ");
  11.         N = scanner.nextInt();
  12.         M = scanner.nextInt();
  13.        
  14.         PageRank pg = new PageRank(N,M);
  15.         pg.input();
  16.        
  17.         //-- iterate 50 times or until no difference between two iteration.
  18.         for(int i = 0; i < 50; i++){
  19.             pg.iteration();
  20.             System.out.println(i+" Iteration Done");
  21.             if(pg.stop()){
  22.                 System.out.println("NO Differece between Iteration");
  23.                 break;
  24.             }
  25.         }
  26.         pg.showPageRank();
  27.        
  28.     }
  29.    
  30. }
  31.  
  32. class PageRank{
  33.     int Node=100,Edge=100;
  34.     double epsilone = 0.0000000001;
  35.    
  36.     double[][] matrix = new double[Node][Edge];
  37.     double[]  rank = new double[Node];
  38.     double[]  old_rank = new double[Node];
  39.    
  40.     Scanner scanner = new Scanner(System.in);
  41.    
  42.     PageRank(int N,int M){
  43.         Node = N;
  44.         Edge = M;
  45.        
  46.        
  47.         for(int i =0; i < Node; i++){
  48.             rank[i] = 1/(double)Node;
  49.             //old_rank[i] = 1/ Node;
  50.         }
  51.     }
  52.    
  53.     void input(){
  54.         System.out.println("Input Value of Matrix\n");
  55.        
  56.         for(int i = 0 ; i < Node; i++){
  57.             for(int j =0; j < Node; j++){
  58.                 System.out.print("Enter value for matrix["+i+"]["+j+"] : ");
  59.                 matrix[i][j] = scanner.nextDouble();
  60.             }
  61.         }
  62.        
  63.     }
  64.    
  65.     void show(){
  66.         System.out.println("\nMatrix is :\n");
  67.         for(int i = 0; i < Node; i++){
  68.             for(int j = 0; j < Node; j++){
  69.                 System.out.print(matrix[i][j]+"  ");
  70.             }
  71.             System.out.println();
  72.         }
  73.     }
  74.    
  75.     boolean stop(){
  76.         for(int i = 0; i < Node; i++){
  77.             if(  Math.abs( rank[i] - old_rank[i]) < epsilone  )
  78.                 continue;
  79.             else
  80.                 return false;
  81.             }
  82.         return true;
  83.     }
  84.    
  85.     void iteration(){
  86.        
  87.         double[] product = new double[Node];
  88.         for(int i = 0; i < Node; i++){
  89.             old_rank[i] = rank[i];
  90.             product[i] = 0;
  91.             //System.out.print(rank[i]);
  92.         }
  93.        
  94.         //--multiply
  95.        
  96.        
  97.         for(int i =0; i < Node; i++){
  98.             for(int j = 0; j < Node; j++){
  99.                 product[i] += rank[j] * matrix[i][j];
  100.             }
  101.         }
  102.         //System.out.print("Now Rank is : ");      
  103.         for(int i =0 ; i < Node; i++){
  104.             rank[i] = product[i];
  105.             //System.out.print(product[i]+" ");
  106.         }
  107.     }
  108.    
  109.     void showPageRank(){
  110.         System.out.println("Page Rank is:\n");
  111.         for(int i = 0; i < Node; i++){
  112.             System.out.println("["+rank[i]+"]");
  113.         }
  114.         }
  115.    
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement