コード例 #1
0
def test_get_vertex_by_id_negative2():
    graph = Graph()

    with pytest.raises(DataStructureIsEmptyException) as exception_info:
        vertex = graph.get_vertex_by_id('A')

    assert str(exception_info.value) == data_structure_is_empty_message()
コード例 #2
0
def test_edges():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')

    graph.add_edge('A', 'B', 10)
    graph.add_edge('A', 'C', 20)
    graph.add_edge('B', 'C', 20)

    edges = graph.edges

    assert len(edges) == 3

    first = edges[0]
    assert first.first.identifier == 'A'
    assert first.second.identifier == 'B'
    assert first.weight == 10

    second = edges[1]
    assert second.first.identifier == 'A'
    assert second.second.identifier == 'C'
    assert second.weight == 20

    third = edges[2]
    assert third.first.identifier == 'B'
    assert third.second.identifier == 'C'
    assert third.weight == 20
コード例 #3
0
def test_bfs_positive1():
    graph = Graph()

    graph.create_vertex_by_id('A')

    distance = bfs(graph, graph.get_vertex_by_id('A'))

    assert distance['A'] == 0
コード例 #4
0
def test_topological_sort3(sort):
    graph = Graph()

    graph.create_vertex_by_id('A')

    result = sort(graph)

    assert result[0].identifier == 'A'
コード例 #5
0
def test_get_vertex_by_id_negative1():
    graph = Graph()

    graph.create_vertex_by_id('B')

    with pytest.raises(NotContainsElementException) as exception_info:
        vertex = graph.get_vertex_by_id('A')

    assert str(exception_info.value) == not_contains_vertex_message('A')
コード例 #6
0
def test_dijkstra_negative2(f):
    graph = Graph()

    graph.create_vertex_by_id('A')

    with pytest.raises(NotContainsElementException) as exception_info:
        f(graph, Vertex('B'))

    assert str(exception_info.value) == not_contains_vertex_message('B')
コード例 #7
0
ファイル: test_dfs.py プロジェクト: dosart/Graph_algorithms
def test_all_vertex_visited_dfs_recursion1():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')

    graph.add_edge('A', 'B')
    graph.add_edge('A', 'C')
    graph.add_edge('C', 'C')

    visited = dfs_recursion(graph, pre_visit, post_visit)

    assert all(value == True for value in visited.values())
コード例 #8
0
def test_dijkstra_negative1(f):
    graph = Graph()

    with pytest.raises(DataStructureIsEmptyException) as exception_info:
        f(graph, Vertex('A'))

    assert str(exception_info.value) == data_structure_is_empty_message()
コード例 #9
0
def test_kruskal3():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')

    graph.add_edge('A', 'C', 5)
    graph.add_edge('B', 'C', 2)

    graph.add_edge('C', 'A', 5)
    graph.add_edge('C', 'B', 2)

    edges = kruskal(graph)

    assert len(edges) == 2
    assert find_edge('A', 'C', edges)
    assert find_edge('B', 'C', edges)
コード例 #10
0
ファイル: test_dfs.py プロジェクト: dosart/Graph_algorithms
def test_all_vertex_visited_dfs_iterative2():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')
    graph.create_vertex_by_id('D')
    graph.create_vertex_by_id('E')

    visited = dfs_iterative(graph)

    assert all(value == True for value in visited.values())
コード例 #11
0
def test_vertex_not_found2():
    graph = Graph()
    graph.create_vertex_by_id('B')

    with pytest.raises(NotContainsElementException) as exception_info:
        graph.add_edge('A', 'B')
    assert str(exception_info.value) == not_contains_vertex_message('A')
コード例 #12
0
def test_bfs_positive2():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')
    graph.create_vertex_by_id('D')

    graph.add_edge('A', 'B')
    graph.add_edge('A', 'C')

    distance = bfs(graph, graph.get_vertex_by_id('A'))

    assert distance['A'] == 0
    assert distance['B'] == 1
    assert distance['C'] == 1

    assert distance['D'] is None
コード例 #13
0
def test_one_component():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')

    graph.add_edge('A', 'B')
    graph.add_edge('B', 'C')

    components = connected_components(graph)

    assert components['A'] == 1
    assert components['B'] == 1
    assert components['C'] == 1
コード例 #14
0
def test_add_vertex_second_time():
    graph = Graph()

    graph.create_vertex_by_id('A')

    with pytest.raises(GraphContainsVertexExemption) as exception_info:
        graph.create_vertex_by_id('A')
    assert str(exception_info.value) == graph_contains_vertex_message('A')
コード例 #15
0
def test_contains_negative():
    graph = Graph()

    graph.create_vertex_by_id('K')
    graph.create_vertex_by_id('L')
    graph.create_vertex_by_id('M')

    assert False is ('F' in graph)
コード例 #16
0
def test_contains_positive():
    graph = Graph()

    graph.create_vertex_by_id('K')
    graph.create_vertex_by_id('L')
    graph.create_vertex_by_id('M')

    assert True is ('L' in graph)
コード例 #17
0
def test_get_vertex_by_id_positive():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')
    length = len(graph)

    vertex = graph.get_vertex_by_id('A')

    assert vertex.identifier == 'A'
    assert length == len(graph)
コード例 #18
0
def test_get_graph_iterator():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')

    for vertex in graph:
        pass
コード例 #19
0
def test_add_vertices():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')

    assert graph.size == 3
    assert len(graph) == 3
    assert graph.is_empty == False
コード例 #20
0
def test_four_component():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')
    graph.create_vertex_by_id('D')

    components = connected_components(graph)

    assert components['A'] == 1
    assert components['B'] == 2
    assert components['C'] == 3
    assert components['D'] == 4
