Saturday, 22 October 2016

Program For Web Page Ranking


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 n) {
        Double init;
        double c = 0;
        double temp[] = new double[10];
        int i, j, u = 1, k = 1;
        init = 1 / n;
        System.out.printf(" n value:" + n + "\t init value :" + init + "\n");

        for (i = 1; i <= n; i++)
        {
            this.pagerank[i] = init;
            System.out.printf("\n Initial PageRank Values , 0th Step \n");
        }

        for (i = 1; i <= n; i++)
        {
            System.out.printf(" Page Rank of " + i + " is :\t" + this.pagerank[i] + "\n");

            while (u <= 2) {
                for (i = 1; i <= n; i++)
                {
                    temp[i] = this.pagerank[i];
                    this.pagerank[i] = 0;
                }
            }
            for (j = 1; j <= n; j++)
            {
                for (i = 1; i <= n; i++)
                {
                    if (this.path[i][j] == 1)
                    {
                        k = 1;
                        c = 0;
                        while (k <= n)
                        {
                            if (this.path[i][k] == 1)
                            {
                                c = c + 1;
                                k = k + 1;
                            }
                        }
                        this.pagerank[j] = this.pagerank[j] + temp[i] * (1 / c);
                    }

                    System.out.printf("\n After " + u + "th Step \n");
                    for (i = 1; i <= n; i++)
                    {
                        System.out.printf(" Page Rank of " + i + " is :\t" + this.pagerank[i] + "\n");
                        u = u + 1;
                    }
                }
            }
        }
    }

    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 PATHBetween 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);
    }
}


/*
OUTPUT
Enter the Number of WebPages
5
Enter the Adjacency Matrix with 1->PATH & 0->NO PATH Between two WebPages:
0 1 0 1 0
0 0 1 0 1
0 0 0 1 0
0 0 0 0 1
1 1 1 0 0
n value:5.0
init value :0.2
Initial PageRank Values , 0th Step
Page Rank of 1 is : 0.2
Page Rank of 2 is : 0.2
Page Rank of 3 is : 0.2
Page Rank of 4 is : 0.2
Page Rank of 5 is : 0.2
After 1th Step
Page Rank of 1 is :
Page Rank of 2 is :
Page Rank of 3 is :
Page Rank of 4 is :
Page Rank of 5 is : 0.06666666666666667
0.16666666666666669
0.16666666666666669
0.30000000000000004
0.30000000000000004
After 2th Step
Page Rank of 1 is :
Page Rank of 2 is :
Page Rank of 3 is : 0.1
0.13333333333333333
0.18333333333333335Page Rank of 4 is :
Page Rank of 5 is :
0.2
0.3833333333333334 */

No comments:

Post a Comment