Blog · 5 min read ·
How my Wiki Race solver finds a path in seconds
In a Wiki Race, you start on one Wikipedia article and try to reach another using only the links in the articles. Humans are surprisingly good at it, because we know that almost everything links to a country, a century or a famous person. My Wiki Race Solver does it with plain graph search against the live Wikipedia API. Here's how.
Wikipedia is a graph
Treat every article as a node and every link as an arrow. Finding a race path is then finding a path from the start node to the target node. The classic tool for "shortest path when every step costs the same" is breadth-first search (BFS): look at everything one click away, then everything two clicks away, and so on.
The problem: the graph explodes
A typical article has hundreds of links. Two clicks out you can be looking at tens of thousands of pages, and three clicks out, millions. Every page you expand costs an API request. Plain BFS from the start would be far too slow in a browser.
The trick: search from both ends
Wikipedia's API can also answer the reverse question: which articles link to this page? (list=backlinks). So before searching forward, the solver downloads the set of pages that link to the target. That's a finish line made of hundreds or thousands of pages instead of one.
Then it runs BFS forward from the start article (prop=links), eight pages at a time in parallel. For every link it reads, it checks two things: is this the target, or is it in the "links to the target" set? The moment either is true, it walks the parent pointers back to the start and shows the chain.
Rare targets need one more step
Obscure articles might have only a few backlinks, so the finish line is tiny. When the backlink set has fewer than 400 pages, the solver goes one step further back: it takes the backlinks of up to 40 of those pages too, remembering which page each one leads to. Now the finish line is wide again, and the final path is the forward chain plus the remembered hops to the target.
Making it polite and fast
- Titles are resolved through redirects first, so "USA" and "United States" are the same page.
- Each page's links are fetched with continuation tokens, up to a cap, so huge pages don't stall the search.
- Pages already seen are never expanded twice.
- The next level is sorted so pages known to be near the target come first.
- A hard cap on pages read stops the search from hammering Wikipedia.
In testing, common pairs like Pizza → Moon landing solve in about 1.5 seconds with three clicks, and random obscure pairs usually solve in four or five. Try to stump it.