0

I am a final year student, I am developing page ranking algorithm with java code, I am beginer in java,

I got Weighted page ranking algorithm code for java for my project,but it given

Error when i run it.

And i don't know how can give input in this program,

Help me to resolve this problem... Code below...

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();
     }

}
user2967857
  • 11
  • 1
  • 2
  • what happens if line does not have `-` String[] toNodes = new String[20]; words = br.readLine().split("-"); fromNodes[0] = words[0]; toNodes = words[1].split(","); – Scary Wombat Aug 05 '16 at 02:43
  • Hint: if your question gets closed because it was tagged as a duplicate for a generic question, just deleting the specific mention of that error is probably not going to make this an answerable question. What have you tried to figure out out why it's getting OOB and add guards against it? – Foon Aug 05 '16 at 03:53

0 Answers0