Пример #1
0
def test_get_nodes():
    graph = Graph()
    node1 = graph.add_node(1)
    node2 = graph.add_node(2)
    actual = len(graph.get_nodes())
    expected = 2
    assert actual == expected
Пример #2
0
def test_node_graph_2():
    g = Graph()
    node1 = g.add_node('Test')
    node2 = g.add_node('random')
    actual = len(g.get_nodes())
    expected = 2
    assert actual == expected
Пример #3
0
def test_get_neighbors_none():
    g = Graph()
    node_a = g.add_node('a')
    node_b = g.add_node('b')
    node_c = g.add_node('c')
    node_d = g.add_node('d')
    actual = g.get_neighbors(node_a)
    expected = []
    assert actual == expected
Пример #4
0
def test_size_2():
    g = Graph()
    g.add_node(235235)
    g.add_node(3232)
    g.add_node(222)
    actual = g.size()
    expected = 3
    assert actual == expected
Пример #5
0
def test_add_edge_exception_2():
    with pytest.raises(KeyError):
        g1 = Graph()
        g2 = Graph()
        node1 = g1.add_node(1)
        node2 = g2.add_node(2)
        g2.add_edge(node1, node2)
Пример #6
0
def test_get_neighbors_1():
    g = Graph()
    node_a = g.add_node('a')
    node_b = g.add_node('b')
    node_c = g.add_node('c')
    node_d = g.add_node('d')
    g.add_edge(node_a, node_b)
    actual = len(g.get_neighbors(node_a))
    expected = 1
    assert actual == expected
Пример #7
0
def test_example_input_3():
    g = Graph()
    pandora = g.add_node('Pandora')
    adrendelle = g.add_node('Arendelle')
    metroville = g.add_node('Metroville')
    monstroplolis = g.add_node('Monstroplolis')
    naboo = g.add_node('Naboo')
    narnia = g.add_node('Narnia')
    g.add_edge(pandora, adrendelle, 150)
    g.add_edge(pandora, metroville, 82)
    g.add_edge(adrendelle, pandora, 150)
    g.add_edge(adrendelle, metroville, 99)
    g.add_edge(adrendelle, monstroplolis, 42)
    g.add_edge(metroville, pandora, 82)
    g.add_edge(metroville, adrendelle, 99)
    g.add_edge(metroville, monstroplolis, 105)
    g.add_edge(metroville, naboo, 26)
    g.add_edge(metroville, narnia, 37)
    g.add_edge(monstroplolis, metroville, 73)
    g.add_edge(monstroplolis, adrendelle, 42)
    g.add_edge(monstroplolis, naboo, 73)
    g.add_edge(naboo, monstroplolis, 73)
    g.add_edge(naboo, metroville, 26)
    g.add_edge(naboo, narnia, 250)
    g.add_edge(narnia, naboo, 250)
    g.add_edge(narnia, metroville, 37)
    actual = get_edges(g, [naboo, pandora])
    expected = (False, '$0')
    assert actual == expected
Пример #8
0
def build_graph():
    test = Graph()
    pandora = test.add_vertex('Pandora')
    arendelle = test.add_vertex('Arendelle')
    metroville = test.add_vertex('Metroville')
    monstroplolis = test.add_vertex('Monstroplolis')
    narnia = test.add_vertex('Narnia')
    naboo = test.add_vertex('Naboo')
    test.add_edge(pandora, arendelle)
    test.add_edge(arendelle, metroville)
    test.add_edge(arendelle, monstroplolis)
    test.add_edge(metroville, narnia)
    test.add_edge(metroville, naboo)
    test.add_edge(metroville, monstroplolis)
    test.add_edge(monstroplolis, naboo)
    test.add_edge(narnia, naboo)
    return test
