Pick two people on a social network with a billion users. "How are we connected?", find the shortest chain of friendships linking them. It sounds like a homework problem. In practice, it's one of the most demanding queries in large-scale computing, and most systems that offer it are either slow, approximate, or quietly cheating.

Why it's hard

The naive approach is breadth-first search: start from person A, explore all their friends, then friends-of-friends, until you reach person B. The problem is the branching factor. If the average user has 200 connections, two hops reaches 40,000 people. Three hops: 8 million. Four hops: more than the population of most countries.

This exponential explosion is why the naive approach dies around hop three on any real network. And the interesting answers, the surprising connections, live at hops four, five, six.

The interesting answers, the surprising connections, live at hops four, five, six. Exactly where naive search dies.

The bidirectional trick

Enjoying this story?

Get the five most important stories in tech, every morning. Free.

The standard optimization is bidirectional BFS: search from both ends simultaneously and meet in the middle. This cuts the effective depth in half, a 6-hop path becomes two 3-hop searches. It's an enormous improvement, and it's what most production systems actually do.

But bidirectional search has a subtle enemy: high-degree nodes. Celebrities, brands, and power users with millions of connections act as black holes in the graph. The moment your search frontier touches one, it explodes outward to millions of nodes in a single step. Production systems handle this with degree-based pruning, refusing to expand through nodes above a threshold, but every prune risks missing the true shortest path.

Landmarks and approximations

For networks that need millisecond responses, exact search isn't feasible. The alternative is landmark-based approximation: precompute distances from a set of well-chosen landmark nodes to everyone in the graph. Then the distance between any two people is approximated via triangle inequality through nearby landmarks.

This is fast, effectively constant time per query after preprocessing, but it's approximate. The answers are upper bounds, not shortest paths. For "how are we connected?" features, that's usually fine. Users want an interesting chain, not a mathematically optimal one.

The dirty secret

Social network connections
Mapping human connection is harder than it looks. (Photo: Leadflask)

Here's what nobody advertises: many "connection finder" features don't search the live graph at all. They search a stale snapshot, or a sampled subgraph, or they cap the depth at 3 and report "no connection found" for anything deeper. The feature works well enough that nobody notices.

Many "connection finder" features don't search the live graph at all. They search a stale snapshot and hope nobody notices.

There's a deeper reason the problem stays hard: social graphs are dynamic. Friendships form and dissolve constantly. Any precomputed index starts decaying the moment it's built. Systems must choose between fresh-but-slow and fast-but-stale, and there's no universally right answer.

Why it matters

Beyond the novelty feature, shortest-path queries power friend recommendations, trust scoring, fraud detection, and influence analysis. The same algorithmic core that finds your connection to a stranger also finds sockpuppet networks and coordinated manipulation campaigns.

The friend graph problem is a reminder that some of computing's hardest problems hide inside its simplest questions. "How are we connected?", six words, and a research literature spanning decades.

The privacy angle

There's a reason connection-finder features make privacy advocates nervous. A shortest path between two people reveals information neither of them explicitly shared: the structure of their social world. "You're connected through your ex's new partner" is interesting; it's also the kind of thing people might prefer not to surface.

This creates a genuine design tension. The feature's value comes from revealing hidden structure, but hidden structure is sometimes hidden for a reason. Thoughtful implementations let users control their visibility in path results, appearing in searches only for certain degrees, or opting out entirely. The systems that ignore this eventually face backlash; the ones that respect it build trust.

There's also a security dimension. Path queries can be weaponized for social engineering: mapping an organization's hierarchy, identifying high-value targets, finding the weakest link for a phishing attack. Rate limiting and access controls aren't just performance engineering here: they're safety engineering.