예제 #1
0
def test_bfs():
    g = graph03()
    path = g.breadth_first_search(1, 7)
    assert path == [1, 3, 7], path

    path = g.breadth_first_search(1, 900)  # 900 doesn't exit.
    assert path == []
예제 #2
0
def test_bfw():
    g = graph03()
    bfw = g.breadth_first_walk(1)
    walk = [n for n in bfw]
    assert walk == [1, 2, 3, 4, 5, 6, 8, 7], walk

    bfw = g.breadth_first_walk(1, 5)
    walk = [n for n in bfw]
    assert walk == [1, 2, 3, 4, 5]
예제 #3
0
def test_bfs():
    g = graph03()
    d, path = g.breadth_first_search(1, 7)
    assert d == 2, d
    assert path == [1, 3, 7], path

    d, path = g.breadth_first_search(1, 900)  # 900 doesn't exit.
    assert d == float('inf')
    assert path == []
예제 #4
0
def test_bfs():
    g = graph03()
    path = g.breadth_first_search(1, 7)
    assert path == [1, 3, 7], path

    try:
        g.breadth_first_search(1, 900)  # 900 doesn't exit.
        assert False, "900 isn't in the graph"
    except ValueError:
        assert True
예제 #5
0
def test_all_pairs_shortest_path():
    g = graph03()
    d = g.all_pairs_shortest_paths()
    g2 = Graph(from_dict=d)
    for n1 in g.nodes():
        for n2 in g.nodes():
            if n1 == n2:
                continue
            d, path = g.shortest_path(n1, n2)
            d2 = g2.edge(n1, n2)
            assert d == d2

    g2.add_node(100)
    d = g2.all_pairs_shortest_paths()
    # should trigger print of isolated node.
예제 #6
0
def test_shortest_path01():
    g = graph03()
    dist_g, path_g = g.shortest_path(1, 8)
    assert path_g == [1, 2, 4, 8], path_g

    h = graph04()
    distH, pathH = g.shortest_path(1, 8)
    assert pathH == [1, 2, 4, 8], pathH
    pathH = pathH[2:] + pathH[:2]
    assert path_g != pathH, (path_g, pathH)

    assert g.same_path(path_g, pathH)
    assert h.same_path(path_g, pathH)

    reverseG = list(reversed(path_g))
    assert not g.same_path(path_g, reverseG)

    assert g.has_path(path_g)