def test_get_nodes():
    graph = Graph()
    a = graph.add_node('a')
    b = graph.add_node('b')
    c = graph.add_node('c')
    graph.add_edge(a, b)
    actual = graph.get_nodes()
    expected = ['a', 'b', 'c']
    assert actual == expected
def test_breadth_first_circle_start_a():
    graph = Graph()
    a = graph.add_node('a')
    b = graph.add_node('b')
    c = graph.add_node('c')
    graph.add_edge(a, b)
    graph.add_edge(b, c)
    graph.add_edge(c, a)
    actual = graph.breadth_first(a)
    expected = ['a', 'b', 'c']
    assert actual == expected
def test_graph_missing_start_vertex():
    """
    A walk of a graph without a starting vertex sees no vertices.
    """

    graph = Graph()
    graph.add_node('c')

    vertex = None

    expected = set({})
    actual = depth_first(graph, vertex)
    assert actual == expected
def test_get_neighbor():
    graph = Graph()
    a = graph.add_node('a')
    b = graph.add_node('b')
    c = graph.add_node('c')
    graph.add_edge(a, b)
    graph.add_edge(b, a)
    graph.add_edge(b, c)

    actual = graph.get_neighbor(b)

    expected = [('a', 1), ('c', 1)]

    assert actual == expected
def test_one_node_and_edge():
    graph = Graph()
    a = graph.add_node('a')
    graph.add_edge(a, a)
    actual = graph.get_neighbor(a)
    expected = [('a', 1)]
    assert actual == expected
def test_size_fail():
    graph = Graph()
    a = graph.add_node('a')
    b = graph.add_node('b')
    expected = 3
    actual = graph.size()
    assert actual != expected
def test_graph_only_islands():
    """
    A walk of a graph of islands sees only the starting vertex.
    """

    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    c = graph.add_node('c')

    vertex = c

    expected = {
        c,
    }
    actual = depth_first(graph, vertex)
    assert actual == expected
def test_graph_with_edges():
    """
    A walk of a graph with edges sees many vertices.
    """

    graph = Graph()
    a = graph.add_node('a')
    b = graph.add_node('b')
    c = graph.add_node('c')
    d = graph.add_node('d')
    e = graph.add_node('e')
    f = graph.add_node('f')
    g = graph.add_node('g')
    h = graph.add_node('h')

    graph.add_edge(a, b)
    graph.add_edge(a, d)

    graph.add_edge(b, a)
    graph.add_edge(b, c)
    graph.add_edge(b, d)

    graph.add_edge(c, b)
    graph.add_edge(c, h)

    graph.add_edge(d, a)
    graph.add_edge(d, b)
    graph.add_edge(d, e)
    graph.add_edge(d, f)
    graph.add_edge(d, g)

    graph.add_edge(e, d)

    graph.add_edge(f, d)
    graph.add_edge(f, g)

    graph.add_edge(g, d)
    graph.add_edge(g, f)

    graph.add_edge(h, c)

    expected = {a, b, c, h, d, e, f, g}
    actual = depth_first(graph, a)

    assert actual == expected
Ejemplo n.º 9
0
def graph():
    """
    Graph instance.
    """

    graph = Graph()

    a = graph.add_node('a')
    b = graph.add_node('b')
    c = graph.add_node('c')
    d = graph.add_node('d')
    e = graph.add_node('e')
    f = graph.add_node('f')

    graph.add_edge(a, b, 150)
    graph.add_edge(a, c, 82)

    graph.add_edge(b, a, 150)
    graph.add_edge(b, c, 99)
    graph.add_edge(b, d, 42)

    graph.add_edge(c, a, 82)
    graph.add_edge(c, b, 99)
    graph.add_edge(c, d, 105)
    graph.add_edge(c, e, 37)
    graph.add_edge(c, f, 26)

    graph.add_edge(d, b, 42)
    graph.add_edge(d, c, 105)
    graph.add_edge(d, f, 73)

    graph.add_edge(e, c, 37)
    graph.add_edge(e, f, 250)

    graph.add_edge(f, c, 26)
    graph.add_edge(f, d, 73)
    graph.add_edge(f, e, 250)

    return graph
def test_breadth_first_code_challenge_example():
    graph = Graph()
    a = graph.add_node('Pandora')
    b = graph.add_node('Arendelle')
    c = graph.add_node('Metroville')
    d = graph.add_node('Monstropolis')
    e = graph.add_node('Naboo')
    f = graph.add_node('Narnia')
    graph.add_edge(a, b)
    graph.add_edge(b, d)
    graph.add_edge(b, c)
    graph.add_edge(c, d)
    graph.add_edge(c, e)
    graph.add_edge(c, f)
    graph.add_edge(f, e)
    actual = graph.breadth_first(a)
    expected = [
        'Pandora', 'Arendelle', 'Monstropolis', 'Metroville', 'Naboo', 'Narnia'
    ]
    assert actual == expected
def test_add_edge_true():
    graph = Graph()
    a = graph.add_node('a')
    b = graph.add_node('b')
    graph.add_edge(a, b)
    assert True
def test_add_node():
    graph = Graph()
    expected = 'a'
    actual = graph.add_node('a').value
    assert expected == actual