def test_subgraph(): g = graph3x3() 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 = graph3x3() g3.add_edge(3, 100, 7) assert not g3.is_subgraph(g2)
def test_adjacency_matrix(): g = graph3x3() 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_phase_lines_with_inner_loop3(): g = graph3x3() 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
def test_distance(): g = graph3x3() 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.
def test_all_paths_start_is_end(): g = graph3x3() try: g.all_paths(2, 2) raise AssertionError("a value error should have been raised.") except ValueError: pass
def test_all_paths02(): g = graph3x3() 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_loop(): g = graph3x3() g.add_edge(9, 1) try: _ = g.phase_lines() raise AssertionError("The graph is a cycle.") except AttributeError: assert True
def test_distance_map_multiple_starts_and_ends(): g = graph3x3() d = g.distance_map(starts=[1, 3], ends=[7, 9]) assert d == {1: 0, 3: 0, 2: 1, 4: 1, 6: 1, 5: 2, 7: 2, 9: 2, 8: 3}, d
def test_in_and_out_degree(): g = graph3x3() in_degree = {1: 0, 2: 1, 3: 1, 4: 1, 5: 2, 6: 2, 7: 1, 8: 2, 9: 2} out_degree = {1: 2, 2: 2, 3: 1, 4: 2, 5: 2, 6: 1, 7: 1, 8: 1, 9: 0} for node in g.nodes(): assert g.in_degree(node) == in_degree[node] assert g.out_degree(node) == out_degree[node]
def test_distance_map(): g = graph3x3() d = g.distance_map(starts=1) assert all(d[i] == 1 for i in [2, 4]) assert all(d[i] == 2 for i in [3, 5, 7]) assert all(d[i] == 3 for i in [6, 8]) assert all(d[i] == 4 for i in [9])
def test_all_paths03(): g = graph3x3() 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_distance_map_with_starts_and_ends(): g = graph3x3() g.add_edge(0, 1, 1) d = g.distance_map(starts=[2, 4], ends=[6, 8]) assert d == {2: 0, 4: 0, 3: 1, 5: 1, 7: 1, 6: 2, 8: 2, 9: 3}, d assert 0 not in d
def test_distance_map_with_ends(): g = graph3x3() d = g.distance_map(starts=1, ends=5) assert d[5] == 2 assert d == {1: 0, 2: 1, 4: 1, 3: 2, 5: 2, 7: 2, 6: 3, 8: 3}, d
def test_phase_lines_with_inner_loop2(): g = graph3x3() 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
def test_distance(): g = graph3x3() p = [1, 2, 3, 6, 9] assert g.distance_from_path(p) == 4 try: g.distance_from_path([1, 2, 3, 900]) # 900 doesn't exist. assert False, "900 isn't in the graph." except ValueError: assert True
def test_distance_map_reverse_with_start(): g = graph3x3() g.add_edge(0, 1, 1) d = g.distance_map(starts=[2, 4], ends=9, reverse=True) assert d == {9: 0, 6: 1, 8: 1, 3: 2, 5: 2, 7: 2, 2: 3, 4: 3, 1: 4}, d assert 0 not in d
def test_network_size(): g = graph3x3() 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.
def test_nodes_from_node(): g = graph3x3() 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} g.add_node(44) assert set(g.nodes(out_degree=0)) == {9, 44} 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
def test_add_node_attr(): g = graph3x3() 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.
def test_adjacency_matrix(): g = graph3x3() am = g.adjacency_matrix() g2 = Graph(from_dict=am) assert g.is_subgraph(g2) assert not g2.is_subgraph(g)
def test_edges_with_node(): g = graph3x3() 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.
def test_all_paths01(): g = graph3x3() paths = g.all_paths(1, 3) assert len(paths) == 1, paths assert paths[0] == [1, 2, 3]
def test03(): g = graph3x3() all_edges = g.edges() edges = g.edges(path=[1, 2, 3, 6, 9]) for edge in edges: assert edge in all_edges, edge
def test_shortest_path_fail(): g = graph3x3() d, p = g.shortest_path(start=9, end=1) # there is no path. assert d == float('inf') assert p == []
def test_is_not_cyclic(): g = graph3x3() assert not g.has_cycles()
def test_distance_map_reverse(): g = graph3x3() d = g.distance_map(ends=9, reverse=True) assert d == {9: 0, 6: 1, 8: 1, 3: 2, 5: 2, 7: 2, 2: 3, 4: 3, 1: 4}
def test_distance_map_fail(): g = graph3x3() d = g.distance_map(starts=9) # All edges end in 9, so the dict should contain nothing. assert d == {9: 0}