n = 20 m = 30 G = graph.generate_random_graph(n, m) (V, E) = G # print(G) print("Number of edges is {}, we want {}".format(len(E), m)) start = random.choice(list(V)) stop = random.choice(list(V)) cur = start print("Starting at {}".format(cur)) if len(graph.neighbours_of(G, cur)) == 0: raise Exception("Bad luck, {} has no neighbours".format(cur)) num_steps = 0 max_num_steps = 1000 history = [cur] while cur != stop and num_steps < max_num_steps: num_steps += 1 # pick a neighbour of cur at random neighbours = graph.neighbours_of(G, cur) # print(neighbours) # pick one of the neighbours # cur = random.sample(neighbours, 1)[0]
# visited set, start with root being visited visited = set([root]) visit_stack = [ root ] edge_seq_num = 0 level = { root: 0} while visit_stack: print("visited:" + str(visited)) print("visit stack:" + str(visit_stack)) # get the top of the stack, but don't remove v = visit_stack[-1] # find unvisited neighbour of v w = None for x in graph.neighbours_of(G, v): if x not in visited: w = x break if w is not None: # mark w as visited visited.add(w) print("visiting {} at step {}".format(w, edge_seq_num)) level[w] = level[v] + 1 # color and label the visited vertex attr["vertex_color"][w]="orange" attr["vertex_label"][w]= str(w) + ":" + str(level[w])