random_walk

Contents

random_walk#

graph_tool.structure.random_walk(g, source, length, weight=None, p=1.0, q=1.0, edges=False, restart=0.0, stuck='stop')[git][source]#

Sample random walks on the graph.

Parameters:
gGraph

Graph to be used.

sourceVertex, int, or iterable of int

Starting vertex of the walk, or of each walk.

lengthint

Number of steps of each walk.

weightEdgePropertyMap (optional, default: None)

Non-negative edge weights. If given, the steps are taken along each out-edge with probability proportional to its weight; otherwise, all out-edges are equally likely.

pfloat (optional, default: 1.)

Return parameter of the second-order walk of node2vec (see below).

qfloat (optional, default: 1.)

In-out parameter of the second-order walk of node2vec (see below).

edgesbool (optional, default: False)

If True, the walks are returned as sequences of edge indices, instead of vertices.

restartfloat (optional, default: 0.)

Probability with which every step is a jump back to the starting vertex, instead of a move along an edge.

stuckstr (optional, default: "stop")

What happens when a walk reaches a vertex without out-edges: with "stop" the walk ends, with "restart" it jumps back to the starting vertex, and with "error" a ValueError is raised.

Returns:
walksnumpy.ndarray

If source is a single vertex, an array with the length + 1 visited vertices (or the length traversed edges, if edges == True); otherwise a two-dimensional array with one walk per row. If a walk reaches a vertex without out-edges and stuck == "stop", the remaining entries are -1. In the sequences of edges, the jumps back to the starting vertex are -1.

See also

loop_erased_random_walk

Random walk with its loops erased

random_shortest_path

Uniformly sampled shortest path

graph_tool.spectral.transition

Transition matrix of the random walk

Notes

Each step moves from the current vertex \(v\) to an out-neighbor along an edge \(e\) with probability \(w_e/\sum_{e'}w_{e'}\), where the sum runs over the out-edges of \(v\) (for undirected graphs, all incident edges). If p or q differ from one, the walk is the second-order walk of node2vec [grover-node2vec-2016]: the probability of moving from \(v\) to \(x\), having arrived at \(v\) from \(t\), is further multiplied by \(1/p\) if \(x=t\), by \(1\) if \(x\) is a neighbor of \(t\), and by \(1/q\) otherwise, so that small values of \(p\) keep the walk local, and small values of \(q\) drive it away. These steps are sampled by rejection, which requires \(O(\max(p, 1/p, q, 1/q))\) attempts in the worst case, each with a test of adjacency in time \(O(\log k)\).

With restart > 0, the walk is a random walk with restart, whose stationary distribution is the personalized PageRank of the starting vertex (see pagerank()), and a jump back resets the node2vec memory of the previous vertex.

The walks are sampled in parallel. After an initial pass over the edges in time \(O(E\log k)\), each step takes time \(O(1)\), or \(O(\log k)\) with weights, where \(k\) is the degree of the current vertex.

Parallel implementation.

If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.

References

[grover-node2vec-2016]

Aditya Grover and Jure Leskovec, “node2vec: Scalable feature learning for networks”, Proceedings of the 22nd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining, 855-864 (2016), DOI: 10.1145/2939672.2939754 [sci-hub, @tor], arXiv: 1607.00653

Examples

>>> g = gt.collection.data["karate"]
>>> print(gt.random_walk(g, g.vertex(0), 10))
[ 0 13  1 17  0 10  5 10  0 13  0]

A common use is the generation of the “sentences” of vertex embeddings such as DeepWalk and node2vec, by sampling several walks from every vertex:

>>> walks = gt.random_walk(g, np.repeat(g.get_vertices(), 10), 20, p=0.5, q=2)
>>> print(walks.shape)
(340, 21)

With restarts, the walk stays near the starting vertex:

>>> print(gt.random_walk(g, g.vertex(0), 10, restart=0.5))
[ 0  0  0  0 31  0  6  0  5  0 11]