Lesson 25.1 · 1 of 3 · 4 min read · Reviewed 24 Nov 2026
Knowledge graphs
By the end, you can
- Explain knowledge graphs.
- See what graphs capture that chunks don't.
Nodes and edges
(Team Atlas) --OWNS--> (Billing service) --DEPENDS_ON--> (Auth service)
| ^
LED_BY |
v (Search service) --DEPENDS_ON--+
(R. Mensah)
Each node has a type and properties; each edge has a type and direction.
What graphs are good at
| Question | Graph query |
|---|---|
| What depends on Auth? | Follow DEPENDS_ON edges into Auth |
| Who is affected if Team Atlas reorganises? | Traverse from the team outward |
| How are these two customers connected? | Shortest path between nodes |
| Which products use this supplier’s part? | Two-hop traversal |
What chunks are good at
Explanations, nuance, procedures, anything that reads as prose. A graph says Billing depends on Auth; a document explains why and what breaks.
Graphs and RAG together
GraphRAG uses a graph built from documents to guide retrieval or summarise across them (25.2). Many systems mix both: graph traversal for structure, chunkChunking: Splitting documents into smaller passages before embedding and indexing them. retrieval for detail.
Key points
- A knowledge graph stores entities (people, products, teams) as nodes and relationships as edges.
- Graphs answer relationship questions directly: who reports to whom, what depends on what.
- Text chunks hold facts in prose; graphs hold the connections explicitly.
Query a small knowledge graph
Answer relationship questions by following typed edges, including a two-hop question and a shortest path.
You will have: Three graph query functions and their answers for the sample graph.
Steps
- Read the edge list: (from, type, to) triples about teams, services and leads.
- Write incoming(node, type): every node with an edge of that type into node.
- Answer 'what depends on Auth, directly or indirectly?' by following DEPENDS_ON edges backwards.
- Answer 'who leads the team that owns Billing?' in two hops, then find the shortest path between Reports and J. Osei with breadth-first search.
Done when
- Direct dependents of Auth are Billing and Search.
- Everything affected if Auth fails includes Reports.
- The lead of Billing's owner is R. Mensah.
Open in the Playground No API key needed.
Starter code (Python)
EDGES = [
("Team Atlas", "OWNS", "Billing"), ("Team Atlas", "LED_BY", "R. Mensah"),
("Billing", "DEPENDS_ON", "Auth"), ("Search", "DEPENDS_ON", "Auth"),
("Reports", "DEPENDS_ON", "Billing"), ("Team Nova", "OWNS", "Search"),
("Team Nova", "LED_BY", "J. Osei"),
]
def incoming(node, etype):
# TODO: return sorted sources of edges (src, etype, node)
return []
def affected(node):
# TODO: everything that depends on node directly or through other services
return set()
def lead_of_owner(service):
# TODO: find the team that OWNS service, then who that team is LED_BY
return None
print("depends on Auth:", incoming("Auth", "DEPENDS_ON"))
print("affected if Auth fails:", sorted(affected("Auth")))
print("lead of Billing's owner:", lead_of_owner("Billing"))
Show a solution
EDGES = [
("Team Atlas", "OWNS", "Billing"), ("Team Atlas", "LED_BY", "R. Mensah"),
("Billing", "DEPENDS_ON", "Auth"), ("Search", "DEPENDS_ON", "Auth"),
("Reports", "DEPENDS_ON", "Billing"), ("Team Nova", "OWNS", "Search"),
("Team Nova", "LED_BY", "J. Osei"),
]
from collections import deque
def incoming(node, etype):
return sorted(s for s, t, d in EDGES if t == etype and d == node)
def affected(node):
seen, todo = set(), [node]
while todo:
for s in incoming(todo.pop(), "DEPENDS_ON"):
if s not in seen:
seen.add(s); todo.append(s)
return seen
def lead_of_owner(service):
owners = incoming(service, "OWNS")
leads = [d for s, t, d in EDGES if t == "LED_BY" and s in owners]
return leads[0] if leads else None
def shortest_path(a, b):
nbrs = {}
for s, t, d in EDGES:
nbrs.setdefault(s, []).append(d); nbrs.setdefault(d, []).append(s)
q, prev = deque([a]), {a: None}
while q:
n = q.popleft()
if n == b:
path = []
while n is not None:
path.append(n); n = prev[n]
return path[::-1]
for m in nbrs.get(n, []):
if m not in prev:
prev[m] = n; q.append(m)
return None
print("depends on Auth:", incoming("Auth", "DEPENDS_ON"))
print("affected if Auth fails:", sorted(affected("Auth")))
print("lead of Billing's owner:", lead_of_owner("Billing"))
print("path Reports to J. Osei:", " -> ".join(shortest_path("Reports", "J. Osei")))