packagecom.thealgorithms.others;importjava.util.*;classPageRank{publicstaticvoidmain(String args[]){int nodes, i, j;Scanner in =newScanner(System.in);System.out.print("Enter the Number of WebPages: ");
nodes = in.nextInt();PageRank p =newPageRank();System.out.println("Enter the Adjacency Matrix with 1->PATH & 0->NO PATH Between two WebPages: ");for(i =1; i <= nodes; i++){for(j =1; j <= nodes; j++){
p.path[i][j]= in.nextInt();if(j == i){
p.path[i][j]=0;}}}
p.calc(nodes);}publicint path[][]=newint[10][10];publicdouble pagerank[]=newdouble[10];publicvoidcalc(double totalNodes){doubleInitialPageRank;doubleOutgoingLinks=0;doubleDampingFactor=0.85;doubleTempPageRank[]=newdouble[10];intExternalNodeNumber;intInternalNodeNumber;int k =1;// For Traversingint ITERATION_STEP =1;InitialPageRank=1/ totalNodes;System.out.printf(" Total Number of Nodes :"+ totalNodes +"\t Initial PageRank of All Nodes :"+InitialPageRank+"\n");// 0th ITERATION _ OR _ INITIALIZATION PHASE //for(k =1; k <= totalNodes; k++){this.pagerank[k]=InitialPageRank;}System.out.printf("\n Initial PageRank Values , 0th Step \n");for(k =1; k <= totalNodes; k++){System.out.printf(" Page Rank of "+ k +" is :\t"+this.pagerank[k]+"\n");}while(ITERATION_STEP <=2)// Iterations{// Store the PageRank for All Nodes in Temporary Arrayfor(k =1; k <= totalNodes; k++){TempPageRank[k]=this.pagerank[k];this.pagerank[k]=0;}for(InternalNodeNumber=1;InternalNodeNumber<= totalNodes;InternalNodeNumber++){for(ExternalNodeNumber=1;ExternalNodeNumber<= totalNodes;ExternalNodeNumber++){if(this.path[ExternalNodeNumber][InternalNodeNumber]==1){
k =1;OutgoingLinks=0;// Count the Number of Outgoing Links for each ExternalNodeNumberwhile(k <= totalNodes){if(this.path[ExternalNodeNumber][k]==1){OutgoingLinks=OutgoingLinks+1;// Counter for Outgoing Links}
k = k +1;}// Calculate PageRankthis.pagerank[InternalNodeNumber]+=TempPageRank[ExternalNodeNumber]*(1/OutgoingLinks);}}System.out.printf("\n After "+ ITERATION_STEP +"th Step \n");for(k =1; k <= totalNodes; k++){System.out.printf(" Page Rank of "+ k +" is :\t"+this.pagerank[k]+"\n");}
ITERATION_STEP = ITERATION_STEP +1;}// Add the Damping Factor to PageRankfor(k =1; k <= totalNodes; k++){this.pagerank[k]=(1-DampingFactor)+DampingFactor*this.pagerank[k];}// Display PageRankSystem.out.printf("\n Final Page Rank : \n");for(k =1; k <= totalNodes; k++){System.out.printf(" Page Rank of "+ k +" is :\t"+this.pagerank[k]+"\n");}}}}