Exemple #1
0
def test_subgraph():
    g = graph02()
    g2 = g.subgraph_from_nodes([1, 2, 3, 4])
    d = {1: {2: 1, 4: 1},
         2: {3: 1},
         }
    assert g2.is_subgraph(g)
    for k, v in d.items():
        for k2, d2 in v.items():
            assert g.edge(k, k2) == g2.edge(k, k2)

    g3 = graph02()
    g3.add_edge(3, 100, 7)
    assert not g3.is_subgraph(g2)
Exemple #2
0
def test_distance():
    g = graph02()
    p = [1, 2, 3, 6, 9]
    assert g.distance_from_path(p) == 4

    assert float('inf') == g.distance_from_path([1, 2, 3,
                                                 900])  # 900 doesn't exist.
Exemple #3
0
def test_adjacency_matrix():
    g = graph02()
    am = g.adjacency_matrix()
    g2 = Graph(from_dict=am)
    assert g.is_subgraph(g2)
    assert g2._max_edge_value == float('inf') != g._max_edge_value
    assert not g2.is_subgraph(g)
def test_topological_sort():
    g = graph02()
    outcome = [n for n in g.topological_sort()]
    assert outcome == [1, 2, 7, 3, 4, 5, 6]

    outcome = [n for n in g.topological_sort(key=lambda x: -x)]
    assert outcome == [7, 2, 1, 4, 3, 5, 6]
Exemple #5
0
def test_all_paths_start_is_end():
    g = graph02()
    try:
        g.all_paths(2, 2)
        raise AssertionError("a value error should have been raised.")
    except ValueError:
        pass
Exemple #6
0
def test_all_paths02():
    g = graph02()
    paths = g.all_paths(1, 6)
    assert len(paths) == 3
    expected = [[1, 2, 3, 6], [1, 2, 5, 6], [1, 4, 5, 6]]
    assert all(i in expected for i in paths) and all(i in paths
                                                     for i in expected)
def test_phase_lines_with_inner_loop3():
    g = graph02()
    g.add_edge(9, 10)
    g.add_edge(10, 5)
    p = g.phase_lines()
    expected = {1: 0, 2: 1, 4: 1, 3: 2, 5: 2, 7: 2, 6: 3, 8: 3, 9: 4, 10: 5}
    assert p == expected
Exemple #8
0
def test_all_paths03():
    g = graph02()
    paths = g.all_paths(1, 9)
    assert len(paths) == 6
    expected_result = [[1, 2, 3, 6, 9], [1, 2, 5, 6, 9], [1, 2, 5, 8, 9],
                       [1, 4, 5, 6, 9], [1, 4, 5, 8, 9], [1, 4, 7, 8, 9]]
    assert all(i in expected_result
               for i in paths) and all(i in paths for i in expected_result)
def test_phase_lines_with_loop():
    g = graph02()
    g.add_edge(9, 1)
    try:
        _ = g.phase_lines()
        raise AssertionError("The graph is a cycle.")
    except AttributeError:
        assert True
Exemple #10
0
def test_path_permutations03():
    g = graph02()
    paths = g.all_paths(1, 9)
    assert len(paths) == 6
    assert paths == [[1, 2, 3, 6, 9],
                     [1, 2, 5, 6, 9],
                     [1, 2, 5, 8, 9],
                     [1, 4, 5, 6, 9],
                     [1, 4, 5, 8, 9],
                     [1, 4, 7, 8, 9]], paths
def test_network_size():
    g = graph02()
    ns1 = g.network_size(n1=1)
    assert len(ns1) == 9  # all nodes.

    ns1_2 = g.network_size(n1=1, degrees_of_separation=2)
    assert all(i not in ns1_2 for i in [6, 8, 9])

    ns5 = g.network_size(n1=5)
    assert len(ns5) == 4  # all nodes downstream from 5 (plus 5 itself)

    ns9 = g.network_size(n1=9)
    assert len(ns9) == 1  # just node 9, as there are no downstream peers.
