node2vec: Scalable Feature Learning for Networks

node2vec banner Kaggle

Summary

node2vec learns a vector for every node in a network so that nodes with similar neighbourhoods get similar vectors. Like DeepWalk, it samples random walks and trains a Skip-gram model on them. The new part is a biased second-order walk with two parameters, p and q, that move the walk between breadth-first (BFS-like) and depth-first (DFS-like) exploration (Abstract, Section 3.2).

The paper reports that node2vec beats state-of-the-art methods by up to 26.7% on multi-label classification and up to 12.6% on link prediction (Section 1). DeepWalk’s sampling is the special case p = 1 and q = 1 (Section 4.2).

What Problem Does It Solve

Think of two kinds of “similar” people at a company. Two people on the same team are similar because they work together. Two team leads on different floors are similar because they have the same role, even if they never meet. A good description of a network should be able to capture both.

The paper calls these homophily (nodes in the same community) and structural equivalence (nodes with the same role, such as hubs). Earlier walk-based methods used one fixed way of sampling neighbourhoods. node2vec makes the sampling tunable, so one method can lean towards either kind of similarity (Sections 1 and 3.1, Figure 1).

Core Idea

  • Objective: maximise the log-probability of each node’s sampled neighbourhood N_S(u) given its vector f(u), as in Skip-gram; the normaliser is approximated with negative sampling (Section 3, Eqs. 1 and 2).
  • Second-order walk: after moving from t to v, the walk picks the next node x with weight α: 1/p if x = t (go back), 1 if x is a neighbour of t, and 1/q otherwise (Section 3.2.2).
  • Return parameter p: a high p makes going straight back less likely; a low p keeps the walk local (Section 3.2.2).
  • In-out parameter q: q > 1 keeps the walk near t (BFS-like); q < 1 pushes it outward (DFS-like) (Section 3.2.2).

Key Method Details

  • Algorithm 1: precompute the transition probabilities, simulate r walks of length l from every node, then optimise with SGD (Section 3.2.3).
  • Alias sampling: after preprocessing, each walk step is sampled in O(1) time (Section 3.2).
  • Settings: d = 128, r = 10, l = 80, context size k = 10, one epoch, 10 random seeds. p and q are chosen by a grid search over {0.25, 0.5, 1, 2, 4} with 10-fold cross-validation on 10% labelled data (Section 4.2).
  • Case study: on the Les Misérables network (77 nodes, 254 edges) with d = 16, p = 1 and q = 0.5 gave clusters that match communities, and p = 1 and q = 2 gave clusters that match structural roles, such as bridge characters (Section 4.1, Figure 3).
  • Edge features: node vectors are combined with operators such as the Hadamard product for link prediction (Section 3.3).

Simplifications in This Notebook

  • Graph: a synthetic graph with 60 nodes in 3 communities joined by 4 bridge edges, instead of Les Misérables or the paper’s benchmark networks.
  • Walks and training: 10 walks of length 10 per node, window 3, 16 dimensions, 5 epochs, 5 negatives drawn uniformly (not from the unigram distribution).
  • Evaluation: only community recovery (K-Means NMI and silhouette), not multi-label classification, link prediction or structural roles.

What the Notebook Result Shows

  • Communities recovered with both settings: K-Means matched the 3 communities exactly (NMI 1.0000) with q = 0.5 and with q = 2.
  • Same over 5 seeds: over seeds 0 to 4, both settings reached NMI 1.000 every time, so NMI does not separate them on this graph.
  • Small silhouette difference: the true communities were slightly better separated with q = 0.5 (silhouette 0.361 ± 0.003) than with q = 2 (0.315 ± 0.004), in the same direction as the paper’s case study.
  • Structural roles not tested: the notebook does not measure whether q = 2 groups nodes by role.

Influence

The authors released a reference implementation (aditya-grover/node2vec) and a project page. PyTorch Geometric includes a Node2Vec model. OpenAlex lists about 14,700 citations (14,691 when checked on 3 December 2026).


Resources

File Description
README.md This overview — a 5-minute read covering the paper’s summary, core idea, key method details, influence, and a plain-English explanation of the problem it solves
solution.ipynb From-scratch NumPy node2vec (second-order biased walks, alias sampling, Skip-gram with negative sampling) on a synthetic 3-community graph, with a 5-seed comparison of q = 0.5 and q = 2
CODE_ARCHITECTURE.md Section-by-section breakdown of the notebook — what each cell does, data flow, tensor shapes, and key design decisions
TALK.md Press coverage, Q&A (answers from the paper), citations, and common misconceptions about the paper

Table of contents