0% found this document useful (0 votes)
191 views10 pages

Java PageRank Algorithm Guide

The Java code implements a simple PageRank algorithm. It initializes node rankings evenly, calculates new rankings by distributing weight from linked nodes, iterates the calculation a few times, and applies a damping factor to weight rankings by past values. Node rankings converge towards the PageRank values.

Uploaded by

Pavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views10 pages

Java PageRank Algorithm Guide

The Java code implements a simple PageRank algorithm. It initializes node rankings evenly, calculates new rankings by distributing weight from linked nodes, iterates the calculation a few times, and applies a damping factor to weight rankings by past values. Node rankings converge towards the PageRank values.

Uploaded by

Pavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Implementation of Simple PageRank Algorithm

//[Link]

import [Link].*;
import [Link].*;
public class PageRank {
public int path[][] = new int[10][10];
public double pagerank[] = new double[10];
public void calc(double totalNodes){
double InitialPageRank;
double OutgoingLinks=0;
double DampingFactor = 0.85;
double TempPageRank[] = new double[10];
int ExternalNodeNumber;
int InternalNodeNumber;
int k=1; // For Traversing
int ITERATION_STEP=1;
InitialPageRank = 1/totalNodes;
[Link](" Total Number of Nodes :"+totalNodes+"\t Initial PageRank of
All Nodes :"+InitialPageRank+"\n");
// 0th ITERATION _ OR _ INITIALIZATION PHASE
for(k=1;k<=totalNodes;k++)
{
[Link][k]=InitialPageRank;
}

[Link]("\n Initial PageRank Values , 0th Step \n");


for(k=1;k<=totalNodes;k++)
{
[Link](" Page Rank of "+k+" is :\t"+[Link][k]+"\n");
}
while(ITERATION_STEP<=2) // Iterations
{
// Store the PageRank for All Nodes in Temporary Array
for(k=1;k<=totalNodes;k++)
{
TempPageRank[k]=[Link][k];
[Link][k]=0;
}
for(InternalNodeNumber=1;InternalNodeNumber<=totalNodes;InternalNodeNumb
er++)
{
for(ExternalNodeNumber=1;ExternalNodeNumber<=totalNodes;ExternalNodeNum
ber++)
{
if([Link][ExternalNodeNumber][InternalNodeNumber] == 1)
{
k=1;
OutgoingLinks=0;
while(k<=totalNodes)
{
if([Link][ExternalNodeNumber][k] == 1 )
{
OutgoingLinks=OutgoingLinks+1; // Counter for Outgoing Links
}
k=k+1;
}
// Calculate PageRank
[Link][InternalNodeNumber]
+=TempPageRank[ExternalNodeNumber]*(1/OutgoingLinks);
}
}
}
[Link]("\n After "+ITERATION_STEP+"th Step \n");

for(k=1;k<=totalNodes;k++)
[Link](" Page Rank of "+k+" is :\t"+[Link][k]+"\n");

ITERATION_STEP = ITERATION_STEP+1;
}
for(k=1;k<=totalNodes;k++)
{
[Link][k]=(1-DampingFactor)+ DampingFactor*[Link][k];
}
[Link]("\n Final Page Rank : \n");
for(k=1;k<=totalNodes;k++)
{
[Link](" Page Rank of "+k+" is :\t"+[Link][k]+"\n");
}
}
public static void main(String args[])
{
int nodes,i,j,cost;
Scanner in = new Scanner([Link]);
[Link]("Enter the Number of WebPages \n");
nodes = [Link]();
PageRank p = new PageRank();
[Link]("Enter the Adjacency Matrix with 1->PATH & 0->NO PATH
Between two WebPages: \n");
for(i=1;i<=nodes;i++)
for(j=1;j<=nodes;j++)
{
[Link][i][j]=[Link]();
if(j==i)
[Link][i][j]=0;
}
[Link](nodes);
} }
Sample Input-Output

K-means clustering Algorithm using Java

//KMeans_Ex4a.java

import [Link];
public class KMeans_Ex4a
{
private static final int NUM_CLUSTERS = 2; // Total clusters.
private static final int TOTAL_DATA = 7; // Total data points.
private static final double SAMPLES[][] = new double[][] {{1.0, 1.0},
{1.5, 2.0},
{3.0, 4.0},
{5.0, 7.0},
{3.5, 5.0},
{4.5, 5.0},
{3.5, 4.5}};
private static ArrayList<Data> dataSet = new ArrayList<Data>();
private static ArrayList<Centroid> centroids = new ArrayList<Centroid>();
private static void initialize()
{
[Link]("Centroids initialized at:");
[Link](new Centroid(1.0, 1.0)); // lowest set.
[Link](new Centroid(5.0, 7.0)); // highest set.
[Link](" (" + [Link](0).X() + ", " + [Link](0).Y() + ")");
[Link](" (" + [Link](1).X() + ", " + [Link](1).Y() + ")");
[Link]("\n");
return;
}
private static void kMeanCluster()
{
final double bigNumber = [Link](10, 10); // some big number that's sure to be
larger than our data range.
double minimum = bigNumber; // The minimum value to beat.
double distance = 0.0; // The current minimum value.
int sampleNumber = 0;
int cluster = 0;
boolean isStillMoving = true;
Data newData = null;
while([Link]() < TOTAL_DATA)
{
newData = new Data(SAMPLES[sampleNumber][0], SAMPLES[sampleNumber][1]);
[Link](newData);
minimum = bigNumber;
for(int i = 0; i < NUM_CLUSTERS; i++)
{
distance = dist(newData, [Link](i));
if(distance < minimum){
minimum = distance;
cluster = i;
}
}
[Link](cluster);
// calculate new centroids.
for(int i = 0; i < NUM_CLUSTERS; i++)
{
int totalX = 0;
int totalY = 0;
int totalInCluster = 0;
for(int j = 0; j < [Link](); j++)
{
if([Link](j).cluster() == i){
totalX += [Link](j).X();
totalY += [Link](j).Y();
totalInCluster++;
}
}
if(totalInCluster > 0){
[Link](i).X(totalX / totalInCluster);
[Link](i).Y(totalY / totalInCluster);
}
}
sampleNumber++;
}
// Now, keep shifting centroids until equilibrium occurs.
while(isStillMoving)
{
// calculate new centroids.
for(int i = 0; i < NUM_CLUSTERS; i++)
{
int totalX = 0;
int totalY = 0;
int totalInCluster = 0;
for(int j = 0; j < [Link](); j++)
{
if([Link](j).cluster() == i){
totalX += [Link](j).X();
totalY += [Link](j).Y();
totalInCluster++;
}
}
if(totalInCluster > 0){
[Link](i).X(totalX / totalInCluster);
[Link](i).Y(totalY / totalInCluster);
}
}
// Assign all data to the new centroids
isStillMoving = false;
for(int i = 0; i < [Link](); i++)
{
Data tempData = [Link](i);
minimum = bigNumber;
for(int j = 0; j < NUM_CLUSTERS; j++)
{
distance = dist(tempData, [Link](j));
if(distance < minimum){
minimum = distance;
cluster = j;
}
}
[Link](cluster);
if([Link]() != cluster){
[Link](cluster);
isStillMoving = true;
}
}
}
return;
}

private static double dist(Data d, Centroid c)


{
return [Link]([Link]((c.Y() - d.Y()), 2) + [Link]((c.X() - d.X()), 2));
}
private static class Data
{
private double mX = 0;
private double mY = 0;
private int mCluster = 0;
public Data()
{
return;
}
public Data(double x, double y)
{
this.X(x);
this.Y(y);
return;
}
public void X(double x)
{
[Link] = x;
return;
}
public double X()
{
return [Link];
}
public void Y(double y)
{
[Link] = y;
return;
}
public double Y()
{
return [Link];
}
public void cluster(int clusterNumber)
{
[Link] = clusterNumber;
return;
}
public int cluster()
{
return [Link];
}
}
private static class Centroid
{
private double mX = 0.0;
private double mY = 0.0;

public Centroid()
{
return;
}
public Centroid(double newX, double newY)
{
[Link] = newX;
[Link] = newY;
return;
}
public void X(double newX)
{
[Link] = newX;
return;
}
public double X()
{
return [Link];
}
public void Y(double newY)
{
[Link] = newY;
return;
}
public double Y()
{
return [Link];
}
}
public static void main(String[] args)
{
initialize();
kMeanCluster();
for(int i = 0; i < NUM_CLUSTERS; i++)
{
[Link]("Cluster " + i + " includes:");
for(int j = 0; j < TOTAL_DATA; j++)
{
if([Link](j).cluster() == i){
[Link](" (" + [Link](j).X() + ", " + [Link](j).Y() + ")");
}
}
[Link]();
}
[Link]("Centroids finalized at:");
for(int i = 0; i < NUM_CLUSTERS; i++)
{
[Link](" (" + [Link](i).X() + ", " + [Link](i).Y());
}
[Link]("\n");
return;
}
}

Sample Input-Output

You might also like