Discrete Math & Graphs — Cheat Sheet
Math for AI · 2 topics. Download the PDF or the Instagram carousel and share it.
Cheat Sheet · AiCanCode.org
Discrete Math & Graphs
Math for AI2 topicsQuick revision reference
1
Sets, Logic & Combinatorics
Sets group distinct items, logic combines true/false conditions, and combinatorics counts possibilities — the discrete foundations behind algorithms, feature engineering, and reasoning systems.
- ✓Set operations (∪, ∩, −) power dedup, vocabularies, and Jaccard similarity
- ✓Boolean logic underlies rule systems, decision-tree splits, and guardrail conditions
- ✓Combinatorics counts arrangements (permutations) and selections (combinations)
- ✓Factorial growth explains the combinatorial explosion that motivates heuristics & search
Jaccard = |A ∩ B| / |A ∪ B| — set similarity
a = {"nlp", "python", "ml", "data"}
b = {"python", "ml", "cloud"}
print(a & b) # {'ml','python'} intersection
print(a | b) # union
jaccard = len(a & b) / len(a | b)
print(round(jaccard, 3)) # 0.4 -> set-based similarity2
Graphs, Trees & Traversal (BFS/DFS)
Graphs model entities and their relationships; traversals like BFS and DFS explore them — the structures behind knowledge graphs, recommendation engines, and graph neural networks.
- ✓Graphs = nodes + edges; the model for relational data (social, knowledge, recommenders)
- ✓BFS explores level by level (shortest paths); DFS goes deep (reachability, cycles)
- ✓DAGs model dependencies — including the autograd computation graph
- ✓Graph Neural Networks let nodes aggregate neighbour info — fraud, molecules, recommendations
BFS with a queue — shortest paths in unweighted graphs
from collections import deque
graph = {
"A": ["B", "C"], "B": ["A", "D"],
"C": ["A", "D"], "D": ["B", "C", "E"], "E": ["D"],
}
def bfs(start):
seen, order, q = {start}, [], deque([start])
while q:
node = q.popleft()
order.append(node)
for nb in graph[node]:
if nb not in seen:
seen.add(nb); q.append(nb)
return order
print(bfs("A")) # ['A','B','C','D','E'] level by levelLearn this free with Aria, your AI tutor → AiCanCode.org/learn/math-for-ai