Quant Memo
Advanced

node2vec and Biased Random Walks

node2vec turns a graph into training data for word2vec by taking random walks over the nodes and tuning two knobs that trade off exploring broadly versus staying local, producing embeddings that capture whatever notion of similarity — community membership or structural role — the task needs.

Prerequisites: DeepWalk and Skip-Gram on Graphs, Word Embeddings and Word2Vec

Word2vec turns words into useful numeric vectors by looking at which words tend to sit near each other in sentences. A graph has no sentences — but it does have paths. node2vec's trick is to manufacture sentences by taking random walks across the graph: start at a node, hop to a random neighbor, hop again, and again, recording the sequence of nodes visited as if it were a sentence of "words." Feed enough of these walk-sentences into the same skip-gram model word2vec uses, and nodes that tend to appear near each other in walks end up with similar vectors — a graph embedding, built entirely by borrowing an NLP algorithm.

The analogy: two ways to explore a city

Imagine exploring an unfamiliar city on foot with two very different strategies. One: stick close, wandering the same few blocks over and over, learning every alley of this specific neighborhood in depth. The other: keep pushing outward, block after block, sampling many different neighborhoods but shallowly. Neither is "correct" — it depends what you want to learn. If you want to know "which houses belong to the same tight community," staying local works better. If you want to know "which houses play a similar structural role — corner house, cul-de-sac, main-street house — regardless of which neighborhood they're in," ranging widely works better. node2vec's whole contribution is giving the random walk a dial to choose between these two exploration styles.

The two knobs: pp and qq

A plain DeepWalk takes a uniform random walk — from the current node, jump to any neighbor with equal probability. node2vec instead biases the next step based on where the walk just came from, using two parameters:

  • Return parameter pp: controls how likely the walk is to immediately go back to the node it just left. A high pp discourages backtracking, pushing the walk to keep exploring outward (like the "range widely" strategy above).
  • In-out parameter qq: controls how likely the walk is to move to a node farther from where it started versus staying close. A low qq encourages venturing outward (DFS-like, structural/role similarity); a high qq keeps the walk hugging the local neighborhood (BFS-like, community similarity).

Formally, if the walk just moved from node tt to node vv, and is choosing the next node xx among vv's neighbors, the unnormalized transition weight is:

π(v,x)={1/pif x=t1if x is also a neighbor of t1/qif x is farther from t\pi(v, x) = \begin{cases} 1/p & \text{if } x = t \\ 1 & \text{if } x \text{ is also a neighbor of } t \\ 1/q & \text{if } x \text{ is farther from } t \end{cases}

In words: going straight back to where you came from is scaled by 1/p1/p; staying within the previous node's neighborhood (a "local" move) gets weight 11; pushing further away from where you started gets scaled by 1/q1/q. Small pp makes backtracking cheap (more local, BFS-flavored); small qq makes pushing outward cheap (more exploratory, DFS-flavored).

Worked example 1: computing transition weights by hand

Take a walk that just moved from node AA to node BB, and BB has three neighbors: AA itself, node CC (which is also a neighbor of AA), and node DD (which is not a neighbor of AA, i.e. farther away). Set p=0.5p = 0.5, q=2q = 2. The unnormalized weights are: back to AA: 1/p=1/0.5=21/p = 1/0.5 = 2; to CC (shared neighbor): 11; to DD (farther): 1/q=1/2=0.51/q = 1/2 = 0.5. Total =2+1+0.5=3.5= 2 + 1 + 0.5 = 3.5, so the walk returns to AA with probability 2/3.50.572/3.5 \approx 0.57, goes to CC with probability 1/3.50.291/3.5 \approx 0.29, and to DD with probability 0.5/3.50.140.5/3.5 \approx 0.14. With this p,qp,q pair the walk strongly favors staying local — exactly the BFS-like, community-detecting regime.

Worked example 2: same graph, opposite regime

Same setup, but now p=2p = 2, q=0.5q = 0.5: back to AA gets 1/2=0.51/2 = 0.5; to CC (shared) gets 11; to DD (farther) gets 1/0.5=21/0.5 = 2. Total =3.5= 3.5, so probabilities are 0.140.14, 0.290.29, 0.570.57 respectively — now the walk strongly favors pushing outward to DD, the farther node. The exact same graph, the exact same starting position, produces an almost mirror-image walk simply by flipping pp and qq — and the resulting embeddings end up organized around different notions of similarity: community membership in the first case, structural role in the second.

Path explorer
13055time →
end (bold path) 100.38spread of ends 58.966 independent paths, same settings

The path behavior above — some paths drifting, some snapping back toward a center — is the same kind of tension node2vec's p,qp,q knobs control on a graph: whether the walk snaps back locally or keeps drifting outward.

node2vec generates "sentences" by biased random walks over a graph, then reuses word2vec's skip-gram model to turn co-occurrence in those walks into node embeddings. The pp (return) and qq (in-out) parameters tune the walk between local, BFS-like exploration — good for finding communities — and outward, DFS-like exploration — good for finding structural roles.

What this means in practice

In finance, node2vec-style embeddings are used on networks like supply chains, co-ownership graphs, or lead-lag correlation graphs to produce a numeric vector per company that downstream models (a classifier, a clustering algorithm, a similarity search) can consume directly, without hand-engineering graph features. The p,qp,q choice matters for what the embedding actually captures: tuned for community structure, embeddings will cluster stocks by sector or supply-chain neighborhood; tuned for structural role, they will group companies that play a similar position in their respective networks — say, "hub supplier" — even across unrelated sectors.

Practice

  1. Redo worked example 1 with p=1,q=1p = 1, q = 1 (equivalent to plain DeepWalk) and confirm the three probabilities are equal.
  2. Explain in plain English why a low qq pushes the walk toward DFS-like, structural-role-capturing behavior.
  3. A desk wants embeddings that group companies by tight supply-chain cluster, not by shared structural role. Would they set qq high or low, and why?

The common confusion is treating pp and qq as arbitrary tuning knobs to grid-search blindly, the way you might tune a learning rate. They are not neutral — they encode a specific hypothesis about what kind of similarity matters for the task (community membership versus structural role), and picking them without that hypothesis in mind can produce embeddings that are internally consistent but answer the wrong question for the downstream use case.

Related concepts

Practice in interviews

Further reading

  • Grover & Leskovec, node2vec: Scalable Feature Learning for Networks (2016)
  • Perozzi, Al-Rfou & Skiena, DeepWalk: Online Learning of Social Representations (2014)
ShareTwitterLinkedIn