コード例 #1
0
def test_if_adding_existing_edge_to_adjacency_list_di_graph_results_in_raising_error(
):
    g = AdjacencyListDiGraph()
    g.add_nodes("a", 1)
    g.add_edges(("a", 1))
    with pytest.raises(ValueError):
        g.add_edges(("a", 1))
コード例 #2
0
def test_if_adding_edge_without_weight_results_in_default_edge_weight_equals_to_one(
):
    g = AdjacencyListGraph()
    g.add_nodes(1, 2)
    g.add_edges((1, 2))
    v = AdjacencyListDiGraph()
    v.add_nodes("x", 4)
    v.add_edges(("x", 4))
    assert g.weight(1, 2) == 1 and v.weight("x", 4) == 1
コード例 #3
0
def test_if_outgoing_neighbors_in_adjacency_list_digraph_results_in_appropriate_nodes(
):
    g = AdjacencyListDiGraph()
    g.add_nodes(1, 2, "a", "b")
    g.add_edges((1, 2), ("a", 2), (2, "b"))
    output = []
    for node in g.outgoing_neighbors(2):
        output.append(node)
    assert output == ["b"]
コード例 #4
0
def test_if_neighbors_in_adjacency_list_digraph_results_in_the_return_of_appropriate_vertices(
):
    g = AdjacencyListDiGraph()
    g.add_nodes(1, 2, 3)
    g.add_edges((2, 1, .3), (1, 3), (1, 2))
    output = []
    for vertex in g.neighbors(1):
        output.append(vertex)
    assert len(output) == 2 and set(output) == {2, 3}
コード例 #5
0
def test_if_adding_the_inverse_of_an_existing_edge_to_adjacency_list_di_graph_results_in_adding_new_edge(
):
    g = AdjacencyListDiGraph()
    g.add_nodes("a", 1)
    g.add_edges(("a", 1))
    g.add_edges((1, "a"))
    assert g._adjacency_list == {
        "a": {
            1: 1
        },
        1: {
            "a": 1
        }
    } and g.number_of_edges == 2
コード例 #6
0
def test_if_designation_edge_weight_results_in_recording_that_value_in_adjacency_list_structure(
):
    g = AdjacencyListGraph()
    g.add_nodes(1, 2)
    g.add_edges((1, 2, .3))
    v = AdjacencyListDiGraph()
    v.add_nodes("x", 4)
    v.add_edges(("x", 4, -.2))
    assert g._adjacency_list == {
        1: {
            2: .3
        },
        2: {
            1: .3
        }
    } and v._adjacency_list == {
        "x": {
            4: -.2
        },
        4: {}
    }
コード例 #7
0
import pytest
from Dijkstra import Dijkstra
from AdjacencyListDiGraph import AdjacencyListDiGraph
from Dijkstra_oop import Dijkstra

G = AdjacencyListDiGraph()
G.add_node("A", "B", "C", "D", "E")
G.add_edge(("A", "B", 2), ("A", "D", 4), ("B", "C", 3), ("B", "D", 3),
           ("C", "E", 2), ("D", "E", 4), ("D", "C", 3))

# def test_if_shortest_path

# def test_if_shortest_path_result_in_appropriate_sequence_of_vertices():
#     dijkstra = Dijkstra()
#     assert dijkstra.shortest_path(G, "A", "E") == ["A", "B", "C", "E"]