def test_algorithm(self):
     for i in range(10):
         graph = graph_utils.random_graph(100, 0.1)
         g1 = graph_utils.graph_to_numpy(graph)
         g2 = graph_utils.graph_to_numpy(graph)
         cover_group1 = shaked_algo_impl_v2(g1)
         cover_group2 = shaked_algo_impl(g2)
         assert_equal(cover_group1, cover_group2)
Example #2
0
def test_degree():
    graph = graph_utils.graph_to_numpy(igraph.Graph.Full(5))
    assert_equal(vsa.all_vertex_degree(graph), [4, 4, 4, 4, 4])

    graph = graph_utils.graph_to_numpy(igraph.Graph.Ring(5))
    assert_equal(vsa.all_vertex_degree(graph), [2, 2, 2, 2, 2])

    graph = graph_utils.graph_to_numpy(
        igraph.Graph(n=5, edges=[(1, 2), (3, 4), (4, 1)]))
    assert_equal(vsa.all_vertex_degree(graph), [0, 2, 1, 1, 2])
Example #3
0
def main():
    iterations = 1000000
    n = 1000
    c = 1
    results = []

    graph: Graph = Graph.Erdos_Renyi(n, c / n)
    randomize = True

    for i in range(iterations):
        np_graph = graph_to_numpy(graph)

        cover_group = shaked_algo_impl_v2.shaked_algo_impl_v2(np_graph, randomize=randomize)
        cover_group_size = len(cover_group)
        if i % 100 == 0:
            print(f'iteration {i}')
        results.append(cover_group_size)
    results = np.array(results)
    print('=========')
    avg = results.mean()
    print(f'average: {avg}')

    variance = results.var()
    print(f'variance: {variance}')
    np.save('theorem5.npy', results)
    bins = np.max(results)-np.min(results)
    results = (results - avg)/np.sqrt(variance)
    plot_hist(results, bins)

    return results
 def test_zero_vertex(self):
     graph = graph_utils.random_graph(10, 0.3)
     npgraph = graph_utils.graph_to_numpy(graph)
     degree_vector = all_vertex_degree(npgraph)
     zero_vertex(npgraph, 4, degree_vector)
     degree_vector_check = all_vertex_degree(npgraph)
     assert_equal(degree_vector, degree_vector_check)
Example #5
0
 def test_algorithm(self):
     graph = Graph.Erdos_Renyi(10, 0.3)
     cover = most_neighbors_with_minimal_degree.most_neighbors_with_minimal_degree_algo(
         None, graph)
     assert_equal(
         graph_utils.check_if_legal_vertex_cover(
             graph_utils.graph_to_numpy(graph), cover), True)
Example #6
0
def test_check_if_legal_vertex_cover():
    graph = graph_utils.graph_to_numpy(
        Graph(n=5, edges=[(1, 2), (3, 4), (4, 1)]))
    assert_equal(graph_utils.check_if_legal_vertex_cover(graph, ['v1', 'v3']),
                 True)
    assert_equal(graph_utils.check_if_legal_vertex_cover(graph, ['v1', 'v4']),
                 True)
    assert_equal(graph_utils.check_if_legal_vertex_cover(graph, ['v3', 'v4']),
                 False)
    assert_equal(graph_utils.check_if_legal_vertex_cover(graph, ['v1']), False)
    assert_equal(graph_utils.check_if_legal_vertex_cover(graph, ['v0']), False)
Example #7
0
def theorem4():
    results = {}

    for c in cs:
        c_results = []
        print(f'c {c}')
        for i in range(iterations):
            if i % 10 == 0:
                print(f'iteration {i}')

            graph: Graph = Graph.Erdos_Renyi(n, c / n)
            np_graph = graph_to_numpy(graph)

            cover_group = shaked_algo_impl_v2.shaked_algo_impl_v2(np_graph, randomize=True)
            cover_group_size = len(cover_group)
            c_results.append(cover_group_size)
        results[c] = c_results
    return results
Example #8
0
def main():
    iterations = 200
    n = 1000
    cs = np.arange(0.5, 0.75, 0.25)  # c = [0.25..10]
    results = {}

    for c in cs:
        c_results = []
        print(f'c {c}')
        for i in range(iterations):
            if i % 10 == 0:
                print(f'iteration {i}')

            graph: Graph = Graph.Erdos_Renyi(n, c / n)
            np_graph = graph_to_numpy(graph)

            cover_group = degree(np_graph)
            #cover_group = shaked_algo_impl_v2.shaked_algo_impl_v2(np_graph, randomize=True)
            cover_group_size = len(cover_group)
            c_results.append(cover_group_size)
        results[c] = c_results
    return results
Example #9
0
def theorem5():
    results = {}

    for c in cs:
        c_results = []
        print(f'c {c}')
        for i in range(iterations):
            if i % 10 == 0:
                print(f'graph iteration {i}')

            graph_results = []
            graph: Graph = Graph.Erdos_Renyi(n, c / n)
            orig = graph_to_numpy(graph)

            for j in range(iterations):
                np_graph = orig.copy()
                cover_group = shaked_algo_impl_v2.shaked_algo_impl_v2(np_graph, randomize=True)
                cover_group_size = len(cover_group)
                graph_results.append(cover_group_size)
            c_results.append(np.array(graph_results))
        results[c] = np.array(c_results)
    return results
