Example #1
0
def test_build_lattice_graph(n=5):

    with pytest.raises(ValueError):
        G = graphs.build_lattice_graph(1)

    for i in range(2, n):
        G = graphs.build_lattice_graph(i)

        assert G.number_of_nodes() == i * i
        assert G.number_of_edges() == 2 * i**2 - 2 * i
def test_number_of_boxes_lattice():

    for n in range(2, 10):
        G = graphs.build_lattice_graph(n)

        # Test lb = 1
        assert greedy.num_boxes_from_graph(G, 1) == n * n

        # Test lb = 2 * n - 2 (2 * n - 2 is the diameter of the graph)
        assert greedy.num_boxes_from_graph(G, 2 * n - 2) == 2
def test_color_matrix_lattice_2():

    G = graphs.build_lattice_graph(2)

    paths = nx.shortest_path(G)

    c = greedy.color_matrix(G, paths)

    lb = 1
    assert c[0][lb - 1] == 0
    assert c[1][lb - 1] == 1
    assert c[2][lb - 1] == 2
    assert c[3][lb - 1] == 3

    lb = 2
    assert c[0][lb - 1] == 0
    assert c[1][lb - 1] == 0
    assert c[2][lb - 1] == 1
    assert c[3][lb - 1] == 1
def test_graph_diameter():

    for i in range(2,10):
        G = graphs.build_path_graph(i)

        paths = nx.shortest_path(G)

        assert greedy.graph_diameter(paths) == i - 1

    for i in range(2, 10):
        G = graphs.build_lattice_graph(i)

        paths = nx.shortest_path(G)

        assert greedy.graph_diameter(paths) == 2 * i - 2

    G = graphs.build_song2007_graph()

    paths = nx.shortest_path(G)

    assert greedy.graph_diameter(paths) == 4
def test_color_matrix_lattice_3():

    G = graphs.build_lattice_graph(3)

    paths = nx.shortest_path(G)

    c = greedy.color_matrix(G, paths)

    lb = 1
    for i in range(9):
        assert c[i][lb - 1] == i

    lb = 4
    assert c[0][lb - 1] == 0
    assert c[1][lb - 1] == 0
    assert c[2][lb - 1] == 0
    assert c[3][lb - 1] == 0
    assert c[4][lb - 1] == 0
    assert c[5][lb - 1] == 0
    assert c[6][lb - 1] == 1
    assert c[7][lb - 1] == 0
    assert c[8][lb - 1] == 1
    print("TDF Path:", p[0])

    f.add_subplot(1, 2, 1)
    plt.title("Path (N = {})".format(N))
    plt.xlabel("log($l_B$)")
    plt.ylabel("log($N_B$)")
    plt.loglog(lb, Nb, 'o')

    x = np.linspace(min(np.log(lb)), max(np.log(lb)), 100)
    plt.loglog(np.exp(x),
               np.exp(x * p[0] + p[1]),
               label="Slope: {:.3f}".format(p[0]))
    plt.legend()

    N = 6
    G = graphs.build_lattice_graph(N, pbc=pbc)
    if fuzzy:
        p, lb, Nb = tfd_fuzzy(G)
    else:
        #p, lb, Nb = tfd_greedy_slow(G)
        p, lb, Nb = tfd_greedy(G)

    print(lb, Nb)
    print("TDF Lattice:", p[0])

    f.add_subplot(1, 2, 2)
    plt.title("Lattice (N = {})".format(N * N))
    plt.xlabel("log($l_B$)")
    plt.ylabel("log($N_B$)")
    plt.loglog(lb, Nb, 'o')