Search

Search papers and lessons

Link Prediction

Level 2 · 9 min read

Link prediction scores every pair of nodes that are not yet joined by how likely they are to be joined, usually from the neighbours they share, so the highest scores suggest the missing edges.

On this page

  • Colour = topic mix
  • Discrete math
  • Linear algebra
  • Probability
  • Calculus
  • Applied ML
  • Learning basics
  • ReadReady to readNot yet

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.

  1. Write each person's neighbours. N(A)={B,C,D}N(A) = \{B, C, D\}, N(B)={A,C}N(B) = \{A, C\}, N(C)={A,B,D}N(C) = \{A, B, D\}, N(D)={A,C,E}N(D) = \{A, C, E\}, N(E)={D}N(E) = \{D\}. Check: the degrees add to 3+2+3+3+1=123 + 2 + 3 + 3 + 1 = 12, which is twice the six edges.

  2. Find the pair to score. B and D are not joined. Look at their neighbour sets, N(B)={A,C}N(B) = \{A, C\} and N(D)={A,C,E}N(D) = \{A, C, E\}. Check: B and D are not in each other's lists, so the pair is a candidate.

  3. Count the shared neighbours. The intersection is {A,C}\{A, C\}, which has 2 members. Check: A and C are each friends with both B and D.

  4. Count everyone who knows either. The union is {A,C,E}\{A, C, E\}, which has 3 members. Check: E is included because D knows E, even though B does not.

  5. Divide. The score is the Jaccard index, 2/3≈0.672 / 3 \approx 0.67. 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 {A,C}\{A, C\} and {D}\{D\}, with no overlap, so the score is 00. 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

Definition: Link prediction score

Let G=(V,E)G = (V, E) be an undirected graph and N(u)N(u) the set of neighbours of node uu. A link prediction score is a function s(u,v)s(u, v) defined for pairs u≠vu \neq v that are not in EE. The predicted links are the pairs with the largest values of ss. A common heuristic is the Jaccard score

s(u,v)=∣N(u)∩N(v)∣∣N(u)∪N(v)∣,s(u, v) = \frac{|N(u) \cap N(v)|}{|N(u) \cup N(v)|},

where ∣S∣|S| is the number of members of a set SS.

Step 1 in symbols: the neighbour sets

The adjacency matrix AA has entry Auv=1A_{uv} = 1 when uu and vv are joined, and 00 otherwise. The neighbour set is N(u)={v:Auv=1}N(u) = \{v : A_{uv} = 1\}. In the walkthrough, row B of AA has ones in columns A and C, which is N(B)={A,C}N(B) = \{A, C\}.

Step 2 in symbols: the shared neighbours

The number of shared neighbours of uu and vv is ∣N(u)∩N(v)∣|N(u) \cap N(v)|. It equals (A2)uv=∑kAukAkv(A^2)_{uv} = \sum_k A_{uk} A_{kv}, the entry of the matrix product A2A^2, because each term is 1 exactly when kk is a neighbour of both. Check: (A2)BD=2(A^2)_{BD} = 2, which is the size of {A,C}\{A, C\}.

Step 3 in symbols: the union and the ratio

Inclusion and exclusion for unions and intersections gives ∣N(u)∪N(v)∣=∣N(u)∣+∣N(v)∣−∣N(u)∩N(v)∣|N(u) \cup N(v)| = |N(u)| + |N(v)| - |N(u) \cap N(v)|. For B and D, this is 2+3−2=32 + 3 - 2 = 3. The Jaccard score is therefore 2/32/3, as in the walkthrough.

A variant: the cosine score

The Cosine Similarity score uses the rows of AA as vectors: s(u,v)=Au⋅Av∥Au∥ ∥Av∥s(u, v) = \dfrac{A_u \cdot A_v}{\|A_u\|\,\|A_v\|}. The dot product Au⋅AvA_u \cdot A_v equals the count of shared neighbours, and the lengths ∥Au∥=∣N(u)∣\|A_u\| = \sqrt{|N(u)|} so the score is ∣N(u)∩N(v)∣/∣N(u)∣ ∣N(v)∣|N(u) \cap N(v)| / \sqrt{|N(u)|\,|N(v)|}. For B and D this is 2/2×3≈0.822 / \sqrt{2 \times 3} \approx 0.82, a slightly different ranking tool with the same idea.

Worked examples

Example
1. Three people in a line (tiny case)

Nodes 1, 2 and 3 with edges 1–2 and 2–3. Score the non-edge 1–3.

  1. Neighbour sets: N(1)={2}N(1) = \{2\} and N(3)={2}N(3) = \{2\}.
  2. Intersection: {2}\{2\}, size 1.
  3. Union: {2}\{2\}, size 1.
  4. Jaccard score: 1/1=11 / 1 = 1.

Answer: the score is 11, the highest possible. Nodes 1 and 3 share their only friend, so the model predicts an edge between them.

Example
2. Ranking every non-edge (standard case)

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.

  1. A–E: N(A)={B,C,D}N(A) = \{B, C, D\}, N(E)={D}N(E) = \{D\}. Intersection {D}\{D\}, size 1. Union {B,C,D}\{B, C, D\}, size 3. Score 1/3≈0.331/3 \approx 0.33.
  2. B–D: score 2/3≈0.672/3 \approx 0.67, from the walkthrough.
  3. B–E: N(B)={A,C}N(B) = \{A, C\}, N(E)={D}N(E) = \{D\}. No overlap, so the score is 00.
  4. C–E: N(C)={A,B,D}N(C) = \{A, B, D\}, N(E)={D}N(E) = \{D\}. Intersection {D}\{D\}, size 1. Union {A,B,D}\{A, B, D\}, size 3. Score 1/31/3.
  5. Sorted: B–D (0.670.67), then A–E and C–E (both 0.330.33), then B–E (00).

Answer: the model predicts B–D first. The two pairs with 1/31/3 are tied, and B–E is ranked last because the two people share nobody.

Example
3. Collaborators in a research network (realistic case)

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.

  1. Intersection: 4 shared collaborators.
  2. Union: 7+7−4=107 + 7 - 4 = 10 people who collaborate with either.
  3. Jaccard score: 4/10=0.44 / 10 = 0.4.
  4. Compare with a random pair of researchers: a pair with no shared collaborators scores 00, so 0.40.4 is a strong signal.

Answer: the score is 0.40.4, 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 3+3−1=53 + 3 - 1 = 5, so the score is 1/5=0.21 / 5 = 0.2.

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, (A2)uv(A^2)_{uv}.
  • 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, N(P)={Q,R,S}N(P) = \{Q, R, S\} and N(T)={R,S}N(T) = \{R, S\}. What is the Jaccard score for P and T?Show answer

Intersection {R,S}\{R, S\}, size 2. Union {Q,R,S}\{Q, R, S\}, size 3. Score 2/3≈0.672/3 \approx 0.67.

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 10+10−5=1510 + 10 - 5 = 15, so the score is 5/15=1/35/15 = 1/3. A score of 1 would mean the two researchers have exactly the same collaborators, which is not the case here.

Sources