コード例 #21
0
def test_sorted_edges():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')

    graph.add_edge('A', 'B', 15)
    graph.add_edge('A', 'C', 10)
    graph.add_edge('B', 'C', 20)

    edges = graph.get_sorted_edges_by(lambda edge: edge.weight)

    first = edges[0]
    assert first.weight == 10

    second = edges[1]
    assert second.weight == 15

    third = edges[2]
    assert third.weight == 20
コード例 #22
0
def test_dijkstra_positive2(f):
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')
    graph.create_vertex_by_id('D')

    graph.add_edge('A', 'C', 2)
    graph.add_edge('A', 'B', 4)

    distance = f(graph, graph.get_vertex_by_id('A'))

    distance_to_b = get_distance(graph.get_vertex_by_id('B'), distance)
    assert distance_to_b == 4

    distance_to_c = get_distance(graph.get_vertex_by_id('C'), distance)
    assert distance_to_c == 2

    distance_to_d = get_distance(graph.get_vertex_by_id('D'), distance)
    assert distance_to_d is None
コード例 #23
0
def test_positive1(f):
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')
    graph.create_vertex_by_id('D')
    graph.create_vertex_by_id('E')

    graph.add_edge('A', 'C', 2)
    graph.add_edge('A', 'B', 4)

    graph.add_edge('B', 'C', 3)
    graph.add_edge('B', 'D', 2)
    graph.add_edge('B', 'E', 3)

    graph.add_edge('C', 'B', 1)
    graph.add_edge('C', 'D', 4)
    graph.add_edge('C', 'E', 5)

    graph.add_edge('E', 'D', 1)

    distance = f(graph, graph.get_vertex_by_id('A'))

    distance_to_c = get_distance(graph.get_vertex_by_id('C'), distance)
    assert distance_to_c == 2

    distance_to_b = get_distance(graph.get_vertex_by_id('B'), distance)
    assert distance_to_b == 3

    distance_to_e = get_distance(graph.get_vertex_by_id('E'), distance)
    assert distance_to_e == 6

    distance_to_d = get_distance(graph.get_vertex_by_id('D'), distance)
    assert distance_to_d == 5
コード例 #24
0
def test_kruskal1():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')
    graph.create_vertex_by_id('D')
    graph.create_vertex_by_id('E')
    graph.create_vertex_by_id('F')

    graph.add_edge('A', 'B', 2)
    graph.add_edge('A', 'C', 1)

    graph.add_edge('B', 'A', 2)
    graph.add_edge('B', 'C', 2)
    graph.add_edge('B', 'D', 1)

    graph.add_edge('D', 'C', 2)
    graph.add_edge('D', 'E', 3)
    graph.add_edge('D', 'F', 4)

    graph.add_edge('C', 'D', 2)
    graph.add_edge('C', 'E', 3)
    graph.add_edge('C', 'A', 1)
    graph.add_edge('C', 'B', 2)

    graph.add_edge('E', 'D', 3)
    graph.add_edge('E', 'C', 3)
    graph.add_edge('E', 'F', 2)

    graph.add_edge('F', 'E', 2)
    graph.add_edge('F', 'D', 4)

    edges = kruskal(graph)

    assert len(edges) == 5
    assert find_edge('A', 'B', edges) is not None
    assert find_edge('A', 'C', edges) is not None
    assert find_edge('B', 'D', edges) is not None
    assert find_edge('D', 'E', edges) is not None
    assert find_edge('E', 'F', edges) is not None
コード例 #25
0
ファイル: test_dfs.py プロジェクト: dosart/Graph_algorithms
def test_all_vertex_visited_dfs_iterative1():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')
    graph.create_vertex_by_id('D')
    graph.create_vertex_by_id('E')

    # first component of connectivity
    graph.add_edge('A', 'B')
    graph.add_edge('B', 'C')
    graph.add_edge('C', 'A')

    # second component of connectivity
    graph.add_edge('D', 'E')

    # third component of connectivity
    graph.create_vertex_by_id('K')

    visited = dfs_iterative(graph)

    assert all(value == True for value in visited.values())
コード例 #26
0
def test_kruskal2():
    graph = Graph()

    edges = kruskal(graph)

    assert len(edges) == 0
コード例 #27
0
def test_topological_sort2():
    graph = Graph()

    graph.create_vertex_by_id('A')
    graph.create_vertex_by_id('B')
    graph.create_vertex_by_id('C')
    graph.create_vertex_by_id('G')
    graph.create_vertex_by_id('D')
    graph.create_vertex_by_id('E')

    graph.add_edge('A', 'B')
    graph.add_edge('A', 'G')
    graph.add_edge('B', 'C')
    graph.add_edge('C', 'D')
    graph.add_edge('B', 'D')
    graph.add_edge('G', 'D')
    graph.add_edge('D', 'E')

    result = topological_sort(graph)
    assert result[0].identifier == 'A'
    assert result[1].identifier == 'B'
    assert result[2].identifier == 'G'
    assert result[3].identifier == 'C'
    assert result[4].identifier == 'D'
    assert result[5].identifier == 'E'
コード例 #28
0
def test_make_graph():
    graph = Graph()

    assert graph.size == 0
    assert graph.is_empty == True
    assert len(graph) == 0
コード例 #29
0
def test_add_edge_in_empty_graph():
    graph = Graph()

    with pytest.raises(DataStructureIsEmptyException) as exception_info:
        graph.add_edge('A', 'B')
    assert str(exception_info.value) == data_structure_is_empty_message()