Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
afnan47
GitHub Repository: afnan47/sem7
Path: blob/main/IR/Assignment 2/two_PageRank.java
418 views
1
import java.util.*;
2
import java.io.*;
3
public class two_PageRank {
4
public int path[][] = new int[10][10];
5
public double pagerank[] = new double[10];
6
public void calc(double totalNodes){
7
double InitialPageRank;
8
double OutgoingLinks=0;
9
double DampingFactor = 0.85;
10
double TempPageRank[] = new double[10];
11
int ExternalNodeNumber;
12
int InternalNodeNumber;
13
int k=1; // For Traversing
14
int ITERATION_STEP=1;
15
InitialPageRank = 1/totalNodes;
16
System.out.printf(" Total Number of Nodes :"+totalNodes+"\t Initial PageRank of All Nodes :"+InitialPageRank+"\n");
17
for(k=1;k<=totalNodes;k++)
18
{
19
this.pagerank[k]=InitialPageRank;
20
}
21
System.out.printf("\n Initial PageRank Values , 0th Step \n");
22
for(k=1;k<=totalNodes;k++)
23
{
24
System.out.printf(" Page Rank of "+k+" is :\t"+this.pagerank[k]+"\n");
25
}
26
while(ITERATION_STEP<=2) // Iterations
27
{
28
for(k=1;k<=totalNodes;k++)
29
{
30
TempPageRank[k]=this.pagerank[k];
31
this.pagerank[k]=0;
32
}
33
for(InternalNodeNumber=1;InternalNodeNumber<=totalNodes;InternalNodeNumber++)
34
{
35
for(ExternalNodeNumber=1;ExternalNodeNumber<=totalNodes;ExternalNodeNumber++)
36
{
37
if(this.path[ExternalNodeNumber][InternalNodeNumber] == 1)
38
{
39
k=1;
40
OutgoingLinks=0; // Count the Number of Outgoing Links for each ExternalNodeNumber
41
while(k<=totalNodes)
42
{
43
if(this.path[ExternalNodeNumber][k] == 1 )
44
{
45
OutgoingLinks=OutgoingLinks+1; // Counter for Outgoing Links
46
}
47
k=k+1;
48
}
49
this.pagerank[InternalNodeNumber]+=TempPageRank[ExternalNodeNumber]*(1/OutgoingLinks);
50
}
51
}
52
}
53
System.out.printf("\n After "+ITERATION_STEP+"th Step \n");
54
for(k=1;k<=totalNodes;k++)
55
System.out.printf(" Page Rank of "+k+" is :\t"+this.pagerank[k]+"\n");
56
ITERATION_STEP = ITERATION_STEP+1;
57
}
58
for(k=1;k<=totalNodes;k++)
59
{
60
this.pagerank[k]=(1-DampingFactor)+ DampingFactor*this.pagerank[k];
61
}
62
System.out.printf("\n Final Page Rank : \n");
63
for(k=1;k<=totalNodes;k++)
64
{
65
System.out.printf(" Page Rank of "+k+" is :\t"+this.pagerank[k]+"\n");
66
}
67
}
68
public static void main(String args[])
69
{
70
int nodes,i,j,cost;
71
Scanner in = new Scanner(System.in);
72
System.out.println("Enter the Number of WebPages \n");
73
nodes = in.nextInt();
74
two_PageRank p = new two_PageRank();
75
System.out.println("Enter the Adjacency Matrix with 1->PATH & 0->NO PATH Between two WebPages: \n");
76
for(i=1;i<=nodes;i++)
77
for(j=1;j<=nodes;j++)
78
{
79
p.path[i][j]=in.nextInt();
80
if(j==i)
81
p.path[i][j]=0;
82
}
83
p.calc(nodes);
84
}
85
}
86
87
// OUTPUT
88
89
// Enter the Number of WebPages
90
91
// 4
92
// Enter the Adjacency Matrix with 1->PATH & 0->NO PATH Between two WebPages:
93
94
// 0 1 0 1
95
// 1 2 3 0
96
// 2 1 0 1
97
// 0 2 1 1
98
// Total Number of Nodes :4.0 Initial PageRank of All Nodes :0.25
99
100
// Initial PageRank Values , 0th Step
101
// Page Rank of 1 is : 0.25
102
// Page Rank of 2 is : 0.25
103
// Page Rank of 3 is : 0.25
104
// Page Rank of 4 is : 0.25
105
106
// After 1th Step
107
// Page Rank of 1 is : 0.25
108
// Page Rank of 2 is : 0.25
109
// Page Rank of 3 is : 0.25
110
// Page Rank of 4 is : 0.25
111
112
// After 2th Step
113
// Page Rank of 1 is : 0.25
114
// Page Rank of 2 is : 0.25
115
// Page Rank of 3 is : 0.25
116
// Page Rank of 4 is : 0.25
117
118
// Final Page Rank :
119
// Page Rank of 1 is : 0.36250000000000004
120
// Page Rank of 2 is : 0.36250000000000004
121
// Page Rank of 3 is : 0.36250000000000004
122
// Page Rank of 4 is : 0.36250000000000004
123