Exemple #12
0
def test_add_node_attr():
    g = graph02()
    g.add_node(1, "this")
    assert set(g.nodes()) == set(range(1, 10))
    node_1 = g.node(1)
    assert node_1 == "this"

    d = {"This": 1, "That": 2}
    g.add_node(1, obj=d)
    assert g.node(1) == d

    rm = 5
    g.del_node(rm)
    for n1, n2, d in g.edges():
        assert n1 != rm and n2 != rm
    g.del_node(rm)  # try again for a node that doesn't exist.
Exemple #13
0
def test_sources():
    g = graph02()
    s = g.sources(5)
    e = {1, 2, 3}
    assert s == e

    s2 = g.sources(1)
    e2 = set()
    assert s2 == e2, s2

    s3 = g.sources(6)
    e3 = {1, 2, 3, 4, 5}
    assert s3 == e3

    s4 = g.sources(7)
    e4 = set()
    assert s4 == e4
Exemple #14
0
def test_nodes_from_node():
    g = graph02()
    nodes = g.nodes(from_node=1)
    assert set(nodes) == {2, 4}
    nodes = g.nodes(to_node=9)
    assert set(nodes) == {6, 8}
    nodes = g.nodes()
    assert set(nodes) == set(range(1, 10))

    try:
        _ = g.nodes(in_degree=-1)
        assert False
    except ValueError:
        assert True

    nodes = g.nodes(in_degree=0)
    assert set(nodes) == {1}
    nodes = g.nodes(in_degree=1)
    assert set(nodes) == {2, 3, 4, 7}
    nodes = g.nodes(in_degree=2)
    assert set(nodes) == {5, 6, 8, 9}
    nodes = g.nodes(in_degree=3)
    assert nodes == []

    try:
        _ = g.nodes(out_degree=-1)
        assert False
    except ValueError:
        assert True

    nodes = g.nodes(out_degree=0)
    assert set(nodes) == {9}
    nodes = g.nodes(out_degree=1)
    assert set(nodes) == {3, 6, 7, 8}
    nodes = g.nodes(out_degree=2)
    assert set(nodes) == {1, 2, 4, 5}
    nodes = g.nodes(out_degree=3)
    assert nodes == []

    try:
        _ = g.nodes(in_degree=1, out_degree=1)
        assert False
    except ValueError:
        assert True
Exemple #15
0
def test_distance():
    G = graph02()
    p = [1, 2, 3, 6, 9]
    assert G.distance_from_path(p) == len(p) - 1

    assert float('inf') == G.distance_from_path([1, 2, 3, 900])  # 900 doesn't exist.
Exemple #16
0
def test_all_paths01():
    g = graph02()
    paths = g.all_paths(1, 3)
    assert len(paths) == 1, paths
    assert paths[0] == [1, 2, 3]
def test_phase_lines_with_inner_loop2():
    g = graph02()
    g.add_edge(3, 2)
    p = g.phase_lines()
    expected = {1: 0, 2: 1, 4: 1, 3: 2, 5: 2, 7: 2, 6: 3, 8: 3, 9: 4}
    assert p == expected
Exemple #18
0
def test_shortest_path_fail():
    g = graph02()
    d, p = g.shortest_path(start=9, end=1)  # there is no path.
    assert d == float('inf')
    assert p == []
Exemple #19
0
def test_edges_with_node():
    g = graph02()
    edges = g.edges(from_node=5)
    assert set(edges) == {(5, 6, 1), (5, 8, 1)}
    assert g.edge(5, 6) == 1
    assert g.edge(5, 600) is None  # 600 doesn't exist.
Exemple #20
0
def test_is_not_cyclic():
    g = graph02()
    assert not g.has_cycles()
Exemple #21
0
def test_path_permutations02():
    g = graph02()
    paths = g.all_paths(1, 6)
    assert len(paths) == 3
    assert paths == [[1, 2, 3, 6], [1, 2, 5, 6], [1, 4, 5, 6]]
Exemple #22
0
def test03():
    g = graph02()
    all_edges = g.edges()
    edges = g.edges(path=[1, 2, 3, 6, 9])
    for edge in edges:
        assert edge in all_edges, edge