Beispiel #1
0
def test_bfs_on_undirected_graph():
    g = Graph()
    g.add_nodes_from([1, 2, 3, 4, 5])
    g.add_edge(1, 2)
    g.add_edge(1, 3)
    g.add_edge(2, 4)
    g.add_edge(3, 4)

    assert bfs(g, 1) == [1, 2, 3, 4]
Beispiel #2
0
def test_reachability_on_undirected_graph():
    g = Graph()
    g.add_nodes_from([1, 2, 3, 4, 5])
    g.add_edge(1, 2)
    g.add_edge(2, 3)
    g.add_edge(2, 4)
    g.add_edge(3, 4)

    assert is_reachable(g, 1, 2)
    assert is_reachable(g, 2, 1)
    assert is_reachable(g, 1, 3)
    assert is_reachable(g, 3, 1)
    assert is_reachable(g, 1, 4)
    assert is_reachable(g, 4, 1)
    assert is_reachable(g, 2, 3)
    assert is_reachable(g, 3, 2)
    assert is_reachable(g, 2, 4)
    assert is_reachable(g, 4, 2)
    assert is_reachable(g, 3, 4)
    assert is_reachable(g, 4, 3)
Beispiel #3
0
def test_bfs_on_graph_with_no_edges():
    g = Graph()
    g.add_nodes_from([1, 2, 3, 4, 5])

    assert bfs(g, 1) == [1]