def test_algorithm():
    graph = Graph.Erdos_Renyi(100, 0.05)
    cover = first_vertex_with_degree.first_vertex_with_degree_algo(None, graph)
    assert_equal(
        graph_utils.check_if_legal_vertex_cover(
            graph_utils.graph_to_numpy(graph), cover), True)
def test_algorithm():
    graph = Graph.Erdos_Renyi(100, 0.05)
    cover = neighbors_algo.neighbors_algo(None, graph)
    assert_equal(
        graph_utils.check_if_legal_vertex_cover(
            graph_utils.graph_to_numpy(graph), cover), True)
Example #12
0
def main():
    what_graph = 'gnp'  # gnp | star | ring
    iterations = 1000000
    n = 1000
    c = 1
    results = []

    # if what_graph == 'gnp':
    rhs_a = (n / c) * (c - 1 + np.e**(-c))
    # rhs_b = (n / (2 * c)) * (np.e ** (-c) - 1) ** 2
    rhs_b_1 = 1 / (2 * (c**3) * (np.exp(2 * c)))
    rhs_b_2 = 2 * c * np.exp(c) * (np.exp(c) - 1 - special.expi(c) +
                                   np.log(c) + np.euler_gamma)
    rhs_b_3 = 2 * ((c * np.exp(c) - np.exp(c) + 1)**2)
    rhs_b_4 = (
        (c**2 - 2 * c) * np.exp(2 * c)) + 4 * c * np.exp(c) - c**2 - 2 * c
    rhs_b = rhs_b_1 * (rhs_b_2 - rhs_b_3 + rhs_b_4) * n

    numerator = rhs_a
    denominator = np.sqrt(rhs_b)

    # if what_graph == 'gnp':
    graph: Graph = Graph.Erdos_Renyi(n, c / n)
    randomize = True
    # elif what_graph == 'ring':
    #     graph: Graph = Graph.Ring(n)
    #     randomize = True
    # elif what_graph == 'star':
    #     graph: Graph = Graph.Star(n)
    #     randomize = True
    # else:
    #     raise Exception("Bad what_graph value")

    for i in range(iterations):
        np_graph = graph_to_numpy(graph)

        cover_group = shaked_algo_impl_v2.shaked_algo_impl_v2(
            np_graph, randomize=randomize)
        cover_group_size = len(cover_group)
        if i % 100 == 0:
            print(f'iteration {i}')
        results.append(cover_group_size)
    if what_graph == 'gnp':
        theorem2_c = (np.array(results) - numerator) / denominator

    print('=========')
    avg = np.array(results).mean()
    print(f'average: {avg}')

    if what_graph == 'gnp':
        print(f'rhs_a {rhs_a}')

    variance = np.array(results).var()
    print(f'variance: {variance}')

    if what_graph == 'gnp':
        print(f'rhs_b {rhs_b}')
        np.save('theorem2_c.npy', theorem2_c)
        plot_hist(theorem2_c)

    if what_graph == 'star':
        expectation = (n**2 + n - 2) / (2 * n)
        print(f'star expectation: {expectation}')
    return results
Example #13
0
def test_vertex_support_all():
    graph = graph_utils.graph_to_numpy(
        igraph.Graph(n=5, edges=[(1, 2), (3, 4), (4, 1)]))
    assert_equal(vsa.vertex_support_all(graph), [0, 3, 2, 2, 3])
Example #14
0
import igraph
from numpy.testing import assert_equal, assert_array_almost_equal
from numpy import matrix
import graph_utils

from algorithms.xyz import xyz

graph = xyz.build_sparse(
    graph_utils.graph_to_numpy(
        igraph.Graph(n=5, edges=[(0, 2), (1, 2), (3, 4), (4, 1)])))
expected_degrees = [1, 2, 2, 1, 2]


def test_degrees():
    assert_equal(xyz.all_vertex_degree(graph), expected_degrees)


def test_x_vector():
    expected_x = [1 / 2, 2 / 3, 2 / 3, 1 / 2, 2 / 3]
    assert_equal(xyz.get_x_vector(xyz.all_vertex_degree(graph)), expected_x)


def test_y_vector():
    x = [1 / 2, 2 / 3, 2 / 3, 1 / 2, 2 / 3]
    expected_y = [
        x[0] + x[2], x[1] + x[2] + x[4], x[2] + x[1] + x[0], x[3] + x[4],
        x[4] + x[1] + x[3]
    ]

    degree_vector = xyz.all_vertex_degree(graph)
    x_vector = xyz.get_x_vector(degree_vector)
Example #15
0
 def test_algorithm(self):
     graph = graph_utils.graph_to_numpy(
         Graph(n=3, edges=[(0, 1), (0, 2), (1, 2)]))
     cover_group = degree_minus(graph)
     assert_equal(len(cover_group), 2)
Example #16
0
 def test_random_graphs(self):
     for i in range(10):
         graph = graph_utils.random_graph(100, 0.1)
         g = graph_utils.graph_to_numpy(graph)
         cover = degree_minus(g)
         assert (graph_utils.check_if_legal_vertex_cover(g, cover))