learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Algorithm Selection

Select an algorithm and generate steps to begin visualization

Link Prediction Visualizer

Which friendships are about to form?

Scores every unlinked pair by its neighbourhoods: common neighbours, Jaccard, Adamic–Adar, resource allocation and preferential attachment

Time: O(V²·d)
Space: O(V²)
Use Case: "People you may know", protein and drug interactions, completing knowledge graphs
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Link Prediction

Link prediction estimates which pairs of vertices that are not linked yet are likely to become linked. The classic scores look at the neighbourhoods of the pair: common neighbours counts the friends they share, Jaccard divides that by all their friends, Adamic–Adar adds 1 / ln k for each shared friend of degree k, resource allocation adds 1 / k, and preferential attachment multiplies the two degrees. Liben-Nowell and Kleinberg compared these scores on co-authorship networks in 2007 and found that the simple neighbourhood scores, Adamic–Adar above all, predict new links far better than chance.

How it works

The visualizer lists every pair of vertices without an edge and how many share at least one neighbour, then scores the best pairs one by one. The pair is drawn large, its common neighbours green with the paths through them, and every vertex shows its degree as a badge, because the scores weigh the shared friends by how many links they have. A shared friend who knows few people says much more than one who knows everybody. In the example X and Y share two quiet friends and score 2.89, while A and B share three friends including the hub H of degree 6 and score only 2.38. Preferential attachment, which ignores shared friends, favours the hub pair H and X. At the end the three best pairs by Adamic–Adar are drawn as dashed predicted links.

Applications

Link prediction is behind "people you may know" in social networks, product and content recommendations, and the search for missing links in incomplete data: protein interactions not yet tested, possible drug targets, collaborators who have not met. In knowledge graphs it fills in missing facts. Neo4j Graph Data Science offers the neighbourhood scores as functions and full link prediction pipelines that feed them, with node embeddings, into a classifier. To judge any score, hide a share of the known links and check how high the hidden ones rank. Graph neural networks learn such scores instead of fixing them by hand.

Pseudocode

Five neighbourhood scores for every pair that is not linked.

LinkPrediction(G):
    for each pair a, b that is not linked:
        Z = common neighbours of a and b
        CN = |Z|,  Jaccard = |Z| / |Γ(a) ∪ Γ(b)|
        AA = sum of 1 / ln k(z)  over z in Z
        RA = sum of 1 / k(z)     over z in Z
        PA = k(a) * k(b)
    rank the pairs by AA
    return the top pairs as predicted links

k(z) is the degree of z and Γ(a) the neighbours of a. A common neighbour always has degree at least 2, so ln k(z) is never 0. Adamic–Adar and resource allocation give a rare common friend more weight than a busy one, while preferential attachment ignores common friends entirely.

Worked example, step by step

A small social network with a hub, loaded on this page.

Example graph: H knows A, B, C, D, E and F. A and B both know C and D. E knows X. X and Y both know M and N. 11 vertices, 15 edges.

  1. Counting the pairs. 11 vertices make 55 pairs, 15 are linked, so 40 are candidates. Only 16 of them share a neighbour. The other 24 score 0 on every local score except preferential attachment.
  2. X – Y. They share M and N, each of degree 2. AA = 1/ln 2 + 1/ln 2 = 2.89, the best. Jaccard 2/3 = 0.67, resource allocation 1/2 + 1/2 = 1, preferential attachment 3 × 2 = 6.
  3. A – B. They share C, D and H, three friends, more than X and Y. But H has 6 links: AA = 1/ln 3 + 1/ln 3 + 1/ln 6 = 2.38. Jaccard is 1, as their friends are identical, and resource allocation 0.83.
  4. C – D. The mirror image: common neighbours A, B and H, AA 2.38.
  5. Preferential attachment. It favours H – X, 6 × 3 = 18, with only one common friend, E. It ranks by how busy the two vertices are, which suits growing networks such as citations better than friendships.

Predicted links by Adamic–Adar: X – Y (2.89), A – B (2.38) and C – D (2.38). Common neighbours and Jaccard would rank A – B first, so the choice of score matters.

Complexity, and where it comes from

Time: O(Σ deg(z)²) for the pairs at distance 2 · Space: O(n + m + candidates)

Every local score except preferential attachment is 0 unless the pair shares a neighbour, so only pairs at distance 2 need scoring. They are generated by taking, for each vertex z, every pair of its neighbours, which costs the sum of the squared degrees. Scoring all n² pairs, as this page does to count them, is only feasible on small graphs.

When to use Link Prediction, and when not to

Beyond the local neighbourhood scores.

AlternativePrefer it whenCost
Katz index or personalised PageRankLinks between vertices more than two steps apart should be predicted too. They count longer walks, damped by their length.O(m) per source and iteration
Embeddings and a classifierThere is labelled history to learn from. Node embeddings and the scores on this page become features of a model trained on past links.training plus O(d) per pair
Graph neural networks (SEAL and others)Large training data and the need for the best accuracy. The network learns its own score from the subgraph round each pair.GPU training

Common pitfalls

  • Leaking the answer in evaluation. When hiding links to test a score, compute the scores on the graph without them. Computing them on the full graph lets every hidden link count itself and makes any score look excellent.
  • Judging by accuracy. Unlinked pairs outnumber future links by thousands to one, so predicting no link at all is almost always right. Measure precision among the top predictions or the area under the precision-recall curve instead.
  • Expecting local scores to reach far. Two vertices with no common neighbour score 0 on common neighbours, Jaccard, Adamic–Adar and resource allocation. For links across communities use a global score such as Katz or an embedding.

Frequently asked questions

Why does Adamic–Adar use a logarithm?
It damps the degree slowly. A common friend of degree 2 counts 1.44, one of degree 6 counts 0.56 and one of degree 1,000 still 0.14. Resource allocation uses 1 / k, which punishes hubs much harder: 0.5, 0.17 and 0.001.
Which score works best?
It depends on the network. In comparisons on social and biological networks, resource allocation and Adamic–Adar are usually among the best local scores, common neighbours is close behind, and preferential attachment does well mainly on growing networks such as citations. Test on hidden links from your own data.
How is this used in practice?
A recommender computes the scores for the pairs near each user, ranks them and shows the top ones, for example as people you may know. In graph databases the scores often become features of a classifier together with embeddings, as in Neo4j's link prediction pipelines.

Related algorithms: Node Similarity (Jaccard), Clustering Coefficient, Katz Centrality, Louvain Community Detection, Leiden Community Detection

Built by Mohammed Islam HadjoudjWork with me

An example graph: you can change it, or start blank.

Zoom Controls

30%
Nodes: 11
Edges: 15