From-Scratch Build · Graph Algorithms
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.
What it is
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
This rebuild is small but touches real-world data plumbing as well as the core algorithm. Here is what each piece does.
The heart of it — explores the graph level by level so the first route to the target is the shortest one.
A FIFO queue drives the frontier; a visited set stops the search revisiting accounts and looping forever.
Follower lists come from a hosted API; the code wraps the endpoints it needs to expand each node.
Follower counts run into the thousands, so requests are fetched in batches with continuation tokens.
A back-pointer for every visited node lets the final path be reconstructed by walking back from the target.
The people along the discovered chain are enriched with profile details and written out as a JSON file.
Architecture
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.
Start from the first account, mark it visited, and put it on the queue at distance zero.
Pull the next account and fetch its full follower list, fetching across all pages.
Compute the connections to explore next and enqueue any not yet seen, one hop deeper.
If the target appears, stop — by the rules of breadth-first search this is the shortest path.
Walk the predecessor pointers back from the target to build the ordered chain of people.
Pull profile details for everyone on the chain and write the result out as JSON.
How it runs
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