Ch.30: Algorithms: How PageRank Ranks Web Pages

Outline

Transcript

0:00 PageRank scores web pages for search engines using the links between them. Counting links alone gives the wrong impression when we ignore which pages provided them. We'll calculate those scores for five pages, then write a short piece of code that handles a page with no link out. Suppose a user searches your site. When several pages match the same words, you still need a way to order those results. Use these five tiles, A through E. Each represents a page. An arrow goes from the page containing a link to its destination.

0:29 A links to C, and C links back to A. B also points to A. We can follow those arrows without opening any page. D has two links, one to A and one to B. E points only to B. Incoming links arrive; outgoing links leave. B has two pages pointing to it, while C has one. Why wouldn't we just give B the higher score? No arrows enter the pages linking to B. C's link comes from A, which has three. Let's calculate how those different inputs change their contributions. Before we do the arithmetic, what does a page's score actually represent?

1:05 A chance of finding an imaginary visitor there. That's a probability, between 0 and 1. All five scores add to one, or 100%. So we're not measuring real traffic. Start the visitor equally likely to be on any page: 20% each. How does it move? On each step, it follows an outgoing link 85% of the time. Otherwise, it restarts on any page, chosen equally, including its current page. That other 15% gets divided across five destinations. 3% reaches each page through restarting, even if no arrow points to it.

1:40 Yes. Each source, the page that provides the link, divides its share among its destinations. For example, D splits its 20% equally between two links. Multiply that 10-percent share by 0.85. Each destination receives 8.5%. Take A. It receives contributions from B and C, plus half of D. Their previous shares add to 50%. Multiply that by 0.85, then add the restart amount. A's new score is 45.5%. We put that in the new column. Let me test a shortcut: D's 10% plus E's 20 gives B 30. What did that calculation leave out?

2:20 Only 85% follows links; restarting adds three. That makes B 28.5%, not 30. C receives A's old 20. So C ends at 20%. D and E have no incoming arrows; each gets only 3% from restarting. The table shows the complete new row. Our total remains one. We redistributed the visitor's chance; giving D two links didn't create another visitor. Harvard's PageRank exercise uses these probabilities adding to one, including the restart choice. The source links are in the description, alongside the original paper.

2:54 That completes a pass, one update of every page. A leads. Do we already have the ranking, or should we use these new scores in another pass? Now we distribute the first pass's scores. They replace the equal starting values, so the sources no longer contribute equally. Consider the single arrow into C. It supplies A's 45.5, multiplied by 0.85, plus restarting. C reaches 41.675%. Meanwhile, B receives half of D's 3% and all of E's 3%. The resulting score is only 6.825%. My mental model still had two links as two equal votes.

3:34 But those sources each have so little score to pass along. The incoming links stayed the same while C passed B. Could repeating the update make B move back ahead? On this particular graph, a 3rd update returns the same 5 values. It won't flip the order because it changes nothing. The last two rows match. For another website, we'd usually stop when every score changes less than a small cutoff, rather than expect exact equality. So C really does outrank B in the finished calculation. One incoming link gives it over 41%, while two give B less than seven.

4:09 Right. That difference comes from the links' sources and their outgoing splits. It isn't a bonus awarded for having fewer links. But without that restart, follow a visitor into the A and C pair. Can it ever leave by taking another outgoing link? No. Those two pages send the visitor back to one another. That's a closed pair: none of its outgoing links reaches anywhere else. Restarting gives the visitor a chance to reach D or E anyway. It can leave a place without needing an arrow out of it. And it keeps every page reachable in the model.

4:44 The 85% choice controls how strongly the links shape that model; it isn't measured human behavior. For instance, remove the arrow leaving E, which used to lead to B. Now E has no outgoing link. Where should its share go? The name for that is a dead end. Model it as having equally likely links to every page, including itself. We already give every destination a restart share. Couldn't we leave E out of the link calculation and rely on that? I don't trust that shortcut, because restarting covers only 15% of each step.

5:17 E's other 85% would disappear from the calculation. Take the original 20% per page again. E holds a 17% contribution through following links. Dropping it leaves only 83% across the new scores. An absent link shouldn't make part of the visitor vanish. Split that 17% five ways instead: 3.4% per destination. Including back to E, even though we didn't draw a real link from E to itself? Yes. That's the rule for this no-link case, not a claim that its web page contains another link. Every destination receives an equal share.

5:54 Then for D, add its restart allocation to the extra amount arriving from E. That yields 6.4%; E receives just as much. Adding that extra share lifts A to 48.9%. The other two pages receiving real links gain too; their exact numbers are on screen. Those entries restore the full 100%. They describe the changed graph's first update, rather than our earlier final answer. We would repeat updates on this graph as well. The restart and the dead-end rule solve different problems, so our code needs both.

6:28 And we keep E in the input. Deleting an awkward page would change which pages we're ranking, rather than calculate its score. With both the restart and dead-end rules in our code, restore E's link to B. We'll trace the original scores after one update. Our inputs are the links and that complete old row. The call on screen returns the next one, where C overtakes B. The update function, our code for one full pass, begins by combining restarting with the contribution from dead ends. There are no dead ends on this input, so dead is 0.

7:03 What does that variable add up when a page has nowhere to go? The old scores of pages without outgoing links. We sum them once, then add their equally shared link-following contribution to every new score. Those starting values are the same 3% for all five destinations. No contribution from an ordinary link has been added yet. Next, we go through each source with outgoing links. The loop multiplies its old score by 0.85 and divides by its number of destinations. D starts with 3% in this old row.

7:36 Splitting its contribution sends 1.275% to each of its two destinations. The same operation handles a source with 1 link or many. A page without destinations is skipped because we shared its contribution before processing ordinary arrows. Could I save a dictionary by overwriting each previous value as soon as its replacement is calculated? Then later pages could read values from the current pass while earlier pages read the previous pass. That's a different rule from calculating everything from the previous row.

8:06 The repeat loop resets every page to 1/5. It compares complete updates with their old scores; gap keeps the largest change, ignoring plus or minus. If that change is below our chosen small threshold, we stop. The code calls the threshold tol. We're deciding when the numerical answer is sufficiently stable. And if the loop reaches its maximum number of passes while the scores are still changing? The else part reports an error if every allowed pass runs without breaking out. It hasn't passed our stopping test, so report the calculation as unfinished.

8:39 Storing each page's destinations is an adjacency list. With that input, one pass visits the pages and links rather than every possible pair of pages. Summing dead ends just once preserves that cost. Spreading each dead end across every page inside another loop would do needless repeated work. Repeating passes multiplies the cost, but we don't need a giant table of every page paired with every other page to run this algorithm. Finally, suppose we rewrote every page's words but kept its links. Would this code change any of the scores?

9:12 It would return the same numbers. Brin and Page's original paper describes weighting who links to a page, rather than just counting all those links. But those rewritten words might answer a completely different search. Doesn't that mean PageRank needs something else that looks at what the page says? Yes. Matching a particular question needs other information. Google's own documentation describes many ranking systems and says PageRank has evolved alongside them. Keeping the links unchanged also means these scores don't check whether the words are true. They measure importance within our link-following model.

9:46 For the original graph, though, we can account for the result precisely. A sends a large share toward C, while B receives much smaller contributions. C's single incoming link beats B's two because of those contributions. Divide each source's share, keep the total intact, and repeat the update until it settles. Thanks for listening to Learning Podcasts.