Exemplo n.º 1
0
def test_del_node():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.add_edge('A', 'B', 25)
    new_graph.add_edge('B', 'A', 10)
    new_graph.del_node('A')
    assert new_graph.graph == {'B': {}}
def test_del_edge():
    from simple_graph import SimpleGraph
    instance = SimpleGraph()
    instance.add_edge('waffles', 'waffles2')
    instance.add_edge('waffles', 'waffles3')
    instance.del_edge('waffles', 'waffles2')
    assert instance.graph_dict['waffles'] == {'waffles3': 1}
def test_add_node():
    """Test add node."""
    from simple_graph import SimpleGraph
    instance = SimpleGraph()
    instance.add_node("waffles")
    instance.add_node("waffles2")
    instance.add_node("waffles3")
    assert 'waffles' in instance.graph_dict
    assert 'waffles2' in instance.graph_dict
    assert 'waffles3' in instance.graph_dict
def test_graph():
    test_dict = {
        1: {2: 3, 3: 5},
        2: {1: 3},
        3: {2: 7, 10: 8},
        10: {}
    }
    test_graph = SimpleGraph()
    test_graph._graph_content = test_dict
    return test_graph
Exemplo n.º 5
0
def test_dijkstra():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.graph = {'A': {'B': 5, 'C': 20},
                       'B': {'D': 5, 'E': 3},
                       'D': {},
                       'E': {'F': 13},
                       'C': {'F': 16},
                       'F': {'A': 100},
                       }
    distance, path = new_graph.dijkstra('A', 'F')
    assert distance == 21
    assert path == ['A', 'B', 'E', 'F']
def make_graph():
    g = SimpleGraph()
    g.add_edge('a', 'b')
    g.add_edge('b', 'c')
    g.add_edge('c', 'f')
    g.add_edge('g', 'f')
    g.add_edge('a', 'd')
    g.add_edge('d', 'e')
    g.add_edge('e', 'a')
    return g
Exemplo n.º 7
0
def test_bft_cyclic():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.graph = {'A': {'B': 10, 'C': 20},
                       'B': {'D': 5, 'E': 3},
                       'D': {},
                       'E': {'F': 13},
                       'C': {'F': 16},
                       'F': {'A': 100},
                       }
    visited = new_graph.bft('A')
    keys = list(new_graph.graph.keys())
    for val in visited:
        assert val in keys
Exemplo n.º 8
0
def test_depth_first_traversal():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.graph = {'A': {'B': 10, 'C': 20},
                       'B': {'D': 5, 'E': 3},
                       'D': {},
                       'E': {'F': 13},
                       'C': {'F': 16},
                       'F': {},
                       }
    visited = new_graph.dft('A')
    keys = list(new_graph.graph.keys())
    for val in visited:
        assert val in keys
def test_add_edge():
    from simple_graph import SimpleGraph
    instance = SimpleGraph()
    instance.add_node("waffles")
    instance.add_node("waffles2")
    instance.add_edge('waffles', 'waffles2')
    assert 'waffles2' in instance.graph_dict['waffles']
def make_graph_three():
    my_graph = SimpleGraph()
    my_graph.add_node('a')
    my_graph.add_node('b')
    my_graph.add_node('c')
    my_graph.add_edge('a', 'c')
    my_graph.add_edge('b', 'a')
    return my_graph
def test_display_nodes():
    from simple_graph import SimpleGraph
    instance = SimpleGraph()
    instance.add_node("waffles")
    instance.add_node("waffles2")
    instance.add_node("waffles3")
    for waffle in ['waffles', 'waffles2', 'waffles3']:
        assert waffle in instance.nodes()
Exemplo n.º 12
0
def test_adjacent_error():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.graph = {'A': {'A': 10, 'B': 25}, 'B': {}}
    with pytest.raises(KeyError):
        new_graph.adjacent('A', 'E')
