node2vec is an unsupervised node embedding algorithm that maps every node of a graph to a dense vector by sampling biased second-order random walks and training a skip-gram model on them, so that nodes appearing in similar walk contexts receive similar vectors. It is a foundational method in graph learning, a branch of artificial intelligence, and was introduced by Aditya Grover and Jure Leskovec at the KDD conference in 2016.
The defining idea is a tunable exploration strategy. Two parameters, the return parameter p and the in-out parameter q, let the walk behave more like breadth-first search or more like depth-first search. This flexibility lets one algorithm capture either community membership (homophily) or structural roles (structural equivalence), depending on the task.
How node2vec generates biased random walks
node2vec walks are second-order Markov chains: the probability of the next step depends on the current node v and on the previous node t. For each neighbor x of v, the unnormalized transition probability equals the edge weight multiplied by a search bias alpha, which depends on the shortest-path distance between t and x.
This bias rule is compact. A step back to t is weighted by 1/p, a step to a node that is also a neighbor of t is weighted by 1, and a step to a node farther from t is weighted by 1/q. Precomputed alias tables make each step samplable in constant time, although the tables for second-order walks require memory proportional to the number of edge-neighbor pairs.
| Distance from previous node t to candidate x | Bias weight | Interpretation |
|---|---|---|
| 0 (x is t) | 1/p | Return to where the walk came from |
| 1 (x adjacent to t) | 1 | Stay in the local neighborhood |
| 2 (x not adjacent to t) | 1/q | Move outward, away from t |
The p and q parameters in node2vec
The two parameters control the balance between local and global exploration. A low q (below 1) pushes the walk outward, producing depth-first behavior that spans communities and tends to group nodes by membership in densely connected regions. A high q keeps the walk close to its start, producing breadth-first behavior that describes the immediate neighborhood and tends to reflect structural roles such as hubs or bridges.
A high p discourages immediate backtracking, which reduces redundant two-hop loops, while a low p keeps the walk tightly local. With p = q = 1 the walk is unbiased and node2vec reduces to DeepWalk, its direct predecessor.
| Setting | Walk behavior | What embeddings capture |
|---|---|---|
| p = 1, q = 1 | Uniform random walk | Same as DeepWalk |
| q < 1 | Outward, depth-first | Communities and homophily |
| q > 1 | Local, breadth-first | Structural roles and equivalence |
| p > max(q, 1) | Rarely backtracks | Less redundant sampling |
| p < min(q, 1) | Frequently backtracks | Very local context |
The original paper searched p and q over values from 0.25 to 4 using a labeled validation set. Because the best setting is task dependent, a small grid search is standard practice.
From walks to embeddings with skip-gram
After sampling, node2vec treats each walk as a sentence of node identifiers and trains the skip-gram objective from word2vec. The model maximizes the probability of observing context nodes within a window around each node, approximated with negative sampling so that training scales to millions of nodes.
Common defaults are 128 dimensions, 10 walks per node, a walk length of 80 and a context window of 10. Training cost grows roughly linearly with the number of nodes multiplied by walks per node and walk length. The output is a lookup table of vectors that downstream models, such as logistic regression for node classification, can consume.
For link prediction, node2vec combines two node vectors into an edge feature with a binary operator. The Hadamard product, which multiplies vectors element by element, generally performs best in published comparisons, ahead of averaging and L1 or L2 distances.
node2vec compared with related graph methods
node2vec belongs to the family of shallow embedding methods, which learn one free vector per node and do not use node features. Graph neural networks take a different path by learning feature-aware aggregation functions.
| Method | Core mechanism | Uses node features | Inductive |
|---|---|---|---|
| DeepWalk | Uniform random walks plus skip-gram | No | No |
| node2vec | Biased second-order walks plus skip-gram | No | No |
| LINE | First- and second-order proximity objectives | No | No |
| Spectral embedding | Eigenvectors of the graph Laplacian | No | No |
| GraphSAGE | Learned neighborhood aggregation | Yes | Yes |
| Graph convolutional network | Normalized neighbor averaging layers | Yes | Partly |
Theoretical work has shown that random-walk methods such as DeepWalk and node2vec implicitly factorize a matrix derived from walk co-occurrence statistics, which links them to classical spectral methods.
Strengths, limitations and practical pitfalls
node2vec remains a strong, cheap baseline for node classification and link prediction on graphs without informative features. Its main weakness is that it is transductive: a node unseen during training has no vector, so dynamic graphs require retraining or incremental heuristics.
- Ignoring node attributes wastes information that graph neural networks can use.
- Memory for alias tables becomes a bottleneck on graphs with high-degree hubs.
- Embeddings from separate runs are not aligned, so vectors cannot be compared across retrainings without alignment.
- Leakage in link prediction occurs when test edges remain in the graph used for walks; they must be removed before training.
- Default p and q rarely match the task; tuning often changes results substantially.
Explained without jargon
Imagine a city map where every person is a point and every friendship is a road. node2vec sends many walkers wandering along the roads and writes down who they meet. People who keep appearing on the same walks end up with similar number codes.
Two dials control the walkers. One makes them stay close to home. The other sends them exploring far away. Staying close shows what role someone plays locally. Exploring shows which larger group someone belongs to. The codes let computers compare people, spot groups and guess missing friendships.
A typical use case
A biology research group studies a protein interaction network with about 4,000 proteins and 40,000 known interactions, and it wants to suggest likely interactions for laboratory testing. The team removes 10 percent of edges as a test set, keeping the graph connected, and runs node2vec on the remainder with 128 dimensions, 10 walks per node and walk length 80.
A grid search over p and q finds that q = 0.5 and p = 1 gives the best validation score. Edge features built with the Hadamard product feed a logistic regression classifier, which ranks unobserved protein pairs by predicted probability.
The model clearly outperforms common-neighbor heuristics on the held-out edges, and the top-ranked candidate pairs are passed to the laboratory, which narrows experimental work to a short list instead of thousands of possible pairs.