Graphs and Trees
Exploring graphs and trees in Discrete Mathematics through my Last.fm listening history.
A month ago, I learned about graphs and trees in my Discrete Mathematics class. I understood them on paper, but I wanted to see them work on something I actually care about, so I turned my Last.fm listening history into a graph.
Vertices and Edges
Each artist is a vertex. Two artists share an edge when they share Last.fm tags, the genre and style labels listeners give them. To keep the graph readable, I only keep each artist's three strongest connections.
Each edge has a weight from 0 to 1 for how much the two artists' tags overlap: I add up the smaller of their two scores for every tag, then divide by the sum of the larger ones. This is the weighted Jaccard similarity, where 1 means identical tags and 0 means nothing in common.
The result is a weighted, undirected graph with 50 artists and 99 edges.
The Demo
Pick two artists, here or by clicking them in the graph.
Click node for details · click two for path · hover edge for tags
Hold Ctrl (or ⌘) and scroll, pinch, or use the buttons to zoom. Drag to pan, and click two nodes to explore the path between artists. Hover over nodes and edges to inspect their connections.
Paths
To find the shortest path between two artists, I used Dijkstra's algorithm. The catch is that Dijkstra expects costs, where smaller is better, while my weights are similarities, where bigger is better. So I turned each weight into a cost: c(e) = 1 / w(e).
This changes what "shortest" means. Brent Faiyaz and Steve Lacy share a direct edge, but it's weak (0.09), so it costs about 11.1. The route through Daniel Caesar and Leon Thomas takes three edges but only costs about 10.3, so Dijkstra picks it. The shortest path isn't the one with the fewest steps; it's the one made of the strongest connections.
Trees
A tree is a connected graph with no cycles, and a tree with n vertices always has n − 1 edges. A spanning tree keeps every vertex, but only enough edges to stay connected.
I used Kruskal's algorithm to build a maximum spanning tree, since my strongest edges are the ones worth keeping. It goes through the edges from strongest to weakest and accepts each one unless it would close a cycle. In the demo, each growing tree gets its own color, so a rejected edge explains itself: both of its ends are already the same color.
My graph isn't fully connected, so Kruskal gives a spanning forest instead: one tree for each connected component. The n − 1 rule still checks out: 50 artists in 3 components gives 50 − 3 = 47 edges, which is exactly how many Kruskal kept.
What I Took Away
The most useful thing the graph showed me was a mistake. My first version had an island of eight hip-hop artists, cut off from everyone else. Part of the reason was that many of them were tagged both "Hip-Hop" and "hip hop", which counted as two separate tags, so they looked extra similar to each other and all their strongest connections stayed inside the group. After I merged the spellings and switched to the Jaccard weights, the island joined the rest of the graph.
That's when connected components stopped being just a definition from class. Counting them turned out to be a quick way to catch bad data.
Even the two artists left on their own tell a story: not everything I enjoy has to belong to the same scene.