def test_equal_size(self): a = Matrix(size = 3) a.add_edge(0, 1) b = Matrix(size = 4) b.add_edge(0, 1) assert not a.equals(b)
def combine_adj_matrix(starting_node: int, graph1: Matrix, graph2: Matrix, target_nodes: List[int]) -> Matrix: if graph1.equals(graph2): return graph1 elif graph1.subset(graph2): return graph1 elif graph2.subset(graph1): return graph2 out = Matrix(graph1.size()) def does_lead_to_target(curr_node, visited: Set[int]): if curr_node not in target_nodes: if curr_node not in visited: all_outgoing = set( graph1.get_connected_lst(curr_node) + graph2.get_connected_lst(curr_node)) visited.add(curr_node) for outgoing in all_outgoing: if outgoing not in visited: # out.add_edge(curr_node, outgoing) if does_lead_to_target(outgoing, visited): return True return False else: return True def traverse(curr_node, visited: Set[int]): # Do nothing if we already visited the node if curr_node not in visited: # Get all outgoing edges all_outgoing = set( graph1.get_connected_lst(curr_node) + graph2.get_connected_lst(curr_node)) visited.add(curr_node) for outgoing in all_outgoing: # Make sure we don't go in circles, make sure to prune any extraneous edges if outgoing not in visited and does_lead_to_target( outgoing, deepcopy(visited)): out.add_edge(curr_node, outgoing) traverse(outgoing, visited) traverse(starting_node, set()) return out