Discrete mathematics: graphs and relations
Be able to describe graphs with nodes and edges, and understand directed graphs and cycles — the model behind the knowledge graph itself.
Prerequisites
- BAlgorithmic thinkingrequired
Intuition
A graph is nodes (points) and edges (lines between them). It is one of the most useful models there is: friendships, roads, web links, dependencies between tasks — and this platform's knowledge map.
- Undirected: the edge goes both ways (friendship).
- Directed: the edge has a direction (A is a prerequisite for B).
- A cycle: a path that comes back to itself.
- A DAG (directed acyclic graph): directed without cycles — then it can be topologically sorted, that is, put in an order where every node comes after everything it depends on.
AI-grafen has to be a DAG. A cycle would mean «A requires B which requires A» — impossible to learn in any order.
Code
graph = {"vectors": [], "matrices": ["vectors"],
"neuralnet": ["matrices", "derivative"], "derivative": [],
"backprop": ["neuralnet"]} # node -> prerequisites
def topological(g):
visited, order, in_progress = set(), [], set()
def visit(n):
if n in visited: return
if n in in_progress: raise ValueError(f"a cycle at {n}")
in_progress.add(n)
for f in g[n]:
visit(f)
in_progress.discard(n); visited.add(n); order.append(n)
for n in g: visit(n)
return order
print(topological(graph))
# ['vectors', 'matrices', 'derivative', 'neuralnet', 'backprop']
Every prerequisite comes before the node that requires it. That is exactly how the learning paths in the platform are built — and the in_progress set is the cycle check that the graph invariants run at every import.
Mastery means
- Describes a graph with nodes and edges
- Tells a directed graph from an undirected one and detects cycles
Sign in to do the exercises and build your mastery up.
Sources
- Wikipedia — Graf (matematik) (CC BY-SA 4.0) — CC BY-SA 4.0
- Wikipedia — Topologisk sortering (CC BY-SA 4.0) — CC BY-SA 4.0