Esempio n. 1
0
def test_add_multiple_edge_weight():
    g = WeightedGraph()
    g.add_node(u"A")
    g.add_edge(u"A", u"C", 10)
    g.add_edge(u"C", u"D", 5)
    g.add_edge(u"A", u"B", 1)
    g.add_edge(u"C", u"A", 5)
    assert g.edgeWeights[(u"A", u"C")] == 10
    assert g.edgeWeights[(u"C", u"A")] == 5
Esempio n. 2
0
def test_del_edge():
    g = WeightedGraph()
    g.add_edge(u"A", u"C")
    g.del_edge(u"A", u"C")
    assert (u"A", u"C") not in g.edges()
    assert u"A" in g.nodes()
    assert u"C" in g.nodes()
    assert g.edgeWeights.pop((u"A", u"C"), False) == False
Esempio n. 3
0
def test_neighbors():
    g = WeightedGraph()
    g.add_edge(u"A", u"C")
    g.add_edge(u"A", u"B")
    g.add_edge(u"A", u"D")
    g.add_edge(u"D", u"E")
    assert u"B" in g.neighbors(u"A")
    assert u"C" in g.neighbors(u"A")
    assert u"D" in g.neighbors(u"A")
def test_shortest_path_triangle_one_edge_path():
    wg = WeightedGraph()
    wg.add_edge('S', 'A', 1)
    wg.add_edge('A', 'E', 3)
    wg.add_edge('S', 'E', 3)
    path, distance = dijkstra(wg, 'S', 'E')
    assert path == ['S', 'E']
    assert distance == 3
def test_shortest_path_triangle_two_edge_path_bfm():
    wg = WeightedGraph()
    wg.add_edge('S', 'A', 1)
    wg.add_edge('A', 'E', 1)
    wg.add_edge('S', 'E', 3)
    path, distance = bellman_ford_moore(wg, 'S', 'E')
    assert path == ['S', 'A', 'E']
    assert distance == 2
Esempio n. 6
0
def test_add_edge_weight():
    g = WeightedGraph()
    g.add_node(u"A")
    g.add_edge(u"A", u"C", 10)
    assert g.edgeWeights[(u"A", u"C")] == 10
Esempio n. 7
0
def test_edges_empty():
    g = WeightedGraph()
    assert g.edges() == set()
Esempio n. 8
0
def test_adj():
    g = WeightedGraph()
    g.add_edge(u"A", u"C")
    g.add_node(u"B")
    print g.edges()
    assert g.adjacent(u"A", u"C") is True
    assert g.adjacent(u"C", u"A") is True
    assert g.adjacent(u"B", u"C") is False
    assert g.adjacent(u"B", u"A") is False
    with pytest.raises(KeyError):
        g.adjacent(u"A", u"D")
Esempio n. 9
0
def test_has_node():
    g = WeightedGraph()
    g.add_node(u"A")
    g.add_node(u"B")
    assert g.has_node(u"A") is True
    assert g.has_node(u"C") is False
Esempio n. 10
0
def test_del_node():
    g = WeightedGraph()
    g.add_edge(u"A", u"C")
    g.del_node(u"A")
    assert u"A" not in g.nodes()
    assert (u"A", u"C") not in g.edges()
Esempio n. 11
0
def test_nodes_empty():
    g = WeightedGraph()
    assert g.nodes() == set()
Esempio n. 12
0
def test_add_edge_two_new_nodes():
    g = WeightedGraph()
    g.add_edge(u"A", u"C")
    assert u"A" in g.nodes()
    assert u"C" in g.nodes()
    assert (u"A", u"C") in g.edges()
Esempio n. 13
0
def test_add_edge():
    g = WeightedGraph()
    g.add_node(u"A")
    g.add_node(u"B")
    g.add_edge(u"A", u"B")
    assert (u"A", u"B") in g.edges()
Esempio n. 14
0
def test_add_node():
    g = WeightedGraph()
    g.add_node("A")
    assert "A" in g.nodes()