Exemplo n.º 13
0
class IsomorfismTest:
    def __init__(self, matrix_1, matrix_2, method, wl_dim=None):
        self.graph_1 = SimpleGraph(matrix_1)
        self.graph_2 = SimpleGraph(matrix_2)
        self.n = len(matrix_1)
        self.method = method
        self.time = 0
        self.result = False
        self.wl_dim = wl_dim

    def make_test(self):
        time_start = time.time()

        if self.method == "Brute":
            self.result = self.brute_method()
        elif self.method == "ParallelBrute":
            self.result = self.parallel_brute_method()
        elif self.method == "QueueBrute":
            self.result = self.queue_brute_method()
        elif self.method == "WL":
            self.result = self.wl_method()

        self.time = time.time() - time_start + 0.000000000001

    def queue_brute_method(self):
        workers_amount = self.n

        confirmed_isomorphism = Event()
        permutations_to_check = Queue()
        to_check_counter = Value('i', self.n)
        worker_processess = []

        #add single permutations to initiall Queue
        for v in range(self.n):
            permutations_to_check.put([v])

        #create and start processess
        for w in list(range(workers_amount)):
            worker = Process(target=queue_worker_function,
                             args=(w, self, permutations_to_check,
                                   confirmed_isomorphism, to_check_counter))
            worker.start()
            worker_processess.append(worker)
            # print("Started worker: %d" % (w))

        for worker in worker_processess:
            worker.join()

        return bool(confirmed_isomorphism.is_set())

    # uses BruteProcess
    def parallel_brute_method(self):
        confirmed_isomorphism = Event()
        processess = []

        # create and start processess
        for v in list(range(self.n)):
            process = Process(target=pararell_brute_try_fix,
                              args=(self, [v], confirmed_isomorphism))
            process.start()
            # print("Started process: %s" % (v))
            processess.append(process)

        # wait untill all processess are completed
        for process in processess:
            process.join()

        return bool(confirmed_isomorphism.is_set())

    def brute_method(self):
        return self.brute_try_fix([])

    def brute_try_fix(self, bijection_list):
        if not (SimpleGraph(self.graph_2.subgraph_by_bijection(
                bijection_list)).matrix_compare_to_another(
                    SimpleGraph(
                        self.graph_1.subgraph_by_bijection(
                            range(len(bijection_list)))))):
            return False

        if len(bijection_list) == self.n:
            return SimpleGraph(
                self.graph_2.subgraph_by_bijection(
                    bijection_list)).matrix_compare_to_another(self.graph_1)
        else:
            unused_vertices = list(range(self.n))
            for used_vertex in bijection_list:
                unused_vertices.remove(used_vertex)
            for vertex in unused_vertices:
                if self.brute_try_fix(bijection_list + [vertex]):
                    return True
            return False

    def wl_method(self):
        graph_1_neighbour_list = self.graph_1.neighbour_list()
        graph_2_neighbour_list = self.graph_2.neighbour_list()

        # vertices' colors
        graph_1_colors = [0] * self.n
        graph_2_colors = [0] * self.n

        # repeat method method_dim times
        for i in range(self.wl_dim):
            # calc collections of neighbours' colors for each vertex
            graph_1_collection = []
            graph_2_collection = []
            for vertex in range(self.n):
                neighbours_colors = []
                for neighbour in graph_1_neighbour_list[vertex]:
                    bisect.insort(neighbours_colors, graph_1_colors[neighbour])
                graph_1_collection.append(neighbours_colors)
                neighbours_colors = []
                for neighbour in graph_2_neighbour_list[vertex]:
                    bisect.insort(neighbours_colors, graph_2_colors[neighbour])
                graph_2_collection.append(neighbours_colors)

            # prepare color - collection pairs
            pairs = []
            color_index = 0
            for vertex in range(self.n):
                collection = graph_1_collection[vertex]
                if not collection in [row[1] for row in pairs]:
                    pairs.append([color_index, collection])
                    color_index += 1

            # check if all collections from graph_2 are in prepared pairs
            for vertex in range(self.n):
                if not graph_2_collection[vertex] in [row[1] for row in pairs]:
                    return False

            # assign new colors
            for vertex in range(self.n):
                graph_1_colors[vertex] = pairs[[row[1] for row in pairs].index(
                    graph_1_collection[vertex])][0]
                graph_2_colors[vertex] = pairs[[row[1] for row in pairs].index(
                    graph_2_collection[vertex])][0]

        # on the end compare vertices' colors
        for vertex in range(self.n):
            if graph_1_colors[vertex] != graph_1_colors[vertex]:
                return False

        # not returned False before
        return True
def gr():
    """Simple fixture to save importing SimpleGraph in every test."""
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    return new_graph
def test_sg_init():
    """Ensure init returns a valid dictionary at self.graph."""
    from simple_graph import SimpleGraph
    test_sg = SimpleGraph()
    assert test_sg.graph == {}
def test_sg_add_node(data, result):
    """Ensure new node is added to the graph."""
    from simple_graph import SimpleGraph
    test_sg = SimpleGraph()
    test_sg.add_node(data)
    assert test_sg.graph[data] == result
Exemplo n.º 17
0
def test_neighbors_error():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.graph = {'A': {'B': 15}}
    with pytest.raises(KeyError):
        new_graph.neighbors('E')
Exemplo n.º 18
0
def test_del_node_error():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    with pytest.raises(KeyError):
        new_graph.del_node('A')
Exemplo n.º 19
0
def test_del_edge_val_error():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.graph = {'A': {'A': 10, 'B': 25}}
    with pytest.raises(ValueError):
        new_graph.del_edge('A', 'E')
def test_sg_has_node_false():
    """Ensure has_node returns false when called with node not in graph."""
    from simple_graph import SimpleGraph
    test_sg = SimpleGraph()
    assert test_sg.has_node("a") is False
def test_sg_has_node_true():
    """Ensure has_node returns true when called with node in graph."""
    from simple_graph import SimpleGraph
    test_sg = SimpleGraph()
    test_sg.add_node("a")
    assert test_sg.has_node("a") is True
