oluwatobi oyinloye
Back to all work

Does Six Degrees of Separation Hold in the Wizarding World?

Six degrees of separation says anyone on Earth is linked to anyone else through six introductions or fewer. I tested it on every character in the Harry Potter universe, building their network in Rust and searching it with breadth-first search.

Individual project. DS210 (Programming for Data Science) final project, Boston University.

Built with

Language
Rust
Graph
petgraph
Data handling
csv and rand crates
Search
Breadth-first search
Data
dpmartin42 Harry Potter network, on GitHub

The Question

Six degrees of separationThe idea that any two people are connected by a chain of six or fewer acquaintances. A friend of a friend of a friend, and so on.

The Six Degrees of Separation theory was made for a world of eight billion people, so I wondered if it would be applicable in the tight-knit wizarding world of Harry Potter. An avid reader and watcher of the series, I thought it would be very true, and most likely less than 6 links apart.

So, I decided to test it out on every character ever mentioned in the series, to check how many steps it takes to get from one character to all the others.

Lord Voldemort, captioned You Know Who
"You Know Who" is one hop from Harry and unsurprisingly, so is almost everyone else.

The Data

A network, or graphA set of points, called nodes, joined by lines, called edges. Here every character is a node and every interaction is an edge.

Unfortunately I couldn't find a dataset that met my needs, but I found a JSON file of Harry Potter character interactions on GitHub (dpmartin42/Networks). I manually turned it into a table of characters and connections, then into a CSV with two columns, one character on each side of an interaction.

The file has 4,904 rows, because every interaction is listed twice, once in each direction. Once duplicates are removed, that's 179 characters and 2,454 unique connections, which together form a network, or graph. A connection simply means the two characters interact somewhere in the series. There are no weights, so a single conversation counts the same as seven books of friendship.

Methods

I wrote the program in Rust as a set of small functions, each doing one job, with a separate test file and the core of the project situated in the last step.

csv_to_txt and read_csv load the CSV, skipping any broken lines.

remove_duplicates stores each pair once, since every interaction was listed twice.

load_data gives every character a number and adds the connections to a petgraph graph.

random_sample chooses a starting character at random.

six_degrees_to_all runs breadth-first search from that character to everyone else.

Breadth-First Search

Why not just search deeper?Depth-first search follows one chain as far as it goes before backing up. It finds a path, but not necessarily the shortest one.

Breadth-first search (BFS) is a graph traversal algorithm, and it's how the program measures distance. It starts at one character, visits everyone they know, then everyone those people know, and so on, one ring at a time. The first time it reaches a character, that ring number is their degree of separation.

Because it always finishes a whole ring before moving outward, BFS is guaranteed to find the shortest path, unlike depth-first search.

Try it Yourself

Pick any two characters and the page runs the same breadth-first search on the full network. The drawing shows the 60 most connected characters, but everyone else still counts in the search.

Results

Why so close?Harry is a hub. When one node touches almost everyone, any two characters can usually reach each other through him in two steps.

My program starts from a random character and in my test, it picked Neville Longbottom. From Neville, 81 characters were one hop away, 96 were two hops away, and only one, Marjorie Dursley, was three. The whole search took 0.001731 seconds (1,731 µs).

My results indicate that all characters in the Harry Potter network are indeed linked in some way, but in 3 steps at most from Neville, rather than 6.

To take this research further, I would build a module that calculates the average degree of separation across all characters, and finds which character is most connected. Running every character as a starting point gives the full picture, so I ran a version of this for the site.

15.4%1 hop80.2%2 hops4.4%3 hops0.0%4 hops
  • The average distance between two characters is 1.89 hops.
  • 95.6% of all pairs are within two hops, and every pair is within four.
  • Harry is directly connected to 166 of the other 178 characters, an average of 1.07 hops from everyone.
  • The next most connected are Dumbledore, with 139 connections, and Hermione, with 127.

The Real Output

The results from testing, on my terminal.

Terminal output of the Rust program

Key Takeaways

The biggest margin for error in this research was in the code, because I did a lot of the data preparation manually. Therefore, some of what I took away was that:

  • The source spells Dudley's aunt both "Marjorie" and "Majorie," so the graph treats her as two different people. Merge them and the longest distance drops from four hops to three.
  • Peter Pettigrew, Wormtail and Scabbers are the same character, but appear as three separate ones.
  • Names with commas in them, like "Vincent Crabbe, Sr.", need careful parsing or they split into two fake characters.
  • Every connection counts the same. Weighting them by how often characters actually interact would make the distances more meaningful.

None of these change the overall result, but fixing them would make the distances more accurate.

Most Characters are Two Hops Apart

Across the whole network, the average distance between two characters is 1.89 hops, 95.6% of pairs are within two hops, and every pair is within four. Six degrees of separation holds, largely because Harry is directly connected to 166 of the other 178 characters.

This was also my first program written in Rust.

Next project
Do GPT-4.5 and Humans Judge Similarity the Same Way?