Exemplo n.º 1
0
def test_graph_depth_first():
    graph = Graph()
    v1 = graph.add_node('a')
    v2 = graph.add_node('b')
    v3 = graph.add_node('c')
    v4 = graph.add_node('d')
    v5 = graph.add_node('e')
    v6 = graph.add_node('f')
    v7 = graph.add_node('g')
    v8 = graph.add_node('h')
    graph.add_edge(v1, v2)
    graph.add_edge(v1, v4)
    graph.add_edge(v2, v1)
    graph.add_edge(v2, v3)
    graph.add_edge(v2, v4)
    graph.add_edge(v3, v2)
    graph.add_edge(v3, v7)
    graph.add_edge(v4, v1)
    graph.add_edge(v4, v2)
    graph.add_edge(v4, v5)
    graph.add_edge(v4, v8)
    graph.add_edge(v4, v6)
    graph.add_edge(v6, v4)
    graph.add_edge(v6, v8)
    graph.add_edge(v8, v6)
    graph.add_edge(v8, v4)
    graph.add_edge(v5, v4)
    values = graph.depth_first(v1)
    actual = [vertex.value for vertex in values]
    assert actual == ['a', 'b', 'a', 'd', 'e', 'h', 'f', 'c', 'g']
def test_neighbors_three():
    g = Graph()
    vertex_a = g.add('a')
    vertex_b = g.add('b')
    vertex_c = g.add('c')
    vertex_d = g.add('d')
    vertex_e = g.add('e')
    vertex_f = g.add('f')
    vertex_g = g.add('g')
    vertex_h = g.add('h')

    g.add_edge(vertex_a, vertex_b)
    g.add_edge(vertex_c, vertex_g)
    g.add_edge(vertex_b, vertex_c)
    g.add_edge(vertex_b, vertex_d)
    g.add_edge(vertex_e, vertex_d)
    g.add_edge(vertex_d, vertex_h)
    g.add_edge(vertex_f, vertex_h)
    g.add_edge(vertex_d, vertex_f)
    g.add_edge(vertex_d, vertex_a)

    actual = g.depth_first('f')

    assert actual == ['f', 'h', 'd', 'b', 'a', 'c', 'g', 'e']