A high-performance "Six Degrees of Wikipedia" solver that finds the shortest path between any two Wikipedia articles through hyperlinks. The engine uses a Compressed Sparse Row (CSR) graph with Bidirectional BFS and Hub Labeling heuristics, exposed via a Python FastAPI service.
graph TB
subgraph Docker Compose
direction TB
Client["π Client"]
API["FastAPI<br/>(Python 3.11)"]
Engine["wiki_engine<br/>(pybind11 C++17)"]
Redis["Redis 7<br/>(LRU Cache)"]
Data["π data/<br/>titles.tsv + edges.tsv"]
Client -->|HTTP :8000| API
API -->|pybind11| Engine
API <-->|cache| Redis
Engine -->|mmap| Data
end
subgraph C++ Engine
direction LR
CSR["CsrGraph<br/>(CSR storage)"]
BFS["BidirectionalBfs"]
Hub["HubLabeling"]
Pool["ThreadPool"]
BFS --> CSR
Hub --> CSR
BFS -.->|parallel| Pool
Hub -.->|precompute| Pool
end
Engine --> CSR
- Docker β₯ 20.10
- Docker Compose β₯ 2.0
- C++ Toolchain: GCC β₯ 10 / Clang β₯ 12 / MSVC β₯ 19.28 (C++17 support)
- CMake β₯ 3.20
- Python β₯ 3.9 with
pip - Git (for FetchContent dependencies)
# Download Wikipedia dumps and preprocess into TSV files
chmod +x scripts/download_dump.sh
./scripts/download_dump.sh ./dataNote: The full English Wikipedia dump is ~6 GB compressed. Preprocessing takes 30β60 minutes and requires ~16 GB RAM.
docker compose up --build -dThis builds the C++ engine, starts the FastAPI server on port 8000, and a
Redis cache on port 6379.
curl "http://localhost:8000/api/path?source=Albert_Einstein&target=Pizza"mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON
cmake --build . --parallel $(nproc)cd build
ctest --output-on-failurepip install -r api/requirements.txt# From the project root, with the built wiki_engine.so on PYTHONPATH
export PYTHONPATH="$(pwd)/build:${PYTHONPATH}"
uvicorn api.main:app --reload --host 0.0.0.0 --port 8000Find the shortest path between two Wikipedia articles.
Query Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
source |
string | β | Source article title |
target |
string | β | Target article title |
Response:
{
"source": "Albert_Einstein",
"target": "Pizza",
"found": true,
"distance": 3,
"path": [
"Albert_Einstein",
"Italy",
"Italian_cuisine",
"Pizza"
],
"timing_ms": 12.4
}Example β path not found:
{
"source": "Some_Article",
"target": "Nonexistent_Page",
"found": false,
"distance": -1,
"path": [],
"timing_ms": 1.2
}Health check endpoint.
curl http://localhost:8000/api/health{
"status": "ok",
"graph_loaded": true,
"num_articles": 6800000,
"num_links": 190000000
}./scripts/download_dump.sh ./data-
Download from dumps.wikimedia.org:
enwiki-YYYYMMDD-page.sql.gzenwiki-YYYYMMDD-pagelinks.sql.gz
-
Preprocess:
python3 scripts/preprocess_dump.py \
--page-sql data/enwiki-YYYYMMDD-page.sql.gz \
--links-sql data/enwiki-YYYYMMDD-pagelinks.sql.gz \
--output-dir data/This produces:
data/titles.tsvβpage_id<TAB>page_title(~300 MB)data/edges.tsvβsource_id<TAB>target_id(~3 GB)
| Metric | Value |
|---|---|
| Graph loading | ~20 s (6.8M nodes, 190M edges) |
| Hub precomputation | ~60 s (top-512 hubs) |
| Query latency (p50) | < 5 ms |
| Query latency (p99) | < 50 ms |
| Memory usage | ~3.5 GB (CSR + hub labels) |
| Cache hit latency | < 1 ms (Redis) |
The engine achieves sub-10ms median latency through:
- CSR storage: Cache-friendly, contiguous memory layout
- Bidirectional BFS: Explores from both ends, reducing search space exponentially
- Hub labeling: Pre-computed shortest paths through high-degree nodes enable O(1) lookups for common queries
- Thread pool: Parallel BFS frontiers for large graphs
Wikipedia_Solver/
βββ CMakeLists.txt # Top-level CMake build configuration
βββ Dockerfile # Multi-stage Docker build
βββ docker-compose.yml # Full-stack orchestration
βββ README.md # This file
βββ include/ # C++ headers
β βββ csr_graph.h # CSR graph data structure
β βββ bidirectional_bfs.h # Bidirectional BFS algorithm
β βββ hub_labeling.h # Hub labeling heuristic
β βββ thread_pool.h # Work-stealing thread pool
β βββ graph_loader.h # TSV file β CsrGraph loader
β βββ path_result.h # Query result struct
βββ src/ # C++ sources
β βββ csr_graph.cpp
β βββ bidirectional_bfs.cpp
β βββ hub_labeling.cpp
β βββ thread_pool.cpp
β βββ graph_loader.cpp
β βββ bindings.cpp # pybind11 Python bindings
βββ api/ # Python FastAPI service
β βββ main.py # Application entry point
β βββ cache.py # Redis cache layer
β βββ schemas.py # Pydantic models
β βββ config.py # Environment configuration
β βββ requirements.txt # Python dependencies
βββ tests/ # C++ Google Test suites
β βββ CMakeLists.txt
β βββ test_csr_graph.cpp
β βββ test_bidirectional_bfs.cpp
β βββ test_hub_labeling.cpp
βββ scripts/ # Data preparation scripts
β βββ download_dump.sh # Download Wikipedia dumps
β βββ preprocess_dump.py # SQL dump β TSV conversion
βββ data/ # Graph data files (not committed)
βββ .gitkeep
MIT