Skip to the workspace
Pebellslearn by building
Graphs  ›  Lesson

Graphs & the Follow Graph

Graph Representation 18 minStoring who connects to whom

dsa / Graphs & the Follow Graph

Explore each scene at your own pace.

01 · Tunebox's follow graph

Tunebox's follow graph

Graphs & the Follow Graph

Definition
A graph is a set of vertices (things) joined by edges (connections between pairs of them); edges can be one-way or two-way and can carry a cost, and the graph is stored as an edge list, an adjacency matrix or an adjacency list.
In plain words
Think of a map of who follows whom: each person is a dot, and each follow is an arrow from one dot to another. Unlike a list or a tree, the arrows are allowed to loop back, and you can keep the map as a plain list of arrows or as a per-person list of who they follow.

In short: a graph is vertices (things) plus edges (connections) — the most general structure there is, because unlike a list or a tree it can loop back on itself. An edge is directed (an arrow, one way) or undirected (a line, both ways), and it can carry a weight (a cost). You can store a graph three ways — an edge list, an adjacency matrix, or an adjacency list — and the right choice depends on how often you ask "who connects to what" versus "does this exact pair connect", and on how sparse the graph is.

Two small graphs side by side: on the left a directed graph where ana, ben and cal are joined by one-way arrows, ana to ben and ben to cal and ana to cal; on the right an undirected graph where glow, drift and aura are joined by plain two-way lines

Tunebox's follow graph

Ana follows Ben, Ben follows Cal, and Cal follows Ana right back — a loop a list or a tree could never hold. Tunebox receives follows exactly like this: one row per follow, a follower and a followed. That raw form is an edge list:

watch it move · graph

Each follow row becomes one arrow, and the arrows together are the graph.

The first row, ana follows ben, becomes an arrow from ana to ben. 4 nodes, 1 edges, directed.

1 / 5

An edge list is cheap to append to but slow to query. Features want an adjacency list instead — every listener's own list of who they follow:

python

Converting rows into that shape is the most common graph code you'll write, and it has one trap: a listener who follows nobody is still a vertex and still needs a key, or the first algorithm that reaches them falls over:

broken — fix itKeyError

Cal follows nobody, so this should print []. Instead it crashes with a KeyError. Fix build so every listener in the graph has a key.

A vertex's degree is how many edges touch it; a directed graph splits that into out-degree (who you follow — free, it's len(graph[name])) and in-degree (who follows you — not stored anywhere, since those arrows live in other people's lists). Counting it is one pass over every list:

step through it
1graph = {"ana": ["ben", "cal"], "ben": ["cal"], "cal": []}
2followers = {name: 0 for name in graph}
3for name in graph:
4 for other in graph[name]:
5 followers[other] += 1
6print(followers)

That's O(V + E): every key once, every edge once.

Scene 1 of 6
Project context and further resources
You're building a piece ofTunebox — recommendations
This piece — build_follow_graph(): Who follows whom, stored as an adjacency list.
Scenario Every tap on Follow reaches Tunebox as one row — [follower, followed]. Before any feature can walk the network, those rows have to become a graph you can ask questions of.
build_follow_graph.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
build_follow_graph(follows) → dictThe follow graph as an adjacency dict: each listener → the sorted listeners they follow.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.