def test_one_node():
    graph = Graph()
    graph.add_node('node1')
    graph.add_edge('node1','node1')
    actual = graph.get_neighbors('node1')
    expected = 'node1 :edge--> node1 ... weight: 1'
    assert expected == actual
Exemple #2
0
def test_add_edge():
    gr = Graph()
    start = gr.add_node('start')
    end = gr.add_node('end')
    gr.add_edge(start, end)

    assert gr.adjacency_list[start][0] == (end, 1)
def test_nodes_retrived():
    graph=Graph()
    graph.add_node('a')
    graph.add_node('b')
    actual = graph.get_nodes()
    expected = {'a': [],'b': []}
    assert expected == actual
def test_size():
    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    actual = graph.size()
    expected = 2
    assert expected == actual
Exemple #5
0
def test_get_nodes():
    gr = Graph()
    gr.add_node('1')
    gr.add_node('2')
    gr.add_node('3')

    assert len(gr.get_nodes()) == 3
Exemple #6
0
def test_depth_first_xhallenge_example():

    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(b, c)
    graph.add_edge(b, d)
    graph.add_edge(c, g)
    graph.add_edge(a, d)
    graph.add_edge(d, e)
    graph.add_edge(d, h)
    graph.add_edge(d, f)

    graph.add_edge(f, h)

    vertices_lst = graph.depth_first(a)

    assert vertices_lst == [
        a.value, b.value, c.value, g.value, d.value, e.value, h.value, f.value
    ]
def test_collection_of_nodes_properly_retrived():
    g = Graph()
    g.add_node('a')
    g.add_node('b')
    g.add_node('c')
    actual = g.get_nodes()
    expected = {'a': [], 'b': [], 'c': []}
    assert expected == actual
def test_one_node_one_edge():
    g = Graph()
    g.add_node('a')

    g.add_edge('a', 'a')
    actual = g.get_neighbors('a')
    expected = 'a --> a edge weight: 1'
    assert expected == actual
def test_edge():
    graph=Graph()
    graph.add_node('a')
    graph.add_node('b')
    graph.add_edge('a', 'b', 8)

    actual = graph.get_neighbors('a')
    expected = 'a :edge--> b ... weight: 8'
    assert actual == expected
Exemple #10
0
def test_add_edge_groovy():

    graph = Graph()

    end = graph.add_node('end')

    start = graph.add_node('start')

    graph.add_edge(start, end)
def test_add_edge_other_case():

    graph = Graph()

    end = graph.add_node('end')

    start = graph.add_node('start')

    graph.add_edge(start, end)
Exemple #12
0
def test_get_neighbors():
    gr = Graph()
    start = gr.add_node('start')
    end = gr.add_node('end')
    gr.add_edge(start, end, 4)
    neighbors = gr.get_neighbors(start)

    assert neighbors[0][0].value == 'end'
    assert neighbors[0][1] == 4
def test_edge_added_to_graph():
    g = Graph()
    g.add_node('a')
    g.add_node('b')
    g.add_edge('a', 'b', 1)

    actual = g.get_neighbors('a')
    expected = 'a --> b edge weight: 1'
    assert actual == expected
def test_proper_size_is_returned():
    g = Graph()
    g.add_node('a')
    g.add_node('b')
    g.add_node('c')
    g.add_node('d')

    actual = g.size()
    expected = 4
    assert expected == actual
Exemple #15
0
def test_size():

    graph = Graph()

    graph.add_node('apple')

    assert graph.size() == 1

    graph.add_node('banana')

    assert graph.size() == 2
def test_get_nodes():

    graph = Graph()

    hi = graph.add_node('hi')

    hello = graph.add_node('hello')

    actual = len(graph.get_nodes())

    assert actual == 2
def test_size():

    graph = Graph()

    graph.add_node('hello')

    expected = 1

    actual = graph.size()

    assert actual == expected
def test_all_neighbors_can_be_retrived_with_weight():
    g = Graph()
    g.add_node('a')
    g.add_node('b')

    g.add_edge('a', 'b', 4)

    actual = g.get_neighbors('a')
    expected = 'a --> b edge weight: 4'

    assert expected == actual
