You are on page 1of 5

PageRankCalculator.

java
/**
* @author Parvez
* @param args
*/
import java.util.*;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import edu.uci.ics.jung.graph.*;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.visualization.VisualizationImageServer;
import edu.uci.ics.jung.visualization.decorators.*;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;
class PRRun
{
//public Set<String> papers=new HashSet<String>();
//public Vector<String> papers;
public Map<String,Double> papers;
public BufferedReader fileRead1;
public BufferedReader fileRead2;
public DirectedSparseGraph<String, String> g;
public String line,node;
public StringTokenizer tok;
public String del;
public Set<Double> PR;
String dataset;
PRRun() throws FileNotFoundException
{
dataset="tiny.txt";
//dataset="citations.txt";
fileRead1= new BufferedReader(new FileReader(dataset));
fileRead2= new BufferedReader(new FileReader(dataset));
g= new DirectedSparseGraph<String,String>();
del=" \n";

//papers = new Vector<String>();


papers=new HashMap<String, Double>();
PR =new HashSet<Double>();
}
public void constructGraph() throws IOException
{
//extracts nodes and add them to graph
while((line= fileRead1.readLine())!= null)
{
if(!line.startsWith("#"))
{
tok=new StringTokenizer(line,del);
while (tok.hasMoreTokens())
{
node=tok.nextToken().trim();
if(!papers.keySet().contains(node))
{
papers.put(node, 1.0);
g.addVertex(node);
}
}
}
}
fileRead1.close();
//System.out.print("Integer max count :"+Integer.MAX_VALUE);
System.out.print("\nTotal vertices :"+ g.getVertexCount() );
//System.out.print("\npaper capacity :"+ papers.size());
//extracts edges and add them to graph
String v1,v2;
while((line= fileRead2.readLine())!= null)
{
if(!line.startsWith("#"))
{
tok=new StringTokenizer(line,del);
v1=tok.nextToken().trim();
v2=tok.nextToken().trim();
g.addEdge("e"+v1+"-"+v2, v1, v2, EdgeType.DIRECTED);
}
}
fileRead2.close();
System.out.print("\nTotal edges :"+ g.getEdgeCount());
//System.out.print("\nis 1 - 7 neighbour :"+ g.isNeighbor("1", "7"));

//System.out.print("\nis 7 - 1 neighbour :"+ g.isNeighbor("7", "1"));


VisualizationImageServer<String, String> vs=new VisualizationImageServer<>(new
CircleLayout<String,String>(g),new Dimension(1200,600));
vs.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>());
vs.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<String>());
vs.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
JFrame fr=new JFrame();
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.getContentPane().add(vs);
fr.pack();
fr.setVisible(true);
}
public void calculatePR()
{
int N=g.getVertexCount();
int noOfIter=12;
double d=0.15;
for(int i=0;i<noOfIter;i++)
{
for (String pap : papers.keySet())
{
//
System.out.println("\n\nVertex :"+pap+"\nLinked by :\n");
papers.put(pap, ((1-d) + d * (Summation(pap))));
}
}
for (String pap : papers.keySet())
{
System.out.print("\nPR["+pap+"] = "+papers.get(pap)+"\n");
}
}
public double Summation(String src)
{
// TODO Auto-generated method stub
double sum=0;
String v;
for (String pap2 : g.getInEdges(src))
{
v=g.getSource(pap2);
//System.out.println("InEdge for vertex :"+src);
//
System.out.print(v+"\t");
sum+=(papers.get(v)/g.outDegree(v));

//sum+=(PR.get(papers.indexOf(pap2))/g.outDegree(pap2));
//(pR[k]/out[k]);
}
return sum;
}
}
class PageRankCalculator
{
public static void main(String[] args) throws Exception
{
PRRun p=new PRRun();
p.constructGraph();
p.calculatePR();
}
}

Dataset - tiny.txt
#
#
1
1
1
1
1
2
3
3
4
4
4
5
5
5
5
6
6
7

Nodes: 7 Edges: 18
FromNodeId
ToNodeId
2
3
4
5
7
1
1
2
2
3
5
1
2
3
6
1
5
5

Graph displayed

OutputTotal vertices :7
Total edges :18
No of Iterations : 12
PR[1] = 1.1890025475392967
PR[2] = 1.0452423983056212
PR[3] = 0.9723185100517405
PR[4] = 0.8856700764261789
PR[5] = 1.1297314614467338
PR[6] = 0.8923649298042525
PR[7] = 0.8856700764261789

You might also like