0% found this document useful (0 votes)
159 views

Java Implementation of Simple Pagerank Algorithm

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views

Java Implementation of Simple Pagerank Algorithm

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Implementation of Simple PageRank Algorithm

//PageRank.java

import java.util.*;
import java.io.*;
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;
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 Array
for(k=1;k<=totalNodes;k++)
{
TempPageRank[k]=this.pagerank[k];
this.pagerank[k]=0;
}
for(InternalNodeNumber=1;InternalNodeNumber<=totalNodes;InternalNodeNumb
er++)
{
for(ExternalNodeNumber=1;ExternalNodeNumber<=totalNodes;ExternalNodeNum
ber++)
{
if(this.path[ExternalNodeNumber][InternalNodeNumber] == 1)
{
k=1;
OutgoingLinks=0;
while(k<=totalNodes)
{
if(this.path[ExternalNodeNumber][k] == 1 )
{
OutgoingLinks=OutgoingLinks+1; // Counter for Outgoing Links
}
k=k+1;
}
// Calculate PageRank
this.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;
}
for(k=1;k<=totalNodes;k++)
{
this.pagerank[k]=(1-DampingFactor)+ DampingFactor*this.pagerank[k];
}
System.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");
}
}
public static void main(String args[])
{
int nodes,i,j,cost;
Scanner in = new Scanner(System.in);
System.out.println("Enter the Number of WebPages \n");
nodes = in.nextInt();
PageRank p = new PageRank();
System.out.println("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++)
{
p.path[i][j]=in.nextInt();
if(j==i)
p.path[i][j]=0;
}
p.calc(nodes);
} }
Sample Input-Output

K-means clustering Algorithm using Java

//KMeans_Ex4a.java

import java.util.ArrayList;
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()
{
System.out.println("Centroids initialized at:");
centroids.add(new Centroid(1.0, 1.0)); // lowest set.
centroids.add(new Centroid(5.0, 7.0)); // highest set.
System.out.println(" (" + centroids.get(0).X() + ", " + centroids.get(0).Y() + ")");
System.out.println(" (" + centroids.get(1).X() + ", " + centroids.get(1).Y() + ")");
System.out.print("\n");
return;
}
private static void kMeanCluster()
{
final double bigNumber = Math.pow(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(dataSet.size() < TOTAL_DATA)
{
newData = new Data(SAMPLES[sampleNumber][0], SAMPLES[sampleNumber][1]);
dataSet.add(newData);
minimum = bigNumber;
for(int i = 0; i < NUM_CLUSTERS; i++)
{
distance = dist(newData, centroids.get(i));
if(distance < minimum){
minimum = distance;
cluster = i;
}
}
newData.cluster(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 < dataSet.size(); j++)
{
if(dataSet.get(j).cluster() == i){
totalX += dataSet.get(j).X();
totalY += dataSet.get(j).Y();
totalInCluster++;
}
}
if(totalInCluster > 0){
centroids.get(i).X(totalX / totalInCluster);
centroids.get(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 < dataSet.size(); j++)
{
if(dataSet.get(j).cluster() == i){
totalX += dataSet.get(j).X();
totalY += dataSet.get(j).Y();
totalInCluster++;
}
}
if(totalInCluster > 0){
centroids.get(i).X(totalX / totalInCluster);
centroids.get(i).Y(totalY / totalInCluster);
}
}
// Assign all data to the new centroids
isStillMoving = false;
for(int i = 0; i < dataSet.size(); i++)
{
Data tempData = dataSet.get(i);
minimum = bigNumber;
for(int j = 0; j < NUM_CLUSTERS; j++)
{
distance = dist(tempData, centroids.get(j));
if(distance < minimum){
minimum = distance;
cluster = j;
}
}
tempData.cluster(cluster);
if(tempData.cluster() != cluster){
tempData.cluster(cluster);
isStillMoving = true;
}
}
}
return;
}

private static double dist(Data d, Centroid c)


{
return Math.sqrt(Math.pow((c.Y() - d.Y()), 2) + Math.pow((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)
{
this.mX = x;
return;
}
public double X()
{
return this.mX;
}
public void Y(double y)
{
this.mY = y;
return;
}
public double Y()
{
return this.mY;
}
public void cluster(int clusterNumber)
{
this.mCluster = clusterNumber;
return;
}
public int cluster()
{
return this.mCluster;
}
}
private static class Centroid
{
private double mX = 0.0;
private double mY = 0.0;

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

Sample Input-Output

You might also like