loop_erased_random_walk#
- graph_tool.structure.loop_erased_random_walk(g, source, targets, weight=None)[git][source]#
Return a loop-erased random walk from a vertex to a set of targets.
- Parameters:
- g
Graph Graph to be used.
- source
Vertexorint Starting vertex.
- targetsiterable of
intorVertex Vertices at which the walk stops.
- weight
EdgePropertyMap(optional, default:None) Edge weights. If given, the next edge is chosen with probability proportional to its weight; otherwise, uniformly.
- g
- Returns:
- path
numpy.ndarray The vertices of the walk after its loops are erased, from
sourceto the first target reached.
- path
See also
random_walkSimple and biased random walks
random_spanning_treeUniformly sampled spanning tree
Notes
The random walk starts at
sourceand moves at each step along an out-edge of the current vertex, chosen uniformly at random, or with probability proportional to its weight, until it reaches a target. Its loop erasure [lawler-self-1980] is obtained chronologically: every time the walk returns to a vertex already on the path, the loop formed since the previous visit is erased from the path. The result is a self-avoiding path; the loop-erased walks from every vertex to the tree built so far yield a uniform random spanning tree in Wilson’s algorithm (seerandom_spanning_tree()). The expected time is the hitting time of the targets. AValueErroris raised if the walk reaches a vertex without out-edges.References
[lawler-self-1980]Gregory F. Lawler, “A self-avoiding random walk”, Duke Mathematical Journal 47, 655-693 (1980), DOI: 10.1215/S0012-7094-80-04741-9 [sci-hub, @tor]
Examples
>>> g = gt.lattice([10, 10]) >>> path = gt.loop_erased_random_walk(g, 0, [99]) >>> print(path[0], path[-1], len(set(path)) == len(path)) 0 99 True
The path can be shown in a drawing of the lattice:
>>> pos = g.new_vp("vector<double>", ... vals=[(i % 10, i // 10) for i in range(100)]) >>> onpath = g.new_ep("bool") >>> for u, v in zip(path[:-1], path[1:]): ... onpath[g.edge(u, v)] = True >>> inpath = g.new_vp("bool") >>> inpath.a[path] = True >>> vcolor = g.new_vp("string", vals=np.where(inpath.a, "#f28c8e", ... "#cccccc")) >>> ecolor = g.new_ep("string", vals=np.where(onpath.a, "#e41a1c", ... "#999999")) >>> ewidth = g.new_ep("double", vals=np.where(onpath.a, 3, 1)) >>> gt.graph_draw(g, pos, vertex_fill_color=vcolor, edge_color=ecolor, ... edge_pen_width=ewidth, ... output="loop_erased_random_walk-lattice.pdf") <...>
Loop-erased random walk from the corner vertex 0 to the opposite corner of a \(10\times 10\) lattice, with its vertices and edges shown in red.#