Efficient way to find degrees of separation between two nodes in a graph
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Degrees of separation between two nodes in an unweighted graph means the length of the shortest path between them measured in edges. The standard efficient solution is breadth-first search, because BFS explores the graph layer by layer and reaches each node at the minimum possible distance from the start node.
Why BFS Is the Right Default
In an unweighted graph, every edge has the same cost. That means the shortest path is simply the path with the fewest hops. BFS is designed exactly for that situation.
If node A is connected to node B, and B is connected to node C, then a BFS starting at A visits:
- distance
0:A - distance
1: neighbors ofA - distance
2: neighbors of those neighbors
The first time BFS reaches the target node, you have found the minimum degree of separation.
A Simple BFS Implementation
A practical Python implementation uses an adjacency list and a queue.
This returns 2, because one shortest route is Alice -> Carol -> Eve.
Return the Actual Path, Not Just the Distance
Sometimes the distance alone is not enough. You may also want the sequence of nodes on the shortest path.
Then the degree of separation is simply len(path) - 1 if a path exists.
Use Bidirectional BFS for Large Graphs
If the graph is large and you only care about one pair of nodes, bidirectional BFS is often faster in practice. Instead of searching from only the start node, it searches outward from both the start and the target until the frontiers meet.
The reason it helps is that graph neighborhoods often grow quickly. Searching halfway from both ends can reduce the total number of explored nodes significantly.
A simplified version:
This version shows the idea, even though production code often keeps more explicit bookkeeping.
When BFS Is Not Enough
BFS is correct for unweighted graphs. If edges have weights, you need a shortest-path algorithm that respects those costs, such as Dijkstra’s algorithm.
So the real decision rule is:
- unweighted graph: BFS or bidirectional BFS
- weighted graph: Dijkstra or another weighted shortest-path method
Degrees of separation problems in social-network style graphs are usually unweighted, which is why BFS is the standard answer.
Represent the Graph Efficiently
An adjacency list is usually the right representation for sparse graphs.
This keeps neighbor scans efficient and avoids the memory overhead of an adjacency matrix when the graph is large but not densely connected.
Common Pitfalls
The most common mistake is using DFS and hoping it will find the minimum separation just because it eventually finds a path. Another is forgetting a visited set, which can cause repeated work or infinite loops in cyclic graphs. Developers also sometimes use BFS on weighted graphs and then wonder why the answer is wrong. A final issue is storing the graph in a form that makes neighbor lookup expensive, which hurts performance even when the algorithm choice is correct.
Summary
- In an unweighted graph, degrees of separation is a shortest-path problem in number of edges.
- BFS is the standard efficient solution because it explores the graph in distance layers.
- Bidirectional BFS can reduce search work for large one-pair queries.
- Use adjacency lists for efficient graph representation in most sparse graphs.
- If edges have weights, switch to a weighted shortest-path algorithm instead of plain BFS.
Related reading
- Efficient Way to Find Pair Orderings?
- Efficient way to insert a number into a sorted array of numbers?
- Efficient way to remove half of the duplicate items in a list
- Efficient way to rotate a list in python
- Efficient way to search an element
- Efficient way to store millions of arrays, and perform IN check
- Efficiently check if two numbers are co-primes relatively primes?
- Efficiently computing a - K / a K with improved accuracy

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseThere is a whole course on this
Basis Lab has written “Graphs and Finding a Path”, a course that teaches this from the ground up rather than answering the single question. It is a separate product from the same team and the course is free to read.
Read the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.