Skip to content

rkr14/SixDegrees

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Six Degrees of Wikipedia β€” Solver Engine

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.


Architecture

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
Loading

Prerequisites

Docker (recommended)

Local Development

  • C++ Toolchain: GCC β‰₯ 10 / Clang β‰₯ 12 / MSVC β‰₯ 19.28 (C++17 support)
  • CMake β‰₯ 3.20
  • Python β‰₯ 3.9 with pip
  • Git (for FetchContent dependencies)

Quick Start with Docker Compose

1. Prepare Data

# Download Wikipedia dumps and preprocess into TSV files
chmod +x scripts/download_dump.sh
./scripts/download_dump.sh ./data

Note: The full English Wikipedia dump is ~6 GB compressed. Preprocessing takes 30–60 minutes and requires ~16 GB RAM.

2. Start Services

docker compose up --build -d

This builds the C++ engine, starts the FastAPI server on port 8000, and a Redis cache on port 6379.

3. Query

curl "http://localhost:8000/api/path?source=Albert_Einstein&target=Pizza"

Local Development Setup

Build the C++ Engine

mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON
cmake --build . --parallel $(nproc)

Run Tests

cd build
ctest --output-on-failure

Install Python Dependencies

pip install -r api/requirements.txt

Run the API Server

# 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 8000

API Documentation

GET /api/path

Find 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
}

GET /api/health

Health check endpoint.

curl http://localhost:8000/api/health
{
  "status": "ok",
  "graph_loaded": true,
  "num_articles": 6800000,
  "num_links": 190000000
}

Data Preparation

Automated (recommended)

./scripts/download_dump.sh ./data

Manual

  1. Download from dumps.wikimedia.org:

    • enwiki-YYYYMMDD-page.sql.gz
    • enwiki-YYYYMMDD-pagelinks.sql.gz
  2. 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)

Performance Characteristics

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

Project Structure

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

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors