TL;DR
A knowledge graph stores facts about things as a graph. Each thing is a node, such as a person, a city or a product, and each fact is an edge with a name, such as "lives in". Facts written as free text or as separate tables are hard to combine; a graph lets a program start at one thing and follow its links to other facts. Because every answer is built from named facts, the answer can point back to the facts it used.
Builds on: Graph, First-Order Logic · Leads to: GraphRAG
Where it's used
Search engines. In May 2012 Google announced its Knowledge Graph, a store of facts about people, places and things. The information panel that appears beside some search results, with a city's population or a film's director, is drawn from a graph of this kind rather than from a single web page.
Shopping catalogues. A product catalogue links each item to its brand, category, material and the activities it suits. A question such as "show me the trail shoes made by this brand" becomes a walk along a few named edges instead of a search through free-text descriptions.
Biology research. Research databases link genes, proteins and diseases, so a scientist can ask which proteins are linked to a given disease through a chain of known interactions. The graph shows the chain; it does not say whether any link is true in every case.
Question answering with language models. A system finds the triples about the entities mentioned in a question and gives them to a language model as context. The model's answer can then cite specific facts, which is easier to check than an answer produced from memory alone. This is the idea behind retrieval-augmented generation applied to a graph.
How it works
Take a tiny Graph with three facts: Ana plays piano, Ben plays piano, and Ben lives in Leeds. The question is: who plays piano and lives in Leeds?
- Write each fact as a triple. A triple has a subject, a relation and an object: (Ana, plays, piano). Subject and object are nodes; the relation is the label on the edge between them. Check: three facts give three triples.
- Draw the triples as a graph. The nodes are Ana, Ben, piano and Leeds. The edges are "plays" from Ana to piano, "plays" from Ben to piano, and "lives in" from Ben to Leeds. Check: 4 nodes and 3 edges.
- Turn the question into two patterns. Pattern A is "?x plays piano". Pattern B is "?x lives in Leeds". The symbol ?x is a blank to be filled by a node. Check: each pattern is one edge with one blank.
- Match pattern A. The edges into piano come from Ana and Ben, so the candidates are Ana and Ben. Check: two candidates.
- Match pattern B on the candidates. Ana has no "lives in" edge. Ben has "lives in" to Leeds, so Ben stays. Check: one candidate left.
- Output the answer with its evidence. The answer is Ben, supported by the two triples (Ben, plays, piano) and (Ben, lives in, Leeds).
flowchart LR Ana -- plays --> piano Ben -- plays --> piano Ben -- lives in --> Leeds
The math
A knowledge graph is a set of triples , where and are entities (the nodes) and is a relation name (a labelled, directed edge from to ). The entities form a set and the relation names a set , so , a subset of the Cartesian product of the three sets. Each such set of triples is a relation between entities.
Step 1 in symbols: the triples
For the tiny graph, and . The set of facts is
The graph holds 3 of the triples that could be formed from these sets. Most possible triples are simply not facts in the graph.
Step 2 in symbols: a pattern is a set
Fix a relation and an object . The set of subjects that match is
For the tiny graph, and .
Step 3 in symbols: the answer is an intersection
A question with two patterns asks for the subjects that satisfy both. The intersection of the two sets is
With adjacency matrices (one per relation, with rows and columns for the nodes), each set can be written as a 0/1 vector with one entry per node, equal to 1 when that node is in the set. The intersection then becomes an elementwise product. For the order (Ana, Ben, piano, Leeds), the vectors are and , and their elementwise product is , which is Ben.
Worked examples
- The graph has one triple: (Paris, capital of, France). Nodes: Paris and France. Edge: "capital of", from Paris to France.
- The question is "what is the capital of France?" Look for a triple whose relation is "capital of" and whose object is France.
- The triple (Paris, capital of, France) matches. The subject is Paris.
- Answer: Paris.
- Ask about Germany instead. No triple has "capital of" with Germany as object, so the graph gives no answer. It does not give a wrong one.
The graph holds six triples: (Ana, plays, piano), (Ben, plays, piano), (Dev, plays, piano), (Ben, lives in, Leeds), (Cara, lives in, Leeds), (Cara, plays, violin).
The question is: who plays piano and lives in Leeds?
- Find the piano players: the subjects of "plays piano" are Ana, Ben and Dev. That is 3 people.
- Find the Leeds residents: the subjects of "lives in Leeds" are Ben and Cara. That is 2 people.
- Intersect the two sets. Ben is in both. Ana and Dev are not in the second set, and Cara is not in the first.
- Count the result: 1 person.
- Answer: Ben.
This catalogue is invented for the example. The triples are: (Trail Runner X, made by, Northwind), (Trail Runner X, suited to, trail running), (Road Lite 2, made by, Northwind), (Road Lite 2, suited to, road running), (Summit Pack 20, made by, Northwind), (Summit Pack 20, suited to, trail running), (Gel Flask, made by, Summit Labs), (Gel Flask, suited to, trail running).
The question is: which products are made by Northwind and suited to trail running?
- Products made by Northwind: Trail Runner X, Road Lite 2 and Summit Pack 20. That is 3 products.
- Products suited to trail running: Trail Runner X, Summit Pack 20 and Gel Flask. That is 3 products.
- Intersect the two sets: Trail Runner X and Summit Pack 20. That is 2 products.
- Road Lite 2 is excluded because it is suited to road running. Gel Flask is excluded because Summit Labs made it.
- Answer: Trail Runner X and Summit Pack 20.
Try it: suppose the triple (Gel Flask, made by, Northwind) is added to the catalogue. Which products now match? Answer: Trail Runner X, Summit Pack 20 and Gel Flask, which is now made by Northwind and suited to trail running.
Common mistakes and tradeoffs
It is tempting to think a knowledge graph is a list of true facts about the world. In fact it holds whatever its builders extracted or entered, and some of it is wrong or in conflict with other entries. This is why good graphs record a source for each triple, so a reader can check where a fact came from.
It is tempting to think a missing edge means "no". In fact it means "not recorded". If the graph has no (Ana, lives in, Leeds) triple, the system does not know that Ana lives elsewhere. A system that treats missing triples as false will give confident wrong answers on an incomplete graph.
It is tempting to think a graph replaces free text search. In fact it works best when the question follows links: who is connected to whom, or what is connected to what through several steps. It is weaker for fuzzy similarity, where word embeddings that place similar meanings close together do better, and for measurements over many rows, where a table is simpler.
When to use one: facts are naturally entities and named links, questions follow those links, and facts are added or corrected one at a time. When not to use one: the data is one table with a fixed set of columns, or the relation names change so often that the graph has no stable meaning. Alternatives include relational tables with joins for fixed queries, and a vector index over text passages when you only need the passages that look similar to the question.
History
In the 1960s, researchers in psychology and artificial intelligence used networks of labelled concepts to model human memory. These were called semantic networks. Their graphs were small and hand-built, and the links were not checked against any shared standard.
In 2001, Tim Berners-Lee and colleagues described a "Semantic Web" in which web pages would carry machine-readable facts, so that programs could combine them across sites. Much of the standard machinery for this was developed in the following decade.
In May 2012, Google announced its Knowledge Graph for search. This was one of the first large, commercial uses of the idea, and it made the term familiar outside research. Since then, knowledge graphs have spread into product catalogues, research databases and question answering, and they are now often combined with language models.
Recap
- A knowledge graph stores facts as triples: subject, relation, object.
- Entities are nodes and relations are labelled edges, so the whole store is a graph.
- A question with several conditions becomes several patterns, and the answer is the intersection of their matches.
- A missing triple means the fact is not recorded, not that it is false.
- Read next: GraphRAG, which combines a knowledge graph with retrieval over text, and Adjacency Matrix, which shows the same graph as a matrix.
Check yourself
QuestionWhat are the three parts of a triple, and which part labels an edge?Show answer
The three parts are the subject, the relation and the object. The relation is the label on the edge that runs from the subject to the object.
QuestionIn a graph whose entities are Ana, Ben, piano and Leeds, and whose relations are plays and lives in, how many distinct triples are possible?Show answer
There are 4 choices for the subject, 2 for the relation and 4 for the object, so triples are possible. A given graph holds only some of them.
QuestionWhy does a triple that is missing from the graph not prove that the fact is false?Show answer
The graph records only what its builders entered or extracted. A missing triple means the fact is not recorded, which may be because the data is incomplete. Treating missing triples as false would give confident wrong answers.
QuestionA shop wants the products that are made by one brand and also rated for wet weather. Which patterns does the query need, and how are the results combined?Show answer
It needs two patterns: "made by the brand" and "rated for wet weather". Each pattern gives a set of products, and the answer is the intersection of the two sets.
Sources
- Wikipedia, "Knowledge graph": https://en.wikipedia.org/wiki/Knowledge_graph
- Hogan et al., "Knowledge Graphs" (survey): https://arxiv.org/abs/2003.02320