The Theory

Six Degrees of Separation

The idea that any two people on Earth are connected through at most six acquaintances.

🎭

The Kevin Bacon Game

Originally based on the actor Kevin Bacon, the parlor game challenges players to connect any actor to Bacon through shared movie appearances. Each shared movie counts as one "degree" of separation.

🦸

The Marvel Twist

We replace Kevin Bacon with the entire MCU ensemble. Instead of connecting to one actor, we find the shortest path from any actor to any of the 30+ core MCU cast members — Iron Man, Captain America, Thor, and beyond.

🌐

Graph Theory

Mathematically, this is a shortest-path problem in a bipartite graph. Actors and movies form nodes, and "appeared in" forms edges. We use Breadth-First Search (BFS) to find the minimum number of hops.

How It Works

👤
Any Actor
🎬
Movie
👤
Actor
🎬
Movie
🦸
MCU Actor

This example shows a 2-degree connection: Actor → Movie → Actor → Movie → MCU Actor

Advertisement
Under the Hood

How the BFS Algorithm Works

A step-by-step breakdown of the Breadth-First Search algorithm that powers every connection discovery.

🔍
01

Actor Search

You type an actor's name and we query the TMDB API (/search/person) in real-time. Results appear with autocomplete as you type.

TMDB API → /search/person?query={name}
💾
02

Cache Check

Before running the algorithm, we check PostgreSQL for a cached result. If this actor was searched before, we return the result instantly.

SELECT * FROM cached_paths WHERE source_actor_id = ?
🌳
03

BFS Initialization

We load the 30+ MCU seed actors as our target set. The search actor becomes the root of our BFS tree. We initialize the visited set.

Queue = [sourceActor], Visited = {sourceActor}, Targets = MCU_SET
🎬
04

Expand Movie Credits

For each actor in the current BFS level, we fetch their movie credits from TMDB. We examine the top 15 movies sorted by cast billing order.

TMDB API → /person/{id}/movie_credits
🎯
05

Check Co-Stars

For each movie, we get the full cast list. We check each co-star: are they in the MCU set? If YES → path found! If NO → add to next BFS level.

TMDB API → /movie/{id}/credits → Check cast ∩ MCU_SET
06

Result & Cache

Once a connection is found, we build the full path, generate graph data for visualization, cache the result in PostgreSQL, and return it.

INSERT INTO cached_paths (path_data) → Return GraphData

Time Complexity: O(bd) where b = branching factor (co-stars per movie × movies per actor) and d = depth.
Space Complexity: O(bd) for the visited set. Cached results return in O(1).

Advertisement