Note

This is the documentation for the current state of the development branch of rustworkx. The documentation or APIs here can change prior to being released.

rustworkx.generate_random_path

generate_random_path(graph, /, source, length, seed=None)[source]

Return a random path (or random walk) on a graph.

The next node to visit is selected uniformly at random from the neighbors (or outgoing neighbors for directed graphs). If a node of the path has no neighbor (or no outgoing neighbor for directed graphs), the path will stop early.

Parameters:
  • graph – Graph on which the random walk is done. This can be a PyGraph or PyDiGraph.

  • source (int) – Starting node of the path.

  • length (int) – Maximum length of the path.

  • seed (Optional[int]) – seed of the random number generator that chooses the next node.

Returns:

List of visited nodes including the initial node source.

Return type:

list[int]

Raises:

IndexError – when the graph doesn’t contain the source node.