Find shortest path between two articles in english Wikipedia in Python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Wikipedia can be modeled as a directed graph where each article is a node and each internal link is an edge. If you want the shortest path between two articles, the core algorithm is usually breadth-first search, because each link traversal has equal cost.
Core Sections
Model the problem as an unweighted graph
When every click from one article to another counts as one step, you do not need Dijkstra’s algorithm or A-star to get started. Breadth-first search, often called BFS, is enough because it explores all articles one level away, then two levels away, and so on.
That guarantee matters. The first time BFS reaches the target article, the discovered path is the shortest in terms of number of links.
Fetch outbound links from the Wikipedia API
The MediaWiki API can return article links page by page. In practice, you need to handle continuation because popular pages may have too many links for a single response.
This function is small, but it already handles one of the biggest mistakes in Wikipedia graph code: assuming one API response contains every outbound link.
Run BFS and remember parents
The easiest way to reconstruct the path is to remember where each discovered article came from. Once the target is found, walk backward through the parent map.
Using the parents dictionary also doubles as your visited set. If an article already has a parent, you have already discovered it.
Make the search practical
A naive BFS across Wikipedia grows very fast. Even a few levels can explode into tens of thousands of pages. For experiments, add guardrails:
- limit the maximum depth
- sleep between requests to avoid hammering the API
- cache fetched link lists locally
- normalize titles so redirects and capitalization do not create duplicate work
A simple depth cap can prevent runaway searches.
Think about API behavior and data quality
Wikipedia has redirects, disambiguation pages, missing pages, and occasionally pages whose link structure is not useful for shortest-path exploration. If you want strong results, you should decide whether to follow redirects explicitly and whether to skip maintenance pages or namespaces that are not normal articles.
For small tools, it is often enough to start with article titles only and improve later once you can measure search size and response times.
Common Pitfalls
- Using depth-first search for this problem, which does not guarantee the first found path is the shortest.
- Ignoring API continuation and therefore missing many outgoing links from large pages.
- Failing to track visited pages, which can create cycles and huge amounts of duplicate work.
- Running unrestricted BFS on Wikipedia without depth limits or caching, which quickly becomes slow and expensive.
- Treating redirects and disambiguation pages as normal articles without deciding how they should affect the search.
Summary
- Wikipedia shortest-path search is naturally modeled as BFS on a directed graph.
- The MediaWiki API can provide article links, but you must handle continuation correctly.
- Store parent pointers during BFS so you can reconstruct the path efficiently.
- Add depth limits, caching, and rate control to keep the search practical.
- Redirects, disambiguation pages, and graph growth are the main engineering complications.

