In late 90’s as the number of webpages on the internet were growing exponentially different search engines were trying different approaches to rank the webpages. At Stanford, two computer science PhD students, Sergey Brin and Larry Page were working on the following questions: How can we trust information? Why are some web pages more important than others? Their research led to the formation of the Google search engine. In this programming assignment, you are required to implement a simplified version of the original PageRank algorithm on which Google was built.
Representing the Web as a Graph
The idea that the entire internet can be represented as a graph. Each node represents a webpage and each edge represents a link between two webpages. This graph can be implemented as an Adjacency Matrix or an Adjacency List. Feel free to use any data structure.
Now for the sake of simplicity, we are explaining the assignment in the form of an Adjacency Matrix. We represent the graph in the form of |V|x|V| matrix where |V| is the total number of vertices in the graph. This is mapped to the webpages in the entire internet. Thus, if there is an edge from Vi to Vj (the from_page points to_page), we have the value in our adjacency matrix Mij = 1 and 0 otherwise.
Important web pages will point to other important webpages. Each page will have a score and the results of the search will be based on the page score (called page rank).
Rank(i) = j/out_degree(j) + k/out_degree(k)
Each webpage is thus a node in the directed graph and has incoming edges and outgoing edges. Each node has a rank. According to PageRank, this rank is equally split among the node’s outgoing links and this rank is equal to the sum of the incoming ranks. The rank is based on the indegree (the number of nodes pointing to it) and the importance of incoming node. This is important considering let’s say you create your personal website and have a million links to other pages of importance. If this was not the case and rank used out links, we can easily dupe the algorithm. Therefore, the rank is based on in-links.
Goal
In this assignment, you need to compute the rank of the webpages using a Simplified Algorithm explained in the example below.
Input
Line 1 contains the number of lines (n) that will follow and the number of power iterations you need to perform. Each line from
2 to n will contain two URL’s – from_page to_page separated by a space. This means from_page points to the URL to_page.
Output
Print the PageRank of all pages after n powerIterations in ascending alphabetical order of webpage. Also, round off the rank of the page to two decimal places.
This means that a rank of the webpage at time t+1 is equal to the rank of that page at time t multiplied by matrix, M. To achieve this, we create our matrix M based on input. Next, we initialize r(t) which is a matrix of size |V|x1 and consists of the ranks of every webpage. We initialize r(t) to 1/|V|. Next we compute power_iterations based on our input.
In this input case, the number of power_iterations is 2, if it is 1 then return the initializing rank matrix or r(0). If iterations2, the process repeats where you multiply the matrix, M with the new rank matrix r(t+1) at the next iteration.
Stepik Test Case Two Explanation:- (Power_iteration=3)
You are allowed to use your own template but make sure your code passes the sample test cases. An example template to think about the problem is :
Class AdjacencyListorMatrix { private:
//Think about what member variables you need to initialize public:
//Think about what helper functions you will need in the algorithm
};
void AdjacencyListorMatrix::PageRank(int n){ } // prints the PageRank of all pages after n powerIterations in ascending alphabetical order of webpage and rounding rank to two decimal places]
// This class and method are optional. To accept input, you can use this method: