Case-based lecture: you attempt, we discuss, then we reveal.
cs-093, cs-094, cs-095, cs-096, cs-136Students 1..6; friendships (1,2), (1,3), (4,5). How many separate friend circles, and how big is each?
from collections import deque
pairs = [(1, 2), (1, 3), (4, 5)]
adj = {}
for u, v in pairs:
adj.setdefault(u, []).append(v)
adj.setdefault(v, []).append(u)
visited = set()
sizes = []
for start in range(1, 7):
if start in visited:
continue
frontier = deque([start])
visited.add(start)
size = 0
while frontier:
u = frontier.popleft()
size += 1
for w in adj.get(u, []):
if w not in visited:
visited.add(w)
frontier.append(w)
sizes.append(size)
print(len(sizes), sorted(sizes))Expected: 3 circles of sizes 3, 2, 1
Cost: O(V + E) - each vertex enqueued once, each edge scanned twice (undirected).
Courses a..e; prerequisites b<a, c<a, d<b, e<c. Order the courses so every prerequisite comes first.
deps = {"a": ["b", "c"], "b": ["d"], "c": ["e"], "d": [], "e": []}
indeg = {k: len(v) for k, v in deps.items()}
queue = [k for k, v in indeg.items() if v == 0]
order = []
while queue:
c = queue.pop()
order.append(c)
for k, v in deps.items():
if c in v:
indeg[k] -= 1
if indeg[k] == 0:
queue.append(k)
print(sorted(order))Expected: a valid order exists: a, b, c, d, e (e.g. d, e, c, b, a)
Cost: O(V + E) with a real reverse-adjacency map (the demo re-scans deps for teaching clarity - worth flagging as the O(V*E) version).
Contacts spread infection per day: edges (1,2), (2,3), (2,4), (4,5). Infection starts at 1. On which day does each person fall ill?
from collections import deque
edges = [(1, 2), (2, 3), (2, 4), (4, 5)]
adj = {}
for u, v in edges:
adj.setdefault(u, []).append(v)
adj.setdefault(v, []).append(u)
dist = {1: 0}
q = deque([1])
while q:
u = q.popleft()
for w in adj.get(u, []):
if w not in dist:
dist[w] = dist[u] + 1
q.append(w)
print([dist.get(p) for p in range(1, 6)])Expected: infection days: person 1:0, 2:1, 3:2, 4:2, 5:3
Cost: O(V + E); the day count equals the BFS layer, computed with no extra work.
Model friendships as a graph; find the size of someone's circle of direct and indirect friends. Modelling first, traversal second.
**Input:** friends: ada-bo, bo-cy, cy-dee, dee-ada, bo-dee (5 edges among 4); ego network of ada**Output:** ada's circle: everyone (size 4) - connected graph; in general: BFS from the personSolve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-093 — not embedded in this deck.
Fewest corridors between two rooms. Breadth-first search on a small grid/campus map; levels as distances.
**Input:** grid campus: S at (0,0), T at (2,2); corridors right/down/up; wall at (1,1)**Output:** shortest walk 4 steps (e.g. down, right, down, right) - BFS levels are distancesSolve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-094 — not embedded in this deck.
Order courses so prerequisites come first (topological sort). Detect cycles as 'impossible semester plans'.
**Input:** prereqs: CS2 needs CS1; DS2 needs CS1 and Stats; AI needs CS2 and DS2**Output:** order: CS1, Stats, CS2, DS2, AI (or CS1, CS2, Stats, DS2, AI); cycle -> impossibleSolve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-095 — not embedded in this deck.
Rumour spreads in a social graph each hour from a set of starters; when does everyone know it? Multi-source BFS as simulation; ties to Lecture 29.
**Input:** starters {ada}; edges 1 hour each; graph of 6 friends**Output:** time until everyone knows = max BFS level over all reachable; disconnected -> 'never' for someSolve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-096 — not embedded in this deck.
Four study groups — G1, G2, G3, G4 — must book presentation slots. Some pairs share members and cannot present in the same slot:
**Input:** the five conflict pairs**Output:** minimum 3 slots — e.g. slot 1: G1, G4; slot 2: G2;slot 3: G3 — plus the lower-bound argumentSolve for the worked instance first. Then ask what would change if onePseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-136 — not embedded in this deck.