Algorithms · Counting & data

PageRank

PageRank: how Google decided which page matters. A page is important if important pages link to it. Sounds circular; turns out it’s solvable.

The problem

In 1998, Larry Page and Sergey Brin (with Motwani and Winograd) were at Stanford staring at the web. Millions of pages, and you type two words. Thousands match. Which one goes at the top?

Matching words isn't enough, because anyone can write a page full of the right words. They needed a signal the page's author couldn't easily fake: what the rest of the web thinks of it.

The obvious way

Count incoming links: each link is a vote, most votes wins. It breaks fast. Votes aren't equal: a link from a respected site means more than a link from an empty page someone made last night. Make 1,000 throwaway pages that all link to yours and you "win" the count.

The clever idea

Imagine someone clicking around the web forever, never reading, just following random links. Pages with lots of traffic from busy pages get visited often. Pages only reachable from dead-end corners get visited rarely. Importance becomes: how much time does this wanderer spend here?

Page and Brin called this wanderer the random surfer. One twist: sometimes the surfer gets bored and jumps to a random page instead (about 15% of the time). That keeps them from getting stuck in a loop of pages forever.

We don't need to simulate the surfer for years. A page's rank is the long-run share of time spent there, and we can find it with power iteration: start everyone equal, then repeatedly have each page hand its rank out along its links until the numbers settle.

How the code does it

The web is a plain dict: each page maps to the pages it links to. The naive count walks every link and adds one to its target. Spam wins with 3; Gem gets just 1.

The real work is the round loop. Each round, every page splits its rank among its links and the targets collect it. Since Wiki is important and links only to Gem, Gem inherits most of it. Meanwhile the fans have no inbound links, so the printed change drops toward zero as the numbers settle, and Spam's three votes carry almost nothing.

Compare the two charts: Gem has the fewest links among the top contenders yet ranks near the top, and Spam falls to the middle of the pack. Same web, very different verdicts.

Trade-offs

PageRank needs the whole link graph, and every round touches every link, so it's costly on a huge web. It also says nothing about your query: it's a global importance score, so it can't tell which page best answers your question. Modern Google uses many more signals, so treat this as one ingredient, not the whole recipe.

Where else you'll see it

Any time you want to rank things in a network by who points to them, the same "importance flows along connections" idea applies: citations between papers, followers between accounts, or links between your own notes.

Why does Gem outrank Spam despite having fewer incoming links?

  • Its name comes first alphabetically
  • Its one link comes from Wiki, a high-rank page that passes along most of its rank
  • Spam's links don't count because they're from fans
Show the answer

Its one link comes from Wiki, a high-rank page that passes along most of its rank. Rank flows along links. Wiki is important and links only to Gem, so Gem inherits a big share. Spam's three links come from pages with almost no rank to give.

Change it and see
  • Change d = 0.85 to 0.5
  • Make Wiki link to ["Gem", "Spam"]
  • Change range(30) to range(3)

The code

Python, with what each line means beside it. It compares the clever way with just counting incoming links.

links = {Start the web as a dict of links
"Wiki": ["Gem"],Wiki links only to Gem
"News": ["Wiki"],News links to Wiki
"Gem": ["Wiki", "News"],Gem links to Wiki and News
"Spam": ["News"],Spam links to News
"fan1": ["Spam"],fan1 links to Spam
"fan2": ["Spam"],fan2 links to Spam
"fan3": ["Spam"],fan3 links to Spam
}
pages = list(links)List all the page names
n = len(pages)Count the pages
# the obvious way: count incoming linksObvious way: count incoming links
naive = {p: 0 for p in pages}Give every page a score of zero
for p in pages:Visit each page
for target in links[p]:Look at each page it links to
naive[target] += 1Add one to that target's count
# the clever way: pass rank along linksClever way: pass rank along links
d = 0.85Set the follow-a-link chance to 0.85
rank = {p: 1 / n for p in pages}Everyone starts with an equal share
for step in range(30):Repeat for thirty rounds
new = {p: (1 - d) / n for p in pages}Everyone gets a small random-jump gift
for p in pages:Visit each page
share = rank[p] / len(links[p])Split its rank evenly among its links
for target in links[p]:Look at each target
new[target] += d * sharePass the damped share along
change = sum(abs(new[p] - rank[p]) for p in pages)Measure how much the ranks moved
rank = newAdopt the new ranks
if step % 5 == 0:Every fifth round only
print(f"round {step:2d}: change = {change:.5f}")Print the round and the change
show([naive[p] for p in pages], "naive: incoming links")Bar chart of link counts
show([round(rank[p], 3) for p in pages], "PageRank")Bar chart of PageRank
print("order:", pages)Print the page order for reading charts

Read more

Run it. Then change it.

The code runs right in the page and draws as it goes. Free, nothing to install.

More on counting & data