def test_example_input1():
    my_graph = Graph()
    a = my_graph.add_node("A")
    b = my_graph.add_node("B")
    c = my_graph.add_node("C")
    d = my_graph.add_node("D")
    e = my_graph.add_node("E")
    f = my_graph.add_node("F")
    g = my_graph.add_node("G")
    h = my_graph.add_node("H")
    my_graph.add_edge(a, b)
    my_graph.add_edge(a, d)

    my_graph.add_edge(b, c)
    my_graph.add_edge(b, d)

    my_graph.add_edge(c, g)

    my_graph.add_edge(d, e)
    my_graph.add_edge(d, h)
    my_graph.add_edge(d, f)
    my_graph.add_edge(d, b)

    actual = depth_first(my_graph, a)
    expected = ['A', 'B', 'C', 'G', 'D', 'E', 'H', 'F']
    assert actual == expected
Пример #10
0
def test_breadth_first_example():
    """ Refer to the README Examples for visual aids.
    """
    g = Graph()
    pandora = g.add_node('Pandora')
    adrendelle = g.add_node('Arendelle')
    metroville = g.add_node('Metroville')
    monstroplolis = g.add_node('Monstroplolis')
    naboo = g.add_node('Naboo')
    narnia = g.add_node('Narnia')
    g.add_edge(pandora, adrendelle)
    g.add_edge(adrendelle, metroville)
    g.add_edge(adrendelle, monstroplolis)
    g.add_edge(adrendelle, pandora)
    g.add_edge(metroville, adrendelle)
    g.add_edge(metroville, monstroplolis)
    g.add_edge(metroville, narnia)
    g.add_edge(metroville, naboo)
    g.add_edge(monstroplolis, adrendelle)
    g.add_edge(monstroplolis, metroville)
    g.add_edge(monstroplolis, naboo)
    g.add_edge(naboo, narnia)
    g.add_edge(naboo, metroville)
    g.add_edge(naboo, monstroplolis)
    actual = g.breadth_first(pandora)
    expected = [
        'Pandora', 'Arendelle', 'Metroville', 'Monstroplolis', 'Narnia',
        'Naboo'
    ]
    assert actual == expected
Пример #11
0
def test_size_1():
    g = Graph()
    g.add_node(1)
    actual = g.size()
    expected = 1
    assert actual == expected
Пример #12
0
def test_size_none():
    g = Graph()
    actual = g.size()
    expected = None
    assert actual == expected
Пример #13
0
def test_node_graph():
    graph = Graph()
    node = graph.add_node('Test Node')
    actual = str(node)
    expected = 'Test Node'
    assert actual == expected
Пример #14
0
def test_breadth_first_example_2_different_start():
    g = Graph()
    bob = g.add_node('Bob')
    tom = g.add_node('Tom')
    sam = g.add_node('Sam')
    joe = g.add_node('Joe')
    jon = g.add_node('Jon')
    g.add_edge(bob, tom)
    g.add_edge(tom, bob)
    g.add_edge(tom, jon)
    g.add_edge(tom, sam)
    g.add_edge(tom, joe)
    g.add_edge(jon, tom)
    g.add_edge(jon, sam)
    g.add_edge(sam, jon)
    g.add_edge(sam, tom)
    g.add_edge(sam, joe)
    g.add_edge(joe, sam)
    g.add_edge(joe, tom)
    actual = g.breadth_first(tom)
    expected = ['Tom', 'Bob', 'Jon', 'Sam', 'Joe']
    assert actual == expected
Пример #15
0
def test_breadth_first_example_2():
    """ Refer to the README Examples for visual aids.
    """
    g = Graph()
    bob = g.add_node('Bob')
    tom = g.add_node('Tom')
    sam = g.add_node('Sam')
    joe = g.add_node('Joe')
    jon = g.add_node('Jon')
    g.add_edge(bob, tom)
    g.add_edge(tom, bob)
    g.add_edge(tom, jon)
    g.add_edge(tom, sam)
    g.add_edge(tom, joe)
    g.add_edge(jon, tom)
    g.add_edge(jon, sam)
    g.add_edge(sam, jon)
    g.add_edge(sam, tom)
    g.add_edge(sam, joe)
    g.add_edge(joe, sam)
    g.add_edge(joe, tom)
    actual = g.breadth_first(bob)
    expected = ['Bob', 'Tom', 'Jon', 'Sam', 'Joe']
    assert actual == expected