TL;DR
Link prediction asks which pairs of nodes in a graph should be joined but are not, or will be joined later. It gives every non-adjacent pair a score, usually computed from the neighbours the two nodes share, and it ranks the pairs by that score. The highest-ranked pairs are the predicted missing edges.
Builds on: Graph, Adjacency Matrix, Jaccard index, Cosine Similarity · Leads to: node embeddings, graph neural networks, and knowledge graph completion
Where it's used
Friend suggestions. A social network suggests people you may know. Two users who share many friends but are not yet connected get a high score, and the app shows them as suggestions. Early research on this problem compared which simple scores predict new links, using co-authorship networks of scientists.
Knowledge base completion. A knowledge graph stores facts such as "Marie Curie won the Nobel Prize in Physics", as edges between entities. Most real knowledge graphs are incomplete, and link prediction proposes the facts that are probably true but missing. Systems use it to fill gaps in encyclopedic and product catalogues.
Recommendation and co-purchase. In a graph of customers and products, a customer who has not bought a product but whose neighbours have is a candidate pair. Scores of this kind are the first thing a recommender tries before learning anything more complicated.
How it works
Take five people, A to E, and six friendships: A is friends with B, C and D; B with C; C with D; and D with E. Ask whether B and D should be joined. Each step scores one non-edge.
-
Write each person's neighbours. , , , , . Check: the degrees add to , which is twice the six edges.
-
Find the pair to score. B and D are not joined. Look at their neighbour sets, and . Check: B and D are not in each other's lists, so the pair is a candidate.
-
Count the shared neighbours. The intersection is , which has 2 members. Check: A and C are each friends with both B and D.
-
Count everyone who knows either. The union is , which has 3 members. Check: E is included because D knows E, even though B does not.
-
Divide. The score is the Jaccard index, . Check: the score is high because most of the people connected to either of them are shared.
Do the same for the pair B and E. Their neighbour sets are and , with no overlap, so the score is . Ranking all four non-edges by score puts B–D first. The flow of the scoring is:
flowchart LR G["Graph of joined pairs"] --> N["Neighbour set of each node"] N --> S["Score each non-joined pair"] S --> R["Rank pairs by score"] R --> P["Top pairs are predicted links"]
The math
Definition
Let be an undirected graph and the set of neighbours of node . A link prediction score is a function defined for pairs that are not in . The predicted links are the pairs with the largest values of . A common heuristic is the Jaccard score
where is the number of members of a set .
Step 1 in symbols: the neighbour sets
The adjacency matrix has entry when and are joined, and otherwise. The neighbour set is . In the walkthrough, row B of has ones in columns A and C, which is .
Step 2 in symbols: the shared neighbours
The number of shared neighbours of and is . It equals , the entry of the matrix product , because each term is 1 exactly when is a neighbour of both. Check: , which is the size of .
Step 3 in symbols: the union and the ratio
Inclusion and exclusion for unions and intersections gives . For B and D, this is . The Jaccard score is therefore , as in the walkthrough.
A variant: the cosine score
The Cosine Similarity score uses the rows of as vectors: . The dot product equals the count of shared neighbours, and the lengths so the score is . For B and D this is , a slightly different ranking tool with the same idea.
Worked examples
Nodes 1, 2 and 3 with edges 1–2 and 2–3. Score the non-edge 1–3.
- Neighbour sets: and .
- Intersection: , size 1.
- Union: , size 1.
- Jaccard score: .
Answer: the score is , the highest possible. Nodes 1 and 3 share their only friend, so the model predicts an edge between them.
Use the five-person graph from the walkthrough. Its six edges are A–B, A–C, A–D, B–C, C–D and D–E. The four non-edges are A–E, B–D, B–E and C–E.
- A–E: , . Intersection , size 1. Union , size 3. Score .
- B–D: score , from the walkthrough.
- B–E: , . No overlap, so the score is .
- C–E: , . Intersection , size 1. Union , size 3. Score .
- Sorted: B–D (), then A–E and C–E (both ), then B–E ().
Answer: the model predicts B–D first. The two pairs with are tied, and B–E is ranked last because the two people share nobody.
In a co-authorship network, researcher X has 7 collaborators, and researcher Y has 7 collaborators. They share 4 of them. X and Y have not written a paper together.
- Intersection: 4 shared collaborators.
- Union: people who collaborate with either.
- Jaccard score: .
- Compare with a random pair of researchers: a pair with no shared collaborators scores , so is a strong signal.
Answer: the score is , and the model would rank the pair as a likely future collaboration.
Try it: Researchers Z and W have 3 collaborators each, and they share 1 of them. What is the Jaccard score for Z and W? Answer: the union is , so the score is .
Common mistakes and tradeoffs
It is tempting to think a high score means the link exists. In fact the score is a ranking, not a fact. The pairs at the top are candidates to check, and many will be false positives, because the graph shows only the edges that have already been observed. A top-ranked pair that is not a real link is a false positive.
It is tempting to think one heuristic works for every graph. In fact the Jaccard score divides by the size of the union, so a person with many friends gets a low score with almost everyone, even when they share several. The raw count of shared neighbours does the opposite and favours the best-connected nodes, which matters in graphs with a Power Law spread of degrees. Other heuristics, such as Adamic–Adar, weight each shared neighbour by how rare it is.
It is tempting to test by hiding a random sample of edges. In fact the hidden edges should be the ones that arrived later in time when that is possible, because a random split can leak structure that a real prediction would not have. The train, validation and test split should respect the order in which the edges appeared.
Heuristics like Jaccard are cheap, need no training and are easy to explain, so they are the right first tool. They run out of power when the signal lies in features that neighbourhoods do not show. Then node embeddings, graph neural networks and logistic-regression models over pair features can learn more, at the cost of training and tuning.
History
The Jaccard index dates to 1901, when the botanist Paul Jaccard used it to compare the plant species found in different regions. Link prediction as a problem on social networks was framed in the early 2000s. In 2001 Lada Adamic and Eytan Adar studied shared-neighbour measures on the web. In 2003 David Liben-Nowell and Jon Kleinberg tested many such scores on scientific collaboration networks and showed that simple neighbourhood measures already predict future collaborations well. The field later extended the same idea to knowledge graphs, where each edge carries a relation type.
Recap
- Link prediction scores each pair of nodes that are not joined and ranks them.
- Shared neighbours are the common signal, measured by the Jaccard index or by cosine similarity on adjacency rows.
- The adjacency matrix makes the shared-neighbour count a matrix product, .
- Evaluate with a split that respects time and treat the top scores as candidates, not facts.
- Read next: node embeddings for learned scores, and knowledge graph embeddings for facts with relation types.
Check yourself
QuestionIn one sentence, what does link prediction output for a graph?Show answer
A score for each pair of nodes that are not joined, which ranks the pairs so the highest ones are the predicted missing edges.
QuestionIn a graph, and . What is the Jaccard score for P and T?Show answer
Intersection , size 2. Union , size 3. Score .
QuestionWhy does a random split of edges into training and test sets make the evaluation too optimistic for link prediction in a growing network?Show answer
A random split lets the training graph contain edges that formed after some test edges, so the model sees the future. The test would then not match the real task, which is predicting edges that have not yet formed.
QuestionTwo researchers each have 10 collaborators and share 5. What is the Jaccard score, and what does it mean that the score is 1/3 rather than 1?Show answer
The union is , so the score is . A score of 1 would mean the two researchers have exactly the same collaborators, which is not the case here.