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:
- g
Graph Graph to be used.
- source
Vertex,int, or iterable ofint Starting vertex of the walk, or of each walk.
- length
int Number of steps of each walk.
- weight
EdgePropertyMap(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.
- p
float(optional, default:1.) Return parameter of the second-order walk of node2vec (see below).
- q
float(optional, default:1.) In-out parameter of the second-order walk of node2vec (see below).
- edges
bool(optional, default:False) If
True, the walks are returned as sequences of edge indices, instead of vertices.- restart
float(optional, default:0.) Probability with which every step is a jump back to the starting vertex, instead of a move along an edge.
- stuck
str(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"aValueErroris raised.
- g
- Returns:
- walks
numpy.ndarray If
sourceis a single vertex, an array with thelength + 1visited vertices (or thelengthtraversed edges, ifedges == True); otherwise a two-dimensional array with one walk per row. If a walk reaches a vertex without out-edges andstuck == "stop", the remaining entries are-1. In the sequences of edges, the jumps back to the starting vertex are-1.
- walks
See also
loop_erased_random_walkRandom walk with its loops erased
random_shortest_pathUniformly sampled shortest path
graph_tool.spectral.transitionTransition 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
porqdiffer 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 (seepagerank()), 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]