Back to Works

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

ABC

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:

12

For this example, the link matrix is:

H=[00112001210]

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_links

4.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:

G=dH+(1−d)J

where:

  • H is the link matrix;
  • J gives every webpage an equal probability;
  • d is the damping factor.

We use:

d=0.85

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_pages

We repeatedly update the rank vector using:

rk+1=Grk

new_rank = google_matrix @ curr_rank

We stop when the difference between two consecutive rank vectors becomes very small:

error = np.sum(np.abs(new_rank - curr_rank))

if error < epsilon:
    break

6.Results

  1. Page C: 0.397400
  2. Page A: 0.387790
  3. 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

© 2026 Seoungwan Song. All rights reserved.