Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
98 views

Java Program To Implement Simple PageRank Algorithm

The document describes an algorithm called PageRank developed by Larry Page and Sergey Brin at Stanford University in 1996 to determine the importance of web pages. It does this by assigning a numerical ranking to each page based on the number and quality of links to that page. The document then provides details on implementing the original PageRank algorithm using Java code, including initializing page ranks, calculating page ranks over multiple iterations, and displaying the final page ranks.

Uploaded by

Abdataa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Java Program To Implement Simple PageRank Algorithm

The document describes an algorithm called PageRank developed by Larry Page and Sergey Brin at Stanford University in 1996 to determine the importance of web pages. It does this by assigning a numerical ranking to each page based on the number and quality of links to that page. The document then provides details on implementing the original PageRank algorithm using Java code, including initializing page ranks, calculating page ranks over multiple iterations, and displaying the final page ranks.

Uploaded by

Abdataa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Program to Implement Simple PageRank Algorithm

 Shravan Chitpady     12/14/2015 03:57:00 pm     Algorithms, Java     11 comments   


When you go and type some keywords in Google Search Engine a list of Web
Pages will be displayed ,but how does the search engine know which page to be
shown first to the user ? To solve this problem a  algorithm called PageRank
was developed at Stanford university by Larry Page and Sergey Brin in
1996.The PageRank Algorithm uses probabilistic distribution to calculate rank
of a Web page and using this rank display the search results to the user. The
Pagerank is recalculated every time  the search engine crawls the web.

The original Page Rank algorithm which was described by Larry Page and
Sergey Brin is :

PR(A) = (1-d) + d (PR(W1)/C(W1) + ... + PR(Wn)/C(Wn))


Where :
PR(A) – Page Rank of page A
PR(Wi) – Page Rank of pages Wi which link to page A
C(Wi) - number of outbound links on page Wi
d - damping factor which can be set between 0 and 1

To calculate PageRank for the n Webpages ,First we initialise all Webpages


with equal page rank of 1/n each.Then Step by Step we calculate Page Rank for
each Webpage one after the other.

Let us take one example :

There are 5 Web pages represented by Nodes A, B, C , D, E .The hyperlink


from each webpage to the other is represented by the arrow head.

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 .

Edith Law - lecture12

Java Code for Page Rank Algorithm :

?
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

You might also like