def test_sg_adjacent_true():
    """Ensure adjacent returns true if a and b share an edge."""
    from simple_graph import SimpleGraph
    test_sg = SimpleGraph()
    test_sg.add_edge("a", "b")
    assert test_sg.adjacent("a", "b") is True
def test_sg_nodes():
    """Ensure nodes returns a correct list of nodes in graph."""
    from simple_graph import SimpleGraph
    test_sg = SimpleGraph()
    test_node = test_sg.nodes()
    assert test_node == []
def test_sg_edges_empty_graph():
    """Ensure edges returns a correct list of edges in graph."""
    from simple_graph import SimpleGraph
    test_edge = SimpleGraph().edges()
    assert test_edge == []
Exemplo n.º 25
0
def graph_test_case_three():
    """Return a test instance that is cyclic for graph traversal."""
    from simple_graph import SimpleGraph
    instance = SimpleGraph()
    instance.add_edge("a", "b")
    instance.add_edge("b", "c")
    instance.add_edge("c", "d")
    instance.add_edge("c", "e")
    instance.add_edge("d", "h")
    instance.add_edge("h", "d")
    instance.add_edge("e", "f")
    instance.add_edge("f", "g")
    instance.add_edge("g", "a")
    instance.add_edge("g", "i")
    instance.add_edge("i", "j")
    return instance
Exemplo n.º 26
0
def test_has_node():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.graph = {'A': {'A': 15}}
    assert new_graph.has_node('A') is True
    assert new_graph.has_node('B') is False
Exemplo n.º 27
0
def test_nodes():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.graph = {'A': ['A', 'B', 'C']}
    assert new_graph.nodes() == ['A']
def test_sg_edges():
    """Ensure edges returns correct list of edges in graph."""
    from simple_graph import SimpleGraph
    test_sg = SimpleGraph()
    test_sg.add_edge("node_a", "node_b")
    assert test_sg.edges() == [("node_a", "node_b")]
Exemplo n.º 29
0
def test_add_edge():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.add_edge('B', 'A', 25)
    assert 'A' in new_graph.graph['B']
    assert new_graph.graph['B']['A'] == 25
def test_has_node_doesnt_exist():
    from simple_graph import SimpleGraph
    instance = SimpleGraph()
    assert instance.has_node("waffles") is False
Exemplo n.º 31
0
def test_edges():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.add_edge('A', 'B', 25)
    assert new_graph.edges() == [('A', 'B', 25)]
def test_neighbors():
    from simple_graph import SimpleGraph
    instance = SimpleGraph()
    instance.add_edge('waffles', 'waffles2')
    instance.add_edge('waffles', 'waffles3')
    assert instance.neighbors('waffles') == {'waffles2', 'waffles3'}
Exemplo n.º 33
0
def test_add_node():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.add_node('A')
    assert list(new_graph.graph.keys()) == ['A']
    assert new_graph.graph['A'] == {}
def test_neighbors_empty():
    from simple_graph import SimpleGraph
    instance = SimpleGraph()
    assert instance.neighbors('waffles') is None
Exemplo n.º 35
0
def test_del_edge():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.graph = {'A': {'A': 10, 'B': 25}}
    new_graph.del_edge('A', 'B')
    assert new_graph.graph == {'A': {'A': 10}}
def test_adjacent():
    from simple_graph import SimpleGraph
    instance = SimpleGraph()
    instance.add_edge('waffles', 'waffles2')
    assert instance.adjacent('waffles', 'waffles2')
Exemplo n.º 37
0
def test_del_edge_key_error():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.graph = {'A': {'A': 10, 'B': 25}}
    with pytest.raises(KeyError):
        new_graph.del_edge('G', 'C')
def test_adjacent_empty():
    from simple_graph import SimpleGraph
    instance = SimpleGraph()
    assert instance.adjacent('waffles', 'waffles2') is False
Exemplo n.º 39
0
def test_neighbors():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.graph = {'A': {'B': 15}}
    assert new_graph.neighbors('A') == ['B']
def fixture_graph():
    """Create a fixture."""
    from simple_graph import SimpleGraph
    obj = SimpleGraph()
    obj.add_node('A')
    obj.add_node('B')
    obj.add_node('C')
    obj.add_node('D')
    obj.add_node('E')
    obj.add_node('F')
    obj.add_node('G')
    obj.add_node('H')
    obj.add_edge('A', 'B', 45000000)
    obj.add_edge('A', 'C', .5)
    obj.add_edge('A', 'D', 8)
    obj.add_edge('C', 'E', 5)
    obj.add_edge('C', 'F', 1)
    obj.add_edge('F', 'E', 2)
    obj.add_edge('D', 'G', 7)
    obj.add_edge('G', 'H')
    return obj
Exemplo n.º 41
0
def test_adjacent():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    new_graph.graph = {'A': {'A': 10, 'B': 25}, 'B': {}, 'C': {}}
    assert new_graph.adjacent('A', 'B') is True
    assert new_graph.adjacent('A', 'C') is False
Exemplo n.º 42
0
def gr():
    from simple_graph import SimpleGraph
    new_graph = SimpleGraph()
    return new_graph