learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Algorithm Selection

Select an algorithm and generate steps to begin visualization

Node Similarity Visualizer

Two customers are alike when they buy the same things

Compares vertices by their neighbours, shared over all, and keeps the k most similar of each vertex: the basis of many recommendations

Time: O(V²·d)
Space: O(V·k)
Use Case: Recommendations, collaborative filtering, duplicate detection, graph databases
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Node Similarity (Jaccard)

Node similarity compares two vertices by the vertices they link to. The Jaccard score divides the neighbours they share by all the neighbours either of them has: 1 means exactly the same neighbours, 0 nothing in common. The overlap coefficient divides by the smaller neighbourhood instead, so a vertex whose few links all appear in a larger neighbourhood scores 1. On a directed graph the neighbours are the out-neighbours, the products a customer bought or the films a person watched, which makes the graph bipartite in spirit: people on one side, items on the other.

How it works

The visualizer takes each vertex that has links in turn, lists every other vertex that shares at least one neighbour with it and writes each score as a fraction, for instance Ben 2/3 = 0.67. Pairs with nothing in common are skipped, which is what keeps the method fast on large sparse graphs. Each vertex keeps its two most similar others, and these become new SIMILAR links, drawn dashed with their score. The panel lists every compared pair with Jaccard and overlap. In the example, five people and five items, Ann and Ben share two of three items, the most similar pair, so Game, which Ann has and Ben lacks, is the natural recommendation for Ben.

Applications

Node similarity is the core of collaborative filtering on a graph: customers who bought the same products, users who rated the same films, readers of the same papers. Neo4j Graph Data Science runs it as nodeSimilarity, with Jaccard, overlap or cosine, a topK and a cutoff, and writes the result back as SIMILAR relationships that later queries or community detection use. It also deduplicates records that point to the same addresses and phone numbers in fraud detection, groups genes by shared interactions and links documents that cite the same sources. For millions of vertices, approximate k-nearest-neighbour methods replace the exact comparison.

Pseudocode

Compare only the pairs that share a neighbour, then keep each vertex's top k.

NodeSimilarity(G, k):
    for each vertex a with outgoing links:
        for each vertex b sharing a neighbour with a:
            J(a, b) = |N(a) ∩ N(b)| / |N(a) ∪ N(b)|
            O(a, b) = |N(a) ∩ N(b)| / min(|N(a)|, |N(b)|)
        keep the k largest J(a, b) as SIMILAR links
    return the SIMILAR links

The pairs sharing a neighbour are found through the neighbours themselves: for each item, every pair of vertices linked to it. Pairs with nothing in common score 0 and are never touched, which is what makes the method practical on large sparse graphs.

Worked example, step by step

Five people and the items they have, loaded on this page, with k = 2.

Example graph: Ann → Book, Film, Game. Ben → Book, Film. Cat → Film, Game, Tea. Dan → Tea, Mug. Eve → Game, Tea, Mug.

  1. Ann. Ben shares Book and Film of 3 items in all: 2/3 = 0.67. Cat shares Film and Game of 4: 0.5. Eve shares Game of 5: 0.2. Ann keeps Ben and Cat.
  2. Ben. Ann 2/3 = 0.67 and Cat 1/4 = 0.25, Film being the only item Ben and Cat share. Ben keeps both.
  3. Cat. Ann 2/4 and Eve 2/4 tie at 0.5, ahead of Ben and Dan at 0.25. Cat keeps Ann and Eve.
  4. Dan. Eve 2/3 = 0.67 through Tea and Mug, and Cat 1/4 = 0.25 through Tea. Dan keeps both.
  5. Eve. Dan 0.67, Cat 0.5 and Ann 0.2. Eve keeps Dan and Cat.

6 SIMILAR links: Ann – Ben and Dan – Eve at 0.67, Ann – Cat and Cat – Eve at 0.5, Ben – Cat and Dan – Cat at 0.25. Ben is most like Ann, so Game, which Ann has and Ben lacks, is the first recommendation for Ben.

Complexity, and where it comes from

Time: O(Σ deg(z)²) over the items z · Space: O(n + m + number of pairs)

Every pair that shares an item z is generated once per shared item, so the work is the sum over the items of their degree squared. Popular items dominate: an item held by 10,000 people creates 50 million pairs. Libraries therefore cap the degree, sample, or switch to an approximate k-nearest-neighbour search, which compares each vertex with a few candidates and improves the list in rounds.

When to use Node Similarity (Jaccard), and when not to

Similarity scores and methods.

AlternativePrefer it whenCost
Overlap coefficientBeing a subset should count as fully similar. Ben's two items are both among Ann's three, so overlap gives 1 where Jaccard gives 0.67.same as Jaccard
Cosine similarityThe links carry weights, such as ratings or counts. With 0 or 1 weights it equals |N(a) ∩ N(b)| / √(|N(a)| · |N(b)|).same as Jaccard
Approximate k-nearest neighbours on embeddingsMillions of vertices, or similarity beyond direct neighbours. Embed the vertices, for instance with node2vec or FastRP, then search for close vectors.near-linear

Common pitfalls

  • Letting popular items decide. Sharing a bestseller says little, sharing a rare item says a lot, but Jaccard counts both as 1. Weight the items, as Adamic–Adar does in link prediction, or remove items that almost everyone has.
  • Comparing the wrong side. On a directed graph only vertices with outgoing links are compared. To find similar items instead of similar people, reverse the edges. The method itself does not care which side is which.
  • Reading too much into tiny sets. Two people with one item each, the same one, score 1. Set a minimum number of shared neighbours or a degree cutoff before trusting a perfect score.

Frequently asked questions

How do the SIMILAR links give recommendations?
For a person, take the items that their most similar people have and they do not, weighted by the similarity. Here Ben is most like Ann, so Game is recommended, and Cat adds Tea and Game with a lower weight. This is user-based collaborative filtering.
Why keep only the top k?
The full list of similar pairs can be quadratic in size. Keeping the k most similar per vertex gives a sparse graph that is cheap to store and query, and enough for recommendations or for community detection on the similarity graph.
Is the result symmetric?
The score is, J(a, b) = J(b, a). The top-k lists are not: Cat is in Ben's list, but Ben is not in Cat's. Tools usually write the links in both directions or keep a link when either side lists it, as this page does.

Related algorithms: Link Prediction, Hopcroft-Karp Matching, Bipartite Check, Clustering Coefficient, Louvain Community Detection

Built by Mohammed Islam HadjoudjWork with me

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

Zoom Controls

40%
Nodes: 10
Edges: 13