← all builds

From-Scratch Build · Graph Algorithms

Mutual-Connection Path Finder

Give it two social-media accounts and it finds the shortest chain of mutual friends linking them — the real "you two have a friend in common" path, computed instead of guessed. Built from scratch to learn how graph search turns a tangle of follows into a clean answer.

PythonBreadth-first searchREST API PaginationGraph traversal

What it is

Six degrees, actually measured

The tool treats a social network as a graph: every account is a node, every follow an edge. Given a start account and a target, it explores outward through mutual connections until it reaches the target, then reports both the number of hops and the exact chain of people in between. The result is the literal shortest path of friends-of-friends.

I built this to make the "small world" idea concrete. Everyone says any two people are a handful of connections apart — this is the algorithm that proves it for a specific pair, and shows the route.

The core idea I wanted to learn: the shortest path through a network of equal-weight links is exactly what breadth-first search finds. Explore layer by layer — all direct contacts, then their contacts — and the first time you reach the target, you've found the shortest possible chain, guaranteed.

The stack

Tools under the hood

This rebuild is small but touches real-world data plumbing as well as the core algorithm. Here is what each piece does.

algorithm

Breadth-first search

The heart of it — explores the graph level by level so the first route to the target is the shortest one.

structure

Queue + visited set

A FIFO queue drives the frontier; a visited set stops the search revisiting accounts and looping forever.

data

Social REST API

Follower lists come from a hosted API; the code wraps the endpoints it needs to expand each node.

paging

Pagination handling

Follower counts run into the thousands, so requests are fetched in batches with continuation tokens.

trace

Predecessor map

A back-pointer for every visited node lets the final path be reconstructed by walking back from the target.

output

JSON export

The people along the discovered chain are enriched with profile details and written out as a JSON file.

Architecture

How the search unfolds

The program runs as a single traversal with a depth cap, so it stays bounded even on a huge network. Each step does one clear thing.

  1. Seed the queue live

    Start from the first account, mark it visited, and put it on the queue at distance zero.

  2. Expand a node live

    Pull the next account and fetch its full follower list, fetching across all pages.

  3. Find mutuals live

    Compute the connections to explore next and enqueue any not yet seen, one hop deeper.

  4. Check the target live

    If the target appears, stop — by the rules of breadth-first search this is the shortest path.

  5. Reconstruct live

    Walk the predecessor pointers back from the target to build the ordered chain of people.

  6. Enrich & save live

    Pull profile details for everyone on the chain and write the result out as JSON.

How it runs

Bounded, paged, traceable

Real social graphs are enormous, so the design is as much about restraint as about search:

In my rebuild I focused on keeping the traversal correct and bounded — on a graph this large, an unguarded search is the difference between an answer and a hang.

Reflection

What rebuilding it taught me