Understanding Google PageRank from Scratch Using Python
Inspired by MAT223: Linear Algebra I, a course I took at the University of Toronto.
When we search for something on Google, countless webpages may contain the same words. How does Google decide which pages should be prioritized? One answer to this question is in an algorithm called PageRank. It assigns a ranking score to each webpage based on the links between them. Herein, we will briefly explore the mathematics behind the PageRank and implement it from scratch using Python.
1.The Role of PageRank
The PageRank score represents the relative importance of a webpage within a network.
The score of a webpage is based on:
- the number of webpages that link to it;
- the importance of the webpages that link to it;
- the number of outgoing links from those webpages.
2.Representing the Web
Suppose there are three webpages: A, B, and C.
- Page A links to pages B and C.
- Page B links to page C.
- Page C links to page A.
A → B
A → C
B → C
C → A
These links can be represented using a Python dictionary:
links = {
"A": ["B", "C"],
"B": ["C"],
"C": ["A"]
}3.Creating the Link Matrix
The link matrix stores the probability of moving from one webpage to another.
If page A has two outgoing links, each link receives probability:
For this example, the link matrix is:
Each column represents the webpage we are leaving. Each row represents the webpage we are moving to.
for curr_page, linked_pages in links.items():
total_links = len(linked_pages)
source_index = page_index[curr_page]
for linked_page in linked_pages:
linked_index = page_index[linked_page]
link_matrix[linked_index, source_index] = 1 / total_links4.The Google Matrix
A user usually follows a link on the current webpage, but sometimes jumps to a random webpage.
This random jump prevents the user from becoming trapped within a small group of webpages.
The Google matrix combines these two behaviours:
where:
is the link matrix; gives every webpage an equal probability; is the damping factor.
We use:
This means the user follows a link with probability 0.85 and randomly jumps with probability 0.15.
random_jump = np.ones(
(total_pages, total_pages)
) / total_pages
google_matrix = (
damping_factor * link_matrix
+ (1 - damping_factor) * random_jump
)5.Finding the PageRank Scores
We begin by giving every webpage the same initial rank:
curr_rank = np.ones(total_pages) / total_pagesWe repeatedly update the rank vector using:
new_rank = google_matrix @ curr_rankWe stop when the difference between two consecutive rank vectors becomes very small:
error = np.sum(np.abs(new_rank - curr_rank))
if error < epsilon:
break6.Results
- Page C: 0.397400
- Page A: 0.387790
- Page B: 0.214811
Page C has the highest score following with A and B.
7.Conclusion
PageRank can be summarised as:
Web links → Link matrix → Google matrix → PageRank scores