Java Program To Implement Simple PageRank Algorithm
Java Program To Implement Simple PageRank Algorithm
The original Page Rank algorithm which was described by Larry Page and
Sergey Brin is :
At 0th Step we have all Webpages PageRank values 0.2 that is 1/5 (1/n) . To get
PageRank of Webpage A ,consider all the incoming links to A .So we have
1/4th the Page Rank of C is pointed to A. So it will be (1/5)*(1/4) which is
(1/20) or 0.05 the Page Rank of A.
Similarly the Page Rank of B will be (1/5)*(1/4)+(1/5)*(1/1) which is (5/20) or
0.25 because A's PageRank value is 1/5 or 0.2 from Step 0 . Even though we got
0.05 of A's PageRank in Step 1 we are considering 0.05 when we are
Calculating Page Rank of B in Step 2.
The general rule is --> we consider (N-1)th step values when we are calculating
the Page Rank values for Nth Step . Not Clear ? Please Comment it below .
In Similar way we calculate all the Page Rank Values and Sort them to Get the
Most important Webpage to be displayed in the Search Results .
?
1 import java.util.*;
import java.io.*;
2
public class PageRank {
3
4 public int path[][] = new int[10][10];
5 public double pagerank[] = new double[10];
6
7
8 public void calc(double totalNodes){
9
10 double InitialPageRank;
double OutgoingLinks=0;
11 double DampingFactor = 0.85;
12 double TempPageRank[] = new double[10];
13
14 int ExternalNodeNumber;
15 int InternalNodeNumber;
16 int k=1; // For Traversing
int ITERATION_STEP=1;
17
18 InitialPageRank = 1/totalNodes;
19 System.out.printf(" Total Number of Nodes :"+totalNodes+"\t Initial
20 PageRank of All Nodes :"+InitialPageRank+"\n");
21
22 // 0th ITERATION _ OR _ INITIALIZATION PHASE
23 for(k=1;k<=totalNodes;k++)
{
24 this.pagerank[k]=InitialPageRank;
25 }
26
27 System.out.printf("\n Initial PageRank Values , 0th Step \n");
28 for(k=1;k<=totalNodes;k++)
{
29 System.out.printf(" Page Rank of "+k+" is :\t"+this.pagerank[k]+"\n");
30 }
31
32 while(ITERATION_STEP<=2) // Iterations
33 {
34 // Store the PageRank for All Nodes in Temporary Array
for(k=1;k<=totalNodes;k++)
35 {
36 TempPageRank[k]=this.pagerank[k];
37 this.pagerank[k]=0;
38 }
39
40 for(InternalNodeNumber=1;InternalNodeNumber<=totalNodes;InternalNodeNumber
++)
41 {
42 for(ExternalNodeNumber=1;ExternalNodeNumber<=totalNodes;ExternalNodeNumbe
43 r++)
44 {
45 if(this.path[ExternalNodeNumber][InternalNodeNumber]
{
== 1)
46 k=1;
47 OutgoingLinks=0; // Count the Number of Outgoing Links for each
48 ExternalNodeNumber
49 while(k<=totalNodes)
{
50 if(this.path[ExternalNodeNumber][k] == 1 )
51 {
52 OutgoingLinks=OutgoingLinks+1; // Counter for Outgoing Links
}
53 k=k+1;
54 }
// Calculate PageRank
55 this.pagerank[InternalNodeNumber]
56 +=TempPageRank[ExternalNodeNumber]*(1/OutgoingLinks);
57 }
58 }
}
59
60
System.out.printf("\n After "+ITERATION_STEP+"th Step \n");
61
62 for(k=1;k<=totalNodes;k++)
63 System.out.printf(" Page Rank of "+k+" is :\t"+this.pagerank[k]+"\
64 n");
65
66 ITERATION_STEP = ITERATION_STEP+1;
67 }
68 // Add the Damping Factor to PageRank
69 for(k=1;k<=totalNodes;k++)
70 {
71 this.pagerank[k]=(1-DampingFactor)+ DampingFactor*this.pagerank[k];
72 }
73
// Display PageRank
74 System.out.printf("\n Final Page Rank : \n");
75 for(k=1;k<=totalNodes;k++)
76 {
77 System.out.printf(" Page Rank of "+k+" is :\t"+this.pagerank[k]+"\n");
78 }
79
}
80
81 public static void main(String args[])
82 {
83 int nodes,i,j,cost;
84 Scanner in = new Scanner(System.in);
85 System.out.println("Enter the Number of WebPages \n");
nodes = in.nextInt();
86 PageRank p = new PageRank();
87 System.out.println("Enter the Adjacency Matrix with 1->PATH & 0->NO
88 PATH Between two WebPages: \n");
89 for(i=1;i<=nodes;i++)
for(j=1;j<=nodes;j++)
90 {
91 p.path[i][j]=in.nextInt();
92 if(j==i)
93 p.path[i][j]=0;
94 }
p.calc(nodes);
95
96
97 }
98
99
10
0
10
1
10
2 }
10
3
10
4
10
5