def pararell_brute_try_fix(self, bijection_list): print("%s # %s" % (self.base_vertex, bijection_list)) if self.finish.check(): print("%s # %s # FINISHED" % (self.base_vertex, bijection_list)) # return True without calculations because another thread confimed isomorphism return True else: if len(bijection_list) == self.isomorfism_test.n: if SimpleGraph(self.isomorfism_test.graph_2.subgraph_by_bijection(bijection_list)).matrix_compare_to_another( self.isomorfism_test.graph_1): print("%s # %s # FINISHED FIRST #############################" % (self.base_vertex, bijection_list)) self.finish.set_finish() return True else: return False elif not (SimpleGraph(self.isomorfism_test.graph_2.subgraph_by_bijection(bijection_list)).matrix_compare_to_another( SimpleGraph(self.isomorfism_test.graph_1.subgraph_by_bijection(range(len(bijection_list)))))): return False else: unused_vertices = list(range(self.isomorfism_test.n)) for used_vertex in bijection_list: unused_vertices.remove(used_vertex) for vertex in unused_vertices: if self.pararell_brute_try_fix(bijection_list + [vertex]): return True return False
def pararell_brute_try_fix(isomorfism_test, bijection_list, finish_flag): # print("%s # %s" % (bijection_list[0], bijection_list)) if finish_flag.is_set(): # print("%s # %s # FINISHED" % (bijection_list[0], bijection_list)) # return True without calculations because another thread confimed isomorphism return True else: if len(bijection_list) == isomorfism_test.n: if SimpleGraph( isomorfism_test.graph_2.subgraph_by_bijection( bijection_list)).matrix_compare_to_another( isomorfism_test.graph_1): # print("%s # %s # FINISHED FIRST #############################" % (bijection_list[0], bijection_list)) finish_flag.set() return True else: return False elif not (SimpleGraph( isomorfism_test.graph_2.subgraph_by_bijection( bijection_list)).matrix_compare_to_another( SimpleGraph( isomorfism_test.graph_1.subgraph_by_bijection( range(len(bijection_list)))))): return False else: unused_vertices = list(range(isomorfism_test.n)) for used_vertex in bijection_list: unused_vertices.remove(used_vertex) for vertex in unused_vertices: if pararell_brute_try_fix(isomorfism_test, bijection_list + [vertex], finish_flag): return True return False
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 test_sg_neighbors_error(): """Ensure error is raised if node does not exist in graph.""" from simple_graph import SimpleGraph test_sg = SimpleGraph() with pytest.raises(IndexError) as message: test_sg.neighbors("a") assert "That node is not in the graph." in str(message)
def test_sg_neighbors(): """Ensure neighbors for node that exists are returned.""" from simple_graph import SimpleGraph test_sg = SimpleGraph() test_sg.add_edge("b", "z") test_sg.add_edge("b", "c") assert test_sg.neighbors("b") == ["z", "c"]
def test_sg_adjacent_false(): """Ensure adjacent returns false if a and b don't share an edge.""" from simple_graph import SimpleGraph test_sg = SimpleGraph() test_sg.add_node("a") test_sg.add_node("b") assert test_sg.adjacent("a", "b") is False
def parse_adj_list(lines): V = None try: V = int(lines[0][1]) except: fatal_parse_error(f'Failed to parse number of nodes from {lines[0][1]}', line=0) g = SimpleGraph(V) for line_number, line in lines[1:]: src_dist_strings = line.split("-") if "-" not in line or len(src_dist_strings) != 2: fatal_parse_error(f"Couldn't parse line: {line}, expecting format: source_node - dest1, dest2, ...", line=line_number) src_string = src_dist_strings[0] dst_string = src_dist_strings[1] src = None dests = None try: src = int(src_string) except: fatal_parse_error(f"Non-integer source node: {src_string}", line=line_number) try: dests = list(map(int, dst_string.split(","))) except: fatal_parse_error(f"Non-integer(s) found in destination nodes: {dst_string}", line=line_number) for dest in dests: g.connect(src, dest) return g
def test_sg_del_node_error(): """Ensure del_node throws IndexError if called with node not in graph.""" from simple_graph import SimpleGraph test_sg = SimpleGraph() with pytest.raises(IndexError) as message: test_sg.del_node("node_a") assert "That node is not in the graph." in str(message)
def test_sg_verify_edge_weights(): """Verify that when edges are added to the graph with a specified weight they have that weight.""" from simple_graph import SimpleGraph test_graph = SimpleGraph() test_graph.add_edge("a", "b", 7) assert test_graph.graph["a"][0][1] == 7
def example1(): g = SimpleGraph(4) g.connect(0, 1) g.connect(0, 2) g.connect(1, 2) g.connect(2, 3) print(fleury_algorithm(g))
def test_sg_del_edge(): """Ensure supplied edge is deleted from appropriate node.""" from simple_graph import SimpleGraph test_sg = SimpleGraph() test_sg.add_edge("B", "c") test_sg.del_edge("B", "c") assert ("B", "c") not in test_sg.edges()
def test_sg_breadth_first_traversal_empty(): """Ensure an empty result is handed back when called on empty graph.""" from simple_graph import SimpleGraph test_sg = SimpleGraph() with pytest.raises(IndexError) as message: test_sg.breadth_first_traversal("a") assert "That starting point is not in the graph." in str(message)
def test_sg_del_edge_error(): """Ensure del_edge throws IndexError if called with node not in graph.""" from simple_graph import SimpleGraph test_sg = SimpleGraph() test_sg.add_edge("a", "b") with pytest.raises(IndexError) as message: test_sg.del_edge("c", "b") assert "That edge is not in the graph." in str(message)
def test_sg_add_node_error(): """Ensure duplicate node raises IndexError.""" from simple_graph import SimpleGraph test_sg = SimpleGraph() test_sg.add_node("node_a") with pytest.raises(IndexError) as message: test_sg.add_node("node_a") assert "That node already exists dumbo." in str(message)
def test_sg_adjace_error_two(): """Ensure adjacent raises an error if either node doesn't exist.""" from simple_graph import SimpleGraph test_sg = SimpleGraph() test_sg.add_node("a") with pytest.raises(IndexError) as message: test_sg.adjacent("a", "b") assert "Second argument is not in the graph." in str(message)
def test_sg_del_node(): """Ensure correct node is deleted, and all its edges are removed.""" from simple_graph import SimpleGraph test_sg = SimpleGraph() test_sg.add_edge("a", "c") test_sg.add_edge("b", "c") test_sg.add_edge("c", "b") test_sg.del_node("c") assert "c" not in test_sg.edges()
def make_a_simplegraph(): nodes = (Node(5), Node("woo"), Node(object()), Node("pikachu")) nodes[0].pointers.append(nodes[1]) nodes[0].pointers.append(nodes[2]) nodes[1].pointers.append(nodes[2]) nodes[2].pointers.append(nodes[3]) nodes[3].pointers.append(nodes[1]) nodes[3].pointers.append(nodes[0]) return SimpleGraph(nodes)
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