Skip to the workspace
Pebellslearn by building
Graphs  ›  Capstone Project

Capstone — Recommendations

30 minTurning graph walks into suggestions

dsa / Capstone — Recommendations

Explore each scene at your own pace.

01 · Finding Ana's neighbourhood

Finding Ana's neighbourhood

Capstone — Recommendations

Definition
A graph-based recommender combines a breadth-first search limited to a number of hops, to find who is near a listener, with a weighted tally in which nearer people count for more, then sorts the tracks by their total score.
In plain words
It is asking your friends what they are playing, with closer friends getting a louder voice than friends of friends. You add up each track's votes, drop what you already play, and show the highest scores first.

In short: Ana's Discover page should fill with tracks her network plays — and a close friend's favourite should outrank a stranger three follows away. That's this whole section in one feature: a graph of follows, a BFS traversal capped at max_hops that finds who's nearby and how near, and a weighted tally that turns nearness into influence. Every piece — capped BFS O(V + E) within the radius, a tally over the plays found, and a final O(K log K) sort of K candidate tracks — is something you've already built in this section. This capstone wires them together.

Ana's two direct friends ben and cal each voting weight 2 for their tracks, and friend-of-a-friend dia voting weight 1, feeding a track tally where Drift (voted by both ben and cal) outscores Aura

recommend_for(follows, plays, listener, max_hops)
  • follows — the follow graph, an adjacency dict. A name with no key follows nobody.
  • plays — each listener's tracks, e.g. {"ben": ["Drift", "Glow"]}. No repeats within one list; a listener with no key plays nothing.
  • listener — who the recommendations are for.
  • max_hops — how far out to look.

The rules, in order: walk the follows breadth-first from listener out to max_hops hops; every listener found at distance d (1 ≤ d ≤ max_hops) has a weight of max_hops - d + 1; each adds their weight to every track they play, except tracks listener already plays; return [track, score] pairs, highest score first, ties alphabetically. Worked by hand with max_hops = 2:

Listener Hops from ana Weight Plays Adds
ben 1 2 Drift, Glow Drift +2 (Glow skipped — ana plays it)
cal 1 2 Drift Drift +2
dia 2 1 Aura Aura +1

So recommend_for(follows, plays, "ana", 2) → [["Drift", 4], ["Aura", 1]].

Finding Ana's neighbourhood

This is BFS from the BFS lesson, with one addition: don't expand anyone already max_hops out. The hops dict records distance and doubles as the visited set, so follow loops can't send you round forever. With max_hops = 2, Eli — whom Dia follows, three hops out — gets no vote at all:

watch it move · graph

A capped BFS finds who is near and how near, and nobody past max_hops gets a vote.

The walk starts at Ana, 0 hops out, and she never votes on her own page. 5 nodes, 6 edges, directed. At ana. Settled: ana. ana: 0.

1 / 5
python

The mistake: forgetting that listener is still in hops at distance 0 — skip distance 0 when you tally, or Ana ends up voting for her own tracks:

What does this print?
from collections import deque
follows = {"ana": ["ben", "cal"], "ben": ["dia"], "cal": ["dia"], "dia": ["ana"]}
hops = {"dia": 0}
queue = deque(["dia"])
while queue:
    name = queue.popleft()
    if hops[name] >= 2:
        continue
    for nxt in follows.get(name, []):
        if nxt not in hops:
            hops[nxt] = hops[name] + 1
            queue.append(nxt)
print(sorted(hops))
Scene 1 of 6
Project context and further resources
You're building a piece ofTunebox — recommendations
This piece — recommend_for(): Suggests tracks from what nearby listeners play.
Scenario Ana's Discover page fills with tracks her network plays — weighted so a close friend's favourite counts for more than a stranger's three follows away.
recommend_for.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
recommend_for(follows, plays, listener, max_hops) → listRecommended [track, score] pairs from nearby listeners, best first.
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.