java错误中的加权页面排序算法

时间:2016-08-05 02:37:59

标签: java arrays algorithm pagerank

我是最后一年的学生,我正在使用java代码开发页面排名算法,我是java的初学者,

我为我的项目获得了java的加权页面排名算法代码,但它给出了

运行时出错。

我不知道如何在这个程序中提供输入,

帮我解决这个问题... 代码如下......

package pagerank;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class PRNew {

     private static int count;          
     static int[][] matrix;        
     static double[][] weightMatrix; 
     static double[] outLinks;     
     static double[] inLinks;     
     static double[] pageRank;  
     static double d = 0.85;       
     boolean flag = true;     

     public PRNew(int count)
     {
         this.count = count;
         pageRank = new double[count];
         for(int i = 0; i < count; i++)
             pageRank[i] = 1/count;         
     }

     public void InitialMatrix() throws IOException
     {
         matrix = new int[count][count];               
         File file = new File("graph.txt"); 


         FileReader fr;
         BufferedReader br;
         if(file.exists())           
         {
              fr = new FileReader(file);
              br = new BufferedReader(fr);  
              int i;
              while(br.ready())
              {
                  String[] words = new String[20];
                  String[] fromNodes = new String[20];  
                  String[] toNodes = new String[20];    
                  words = br.readLine().split("-");
                  fromNodes[0] = words[0];
                  toNodes = words[1].split(",");
                  int row = 0;
                  for(i = 0;i < toNodes.length;i++)
                  {
                      int column = 0;
                      //System.out.println(fromNodes[0]+toNodes[i]);
                      switch(fromNodes[0].charAt(0))
                      {
                      case 'A': row = 0;break;
                      case 'B': row = 1;break;
                      case 'C': row = 2;break;
                      case 'D': row = 3;break;
                      case 'E': row = 4;break;
                      case 'F': row = 5;break;
                      case 'G': row = 6;break;
                      case 'H': row = 7;break;
                      case 'I': row = 8;break;
                      default : row = 0;break;
                      }
                      switch(toNodes[i].charAt(0))
                      {
                      case 'A': column = 0;break;
                      case 'B': column = 1;break;
                      case 'C': column = 2;break;
                      case 'D': column = 3;break;
                      case 'E': column = 4;break;
                      case 'F': column = 5;break;
                      case 'G': column = 6;break;
                      case 'H': column = 7;break;
                      case 'I': column = 8;break;
                      default : column = 0;break;
                      }  
                      matrix[row][column] = 1;      
                      //System.out.println(matrix[row][column]);
                  }

              }
         }
         else         
         {
             System.out.println("file does not exist!");
             System.exit(0);
         }
     }



     public void inLinks()
     {
         inLinks = new double[count];
         for(int i = 0;i < count; i++)
         {
             for(int j = 0; j < count; j++)
             {
                 inLinks[i] += matrix[j][i];
             }
             //System.out.println(inLinks[i]);
         }
     }


     public void outLinks()
     {
         outLinks = new double[count];
         for(int i = 0;i < count; i++)
         {
             for(int j = 0; j < count; j++)
             {
                 outLinks[i] += matrix[i][j];
             }
             //System.out.println(outLinks[i]);
         }
     }

     public void weightMatrix()
     {
         weightMatrix = new double[count][count];
         int[] sumIn = new int[count];
         int[] sumOut = new int[count];
         for(int i = 0;i < count;i++)
         {
             for(int j = 0; j < count; j++)
             {
                 if(matrix[i][j] == 1)               
                 {
                     sumIn[i] += inLinks[j];         
                     sumOut[i] += outLinks[j];      
                 }
             }
         }
         for(int i = 0;i < count;i++)
         {
             for(int j = 0; j < count; j++)
             {
                 if(matrix[i][j] == 1)            
                 {
                     weightMatrix[i][j] = (inLinks[j]/sumIn[i])*(outLinks[j]/sumOut[i]);
                     //System.out.println(weightMatrix[i][j]);
                 }
             }
         }


     }

     public double[] CalculatePR(double[] pagerank)
     {
         double totle = 0;
         double pageRank1 = pagerank[0];           
         double pageRank2;                         
         for(int j = 0; j < count; j++)
         {
             double sum = 0;
             for(int k = 0; k < count; k++)
             {
                 sum += weightMatrix[j][k]*pageRank[k]*matrix[k][j]/outLinks[k]; 
             }
             pageRank[j] = (1-d)/count + d*sum;                  
             totle += pageRank[j];
             System.out.print(pageRank[j]+":");
         }
         pageRank2 = pageRank[0];                        
         if(Math.abs(pageRank1-pageRank2) < Math.pow(10, -10))
             flag = false;
         else
             flag = true;


         for(int i = 0; i < count; i++)
         {
             pageRank[i] = pageRank[i]/totle;
         }
         return pageRank;
     }

     public void CalculateResult()
     {
         double[] pageRanks = pageRank;
         int i = 0;
         while(flag)
         {
             System.out.println("Rank"+(i+1)+"iteration:");
             pageRanks = CalculatePR(pageRanks);           
             System.out.println();
             i++;
         }

         for(int j = 0; j < count; j++)
         {
             System.out.println("the end result is:");
             System.out.println((j+1)+"-----"+pageRanks[j]);
         }

     }
     public static void main(String[] args) throws IOException
     {
         WeightPageRank pg = new WeightPageRank(8); 
         pg.InitialMatrix();
         pg.inLinks();
         pg.outLinks();
         pg.weightMatrix();
         for(int i = 0; i < count;i++)
         {
             for(int j = 0; j < count; j++)
             {
                 System.out.print(matrix[i][j]+"  ");
             }
             System.out.println();
         }

         for(int i = 0; i < count;i++)
         {
             for(int j = 0; j < count; j++)
             {
                 System.out.print(weightMatrix[i][j]+"  ");
             }
             System.out.println();
         }

         pg.CalculateResult();
     }

}

0 个答案:

没有答案