Example #1
0
print("Starting at {}, ending at {}".format(start, stop))
if len(graph.neighbours_of(G, start)) == 0:
    raise Exception("Bad luck, {} has no neighbours".format(start))

num_steps = 0

display.write_dot_desc(G, dot_file_name, attr)

cur = start
while cur != stop and num_steps < max_num_steps:
    display.pause(0)
    num_steps += 1

    print("Step {} at {}".format(num_steps, cur))
    # find a random neighbour of cur
    prev = cur
    neighbours = graph.neighbours_of(G, cur)
    cur = random.choice(list(neighbours))

    # do some coloring before next graph render
    attr["vertex_color"][prev]="white"
    attr["vertex_color"][start]="green"
    attr["vertex_color"][cur]="orange"
    attr["vertex_color"][stop]="red"
    attr["edge_color"][graph.mk_edge(prev, cur)]="orange"
    display.write_dot_desc(G, dot_file_name, attr)

print("Finished, after {} steps we are at {}".format(num_steps, cur))
if cur != stop:
    print("We failed to reach the stop vertex {}".format(stop))
Example #2
0
File: dfs.py Project: elake/inclass
    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])

        # color the new edge v, w 
        attr["edge_label"][graph.mk_edge(v, w)]=edge_seq_num
        edge_seq_num += 1
        attr["edge_color"][graph.mk_edge(v, w)]="orange"

        # update the rendering
        display.write_dot_desc(G, dot_file_name, attr)
        display.pause(0)

        # push w on visit stack
        visit_stack.append(w)
    else:
        # no unvisited neighbours, back track 
        visit_stack.pop()