Exemple #19
0
def test_get_nodes():

    graph = Graph()

    banana = graph.add_node('banana')

    apple = graph.add_node('apple')
    dog = graph.add_node('dog')

    not_in_graph = Vertex('test')

    assert len(graph.get_nodes()) == 3
def test_get_neighbors():

    graph = Graph()

    hi = graph.add_node('hi')
    hello = graph.add_node('hello')

    graph.add_edge(hi, hello)

    actual = graph.get_neighbors(hi)

    assert actual == [(hello, 0)]
Exemple #21
0
def test_breadth_first_regular():

    graph = Graph()

    banana = graph.add_node('banana')
    vertices_lst = graph.breadth_first(banana)

    apple = graph.add_node('apple')
    graph.add_edge(banana, apple)

    vertices_lst = graph.breadth_first(banana)

    assert vertices_lst == [banana, apple]
Exemple #22
0
def test_breadth_first_regular2():
    graph = Graph()

    banana = graph.add_node('banana')

    apple = graph.add_node('apple')

    strawberry = graph.add_node('strawberry')
    graph.add_edge(banana, apple)

    graph.add_edge(strawberry, banana)

    vertices_lst = graph.breadth_first(strawberry)
    assert vertices_lst == [strawberry, banana, apple]
Exemple #23
0
def test_depth_first_given_vertex_not_in_graph():

    graph = Graph()

    a = graph.add_node('a')
    b = graph.add_node('b')
    c = graph.add_node('c')
    d = Vertex('d')

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

    with pytest.raises(ValueError):
        vertices_lst = graph.depth_first(d)
Exemple #24
0
def test_add_edge():

    graph = Graph()

    end = graph.add_node('banana')

    start = graph.add_node('apple')

    graph.add_edge(start, end)

    assert graph._adjacency_list[start][0] == (end, 0)
    assert graph.get_neighbors(start) == [(end, 0)]
    graph.add_edge(end, start)
    assert graph.get_neighbors(end) == [(start, 0)]
Exemple #25
0
def test_add_edge_with_weight():

    graph = Graph()

    end = graph.add_node('banana')

    start = graph.add_node('apple')

    graph.add_edge(start, end, 44)

    assert graph.get_neighbors(start) == [(end, 44)]

    graph.add_edge(end, start, 23)

    assert graph.get_neighbors(end) == [(start, 23)]
Exemple #26
0
def test_add_edge_with_weight_letters():

    graph = Graph()

    end = graph.add_node('dog')

    start = graph.add_node('cat')

    graph.add_edge(start, end, "a")

    assert graph.get_neighbors(start) == [(end, "a")]

    graph.add_edge(end, start, "b")

    assert graph.get_neighbors(end) == [(start, "b")]
Exemple #27
0
def test_breadth_first_one_element():

    graph = Graph()

    banana = graph.add_node('banana')
    vertices_lst = graph.breadth_first(banana)
    assert vertices_lst == [banana]
def test_add_node():

    graph = Graph()

    actual = graph.add_node('hello').value

    assert actual == 'hello'
def graph_test():
    graph = Graph()
    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    graph.add_node('c')
    graph.add_node('d')
    graph.add_edge('a', 'b', 4)
    graph.add_edge('a', 'd', 9)
    graph.add_edge('a', 'c', 3)
    graph.add_edge('b', 'a', 4)
    graph.add_edge('c', 'a', 3)
    graph.add_edge('c', 'd', 6)
    graph.add_edge('d', 'a', 9)
    graph.add_edge('d', 'b', 5)
    graph.add_edge('d', 'c', 6)
    return graph
Exemple #30
0
def test_breadth_first_different_start_node():

    graph = Graph()

    banana = graph.add_node('banana')

    apple = graph.add_node('apple')

    strawberry = graph.add_node('strawberry')

    graph.add_edge(banana, apple)

    graph.add_edge(strawberry, banana)

    vertices_lst = graph.breadth_first(banana)

    assert vertices_lst == [banana, apple]