class Node:
def __init__(self, name):
self.name = name
self.out_neighbors = []
a = Node("a")
b = Node("b")
c = Node("c")
# We must first define the nodes, before we can define the neighbors (which use the nodes!)
a.out_neighbors = [a, b]
b.out_neighbors = [a, c]
c.out_neighbors = []
# Define the graph g as an array containing the three vertices a, b, c.
g = [a, b, c]
from random import *
def random_walk(graph, nb_steps):
if g == []:
print("I can't move on an empty graph.")
return
index = randint(0,len(g)-1)
current_vertex = g[index]
print("I start from " + current_vertex.name + ".")
for i in range(nb_steps):
if current_vertex.out_neighbors == []:
print("I can't move anymore.")
break
index = randint(0,len(current_vertex.out_neighbors)-1)
current_vertex = current_vertex.out_neighbors[index]
print("I move to " + current_vertex.name + ".")
random_walk(g, 50)