You can download this code by clicking the button below.
This code is now available for download.
This function performs a random walk on a given graph starting from a specified node for a number of steps specified by the parameter 'steps'. It records the path of the walk.
Technology Stack : Igraph
Code Type : The type of code
Code Difficulty : Intermediate
def random_walk(graph, start_node, steps=10):
# This function performs a random walk on a graph starting from a given node for a specified number of steps.
current_node = start_node
visited_nodes = set([start_node])
walk = [current_node]
for _ in range(steps):
neighbors = graph.neighbors(current_node)
if neighbors:
next_node = neighbors[0] if neighbors[0] not in visited_nodes else neighbors[1]
visited_nodes.add(next_node)
current_node = next_node
walk.append(current_node)
else:
break
return walk